Generic programming with fixed points for mutually recursive - - PowerPoint PPT Presentation

generic programming with fixed points for mutually
SMART_READER_LITE
LIVE PREVIEW

Generic programming with fixed points for mutually recursive - - PowerPoint PPT Presentation

[ Faculty of Science Information and Computing Sciences] Generic programming with fixed points for mutually recursive datatypes Andres L oh joint work with Alexey Rodriguez, Stefan Holdermans, Johan Jeuring Dept. of Information and


slide-1
SLIDE 1

[Faculty of Science Information and Computing Sciences]

Generic programming with fixed points for mutually recursive datatypes

Andres L¨

  • h

joint work with Alexey Rodriguez, Stefan Holdermans, Johan Jeuring

  • Dept. of Information and Computing Sciences, Utrecht University

P.O. Box 80.089, 3508 TB Utrecht, The Netherlands Web pages: http://www.cs.uu.nl/wiki/Center

September 2, 2009

slide-2
SLIDE 2

[Faculty of Science Information and Computing Sciences] 2

Datatype-generic programming

◮ Write functions that depend on the structure of datatypes. ◮ Equality, parsing, . . . ◮ Traversing data structures, collecting or modifying items. ◮ Type-indexed data types: tries, zippers.

slide-3
SLIDE 3

[Faculty of Science Information and Computing Sciences] 3

This talk

◮ Yet another (datatype-)generic programming library for

Haskell.

◮ Gives you access to recursive positions, i.e., it is easy to

write a generic fold/catamorphism.

◮ Allows you to define type-indexed datatypes, e.g., zippers. ◮ Applicable to a large class of datatypes, in particular

mutually recursive datatypes.

slide-4
SLIDE 4

[Faculty of Science Information and Computing Sciences] 3

This talk

◮ Yet another (datatype-)generic programming library for

Haskell.

◮ Gives you access to recursive positions, i.e., it is easy to

write a generic fold/catamorphism.

◮ Allows you to define type-indexed datatypes, e.g., zippers. ◮ Applicable to a large class of datatypes, in particular

mutually recursive datatypes.

slide-5
SLIDE 5

[Faculty of Science Information and Computing Sciences] 4

What is in a generic programming library?

◮ Represent datatypes generically. ◮ Map between user types and their representations. ◮ Define functions based on representations.

slide-6
SLIDE 6

[Faculty of Science Information and Computing Sciences] 4

What is in a generic programming library?

◮ Represent datatypes generically. ◮ Map between user types and their representations. ◮ Define functions based on representations.

We focus on the first: generic view or universe.

slide-7
SLIDE 7

[Faculty of Science Information and Computing Sciences] 5

PolyP (Jansson and Jeuring 1997)

The first approach to generic programming in Haskell:

◮ Datatypes are represented as fixed points of sums of

products.

slide-8
SLIDE 8

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr

slide-9
SLIDE 9

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr As a functor: data ExprF e = ConstF Val | IfF e e e type Expr′ = Fix ExprF data Fix f = In (f (Fix f))

slide-10
SLIDE 10

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr As a functor: type ExprF e = Val | e e e type Expr′ = Fix ExprF data Fix f = In (f (Fix f))

slide-11
SLIDE 11

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr As a functor: type ExprF e = Val + e e e type Expr′ = Fix ExprF data Fix f = In (f (Fix f))

slide-12
SLIDE 12

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr As a functor: type ExprF e = Val + e × e × e type Expr′ = Fix ExprF data Fix f = In (f (Fix f))

slide-13
SLIDE 13

[Faculty of Science Information and Computing Sciences] 6

Example

data Expr = Const Val | If Expr Expr Expr As a functor: type ExprF = K Val :+: I :×: I :×: I type Expr′ = Fix ExprF data Fix f = In (f (Fix f))

slide-14
SLIDE 14

[Faculty of Science Information and Computing Sciences] 7

Combinators

