Advanced Functional Programming Tim Sheard 1 Lecture 15 Advanced Functional Programming Tim Sheard Algebraic and CoAlgebraic Programs F Algebras Initial.

Slides:



Advertisements
Similar presentations
Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
Advertisements

22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 3 CS 1813 – Discrete Mathematics Truth Inference and the Logical Way.
A Type System for MetaML MetaML types as an Omega program Lecture 12.
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
CSE115/ENGR160 Discrete Mathematics 03/31/11
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
MATH 224 – Discrete Mathematics
GADTs GADTs in Haskell. ADT vs GADT Algebraic Datatype Data List a = Nil | Cons a (List a) Data Tree a b = Tip a | Node (Tree a b) b | Fork (Tree a b)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Advanced Functional Programming Tim Sheard 1 Lecture 1 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology.
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
9.4 Mathematical Induction
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Advanced Programming Andrew Black and Tim Sheard Lecture 11 Parsing Combinators.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
Overview of the Haskell 98 Programming Language
0 Functors in Haskell Adapted from material by Miran Lipovaca.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
INM175 Topic 8 1 Module INM175 Discrete Mathematics Topic 8 Algebraic Theories.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
CSE 311: Foundations of Computing Fall 2013 Lecture 8: Proofs and Set theory.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
MATH 224 – Discrete Mathematics
String is a synonym for the type [Char].
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
Advanced Functional Programming
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Closures and Streams cs784(Prasad) L11Clos
Lecture 15 CS 1813 – Discrete Mathematics
Advanced Functional Programming
PROGRAMMING IN HASKELL
Advanced Functional Programming
PROGRAMMING IN HASKELL
Functional Programming
Advanced Functional Programming
Types and Classes in Haskell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Theory of Computation Lecture 4: Programs and Computable Functions II
?? Programming with Streams Reading assignment
Presentation transcript:

Advanced Functional Programming Tim Sheard 1 Lecture 15 Advanced Functional Programming Tim Sheard Algebraic and CoAlgebraic Programs F Algebras Initial and Final Algebras Induction and CoInduction

Advanced Functional Programming Tim Sheard 2 Lecture 15 Announcements Remember Midterm exam on May 8 th. 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 See link on papers page on class website

Advanced Functional Programming Tim Sheard 3 Lecture 15 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 F1 x = Zero | One | Plus x x data ListF a x = Nil | Cons a x Note how the constructors of the functor play the roles of the constants and functions.

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

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

Advanced Functional Programming Tim Sheard 6 Lecture 15 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.

Advanced Functional Programming Tim Sheard 7 Lecture 15 Example elements of Initial Algebras ex1 :: Initial F1 ex1 = Init(Plus (Init One) (Init Zero)) ex2 :: Initial (ListF Int) ex2 = Init(Cons 2 (Init Nil)) initialAlg :: Algebra f (Initial f) initialAlg = Algebra Init

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

Advanced Functional Programming Tim Sheard 9 Lecture 15 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)

Advanced Functional Programming Tim Sheard 10 Lecture 15 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) a b F b F a Fmap h h f g

Advanced Functional Programming Tim Sheard 11 Lecture 15 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 initialAlg x (cata x) Initial Algebra Arbitrary Algebra Function a -> b

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

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

Advanced Functional Programming Tim Sheard 14 Lecture 15 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))

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

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

Advanced Functional Programming Tim Sheard 17 Lecture 15 Examples We can write CoAlgebras 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. endsIn0s :: CoAlgebra (StreamF Integer) [Integer] endsIn0s = CoAlgebra f where f [] = C 0 [] f (x:xs) = C x xs

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

Advanced Functional Programming Tim Sheard 19 Lecture 15 Final CoAlgebras Final CoAlgebras 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)) unFinal :: Final a -> a (Final a) unFinal (Final x) = x finalCoalg :: CoAlgebra a (Final a) finalCoalg = CoAlgebra unFinal

Advanced Functional Programming Tim Sheard 20 Lecture 15 Example Final CoAlgebra elements f1 :: Final (ListF a) f1 = Final Nil ones :: Final (StreamF Integer) ones = Final(C 1 ones)

Advanced Functional Programming Tim Sheard 21 Lecture 15 Iterating We can write functions producing elements in the sort of Final CoAlgebras 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 (StreamF Integer) nats = g 0 where g n = Final (C n (g (n+1)))

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

Advanced Functional Programming Tim Sheard 23 Lecture 15 CoHomomorphisms A CoHomomorphism is an arrow in the category of F-CoAlgebras data CoHom f a b = CoHom (CoAlgebra f a) (CoAlgebra f b) (a->b) For every arrow in the category (CoHom (CoAlgebra f) (CoAlgebra g) h) it must be the case that covalid :: (Eq (f b), Functor f) => CoHom f a b -> a -> Bool covalid (CoHom (CoAlgebra f) (CoAlgebra g) h) x = fmap h (f x) == g(h x) a b F b F a Fmap h h f g

Advanced Functional Programming Tim Sheard 24 Lecture 15 Final CoAlegbra A F-CoAlgebra, g, is Final if for any other F- CoAlgebra, f, there is a unique F-CoAlgebra homomorphism, h, from f to g. We can show its existence be building a function that computes it from the CoAlgebra, f. ana :: Functor f => (CoAlgebra f seed) -> seed -> (Final f) ana (CoAlgebra phi) seed = Final(fmap (ana (CoAlgebra phi)) (phi seed)) exhibit2 :: Functor f => CoAlgebra f seed -> CoHom f seed (Final f) exhibit2 x = CoHom finalCoalg x (ana x)

Advanced Functional Programming Tim Sheard 25 Lecture 15 Examples We use ana to iteratively unfold any coAgebra to record its observations final1 = ana endsIn0s final2 = ana split final3 = ana fibs tak :: Num a => a -> Final (StreamF b) -> [b] tak 0 _ = [] tak n (Final (C x xs)) = x : tak (n-1) xs fibs5 = tak 5 (final3 (1,1)) endsIn0s = CoAlgebra f where f [] = C 0 [] f (x:xs) = C x xs split = CoAlgebra f where f 0 = Zero f 1 = One f n = Plus (n-1) (n-2) fibs :: CoAlgebra (StreamF Int) (Int,Int) fibs = CoAlgebra f where f (x,y) = C (x+y) (y,x+y)

Advanced Functional Programming Tim Sheard 26 Lecture 15 CoAlgebras and ObjectOrientation Lets use CoAlgebras 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 -> Float -> x} pointF :: (Float,Float) -> P (Float,Float) pointF (x,y) = P { xcoord = x, ycoord = y, move = \ m n -> (m+x,n+y) } type Point = CoAlgebra P (Float,Float) point1 :: Point point1 = CoAlgebra pointF