Advanced Functional Programming Tim Sheard Oregon Graduate Institute

  • Slides: 27
Download presentation
Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture 19:

Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture 19: Algebraic and Co. Algebraic Programs • F Algebras • Initial and Final Algebras • Induction and Co. Induction Lecture 15 Tim Sheard 1

Advanced Functional Programming Announcements Remember Final Projects are due in my office, Friday March

Advanced Functional Programming Announcements Remember Final Projects are due in my office, Friday March 21, 2003 at 4: 00 pm Today’s lecture is drawn from the paper A Tutorial on (Co)Algebras and (Co)Induction by Bart Jacobs and Jan Rutten EATCS Bulletin 62 (1997) pp. 222 -259 See link on papers page on class website Lecture 15 Tim Sheard 2

Advanced Functional Programming Algebras and Functors An F-algebra over a carrier sort x is

Advanced Functional Programming Algebras and Functors An F-algebra over a carrier sort x is set of functions (and constants) that consume an F x object to produce another x object. In Haskell we can simulate this by a data definition for a functor (F x) and a function (F x) -> x data Algebra f c = Algebra (f c -> c) data F 1 x = Zero | One | Plus x x data List. F a x = Nil | Cons a x Note how the constructors of the functor play the roles of the constants and functions. Lecture 15 Tim Sheard 3

Advanced Functional Programming Examples f f : : F 1 Int -> Int Zero

Advanced Functional Programming Examples f f : : F 1 Int -> Int Zero = 0 One = 1 (Plus x y) = x+y g g : : F 1 [Int] -> [Int] Zero = [] One = [1] (Plus x y) = x ++ y alg 1 : : Algebra F 1 Int alg 1 = Algebra f alg 2 : : Algebra F 1 [Int] alg 2 = Algebra g Lecture 15 Tim Sheard 4

Advanced Functional Programming More Examples data List. F a x = Nil | Cons

Advanced Functional Programming More Examples data List. F a x = Nil | Cons a x h : : List. F b Int -> Int h Nil = 0 h (Cons x xs) = 1 + xs alg 3 : : Algebra (List. F a) Int alg 3 = Algebra h Lecture 15 Tim Sheard 5

Advanced Functional Programming Initial Algebra An initial Algebra is the set of terms we

Advanced Functional Programming Initial Algebra An initial Algebra is the set of terms we can obtain be iteratively applying the functions to the constants and other function applications. This set can be simulated in Haskell by the data definition: data Initial alg = Init (alg (Initial alg)) Here the function is : Init : : alg (Init alg) -> Init alg f : : T x -> x Note how this fits the (T x -> x) pattern. Lecture 15 Tim Sheard 6

Advanced Functional Programming Example elements of Initial Algebras ex 1 : : Initial F

Advanced Functional Programming Example elements of Initial Algebras ex 1 : : Initial F 1 ex 1 = Init(Plus (Init One) (Init Zero)) ex 2 : : Initial (List. F Int) ex 2 = Init(Cons 2 (Init Nil)) initial. Alg : : Algebra f (Initial f) initial. Alg = Algebra Init Lecture 15 Tim Sheard 7

Advanced Functional Programming Defining Functions We can write functions by a case analysis over

Advanced Functional Programming Defining Functions We can write functions by a case analysis over the functions and constants that generate the initial algebra len : : Num a => Initial (List. F b) -> a len (Init Nil) = 0 len (Init (Cons x xs)) = 1 + len xs app : : Initial (List. F a) -> Initial (List. F a) app (Init Nil) ys = ys app (Init (Cons x xs)) ys = Init(Cons x (app xs ys)) Lecture 15 Tim Sheard 8

Advanced Functional Programming F-algebra homomorphism An F-algebra, f, is said to be initial to

Advanced Functional Programming F-algebra homomorphism An F-algebra, f, is said to be initial to any other algebra, g, if there is a UNIQUE homomorphism, from f to g (this is an arrow in the category of F-algebras). We can show the existence of this homomorphism by building it as a datatype in Haskell. Note: that for each "f", (Arrow f a b) denotes an arrow in the category of f-algebras. data Arrow f a b = Arr (Algebra f a) (Algebra f b) (a->b) -- plus laws about the function (a->b) Lecture 15 Tim Sheard 9

Advanced Functional Programming F-homomorphism laws For every Arrow (Arr (Algebra f) (Algebra g) h)

