composing and decomposing data types
play

Composing and Decomposing Data Types A Closed Type Families - PowerPoint PPT Presentation

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Faculty of Science Composing and Decomposing Data Types A Closed Type Families Implementation of Data Types ` a la Carte Patrick Bahr


  1. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Faculty of Science Composing and Decomposing Data Types A Closed Type Families Implementation of Data Types ` a la Carte Patrick Bahr University of Copenhagen, Department of Computer Science paba@di.ku.dk 10th ACM SIGPLAN Workshop on Generic Programming, 31st August, 2014 Slide 1

  2. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Experimenting with Closed Type Families • What can we do with them? • How do they compare to type classes? • How do they interact with type classes? Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 2

  3. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Experimenting with Closed Type Families • What can we do with them? • How do they compare to type classes? • How do they interact with type classes? Application: Data Types ` a la Carte Specifically: the subtyping constraint : ≺ : Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 2

  4. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Experimenting with Closed Type Families • What can we do with them? • How do they compare to type classes? • How do they interact with type classes? Application: Data Types ` a la Carte Specifically: the subtyping constraint : ≺ : • Can we get rid of some of the restrictions? • Can we improve error messages? • What price do we have to pay? Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 2

  5. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  6. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type data Exp = Val Int | Add Exp Exp Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  7. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Exp = Val Int = ⇒ | Add a a | Add Exp Exp type Exp = Fix Arith Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  8. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Fix f = In ( f ( Fix f )) data Exp = Val Int = ⇒ | Add a a | Add Exp Exp type Exp = Fix Arith Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  9. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Exp = Val Int = ⇒ | Add a a | Add Exp Exp type Exp = Fix Arith Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  10. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Exp = Val Int = ⇒ | Add a a | Add Exp Exp type Exp = Fix Arith Functors can be combined by coproduct construction :+: Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  11. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Exp = Val Int = ⇒ | Add a a | Add Exp Exp type Exp = Fix Arith Functors can be combined by coproduct construction :+: data Mul a = Mul a a type Exp ′ = Fix ( Arith :+: Mul ) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  12. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte [Swierstra 2008] Idea: Decompose data types into two-level types: Recursive data type Fixpoint of functor data Arith a = Val Int data Exp = Val Int = ⇒ | Add a a | Add Exp Exp data ( f :+: g ) a = Inl ( f a ) type Exp = Fix Arith | Inr ( g a ) Functors can be combined by coproduct construction :+: data Mul a = Mul a a type Exp ′ = Fix ( Arith :+: Mul ) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 3

  13. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte (cont.) Subtyping constraint : ≺ : class f : ≺ : g where inj :: f a → g a prj :: g a → Maybe ( f a ) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 4

  14. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte (cont.) Subtyping constraint : ≺ : class f : ≺ : g where e.g. Mul : ≺ : Arith :+: Mul inj :: f a → g a prj :: g a → Maybe ( f a ) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 4

  15. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte (cont.) Subtyping constraint : ≺ : class f : ≺ : g where e.g. Mul : ≺ : Arith :+: Mul inj :: f a → g a prj :: g a → Maybe ( f a ) Example: smart constructors add :: ( Arith : ≺ : f ) ⇒ Fix f → Fix f → Fix f add x y = In ( inj ( Add x y )) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 4

  16. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Data Types ` a la Carte (cont.) Subtyping constraint : ≺ : class f : ≺ : g where e.g. Mul : ≺ : Arith :+: Mul inj :: f a → g a prj :: g a → Maybe ( f a ) Example: smart constructors add :: ( Arith : ≺ : f ) ⇒ Fix f → Fix f → Fix f add x y = In ( inj ( Add x y )) exp :: Fix ( Arith :+: Mul ) exp = val 1 ‘ add ‘ ( val 2 ‘ mul ‘ val 3 ) Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 4

  17. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Limitations of : ≺ : Definition of : ≺ : f : ≺ : f instance where . . . instance ( f : ≺ : f 1 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . instance ( f : ≺ : f 2 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 5

  18. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Limitations of : ≺ : Definition of : ≺ : f : ≺ : f instance where . . . f : ≺ : ( f :+: f 2 ) where instance . . . instance ( f : ≺ : f 2 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 5

  19. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Limitations of : ≺ : Definition of : ≺ : f : ≺ : f instance where . . . f : ≺ : ( f :+: f 2 ) where instance . . . instance ( f : ≺ : f 2 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . • Asymmetric treatment of :+: • Left-hand side is not inspected • Ambiguity Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 5

  20. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Limitations of : ≺ : Definition of : ≺ : f : ≺ : f instance where . . . f : ≺ : ( f :+: f 2 ) where instance . . . instance ( f : ≺ : f 2 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . • Asymmetric treatment of :+: A : ≺ : A :+: ( B :+: C ) • Left-hand side is not inspected • Ambiguity Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 5

  21. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Limitations of : ≺ : Definition of : ≺ : f : ≺ : f instance where . . . f : ≺ : ( f :+: f 2 ) where instance . . . instance ( f : ≺ : f 2 ) ⇒ f : ≺ : ( f 1 :+: f 2 ) where . . . • Asymmetric treatment of :+: A : �≺ : ( A :+: B ) :+: C • Left-hand side is not inspected • Ambiguity Patrick Bahr — Composing and Decomposing Data Types — WGP ’14, 31st August, 2014 Slide 5

Recommend


More recommend