Personal tools

User:Michiexile/MATH198/Lecture 9

From HaskellWiki

< User:Michiexile | MATH198(Difference between revisions)
Jump to: navigation, search
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
IMPORTANT NOTE: THESE NOTES ARE STILL UNDER DEVELOPMENT. PLEASE WAIT UNTIL AFTER THE LECTURE WITH HANDING ANYTHING IN, OR TREATING THE NOTES AS READY TO READ.
 
 
 
 
===Recursion patterns===
 
===Recursion patterns===
   
Line 33: Line 30:
 
mu f = x where x = f x
 
mu f = x where x = f x
 
</haskell>
 
</haskell>
* MFP write <math>(f\Delta g)</math> for
 
<haskell>
 
Delta f g = \x -> (f x, g x)
 
</haskell>
 
* MFP write <math>(f\nabla g) x</math> for
 
<haskell>
 
(Nabla f g) (Left x) = f x
 
(Nabla f g) (Right x) = g x
 
</haskell>
 
 
These two last constructions are directly motivated by the maps induced from the universal properties of products and coproducts.
 
 
We shall write <math>(f\times g)</math> and <math>(f+g)</math> for the <math>\Delta</math> and <math>\nabla</math> constructions, respectively.
 
   
 
We note that in the situation considered by MFP, inital algebras and final coalgebras coincide, and thus <math>in_A, out_A</math> are the pair of isomorphic maps induced by either the initial algebra- or the final coalgebra-structure.
 
We note that in the situation considered by MFP, inital algebras and final coalgebras coincide, and thus <math>in_A, out_A</math> are the pair of isomorphic maps induced by either the initial algebra- or the final coalgebra-structure.
Line 119: Line 103:
 
MFP define the anamorphism by a fixpoint as well, namely:
 
MFP define the anamorphism by a fixpoint as well, namely:
 
<haskell>
 
<haskell>
  +
ana :: (b -> F a b) -> b -> T a
 
ana psi = mu (\x -> inT . fmap x . psi)
 
ana psi = mu (\x -> inT . fmap x . psi)
 
</haskell>
 
</haskell>
Line 144: Line 129:
 
MFP define it, again, as a fix point:
 
MFP define it, again, as a fix point:
 
<haskell>
 
<haskell>
  +
hylo :: (F a b2 -> b2) -> (b1 -> F a b1) -> b1 -> b2
 
hylo phi psi = mu (\x -> phi . fmap x . psi)
 
hylo phi psi = mu (\x -> phi . fmap x . psi)
 
</haskell>
 
</haskell>
Line 162: Line 148:
 
factorial = hylo phi psi
 
factorial = hylo phi psi
 
</haskell>
 
</haskell>
 
   
 
====Metamorphisms====
 
====Metamorphisms====
  +
  +
The ''metamorphism'' is the ''other'' composition of an anamorphism with a catamorphism. It takes some structure, deconstructs it, and then reconstructs a new structure from it.
  +
  +
As a recursion pattern, it's kinda boring - it'll take an interesting structure, deconstruct it into a ''scalar'' value, and then reconstruct some structure from that scalar. As such, it won't even capture the richness of <math>hom(F x, G y)</math>, since any morphism expressed as a metamorphism will factor through a map <math>x\to y</math>.
   
 
====Paramorphisms====
 
====Paramorphisms====
  +
  +
''Paramorphisms'' were discussed in the MFP paper as a way to extend the catamorphisms so that the operating function can access its arguments in computation as well as in recursion. We gave the factorial above as a hylomorphism instead of a catamorphism precisely because no simple enough catamorphic structure exists.
   
 
====Apomorphisms====
 
====Apomorphisms====
  +
  +
The ''apomorphism'' is the dual of the paramorphism - it does with retention of values along the way what anamorphisms do compared to catamorphisms.
   
 
===Further reading===
 
===Further reading===
   
Terminology in the literature: in and out, inl, inr.
+
* Erik Meijer, Maarten Fokkinga, Ross Paterson: ''Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire'' [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.41.125&rep=rep1&type=pdf]
  +