data I r = I r data K a r = K a data U r = U

  • - for constructors with no arguments

data (f :+: g) r = L (f r) | R (g r) data (f :×: g) r = f r :×: g r Functors are of kind ∗ → ∗. data Fix (f :: ∗ → ∗) = In (f (Fix f))

slide-15
SLIDE 15

[Faculty of Science Information and Computing Sciences] 8

Writing a generic function

class Functor f where fmap :: (a → b) → f a → f b instance Functor (K a) where fmap f (K x) = K x instance Functor I where fmap f (I x) = I (f x)

  • - instances for the other functor combinators
slide-16
SLIDE 16

[Faculty of Science Information and Computing Sciences] 8

Writing a generic function

class Functor f where fmap :: (a → b) → f a → f b instance Functor (K a) where fmap f (K x) = K x instance Functor I where fmap f (I x) = I (f x)

  • - instances for the other functor combinators

fold :: Functor f ⇒ (f r → r) → Fix f → r fold alg (In f) = alg (fmap (fold alg) f)

slide-17
SLIDE 17

[Faculty of Science Information and Computing Sciences] 9

Summary of workflow

◮ Use a limited set of combinators to build functors (library). ◮ Express datatypes as fixed points of functors (user or

Template Haskell).

◮ Express the equivalence using a pair of conversion

functions (user or Template Haskell).

◮ Define functions (and datatypes) on the structure of

functors (library).

◮ Enjoy generic functions on all the represented datatypes

(user).

slide-18
SLIDE 18

[Faculty of Science Information and Computing Sciences] 10

Limitation of the PolyP approach

Only regular datatypes can be represented. data Expr = Const Val | If Expr Expr Expr

slide-19
SLIDE 19

[Faculty of Science Information and Computing Sciences] 10

Limitation of the PolyP approach

Only regular datatypes can be represented. data Expr = Const Val | If Expr Expr Expr | Bin Expr Op Expr

slide-20
SLIDE 20

[Faculty of Science Information and Computing Sciences] 10

Limitation of the PolyP approach

Only regular datatypes can be represented. data Expr = Const Val | If Expr Expr Expr | Bin Expr Op Expr data Op = Add | Mul | Infix Expr | Flip Op

slide-21
SLIDE 21

[Faculty of Science Information and Computing Sciences] 10

Limitation of the PolyP approach

Only regular datatypes can be represented. data Expr = Const Val | If Expr Expr Expr | Bin Expr Op Expr data Op = Add | Mul | Infix Expr | Flip Op Typical ASTs are not regular, but a family of several mutually recursive datatypes.

slide-22
SLIDE 22

[Faculty of Science Information and Computing Sciences] 11

Classic attempts

data Expr = Const Val | If Expr Expr Expr data ExprF e = ConstF Val | IfF e e e type Expr′ = Fix ExprF

slide-23
SLIDE 23

[Faculty of Science Information and Computing Sciences] 11

Classic attempts

data Expr = Const Val | If Expr Expr Expr | Bin Expr Op Expr data Op = Add | Mul | Infix Expr | Flip Op data ExprF e o = ConstF Val | IfF e e e | BinF e o e data OpF e o = AddF | MulF | InfixF e | FlipF o type Expr′ = Fix2,0 ExprF OpF type Op′ = Fix2,1 ExprF OpF

slide-24
SLIDE 24

[Faculty of Science Information and Computing Sciences] 12

Kinds

Fix :: (∗ → ∗) → ∗

slide-25
SLIDE 25

[Faculty of Science Information and Computing Sciences] 12

Kinds

Fix :: (∗ → ∗) → ∗ Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗

slide-26
SLIDE 26

[Faculty of Science Information and Computing Sciences] 12

Kinds

Fix :: (∗ → ∗) → ∗ Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix3,0 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗ Fix3,1 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗ Fix3,2 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗

slide-27
SLIDE 27

