Slide 1

Slide 1 text

Embedded Pattern Matching Trevor L. McDonell Joshua D. Meredith Gabriele Keller

Slide 2

Slide 2 text

Algebraic data types data List a = Nil | Cons a (List a) 2

Slide 3

Slide 3 text

Algebraic data types data List a = Nil | Cons a (List a) length : : List a - > Int length Nil = 0 length (Cons _ xs) = 1 + length xs 2

Slide 4

Slide 4 text

Embedded languages https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/gadt.html data Exp a where Lit : : Int - > Exp Int Succ : : Exp Int - > Exp Int . . . 3

Slide 5

Slide 5 text

ans : : Exp Int ans = Succ (Lit 41) Embedded languages https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/gadt.html data Exp a where Lit : : Int - > Exp Int Succ : : Exp Int - > Exp Int . . . 3

Slide 6

Slide 6 text

ans : : Exp Int ans = Succ (Lit 41) Embedded languages https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/gadt.html data Exp a where Lit : : Int - > Exp Int Succ : : Exp Int - > Exp Int . . . 3 eval : : Exp Int - > Int eval (Lit i) = i eval (Succ x) = 1 + eval x . . .

Slide 7

Slide 7 text

ans : : Exp Int ans = Succ (Lit 41) Embedded languages https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/gadt.html data Exp a where Lit : : Int - > Exp Int Succ : : Exp Int - > Exp Int . . . 3 eval : : Exp Int - > Int eval (Lit i) = i eval (Succ x) = 1 + eval x . . .

Slide 8

Slide 8 text

ans : : Exp Int ans = Succ (Lit 41) Embedded languages https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/gadt.html data Exp a where Lit : : Int - > Exp Int Succ : : Exp Int - > Exp Int . . . 3 eval : : Exp Int - > Int eval (Lit i) = i eval (Succ x) = 1 + eval x . . .

Slide 9

Slide 9 text

Embedded languages … with ADTs? length’ : : Exp (List a) - > Exp Int length’ = . . . ? 4

Slide 10

Slide 10 text

Embedded languages … with ADTs? length’ : : Exp (List a) - > Exp Int length’ = . . . ? 4

Slide 11

Slide 11 text

The Problem • Can we have: - A deeply embedded language, supporting… - User-de fi ned algebraic data-types, together with… - Construction and pattern matching on embedded terms of algebraic data type? 5 https://poorlydrawnlines.com/comic/the-one-they-call/

Slide 12

Slide 12 text

Option 1: Lots of type errors • De fi ne functions to “push” a constructor through an expression 6 Data.Array.Accelerate: https://hackage.haskell.org/package/accelerate-1.3.0.0/docs/Data-Array-Accelerate.html#g:47 pair : : Exp a - > Exp b - > Exp (a, b) pair x y = lift (x, y) fst : : Exp (a, b) - > Exp a fst p = let (a, _) = unlift p in a

Slide 13

Slide 13 text

Option 1: Lots of type errors • De fi ne functions to “push” a constructor through an expression 6 Data.Array.Accelerate: https://hackage.haskell.org/package/accelerate-1.3.0.0/docs/Data-Array-Accelerate.html#g:47 pair : : Exp a - > Exp b - > Exp (a, b) pair x y = lift (x, y) fst : : Exp (a, b) - > Exp a fst p = let (a, _) = unlift p in a Lift.hs:14 : 29 : error: • Couldn't match type ‘b’ with ‘Plain b0’ ‘b’ is a rigid type variable bound by the type signature for: Lift.fst : : forall a b. (Elt a, Elt b) = > Exp (a, b) - > Exp a at Lift.hs:13 : 1-44 Expected type: Exp (Plain (Exp a, b0)) Actual type: Exp (a, b) • In the f i rst argument of ‘unlift’, namely ‘p’ In the expression: unlift p In a pattern binding: (a, _) = unlift p • Relevant bindings include p : : Exp (a, b) (bound at Lift.hs:14 : 5) fst : : Exp (a, b) - > Exp a (bound at Lift.hs:14 : 1) | 14 | fst p = let (a, _) = unlift p in a | ^

Slide 14

Slide 14 text

