SlideShare une entreprise Scribd logo
1  sur  35
datatypes
a La Carte
  08 25 2011




               Yun-Yan Chi
outline

• modular syntax
 •   signature functor
 •   recursion
 •   functor combination
• modular semantics
 •   fold for type Fix
 •   evaluation-algebra
outline

• modular syntax
 •   signature functor
 •   recursion
 •   functor combination
• modular semantics
 •   fold for type Fix
 •   evaluation-algebra
flatten a recursive
      datatype:
data Term = Val Int | Add Term Term
           | Throw | Catch Term Term
flatten a recursive
      datatype:
data Term = Val Int | Add Term Term
           | Throw | Catch Term Term
           | If Bool Term Term
flatten a recursive
       datatype:
data Term = Val Int | Add Term Term
           | Throw | Catch Term Term


data Arith e = Val Int | Add e e
data Except e = Throw | Catch e e      the Menu
flatten a recursive
       datatype:
data Term = Val Int | Add Term Term
           | Throw | Catch Term Term
           | If Bool Term Term
data Arith e = Val Int | Add e e
data Except e = Throw | Catch e e      the Menu

data Branch e = If Bool e e
signature functor:
data Arith e = Val Int | Add e e
data Except e = Throw | Catch e e

instance Functor Except where
   fmap f Throw = Throw
   fmap f (Catch x g) = Catch (f x) (f g)
instance Functor Arith where
   fmap f (Val n) = Val n
   fmap f (Add x y) = Add (f x) (f y)
the “recursive”:

    (Fix F = F(Fix F
          )         )

  data Fix f = In (f (Fix f))
the “recursive”:

              (Fix F = F(Fix F
                    )         )

            data Fix f = In (f (Fix f))

example :
   (In $ Val 2) :: Fix Arith
the “recursive”:

              (Fix F = F(Fix F
                    )         )

            data Fix f = In (f (Fix f))

example :
   In $ (Add (In $ Val 1) (In $ Val 2)) :: Fix Arith
the “recursive”:

              (Fix F = F(Fix F
                    )         )

            data Fix f = In (f (Fix f))

example :
   In $ Add (In $ Throw) (In $ Val 2) :: ???
the “recursive”:

                    (Fix F = F(Fix F
                          )         )

                data Fix f = In (f (Fix f))