* L. Augusteijn: ''Sorting morphisms'' [http://www.springerlink.com/index/w543447353067h67.pdf]
  +
  +
===Further properties of adjunctions===
  +
  +
====RAPL====
  +
  +
'''Proposition''' If <math>F</math> is a ''right adjoint'', thus if <math>F</math> has a left adjoint, then <math>F</math> preserves limits in the sense that <math>F(\lim_{\leftarrow} A_i) = \lim_{\leftarrow} F(A_i)</math>.
  +
  +
Example: <math>(\lim_{\leftarrow_i} A_i)\times X = \lim_{\leftarrow_i} A_i\times X</math>.
  +
  +
We can use this to prove that things cannot be adjoints - since all right adjoints preserve limits, if a functor <math>G</math> doesn't preserve limits, then it doesn't have a left adjoint.
  +
  +
Similarly, and dually, left adjoints preserve colimits. Thus if a functor doesn't preserve colimits, it cannot be a left adjoint, thus cannot have a right adjoint.
  +
  +
The proof of these statements build on the ''Yoneda lemma'':
  +
  +
'''Lemma''' If <math>C</math> is a locally small category (i.e. all hom-sets are sets). Then for any <math>c\in C_0</math> and any functor <math>F: C^{op}\to Sets</math> there is an isomorphism
  +
:<math>hom_{hom_{Sets^{C^{op}}}}(yC, F) = FC</math>
  +
where we define <math>yC = d\mapsto hom_C(d,c) : C^{op}\to Sets</math>.
  +
  +
The Yoneda lemma has one important corollary:
  +
  +
'''Corollary''' If <math>yA = yB</math> then <math>A = B</math>.
  +
  +
Which, in turn has a number of important corollaries:
  +
  +
'''Corollary''' <math>(A^B)^C = A^{B\times C}</math>
  +
  +
'''Corollary''' Adjoints are unique up to isomorphism - in particular, if <math>F: C\to D</math> is a functor with right adjoints <math>U, V: D\to C</math>, then <math>U = V</math>.
  +
  +
'''Proof''' <math>hom_C(C, UD) = hom_D(FC, D) = hom_C(C, VD)</math>, and thus by the corollary to the Yoneda lemma, <math>UD = VD</math>, natural in <math>D</math>.
  +
  +
====Functors that are adjoints====
   
  +
* The functor <math>X\mapsto X\times A</math> has right adjoint <math>Y\mapsto Y^A</math>. The universal mapping property of the exponentials follows from the adjointness property.
  +
* The functor <math>\Delta: C\to C\times C, c\mapsto (c,c)</math> has a left adjoint given by the coproduct <math>(X,Y)\mapsto X + Y</math> and right adjoint the product <math>(X,Y)\mapsto X\times Y</math>.
  +
* More generally, the functor <math>C\to C^J</math> that takes <math>c</math> to the constant functor <math>const_c(j) = c, const_c(f) = 1_c</math> has left andright adjoints given by colimits and limits:
  +
:<math>\lim_\rightarrow -| \Delta -| \lim_\leftarrow</math>
  +
* Pointed rings are pairs <math>(R, r\in R)</math> of rings and one element singled out for attention. Homomorphisms of pointed rings need to take the distinguished point to the distinguished point. There is an obvious forgetful functor <math>U: Rings_* \to Rings</math>, and this has a left adjoint - a free ring functor that adjoins a new indeterminate <math>R\mapsto (R[x], x)</math>. This gives a formal definition of what we mean by ''formal polynomial expressions'' et.c.
  +
* Given sets <math>A, B</math>, we can consider the powersets <math>P(A), P(B)</math> containing, as elements, all subsets of <math>A, B</math> respectively. Suppose <math>f:A\to B</math> is a function, then <math>f^{-1}: P(B)\to P(A)</math> takes subsets of <math>B</math> to subsets of <math>A</math>.
  +
:Viewing <math>P(A)</math> and <math>P(B)</math> as partially ordered sets by the inclusion operations, and then as categories induced by the partial order, <math>f^{-1}</math> turns into a functor between partial orders. And it turns out <math>f^{-1}</math> has a left adjoint given by the operation <math>im(f)</math> taking a subset to the set of images under the function <math>f</math>. And it has a right adjoint <math>f_*(U) = \{b\in B: f^{-1}(b)\subseteq U\}</math>
  +
* We can introduce a categorical structure to logic. We let <math>L</math> be a formal language, say of predicate logic. Then for any list <math>x = x_1, x_2, ..., x_n</math> of variables, we have a preorder <math>Form(x)</math> of formulas with no free variables not occuring in <math>x</math>. The preorder on <math>Form(x)</math> comes from the ''entailment'' operation - <math> f |- g</math> if in every interpretation of the language, <math>f \Rightarrow g</math>.
  +
: We can build an operation on these preorders - a functor on the underlying categories - by adjoining a single new variable: <math>*: Form(x) \to Form(x, y)</math>, sending each form to itself. Obviously, if <math>f |- g</math> with <math>x</math> the source of free variables, if we introduce a new allowable free variable, but don't actually change the formulas, the entailment stays the same.
  +
: It turns out that there is a right adjoint to <math>*</math> given by <math>f\mapsto \forall y. f</math>. And a left adjoint to <math>*</math> given by <math>f\mapsto \exists y. f</math>. Adjointness properties give us classical deduction rules from logic.
   
 
===Homework===
 
===Homework===
Line 179: Line 182:
 
# Write a fold for the data type <hask>data T a = L a | B a a | C a a a</hask> and demonstrate how this can be written as a catamorphism by giving the algebra it maps to.
 
# Write a fold for the data type <hask>data T a = L a | B a a | C a a a</hask> and demonstrate how this can be written as a catamorphism by giving the algebra it maps to.
 
# Write the fibonacci function as a hylomorphism.
 
# Write the fibonacci function as a hylomorphism.
  +
# Write the Towers of Hanoi as a hylomorphism. You'll probably want to use binary trees as the intermediate data structure.
  +
# Write a prime numbers generator as an anamorphism.
 
# * The integers have a partial order induced by the divisibility relation. We can thus take any integer and arrange all its divisors in a tree by having an edge <math>n \to d</math> if <math>d|n</math> and <math>d</math> doesn't divide any other divisor of <math>n</math>. Write an anamorphic function that will generate this tree for a given starting integer. Demonstrate how this function is an anamorphism by giving the algebra it maps from.
 
# * The integers have a partial order induced by the divisibility relation. We can thus take any integer and arrange all its divisors in a tree by having an edge <math>n \to d</math> if <math>d|n</math> and <math>d</math> doesn't divide any other divisor of <math>n</math>. Write an anamorphic function that will generate this tree for a given starting integer. Demonstrate how this function is an anamorphism by giving the algebra it maps from.
 
:''Hint'': You will be helped by having a function to generate a list of all primes. One suggestion is:
 
:''Hint'': You will be helped by having a function to generate a list of all primes. One suggestion is:

Latest revision as of 18:40, 17 November 2009

Contents

[edit] 1 Recursion patterns

Meijer, Fokkinga & Patterson identified in the paper Functional programming with bananas, lenses, envelopes and barbed wire a number of generic patterns for recursive programming that they had observed, catalogued and systematized. The aim of that paper is to establish a number of rules for modifying and rewriting expressions involving these generic recursion patterns.

As it turns out, these patterns are instances of the same phenomenon we saw last lecture: where the recursion comes from specifying a different algebra, and then take a uniquely existing morphism induced by initiality (or, as we shall see, finality).

Before we go through the recursion patterns, we need to establish a few pieces of theoretical language, dualizing the Eilenberg-Moore algebra constructions from the last lecture.

[edit] 1.1 Coalgebras for endofunctors

Definition If P: C\to C is an endofunctor, then a P-coalgebra on A is a morphism a: A\to PA.

A morphism of coalgebras: f: a\to b is some f: A\to B such that the diagram

CoalgebraMorphism.png

commutes.

Just as with algebras, we get a category of coalgebras. And the interesting objects here are the final coalgebras. Just as with algebras, we have

Lemma (Lambek) If a: A\to PA is a final coalgebra, it is an isomorphism.

Finally, one thing that makes us care highly about these entities: in an appropriate category (such as ω − CPO), initial algebras and final coalgebras coincide, with the correspondence given by inverting the algebra/coalgebra morphism. In Haskell not quite true (specifically, the final coalgebra for the lists functor gives us streams...).

Onwards to recursion schemes!

We shall define a few specific morphisms we'll use repeatedly. This notation, introduced here, occurs all over the place in these corners of the literature, and are good to be aware of in general:

  • If a: TA\to A is an initial algebra for T, we denote a = inA.
  • If a: A\to TA is a final coalgebra for T, we denote a = outA.
  • We write μf for the fixed point operator
mu f = x where x = f x

We note that in the situation considered by MFP, inital algebras and final coalgebras coincide, and thus inA,outA are the pair of isomorphic maps induced by either the initial algebra- or the final coalgebra-structure.

[edit] 1.2 Catamorphisms

A catamorphism is the uniquely existing morphism from an initial algebra to a different algebra. We have to define maps down to the return value type for each of the constructors of the complex data type we're recursing over, and the catamorphism will deconstruct the structure (trees, lists, ...) and do a generalized fold over the structure at hand before returning the final value.

The intuition is that for catamorphisms we start essentially structured, and dismantle the structure.

Example: the length function from last lecture. This is the catamorphism for the functor P_A(X) = 1 + A\times X given by the maps

u :: Int
u = 0
 
m :: (A, Int) -> Int
m (a, n) = n+1
MFP define the catamorphism by, supposing
T
is initial for the functor
F
:
cata :: (F a b -> b) -> T a -> b
cata phi = mu (\x -> phi . fmap x . outT)

We can reframe the example above as a catamorphism by observing that here,

data F a b = Nil | Cons a b deriving (Eq, Show)
type T a = [a]
 
instance Functor (F a) where
  fmap _ Nil = Nil
  fmap f (Cons n a) = Cons n (f a)
 
outT :: T a -> F a (T a)
outT [] = Nil
outT (a:as) = Cons a as
 
lphi :: F a Int -> Int
lphi Nil = 0
lphi (Cons a n) = n + 1
 
l = cata lphi
where we observe that
mu
has a global definition for everything we do and
out
is defined once we settle on the functor
F
and its initial algebra. Thus, the definition of
phi
really is the only place that the recursion data shows up.

[edit] 1.3 Anamorphisms

An anamorphism is the categorical dual to the catamorphism. It is the canonical morphism from a coalgebra to the final coalgebra for that endofunctor.

Here, we start unstructured, and erect a structure, induced by the coalgebra structures involved.

Example: we can write a recursive function

first :: Int -> [Int]
first 1 = [1]
first n = n : first (n - 1)

This is an anamorphism from the coalgebra for P_{\mathbb N}(X) = 1 + \mathbb N\times X on \mathbb N generated by the two maps

c 0 = Left ()
c n = Right (n, n-1)

and we observe that we can chase through the diagram

CoalgebraMorphism.png

to conclude that therefore

f 0 = []
f n = n : f (n - 1)

which is exactly the recursion we wrote to begin with.

MFP define the anamorphism by a fixpoint as well, namely:

ana :: (b -> F a b) -> b -> T a
ana psi = mu (\x -> inT . fmap x . psi)

We can, again, recast our illustration above into a structural anamorphism, by:

-- Reuse mu, F, T from above
inT :: F a (T a) -> T a
inT Nil = []
inT (Cons a as) = a:as
 
fpsi :: Int -> F Int Int
fpsi 0 = Nil
fpsi n = Cons n (n-1)
Again, we can note that the implementation of
fpsi
here is exactly the
c
above, and the resulting function will - as we can verify by compiling and running - give us the same kind of reversed list of the n first integers as the
first
function above would.

[edit] 1.4 Hylomorphisms

The hylomorphisms capture one of the two possible compositions of anamorphisms and catamorphisms. Parametrized over an algebra \phi: T A\to A and a coalgebra \psi: B \to T B the hylomorphism is a recursion pattern that computes a value in A from a value in A by generating some sort of intermediate structure and then collapsing it again.

It is, thus the composition of the uniquely existing morphism from a coalgebra to the final coalgebra for an endofunctor, followed by the uniquely existing morphism from the initial algebra to some other algebra.

MFP define it, again, as a fix point:

hylo :: (F a b2 -> b2) -> (b1 -> F a b1) -> b1 -> b2
hylo phi psi = mu (\x -> phi . fmap x . psi)

First off, we can observe that by picking one or the other of inA,outA as a parameter, we can recover both the anamorphisms and the catamorphisms as hylomorphisms.

As an example, we'll compute the factorial function using a hylomorphism:

phi :: F Int Int -> Int
phi Nil = 1
phi (Cons n m) = n*m
 
psi :: Int -> F Int Int
psi 0 = Int
psi n = Cons n (n-1)
 
factorial = hylo phi psi

[edit] 1.5 Metamorphisms

The metamorphism is the other composition of an anamorphism with a catamorphism. It takes some structure, deconstructs it, and then reconstructs a new structure from it.

As a recursion pattern, it's kinda boring - it'll take an interesting structure, deconstruct it into a scalar value, and then reconstruct some structure from that scalar. As such, it won't even capture the richness of hom(Fx,Gy), since any morphism expressed as a metamorphism will factor through a map x\to y.

[edit] 1.6 Paramorphisms

Paramorphisms were discussed in the MFP paper as a way to extend the catamorphisms so that the operating function can access its arguments in computation as well as in recursion. We gave the factorial above as a hylomorphism instead of a catamorphism precisely because no simple enough catamorphic structure exists.

[edit] 1.7 Apomorphisms

The apomorphism is the dual of the paramorphism - it does with retention of values along the way what anamorphisms do compared to catamorphisms.

[edit] 2 Further reading

  • Erik Meijer, Maarten Fokkinga, Ross Paterson: Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire [1]
  • L. Augusteijn: Sorting morphisms [2]

[edit] 3 Further properties of adjunctions

[edit] 3.1 RAPL

Proposition If F is a right adjoint, thus if F has a left adjoint, then F preserves limits in the sense that F(\lim_{\leftarrow} A_i) = \lim_{\leftarrow} F(A_i).

Example: (\lim_{\leftarrow_i} A_i)\times X = \lim_{\leftarrow_i} A_i\times X.

We can use this to prove that things cannot be adjoints - since all right adjoints preserve limits, if a functor G doesn't preserve limits, then it doesn't have a left adjoint.

Similarly, and dually, left adjoints preserve colimits. Thus if a functor doesn't preserve colimits, it cannot be a left adjoint, thus cannot have a right adjoint.

The proof of these statements build on the Yoneda lemma:

Lemma If C is a locally small category (i.e. all hom-sets are sets). Then for any c\in C_0 and any functor F: C^{op}\to Sets there is an isomorphism

hom_{hom_{Sets^{C^{op}}}}(yC, F) = FC

where we define yC = d\mapsto hom_C(d,c) : C^{op}\to Sets.

The Yoneda lemma has one important corollary:

Corollary If yA = yB then A = B.

Which, in turn has a number of important corollaries:

Corollary (A^B)^C = A^{B\times C}

Corollary Adjoints are unique up to isomorphism - in particular, if F: C\to D is a functor with right adjoints U, V: D\to C, then U = V.

Proof homC(C,UD) = homD(FC,D) = homC(C,VD), and thus by the corollary to the Yoneda lemma, UD = VD, natural in D.

[edit] 3.2 Functors that are adjoints

  • The functor X\mapsto X\times A has right adjoint Y\mapsto Y^A. The universal mapping property of the exponentials follows from the adjointness property.
  • The functor \Delta: C\to C\times C, c\mapsto (c,c) has a left adjoint given by the coproduct (X,Y)\mapsto X + Y and right adjoint the product (X,Y)\mapsto X\times Y.
  • More generally, the functor C\to C^J that takes c to the constant functor constc(j) = c,constc(f) = 1c has left andright adjoints given by colimits and limits:
\lim_\rightarrow -| \Delta -| \lim_\leftarrow
  • Pointed rings are pairs (R, r\in R) of rings and one element singled out for attention. Homomorphisms of pointed rings need to take the distinguished point to the distinguished point. There is an obvious forgetful functor U: Rings_* \to Rings, and this has a left adjoint - a free ring functor that adjoins a new indeterminate R\mapsto (R[x], x). This gives a formal definition of what we mean by formal polynomial expressions et.c.
  • Given sets A,B, we can consider the powersets P(A),P(B) containing, as elements, all subsets of A,B respectively. Suppose f:A\to B is a function, then f^{-1}: P(B)\to P(A) takes subsets of B to subsets of A.
Viewing P(A) and P(B) as partially ordered sets by the inclusion operations, and then as categories induced by the partial order, f − 1 turns into a functor between partial orders. And it turns out f − 1 has a left adjoint given by the operation im(f) taking a subset to the set of images under the function f. And it has a right adjoint f_*(U) = \{b\in B: f^{-1}(b)\subseteq U\}
  • We can introduce a categorical structure to logic. We let L be a formal language, say of predicate logic. Then for any list x = x1,x2,...,xn of variables, we have a preorder Form(x) of formulas with no free variables not occuring in x. The preorder on Form(x) comes from the entailment operation - f | − g if in every interpretation of the language, f \Rightarrow g.
We can build an operation on these preorders - a functor on the underlying categories - by adjoining a single new variable: *: Form(x) \to Form(x, y), sending each form to itself. Obviously, if f | − g with x the source of free variables, if we introduce a new allowable free variable, but don't actually change the formulas, the entailment stays the same.
It turns out that there is a right adjoint to * given by f\mapsto \forall y. f. And a left adjoint to * given by f\mapsto \exists y. f. Adjointness properties give us classical deduction rules from logic.

[edit] 4 Homework

  1. Write a fold for the data type
    data T a = L a | B a a | C a a a
    and demonstrate how this can be written as a catamorphism by giving the algebra it maps to.
  2. Write the fibonacci function as a hylomorphism.
  3. Write the Towers of Hanoi as a hylomorphism. You'll probably want to use binary trees as the intermediate data structure.
  4. Write a prime numbers generator as an anamorphism.
  5. * The integers have a partial order induced by the divisibility relation. We can thus take any integer and arrange all its divisors in a tree by having an edge n \to d if d | n and d doesn't divide any other divisor of n. Write an anamorphic function that will generate this tree for a given starting integer. Demonstrate how this function is an anamorphism by giving the algebra it maps from.
Hint: You will be helped by having a function to generate a list of all primes. One suggestion is:
primes :: [Integer]
primes = sieve [2..]
  where
    sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0]
Hint: A good data structure to use is; with expected output of running the algorithm:
data Tree = Leaf Integer | Node Integer [Tree]
 
divisionTree 60 = 
  Node 60 [
    Node 30 [
      Node 15 [
        Leaf 5,
        Leaf 3],
      Node 10 [
        Leaf 5,
        Leaf 2],
      Node 6 [
        Leaf 3,
        Leaf 2]],
    Node 20 [
      Node 10 [
        Leaf 5,
        Leaf 2],
      Node 4 [
        Leaf 2]],
    Node 12 [
      Node 6 [
        Leaf 3,
        Leaf 2],
      Node 4 [
        Leaf 2]]]