Option 2: Don’t do that • Keep terms in the “unlifted” form - Avoids the need to lift and unlift terms - Relies on compiler magic to remove redundancy 7 Combining Deep and Shallow Embedding for EDSL, Svenningsson J and Axelsson, E. dup : : (Exp a, Exp b) dup = ( fst extremly_expensive_thing , snd extremly_expensive_thing ) fst : : (Exp a, Exp b) - > Exp a fst (a, _) = a

Slide 15

Slide 15 text

A subtle limitation • Both of these approaches require the constructor to be polymorphic - There is no way to “unlift” a term of: 8 data Point = Point F l oat F l oat

Slide 16

Slide 16 text

The killer • Neither approach supports sum data types - Although for a selection of “built in” types you can use a combinator instead of pattern matching 9 maybe : : Exp b - - default value if Nothing - > (Exp a - > Exp b) - - function to apply if Just - > Exp (Maybe a) - > Exp b maybe = . . . ?

Slide 17

Slide 17 text

Ingredient #1: Expression language 10

Slide 18

Slide 18 text

Grammar • A minimal expression language - Variables, application, abstraction, etc. are standard - Construction and access to pairs, as well as case expressions - Uniform representation of all user-de fi ned data-types as nested pairs 11 Embedded Pa￿ern Matching 1:9 ⇢ ::= ⇠ Constant | G Variable | let E0A = ⇢ in ⇢ Let binding | _G. ⇢ Abstraction | ⇢ ⇢ Application | ) Tuples | c ⇢ Projection | CA024 ⇢ Pattern match | ⇢ [ ⇢1 ...⇢= ] Case expression ) ::= () Unit | ⇢ Expression | ) ) Pair c ::= ... Projections ⇠ ::= ... Constants E0A, G ::= ... Variable name CA024 ::= ... Match trace Fig. 4. The grammar of our embedded language

Slide 19

Slide 19 text

Grammar • A minimal expression language - Variables, application, abstraction, etc. are standard - Construction and access to pairs, as well as case expressions - Uniform representation of all user-de fi ned data-types as nested pairs 11 Embedded Pa￿ern Matching 1:9 ⇢ ::= ⇠ Constant | G Variable | let E0A = ⇢ in ⇢ Let binding | _G. ⇢ Abstraction | ⇢ ⇢ Application | ) Tuples | c ⇢ Projection | CA024 ⇢ Pattern match | ⇢ [ ⇢1 ...⇢= ] Case expression ) ::= () Unit | ⇢ Expression | ) ) Pair c ::= ... Projections ⇠ ::= ... Constants E0A, G ::= ... Variable name CA024 ::= ... Match trace Fig. 4. The grammar of our embedded language

Slide 20

Slide 20 text

Grammar • A minimal expression language - Variables, application, abstraction, etc. are standard - Construction and access to pairs, as well as case expressions - Uniform representation of all user-de fi ned data-types as nested pairs 11 Embedded Pa￿ern Matching 1:9 ⇢ ::= ⇠ Constant | G Variable | let E0A = ⇢ in ⇢ Let binding | _G. ⇢ Abstraction | ⇢ ⇢ Application | ) Tuples | c ⇢ Projection | CA024 ⇢ Pattern match | ⇢ [ ⇢1 ...⇢= ] Case expression ) ::= () Unit | ⇢ Expression | ) ) Pair c ::= ... Projections ⇠ ::= ... Constants E0A, G ::= ... Variable name CA024 ::= ... Match trace Fig. 4. The grammar of our embedded language

Slide 21

Slide 21 text

Embedding Point 12 data Point = Point F l oat F l oat

Slide 22

Slide 22 text

Embedding Point 12 data Point = Point F l oat F l oat Surface type (extensible)

Slide 23

Slide 23 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) Surface type (extensible)

Slide 24

Slide 24 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) Representation type (closed) Surface type (extensible)

Slide 25

Slide 25 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y Representation type (closed) Surface type (extensible)

Slide 26

Slide 26 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple Representation type (closed) Surface type (extensible)

Slide 27

Slide 27 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple $ Unit `Pair` Exp x `Pair` Exp y Representation type (closed) Surface type (extensible)

Slide 28