example :
   In $ Add (In $ Throw) (In $ Val 2) :: ???
   Couldn't match expected type `Arith' with actual type `Except'
combining functor by
             coproduct:
           C
     f               g         data (f :+: g) e = Inl (f e)
                                                 | Inr (f e)
A           [f, g]         B
                               instance (Functor f, Functor g)
                                 => Functor (f :+: g) where
    inl              inr          fmap h (Inl x) = Inl (fmap h x)
                                  fmap h (Inr y) = Inr (fmap h y)
          A+B
combining functor by
     coproduct:
• In $ Val 2 :: Fix Arith
• In $ Inl $ Val 2 :: Fix (Arith :+: g)
• In $ Add (In $ Throw) (In $ Val 2) :: ???
• In $ Inl $ Add
     (In $ Inr $ Throw)
     (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
combining functor by
     coproduct:
• In $ Val 2 :: Fix Arith
• In $ Inl $ Val 2 :: Fix (Arith :+: g)
• In $ Add (In $ Throw) (In $ Val 2) :: ???
• In $ Inl $ Add
     (In $ Inr $ Throw)
     (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
combining functor by
     coproduct:
• In $ Val 2 :: Fix Arith
• In $ Inl $ Val 2 :: Fix (Arith :+: g)
• In $ Add (In $ Throw) (In $ Val 2) :: ???
• In $ Inl $ Add
     (In $ Inr $ Throw)
     (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
outline

• modular syntax
 •   signature functor
 •   recursion
 •   functor combination
• modular semantics
 •   fold for type Fix
 •   evaluation-algebra
why fold

• A recursive datatype, (Fix f), represents a
  expression language.
• the semantics of such expression language
  has type: Fix f → m Value
• we need fold for type Fix
fold for type Fix
              a
          T        FT


([ f ])                 F([ f ])


          A        FA
              f
fold for type Fix
             a
 (Fix F)           F(Fix F)


([ f ])                F([ f ])


m Value            F(m Value)
             f
fold for type Fix
           (pattern match)
 (Fix F)                     F(Fix F)


fold f                          fmap (fold f )


m Value                      F(m Value)
                   f
fold for type Fix
               (pattern match)
 (Fix F)                         F(Fix F)


fold f                              fmap (fold f )


m Value                          F(m Value)
                       f
fold f = ...
fold for type Fix
             (pattern match)
 (Fix F)                       F(Fix F)


fold f                            fmap (fold f )


m Value                        F(m Value)
                       f
fold f = (In t) → t
fold for type Fix
             (pattern match)
 (Fix F)                         F(Fix F)


fold f                                 fmap (fold f )


m Value                          F(m Value)
                     f
fold f = (In t) → fmap (fold f) $ t
fold for type Fix
             (pattern match)
 (Fix F)                         F(Fix F)


fold f                               fmap (fold f )


m Value                          F(m Value)
                     f
fold f = (In t) → f $ fmap (fold f) $ t
evaluation-algebra

• fold :: Functor f => (f a → a) → Fix f → a
• fold f = (In t) → f (fmap (fold f) t)
• we need a (f a → a) as a parameter
  •   f-algebra

  •   evaluation-algebra
evaluation-algebra

                  eval-alge
   m Value                       F(m Value)

class (Monad m, Functor f) => EvalAlge f m where
   evalAlge :: f (m Value) →(m Value)
evaluation-algebra

instance Monad m => EvalAlge Arith m where
   evalAlge (Val n) = return n
   evalAlge (Add x y) = x >>= m ->
                         y >>= n ->
                         return (m+n)
evaluation-algebra

instance ExceptMonad m =>
   EvalAlge Except m where
      evalAlge Throw = throw
      evalAlge (Catch x h) = x `catch` h
evaluation-algebra
                         m Value
              evalAlge             evalAlge


Arith (m Value)                           Except (m Value)
evaluation-algebra
                         m Value
              evalAlge                                      evalAlge


                          [evalAlge, evalAlge]

                                                 evalAlge
Arith (m Value)                                                    Except (m Value)

                   inl                                       inr

                  Arith :+: Except
evaluation-algebra

instance (EvalAlge f m, EvalAlge g m) =>
   EvalAlge (f :+: g) m where
      evalAlge (Inl x) = evalAlge x
      evalAlge (Inr y) = evalAlge y
evaluation
• fold :: Functor f => (f a → a) → Fix f → a
• evalAlge :: f (m Value) →(m Value)
• eval :: (Functor f, Monad m) => Fix f → m Value
• eval = fold evalAlg
• here, f can be Arith, Except or (Arith :+: Except)
Question?

Contenu connexe

Tendances

Kleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationKleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationPhilip Schwarz
 
5.1 Defining and visualizing functions. A handout.
5.1 Defining and visualizing functions. A handout.5.1 Defining and visualizing functions. A handout.
5.1 Defining and visualizing functions. A handout.Jan Plaza
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersPiotr Paradziński
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Philip Schwarz
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New GameJohn De Goes
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Philip Schwarz
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type ClassesJohn De Goes
 
Category theory for beginners
Category theory for beginnersCategory theory for beginners
Category theory for beginnerskenbot
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- BasicsRabin BK
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsPhilip Schwarz
 
Maple Code for Steepest Descent
Maple Code for Steepest DescentMaple Code for Steepest Descent
Maple Code for Steepest DescentJeremy Lane
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Philip Schwarz
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 

Tendances (20)

Kleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationKleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelation
 
5.1 Defining and visualizing functions. A handout.
5.1 Defining and visualizing functions. A handout.5.1 Defining and visualizing functions. A handout.
5.1 Defining and visualizing functions. A handout.
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
 
Functors
FunctorsFunctors
Functors
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New Game
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'
 
Compiling fµn language
Compiling fµn languageCompiling fµn language
Compiling fµn language
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type Classes
 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
 
Category theory for beginners
Category theory for beginnersCategory theory for beginners
Category theory for beginners
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
 
18560 lecture6
18560 lecture618560 lecture6
18560 lecture6
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
 
Maple Code for Steepest Descent
Maple Code for Steepest DescentMaple Code for Steepest Descent
Maple Code for Steepest Descent
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 

Similaire à Data type a la carte

The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)Eric Torreborre
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to MonadsLawrence Evans
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit partsMaxim Zaks
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10Boipelo Radebe
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in ScalaKnoldus Inc.
 
Unification and Anti-Unification Algorithm
Unification and Anti-Unification AlgorithmUnification and Anti-Unification Algorithm
Unification and Anti-Unification AlgorithmRiotRiot2
 
A1 11 functions
A1 11 functionsA1 11 functions
A1 11 functionsvhiggins1
 
5.6 Function inverse. Dynamic slides.
5.6 Function inverse. Dynamic slides.5.6 Function inverse. Dynamic slides.
5.6 Function inverse. Dynamic slides.Jan Plaza
 

Similaire à Data type a la carte (20)

The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in Scala
 
Unification and Anti-Unification Algorithm
Unification and Anti-Unification AlgorithmUnification and Anti-Unification Algorithm
Unification and Anti-Unification Algorithm
 
Functions.ppt
Functions.pptFunctions.ppt
Functions.ppt
 
Functions.ppt
Functions.pptFunctions.ppt
Functions.ppt
 
functions.pdf
functions.pdffunctions.pdf
functions.pdf
 
A1 11 functions
A1 11 functionsA1 11 functions
A1 11 functions
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 
Thesis
ThesisThesis
Thesis
 
13 05-curl-and-divergence
13 05-curl-and-divergence13 05-curl-and-divergence
13 05-curl-and-divergence
 
L04 (1)
L04 (1)L04 (1)
L04 (1)
 
Monads do not Compose
Monads do not ComposeMonads do not Compose
Monads do not Compose
 
5.6 Function inverse. Dynamic slides.
5.6 Function inverse. Dynamic slides.5.6 Function inverse. Dynamic slides.
5.6 Function inverse. Dynamic slides.
 

Plus de Yun-Yan Chi

Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Yun-Yan Chi
 
Traversing on Algebraic Datatype
Traversing on Algebraic DatatypeTraversing on Algebraic Datatype
Traversing on Algebraic DatatypeYun-Yan Chi
 
for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"Yun-Yan Chi
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013 Yun-Yan Chi
 
Machine X Language
Machine X LanguageMachine X Language
Machine X LanguageYun-Yan Chi
 
Examples for loopless
Examples for looplessExamples for loopless
Examples for looplessYun-Yan Chi
 
Genetic programming
Genetic programmingGenetic programming
Genetic programmingYun-Yan Chi
 
Paper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionPaper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionYun-Yan Chi
 
Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Yun-Yan Chi
 
Number System in Haskell
Number System in Haskell Number System in Haskell
Number System in Haskell Yun-Yan Chi
 
Constructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsConstructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsYun-Yan Chi
 

Plus de Yun-Yan Chi (13)

Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019Tezos Taipei Meetup #2 - 15/06/2019
Tezos Taipei Meetup #2 - 15/06/2019
 
Traversing on Algebraic Datatype
Traversing on Algebraic DatatypeTraversing on Algebraic Datatype
Traversing on Algebraic Datatype
 
for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"for "Parallelizing Multiple Group-by Queries using MapReduce"
for "Parallelizing Multiple Group-by Queries using MapReduce"
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013
 
Machine X Language
Machine X LanguageMachine X Language
Machine X Language
 
Examples for loopless
Examples for looplessExamples for loopless
Examples for loopless
 
Insert 2 Merge
Insert 2 MergeInsert 2 Merge
Insert 2 Merge
 
Any tutor
Any tutorAny tutor
Any tutor
 
Genetic programming
Genetic programmingGenetic programming
Genetic programming
 
Paper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognitionPaper presentation: The relative distance of key point based iris recognition
Paper presentation: The relative distance of key point based iris recognition
 
Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level Deriving a compiler and interpreter for a Multi-level
Deriving a compiler and interpreter for a Multi-level
 
Number System in Haskell
Number System in Haskell Number System in Haskell
Number System in Haskell
 
Constructing List Homomorphisms from Proofs
Constructing List Homomorphisms from ProofsConstructing List Homomorphisms from Proofs
Constructing List Homomorphisms from Proofs
 

Data type a la carte

  • 1. datatypes a La Carte 08 25 2011 Yun-Yan Chi
  • 2. outline • modular syntax • signature functor • recursion • functor combination • modular semantics • fold for type Fix • evaluation-algebra
  • 3. outline • modular syntax • signature functor • recursion • functor combination • modular semantics • fold for type Fix • evaluation-algebra
  • 4. flatten a recursive datatype: data Term = Val Int | Add Term Term | Throw | Catch Term Term
  • 5. flatten a recursive datatype: data Term = Val Int | Add Term Term | Throw | Catch Term Term | If Bool Term Term
  • 6. flatten a recursive datatype: data Term = Val Int | Add Term Term | Throw | Catch Term Term data Arith e = Val Int | Add e e data Except e = Throw | Catch e e the Menu
  • 7. flatten a recursive datatype: data Term = Val Int | Add Term Term | Throw | Catch Term Term | If Bool Term Term data Arith e = Val Int | Add e e data Except e = Throw | Catch e e the Menu data Branch e = If Bool e e
  • 8. signature functor: data Arith e = Val Int | Add e e data Except e = Throw | Catch e e instance Functor Except where fmap f Throw = Throw fmap f (Catch x g) = Catch (f x) (f g) instance Functor Arith where fmap f (Val n) = Val n fmap f (Add x y) = Add (f x) (f y)
  • 9. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))
  • 10. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f)) example : (In $ Val 2) :: Fix Arith
  • 11. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f)) example : In $ (Add (In $ Val 1) (In $ Val 2)) :: Fix Arith
  • 12. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f)) example : In $ Add (In $ Throw) (In $ Val 2) :: ???
  • 13. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f)) example : In $ Add (In $ Throw) (In $ Val 2) :: ??? Couldn't match expected type `Arith' with actual type `Except'
  • 14. combining functor by coproduct: C f g data (f :+: g) e = Inl (f e) | Inr (f e) A [f, g] B instance (Functor f, Functor g) => Functor (f :+: g) where inl inr fmap h (Inl x) = Inl (fmap h x) fmap h (Inr y) = Inr (fmap h y) A+B
  • 15. combining functor by coproduct: • In $ Val 2 :: Fix Arith • In $ Inl $ Val 2 :: Fix (Arith :+: g) • In $ Add (In $ Throw) (In $ Val 2) :: ??? • In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  • 16. combining functor by coproduct: • In $ Val 2 :: Fix Arith • In $ Inl $ Val 2 :: Fix (Arith :+: g) • In $ Add (In $ Throw) (In $ Val 2) :: ??? • In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  • 17. combining functor by coproduct: • In $ Val 2 :: Fix Arith • In $ Inl $ Val 2 :: Fix (Arith :+: g) • In $ Add (In $ Throw) (In $ Val 2) :: ??? • In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  • 18. outline • modular syntax • signature functor • recursion • functor combination • modular semantics • fold for type Fix • evaluation-algebra
  • 19. why fold • A recursive datatype, (Fix f), represents a expression language. • the semantics of such expression language has type: Fix f → m Value • we need fold for type Fix
  • 20. fold for type Fix a T FT ([ f ]) F([ f ]) A FA f
  • 21. fold for type Fix a (Fix F) F(Fix F) ([ f ]) F([ f ]) m Value F(m Value) f
  • 22. fold for type Fix (pattern match) (Fix F) F(Fix F) fold f fmap (fold f ) m Value F(m Value) f
  • 23. fold for type Fix (pattern match) (Fix F) F(Fix F) fold f fmap (fold f ) m Value F(m Value) f fold f = ...
  • 24. fold for type Fix (pattern match) (Fix F) F(Fix F) fold f fmap (fold f ) m Value F(m Value) f fold f = (In t) → t
  • 25. fold for type Fix (pattern match) (Fix F) F(Fix F) fold f fmap (fold f ) m Value F(m Value) f fold f = (In t) → fmap (fold f) $ t
  • 26. fold for type Fix (pattern match) (Fix F) F(Fix F) fold f fmap (fold f ) m Value F(m Value) f fold f = (In t) → f $ fmap (fold f) $ t
  • 27. evaluation-algebra • fold :: Functor f => (f a → a) → Fix f → a • fold f = (In t) → f (fmap (fold f) t) • we need a (f a → a) as a parameter • f-algebra • evaluation-algebra
  • 28. evaluation-algebra eval-alge m Value F(m Value) class (Monad m, Functor f) => EvalAlge f m where evalAlge :: f (m Value) →(m Value)
  • 29. evaluation-algebra instance Monad m => EvalAlge Arith m where evalAlge (Val n) = return n evalAlge (Add x y) = x >>= m -> y >>= n -> return (m+n)
  • 30. evaluation-algebra instance ExceptMonad m => EvalAlge Except m where evalAlge Throw = throw evalAlge (Catch x h) = x `catch` h
  • 31. evaluation-algebra m Value evalAlge evalAlge Arith (m Value) Except (m Value)
  • 32. evaluation-algebra m Value evalAlge evalAlge [evalAlge, evalAlge] evalAlge Arith (m Value) Except (m Value) inl inr Arith :+: Except
  • 33. evaluation-algebra instance (EvalAlge f m, EvalAlge g m) => EvalAlge (f :+: g) m where evalAlge (Inl x) = evalAlge x evalAlge (Inr y) = evalAlge y
  • 34. evaluation • fold :: Functor f => (f a → a) → Fix f → a • evalAlge :: f (m Value) →(m Value) • eval :: (Functor f, Monad m) => Fix f → m Value • eval = fold evalAlg • here, f can be Arith, Except or (Arith :+: Except)