Advanced Functional Programming F-homomorphism laws For every Arrow (Arr (Algebra f) (Algebra g) h) it must be the case that valid : : (Eq b, Functor f) => Arrow f a b -> f a -> Bool valid (Arr (Algebra f) (Algebra g) h) x = h(f x) == g(fmap h x) F a Fmap h f g a Lecture 15 F b h b Tim Sheard 10

Advanced Functional Programming Existence of h To show the existence of "h" for any

Advanced Functional Programming Existence of h To show the existence of "h" for any F-Algebra means we can compute a function with the type (a -> b) from the algebra. To do this we first define cata: cata : : Functor f => (Algebra f b) -> Initial f -> b cata (Algebra phi) (Init x) = phi(fmap (cata (Algebra phi)) x) exhibit : : Functor f => Algebra f a -> Arrow f (Initial f) a exhibit x = Arr initial. Alg x (cata x) Initial Algebra Lecture 15 Arbitrary Algebra Function a -> b Tim Sheard 11

Advanced Functional Programming Writing functions as cata’s Lots of functions can be written directly

Advanced Functional Programming Writing functions as cata’s Lots of functions can be written directly as cata's len 2 x = cata (Algebra phi) x where phi Nil = 0 phi (Cons x n) = 1 + n app 2 x y = cata (Algebra phi) x where phi Nil = y phi (Cons x xs) = Init(Cons x xs) Lecture 15 Tim Sheard 12

Advanced Functional Programming Induction Principle With initiality comes the inductive proof method. So to

Advanced Functional Programming Induction Principle With initiality comes the inductive proof method. So to prove something (prop x) where x: : Initial A we proceed as follows prop 1 : : Initial (List. F Int) -> Bool prop 1 x = len(Init(Cons 1 x)) == 1 + len x Prove: prop 1 (Init Nil) Assume prop 1 xs Then prove: prop 1 (Init (Cons x xs)) Lecture 15 Tim Sheard 13

Advanced Functional Programming Induction Proof Rules For an arbitrary F-Algebra, we need a function

Advanced Functional Programming Induction Proof Rules For an arbitrary F-Algebra, we need a function from F(Proof prop x) -> Proof prop x data Proof p x = Simple (p x) | forall f. Induct (Algebra f (Proof p x)) Lecture 15 Tim Sheard 14

Advanced Functional Programming Co. Algebras An F-Co. Algebra over a carrier sort x is

Advanced Functional Programming Co. Algebras An F-Co. Algebra over a carrier sort x is set of functions (and constants) whose types consume x to produce an F-structure data Co. Algebra f c = Co. Algebra (c -> f c) un. Co. Algebra (Co. Algebra x) = x countdown where f f Lecture 15 : : Co. Algebra (List. F Int) Int = Co. Algebra f 0 = Nil n = Cons n (n-1) Tim Sheard 15

Advanced Functional Programming Stream Co. Algebra The classic Co. Algebra is the infinite stream

Advanced Functional Programming Stream Co. Algebra The classic Co. Algebra is the infinite stream data Stream. F n x = C n x Note that if we iterate Stream. F, there is No nil object, all streams are infinite. What we get is an infinite set of observations (the n-objects in this case). Lecture 15 Tim Sheard 16

Advanced Functional Programming Examples We can write Co. Algebras by expanding a "seed" into

Advanced Functional Programming Examples We can write Co. Algebras by expanding a "seed" into an F structure filled with new seeds. seed -> F seed The non-parameterized slots can be filled with things computed from the seed. These are sometimes called observations. ends. In 0 s : : Co. Algebra (Stream. F Integer) [Integer] ends. In 0 s = Co. Algebra f where f [] = C 0 [] f (x: xs) = C x xs Lecture 15 Tim Sheard 17

Advanced Functional Programming More Examples split : : Co. Algebra F 1 Integer split

Advanced Functional Programming More Examples split : : Co. Algebra F 1 Integer split = Co. Algebra f where f 0 = Zero f 1 = One f n = Plus (n-1) (n-2) fibs : : Co. Algebra (Stream. F Int) (Int, Int) fibs = Co. Algebra f where f (x, y) = C (x+y) (y, x+y) Lecture 15 Tim Sheard 18

Advanced Functional Programming Final Co. Algebras are sequences (branching trees? ) of observations of

Advanced Functional Programming Final Co. Algebras are sequences (branching trees? ) of observations of the internal state. This allows us to iterate all the possible observations. Sometimes these are infinite structures. data Final f = Final (f (Final f)) un. Final : : Final a -> a (Final a) un. Final (Final x) = x final. Coalg : : Co. Algebra a (Final a) final. Coalg = Co. Algebra un. Final Lecture 15 Tim Sheard 19