Slide 28 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple $ Unit `Pair` Exp x `Pair` Exp y matchPoint : : Exp Point - > (Exp F l oat, Exp F l oat) Representation type (closed) Surface type (extensible)

Slide 29

Slide 29 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple $ Unit `Pair` Exp x `Pair` Exp y matchPoint : : Exp Point - > (Exp F l oat, Exp F l oat) matchPoint p = ( Prj . . . p , Prj . . . p ) Representation type (closed) Surface type (extensible)

Slide 30

Slide 30 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple $ Unit `Pair` Exp x `Pair` Exp y matchPoint : : Exp Point - > (Exp F l oat, Exp F l oat) matchPoint p = ( Prj . . . p , Prj . . . p ) Representation type (closed) Surface type (extensible) Haskell pair of 
 embedded expressions

Slide 31

Slide 31 text

Embedding Point 12 data Point = Point F l oat F l oat - - EltR Point = (((), F l oat), F l oat) buildPoint : : Exp F l oat - > Exp F l oat - > Exp Point buildPoint x y = Tuple $ Unit `Pair` Exp x `Pair` Exp y matchPoint : : Exp Point - > (Exp F l oat, Exp F l oat) matchPoint p = ( Prj . . . p , Prj . . . p ) Representation type (closed) Surface type (extensible) Haskell pair of 
 embedded expressions Can only ever add 
 new embedded terms

Slide 32

Slide 32 text

Ingredient #2: Pattern synonyms 13

Slide 33

Slide 33 text

Pattern synonyms • A Haskell extension which allows programmers to de fi ne new patterns 14 Pattern Synonyms, Pickering, M and Érdi, G and Peyton-Jones, S and Eisenburg, R. A. pattern Point_ : : Exp F l oat - > Exp F l oat - > Exp Point pattern Point_ x y < - (matchPoint - > (x, y)) where Point_ = buildPoint

Slide 34

Slide 34 text

Pattern synonyms • A Haskell extension which allows programmers to de fi ne new patterns - The builder is run when used as an expression (when constructing) 14 Pattern Synonyms, Pickering, M and Érdi, G and Peyton-Jones, S and Eisenburg, R. A. pattern Point_ : : Exp F l oat - > Exp F l oat - > Exp Point pattern Point_ x y < - (matchPoint - > (x, y)) where Point_ = buildPoint

Slide 35

Slide 35 text

Pattern synonyms • A Haskell extension which allows programmers to de fi ne new patterns - The builder is run when used as an expression (when constructing) - The matcher is run when used as a pattern (when matching) 14 Pattern Synonyms, Pickering, M and Érdi, G and Peyton-Jones, S and Eisenburg, R. A. pattern Point_ : : Exp F l oat - > Exp F l oat - > Exp Point pattern Point_ x y < - (matchPoint - > (x, y)) where Point_ = buildPoint

Slide 36

Slide 36 text

Pattern synonyms • A Haskell extension which allows programmers to de fi ne new patterns - The builder is run when used as an expression (when constructing) - The matcher is run when used as a pattern (when matching) 14 Pattern Synonyms, Pickering, M and Érdi, G and Peyton-Jones, S and Eisenburg, R. A. pattern Point_ : : Exp F l oat - > Exp F l oat - > Exp Point pattern Point_ x y < - (matchPoint - > (x, y)) where Point_ = buildPoint scale : : Exp F l oat - > Exp Point - > Exp Point scale alpha (Point_ x y) = Point_ (x * alpha) (y * alpha)

Slide 37

Slide 37 text

Sums • Can we extend this to sum data types? 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = . . . ?

Slide 38

Slide 38 text

Sums • Can we extend this to sum data types? 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x - - -XMagicUnderscore . . . ?

Slide 39

Slide 39 text

Sums • Can we extend this to sum data types? - There is a staging problem here 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x - - incomplete!

Slide 40

Slide 40 text

Sums • Can we extend this to sum data types? - There is a staging problem here 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe - - incomplete!

Slide 41

Slide 41 text

Sums • Can we extend this to sum data types? - There is a staging problem here 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe Host runtime generate
 embedded program - - incomplete!

Slide 42

Slide 42 text