[Faculty of Science Information and Computing Sciences] 12

Kinds

Fix :: (∗ → ∗) → ∗ Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix3,0 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗ Fix3,1 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗ Fix3,2 :: (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → (∗ → ∗ → ∗ → ∗) → ∗ . . .

slide-28
SLIDE 28

[Faculty of Science Information and Computing Sciences] 13

Kinds (contd.)

Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗

slide-29
SLIDE 29

[Faculty of Science Information and Computing Sciences] 13

Kinds (contd.)

Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ If we had tuples on the kind level: Fix2 :: (∗2 → ∗)2 → ∗2

slide-30
SLIDE 30

[Faculty of Science Information and Computing Sciences] 13

Kinds (contd.)

Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ If we had tuples on the kind level: Fix2 :: (∗2 → ∗)2 → ∗2 And if we had numbers as kinds: Fix2 :: ((2 → ∗) → (2 → ∗)) → (2 → ∗)

slide-31
SLIDE 31

[Faculty of Science Information and Computing Sciences] 13

Kinds (contd.)

Fix2,0 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ Fix2,1 :: (∗ → ∗ → ∗) → (∗ → ∗ → ∗) → ∗ If we had tuples on the kind level: Fix2 :: (∗2 → ∗)2 → ∗2 And if we had numbers as kinds: Fix2 :: ((2 → ∗) → (2 → ∗)) → (2 → ∗) And this can be generalized: Fixn :: ((n → ∗) → (n → ∗)) → (n → ∗)

slide-32
SLIDE 32

[Faculty of Science Information and Computing Sciences] 14

One fixed point combinator

Fixn :: ((n → ∗) → (n → ∗)) → (n → ∗) Can we express n in Haskell?

slide-33
SLIDE 33

[Faculty of Science Information and Computing Sciences] 14

One fixed point combinator

Fixn :: ((n → ∗) → (n → ∗)) → (n → ∗) Can we express n in Haskell? Yes!

slide-34
SLIDE 34

[Faculty of Science Information and Computing Sciences] 15

Encoding kind n

◮ Choose ∗ rather than n.

slide-35
SLIDE 35

[Faculty of Science Information and Computing Sciences] 15

Encoding kind n

◮ Choose ∗ rather than n. ◮ Ensure that wherever ∗ is used instead of n, we only

instantiate it with one of n different types – the types that make up our family.

slide-36
SLIDE 36

[Faculty of Science Information and Computing Sciences] 15

Encoding kind n

◮ Choose ∗ rather than n. ◮ Ensure that wherever ∗ is used instead of n, we only

instantiate it with one of n different types – the types that make up our family.

◮ Where necessary, provide additional evidence (in the form

  • f a GADT) that the type is actually one of only n

different possibilities.

slide-37
SLIDE 37

[Faculty of Science Information and Computing Sciences] 15

Encoding kind n

◮ Choose ∗ rather than n. ◮ Ensure that wherever ∗ is used instead of n, we only

instantiate it with one of n different types – the types that make up our family.

◮ Where necessary, provide additional evidence (in the form

  • f a GADT) that the type is actually one of only n

different possibilities. ∀ix :: n. . . . becomes ∀ix :: ∗.Fam ix → . . .

slide-38
SLIDE 38

[Faculty of Science Information and Computing Sciences] 16

Example index GADT

data Fam :: ∗ → ∗ where Expr :: Fam Expr Op :: Fam Op A value of Fam t encodes a proof that t is either Expr or Op.

slide-39
SLIDE 39

[Faculty of Science Information and Computing Sciences] 17

Representing a family

data ExprF e o = ConstF Val | IfF e e e | BinF e

  • e

data OpF e o = AddF | MulF | InfixF e | FlipF o

slide-40
SLIDE 40

[Faculty of Science Information and Computing Sciences] 17

Representing a family

data ExprF (r :: ∗ → ∗) (ix :: ∗) = ConstF Val | IfF (r Expr) (r Expr) (r Expr) | BinF (r Expr) (r Op) (r Expr) data OpF (r :: ∗ → ∗) (ix :: ∗) = AddF | MulF | InfixF (r Expr) | FlipF (r Op)

slide-41
SLIDE 41

[Faculty of Science Information and Computing Sciences] 17

Representing a family

data ExprF (r :: ∗ → ∗) (ix :: ∗) = ConstF Val | IfF (r Expr) (r Expr) (r Expr) | BinF (r Expr) (r Op) (r Expr) data OpF (r :: ∗ → ∗) (ix :: ∗) = AddF | MulF | InfixF (r Expr) | FlipF (r Op) data FamF (r :: ∗ → ∗) (ix :: ∗) where ExprF :: ExprF r Expr → FamF r Expr | OpF :: OpF r Op → FamF r Op type Expr′ = Fix FamF Expr type Op′ = Fix FamF Op

slide-42
SLIDE 42

[Faculty of Science Information and Computing Sciences] 17

Representing a family

type ExprF = K Val :+: I Expr :×: I Expr :×: I Expr :+: I Expr :×: I Op :×: I Expr type OpF = U :+: U :+: I Expr :+: I Op data FamF (r :: ∗ → ∗) (ix :: ∗) where ExprF :: ExprF r Expr → FamF r Expr | OpF :: OpF r Op → FamF r Op type Expr′ = Fix FamF Expr type Op′ = Fix FamF Op

slide-43
SLIDE 43

[Faculty of Science Information and Computing Sciences] 17

Representing a family

type ExprF = K Val :+: I Expr :×: I Expr :×: I Expr :+: I Expr :×: I Op :×: I Expr type OpF = U :+: U :+: I Expr :+: I Op type FamF = ExprF :⊲: Expr :+: OpF :⊲: Op type Expr′ = Fix FamF Expr type Op′ = Fix FamF Op

slide-44
SLIDE 44

[Faculty of Science Information and Computing Sciences] 18

Combinators for functors

Recursing on a particular index

data I (ix′ :: ∗) (r :: ∗ → ∗) (ix :: ∗) = I (r ix′)

Selecting a particular index

data (f :⊲: ix′) (r :: ∗ → ∗) (ix :: ∗) where Tag :: f r ix′ → (f :⊲: ix′) r ix′

slide-45
SLIDE 45

[Faculty of Science Information and Computing Sciences] 19

Generalizing Functor

class HFunctor fam (f :: (∗ → ∗) → ∗ → ∗) where hmap :: ∀r r′. (∀ix.fam ix → r ix → r′ ix) → (∀ix.fam ix → f r ix → f r′ ix)

slide-46
SLIDE 46

[Faculty of Science Information and Computing Sciences] 19

Generalizing Functor

class HFunctor fam (f :: (∗ → ∗) → ∗ → ∗) where hmap :: ∀r r′. (∀ix.fam ix → r ix → r′ ix) → (∀ix.fam ix → f r ix → f r′ ix) fold :: ∀fam f r.HFunctor fam f ⇒ (∀ix.fam ix → f r ix → r ix) → (∀ix.fam ix → Fix f ix → r ix)

slide-47
SLIDE 47

[Faculty of Science Information and Computing Sciences] 20

In the paper or the library

Details

◮ Conversion between original family and representation. ◮ Generic function code.

slide-48
SLIDE 48

[Faculty of Science Information and Computing Sciences] 20

In the paper or the library

Details

◮ Conversion between original family and representation. ◮ Generic function code.

Applications

◮ Variants of folds. ◮ Classic examples: show, equality. ◮ Type-indexed datatypes: the zipper. ◮ Generic rewriting.

slide-49
SLIDE 49

[Faculty of Science Information and Computing Sciences] 21

Try it

On Hackage

multirec – library described in the paper zipper – generic zippers based on multirec regular – single-datatype version of the library