Advanced Functional Programming Example Final Co. Algebra elements f 1 : : Final (List.

Advanced Functional Programming Example Final Co. Algebra elements f 1 : : Final (List. F a) f 1 = Final Nil ones : : Final (Stream. F Integer) ones = Final(C 1 ones) Lecture 15 Tim Sheard 20

Advanced Functional Programming Iterating We can write functions producing elements in the sort of

Advanced Functional Programming Iterating We can write functions producing elements in the sort of Final Co. Algebras by expanding a "seed" into an F structure filled with observations and recursive calls in the "slots”. Note then, that all thats really left is the observations. nats : : Final (Stream. F Integer) nats = g 0 where g n = Final (C n (g (n+1))) Lecture 15 Tim Sheard 21

Advanced Functional Programming More Examples data Nat. F x = Z | S x

Advanced Functional Programming More Examples data Nat. F x = Z | S x omega : : Final Nat. F omega = f undefined where f x = Final(S(f x)) n : : Int -> Final Nat. F n x = f x where f 0 = Final Z f n = Final(S (f (n-1))) Lecture 15 Tim Sheard 22

Advanced Functional Programming Co. Homomorphisms A Co. Homomorphism is an arrow in the category

Advanced Functional Programming Co. Homomorphisms A Co. Homomorphism is an arrow in the category of F-Co. Algebras data Co. Hom f a b = Co. Hom (Co. Algebra f a) (Co. Algebra f b) (a->b) For every arrow in the category (Co. Hom (Co. Algebra f) (Co. Algebra g) h) it must be the case that covalid : : (Eq (f b), Functor f) => Co. Hom f a b -> a -> Bool covalid (Co. Hom (Co. Algebra f) (Co. Algebra g) h) x = fmap h (f x) == g(h x) F a Fmap h f g a Lecture 15 F b h b Tim Sheard 23

Advanced Functional Programming Final Co. Alegbra A F-Co. Algebra, g, is Final if for

Advanced Functional Programming Final Co. Alegbra A F-Co. Algebra, g, is Final if for any other FCo. Algebra, f, there is a unique F-Co. Algebra homomorphism, h, from f to g. We can show its existence be building a function that computes it from the Co. Algebra, f. ana : : Functor f => (Co. Algebra f seed) -> seed -> (Final f) ana (Co. Algebra phi) seed = Final(fmap (ana (Co. Algebra phi)) (phi seed)) exhibit 2 : : Functor f => Co. Algebra f seed -> Co. Hom f seed (Final f) exhibit 2 x = Co. Hom final. Coalg x (ana x) Lecture 15 Tim Sheard 24

Advanced Functional Programming Examples We use ana to iteratively unfold any co. Agebra to

Advanced Functional Programming Examples We use ana to iteratively unfold any co. Agebra to record its observations final 1 = ana ends. In 0 s final 2 = ana split final 3 = ana fibs ends. In 0 s = Co. Algebra f where f [] = C 0 [] f (x: xs) = C x xs split = Co. Algebra f where f 0 = Zero f 1 = One f n = Plus (n-1) (n-2) fibs : : Co. Algebra (Stream. F Int) (Int, Int) fibs = Co. Algebra f where f (x, y) = C (x+y) (y, x+y) tak : : Num a => a -> Final (Stream. F b) -> [b] tak 0 _ = [] tak n (Final (C x xs)) = x : tak (n-1) xs fibs 5 = tak 5 (final 3 (1, 1)) Lecture 15 Tim Sheard 25

Advanced Functional Programming Co. Algebras and Object. Orientation Lets use Co. Algebras to represent

Advanced Functional Programming Co. Algebras and Object. Orientation Lets use Co. Algebras to represent Points in the 2 -D plane as we would in an OO-language data P x = P { xcoord : : Float , ycoord : : Float , move : : Float -> x} point. F : : (Float, Float) -> point. F (x, y) = P { xcoord , ycoord , move = P = = (Float, Float) x y m n -> (m+x, n+y) } type Point = Co. Algebra P (Float, Float) point 1 : : Point point 1 = Co. Algebra point. F Lecture 15 Tim Sheard 26

Advanced Functional Programming Course Evaluations Please write course name and instructor name on back

Advanced Functional Programming Course Evaluations Please write course name and instructor name on back of sheet – CSE 583 – Tim Sheard Complete and a volunteer will bring all completed forms to the Dept. of Graduate Education Note that these are new forms – They are set out in reverse of old forms. Disagreement Lecture 15 Agreement Tim Sheard 27