Sums • Can we extend this to sum data types? - There is a staging problem here 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe Host runtime Embedded compile time generate
 embedded program Exp compile .o - - incomplete!

Slide 43

Slide 43 text

Sums • Can we extend this to sum data types? - There is a staging problem here 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe Host runtime Embedded compile time Embedded runtime generate
 embedded program Exp compile .o evaluate
 compiled program … - - incomplete!

Slide 44

Slide 44 text

Sums • Can we extend this to sum data types? - There is a staging problem here - Pattern matching occurs during runtime of the host program, 
 but the value will only be known during execution of the embedded program 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe Host runtime Embedded compile time Embedded runtime generate
 embedded program Exp compile .o evaluate
 compiled program … - - incomplete!

Slide 45

Slide 45 text

Sums • Can we extend this to sum data types? - There is a staging problem here - Pattern matching occurs during runtime of the host program, 
 but the value will only be known during execution of the embedded program 15 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case Nothing_ - > d Just_ x - > f x Host compile time .hs GHC .exe Host runtime Embedded compile time Embedded runtime generate
 embedded program Exp compile .o evaluate
 compiled program … Could be executed on 
 a different device, etc. - - incomplete!

Slide 46

Slide 46 text

Ingredient #3: match 16

Slide 47

Slide 47 text

Key idea • We need to evaluate this function twice - Supply some dummy argument to force each pattern match to succeed in turn - Combine each exposed right-hand-side into an embedded case statement 17 maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = \case - - incomplete! Nothing_ - > d Just_ x - > f x

Slide 48

Slide 48 text

• We can anticipate what the embedded pattern synonym must look like Embedding Maybe pattern Just_ : : Exp a - > Exp (Maybe a) pattern Just_ x < - (matchJust - > Just x) where Just_ = buildJust 18

Slide 49

Slide 49 text

• We can anticipate what the embedded pattern synonym must look like - The builder just wraps the argument in some tag to indicate which variant this term represents Embedding Maybe pattern Just_ : : Exp a - > Exp (Maybe a) pattern Just_ x < - (matchJust - > Just x) where Just_ = buildJust 18

Slide 50

Slide 50 text

• We can anticipate what the embedded pattern synonym must look like - The builder just wraps the argument in some tag to indicate which variant this term represents - But the matcher must signal to the host language pattern matcher somehow Embedding Maybe pattern Just_ : : Exp a - > Exp (Maybe a) pattern Just_ x < - (matchJust - > Just x) where Just_ = buildJust 18

Slide 51

Slide 51 text

• We can anticipate what the embedded pattern synonym must look like - The builder just wraps the argument in some tag to indicate which variant this term represents - But the matcher must signal to the host language pattern matcher somehow Embedding Maybe pattern Just_ : : Exp a - > Exp (Maybe a) pattern Just_ x < - (matchJust - > Just x) where Just_ = buildJust 18

Slide 52

Slide 52 text

Embedding Maybe 19 data Maybe a = Nothing | Just a

Slide 53

Slide 53 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a))

Slide 54

Slide 54 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) Tag

Slide 55

Slide 55 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) Tag Fields of Nothing

Slide 56

Slide 56 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) Tag Fields of Nothing Fields of Just

Slide 57

Slide 57 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x Tag Fields of Nothing Fields of Just

Slide 58

Slide 58 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple Tag Fields of Nothing Fields of Just

Slide 59

Slide 59 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) Tag Fields of Nothing Fields of Just

Slide 60

Slide 60 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) Tag Fields of Nothing Fields of Just

Slide 61

Slide 61 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) Tag Fields of Nothing Fields of Just

Slide 62

Slide 62 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) Tag Fields of Nothing Fields of Just

Slide 63

Slide 63 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) Tag Fields of Nothing Fields of Just Embedded 
 expression

Slide 64

Slide 64 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value!

Slide 65

Slide 65 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! Embedded expression 
 to extract the value

Slide 66

Slide 66 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) matchJust = \case Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! Embedded expression 
 to extract the value

Slide 67

Slide 67 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) matchJust = \case Match a - > Just ( Prj . . . a ) Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! Embedded expression 
 to extract the value

Slide 68

Slide 68 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) matchJust = \case Match a - > Just ( Prj . . . a ) Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! Embedded expression 
 to extract the value

Slide 69

Slide 69 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) matchJust = \case Match a - > Just ( Prj . . . a ) Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! The Haskell pattern 
 match should succeed Embedded expression 
 to extract the value

Slide 70

Slide 70 text

Embedding Maybe 19 data Maybe a = Nothing | Just a - - EltR (Maybe a) = (Word8, ((), EltR a)) buildJust : : Exp a - > Exp (Maybe a) buildJust x = Tuple $ Exp (Const 1) `Pair` (Unit `Pair` Exp x) matchJust : : Exp (Maybe a) - > Maybe (Exp a) matchJust = \case Match a - > Just ( Prj . . . a ) Match _ _ - > Nothing Tag Fields of Nothing Fields of Just Embedded 
 expression Regular Haskell value! The Haskell pattern 
 match should succeed Embedded expression 
 to extract the value

Slide 71

Slide 71 text

Key idea • We need to evaluate this function twice 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 72

Slide 72 text

Key idea • We need to evaluate this function twice 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 73

Slide 73 text

Key idea • We need to evaluate this function twice 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 74

Slide 74 text

Key idea • We need to evaluate this function twice - Match is the dummy argument consumed by the embedded pattern synonym - The indicates which constructor(s) we are interested in 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 75

Slide 75 text

Key idea • We need to evaluate this function twice - Match is the dummy argument consumed by the embedded pattern synonym - The indicates which constructor(s) we are interested in 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 76

Slide 76 text

Key idea • We need to evaluate this function twice - Match is the dummy argument consumed by the embedded pattern synonym - The indicates which constructor(s) we are interested in 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 77

Slide 77 text

Key idea • We need to evaluate this function twice - Match is the dummy argument consumed by the embedded pattern synonym - The indicates which constructor(s) we are interested in 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 78

Slide 78 text

Key idea • We need to evaluate this function twice - Match is the dummy argument consumed by the embedded pattern synonym - The indicates which constructor(s) we are interested in 20 match_maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b match_maybe d f p = let rhs_nothing = maybe d f (Match p) rhs_just = maybe d f (Match p) in Case p [ (, rhs_nothing) , (, rhs_just) ]

Slide 79

Slide 79 text

• We can automate this process with a single function: Embedded Pattern Matching 21 (See paper for details) match : : Matching f = > f - > f

Slide 80

Slide 80 text

• We can automate this process with a single function: Embedded Pattern Matching 21 (See paper for details) match : : Matching f = > f - > f instance Matching (Exp r) instance Matching r = > Matching (Exp e - > r)

Slide 81

Slide 81 text

• We can automate this process with a single function: Embedded Pattern Matching 21 (See paper for details) match : : Matching f = > f - > f instance Matching (Exp r) instance Matching r = > Matching (Exp e - > r) maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = Nothing_ - > d Just_ x - > f x \case

Slide 82

Slide 82 text

match • We can automate this process with a single function: Embedded Pattern Matching 21 (See paper for details) match : : Matching f = > f - > f instance Matching (Exp r) instance Matching r = > Matching (Exp e - > r) maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = Nothing_ - > d Just_ x - > f x \case

Slide 83

Slide 83 text

match • We can automate this process with a single function: Embedded Pattern Matching 21 (See paper for details) match : : Matching f = > f - > f instance Matching (Exp r) instance Matching r = > Matching (Exp e - > r) maybe : : Exp b - > (Exp a - > Exp b) - > Exp (Maybe a) - > Exp b maybe d f = Nothing_ - > d Just_ x - > f x \case

Slide 84

Slide 84 text

Summary We demonstrate how deeply embedded languages can support user-de fi ned algebraic data types and can hijack pattern matching of the host language to provide pattern matching in the embedded language Trevor L. McDonell Josh Meredith Gabriele Keller tmcdonell/embedded-pattern-matching

Slide 85

Slide 85 text

Summary We demonstrate how deeply embedded languages can support user-de fi ned algebraic data types and can hijack pattern matching of the host language to provide pattern matching in the embedded language Trevor L. McDonell Josh Meredith Gabriele Keller tmcdonell/embedded-pattern-matching