parametric compositional data types
play

Parametric Compositional Data Types Patrick Bahr Tom Hvitved - PowerPoint PPT Presentation

Parametric Compositional Data Types Patrick Bahr Tom Hvitved University of Copenhagen, Department of Computer Science { paba , hvitved } @ diku.dk Mathematically Structured Functional Programming 2012, Tallinn, Estonia, March 25th, 2012


  1. Parametric Compositional Data Types Patrick Bahr Tom Hvitved University of Copenhagen, Department of Computer Science { paba , hvitved } @ diku.dk Mathematically Structured Functional Programming 2012, Tallinn, Estonia, March 25th, 2012

  2. Outline Motivation 1 Compositional Data Types 2 Higher-Order Abstract Syntax 3 2

  3. The Issue Implementation/Prototyping of DSLs Rule Language UI Contract Language Language ERP Runtime Ontology Report System Language Language ... ... 3

  4. The Issue Implementation/Prototyping of DSLs Rule Language UI Contract Language Language ERP Runtime Ontology Report System Language Language ... ... 3

  5. The Issue Implementation/Prototyping of DSLs Rule Language UI Contract Language Language ERP Runtime Ontology Report System Language Language ... ... 3

  6. The Issue Implementation/Prototyping of DSLs The abstract picture Rule Language We have a number of UI Contract domain-specific languages. Language Language Each pair of DSLs shares some ERP common sublanguage. Runtime Ontology Report System All of them share a common Language Language language of values. We have the same situation on ... ... the type level! 3

  7. The Issue Implementation/Prototyping of DSLs The abstract picture Rule Language We have a number of UI Contract domain-specific languages. Language Language Each pair of DSLs shares some ERP common sublanguage. Runtime Ontology Report System All of them share a common Language Language language of values. We have the same situation on ... ... the type level! How do we implement this system without duplicating code?! 3

  8. More General Application Even with only one language to implement this issue appears! 4

  9. More General Application Even with only one language to implement this issue appears! Different stages of a compiler work on different languages. Desugaring: FullExp → CoreExp Evaluation: Exp → Value . . . 4

  10. More General Application Even with only one language to implement this issue appears! Different stages of a compiler work on different languages. Desugaring: FullExp → CoreExp Evaluation: Exp → Value . . . Manipulating/extending syntax trees annotating syntax trees adding/removing type annotations 4

  11. Compositional Data Types data Exp = Lit Int | Add Exp Exp | Mult Exp Exp 5

  12. Compositional Data Types data Exp = Lit Int | Add Exp Exp s recursion i g | Mult Exp Exp n a t u r e decompose data Sig e = Lit Int data Term f = | Add e e In ( f ( Term f )) | Mult e e 5

  13. Compositional Data Types data Exp = Lit Int | Add Exp Exp s recursion i g | Mult Exp Exp n a t u r e decompose data Sig e = Lit Int data Term f = | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig 5

  14. Compositional Data Types data Exp = Lit Int | Add Exp Exp s n i o g | Mult Exp Exp n i s a r u t u c r e e r decompose data Sig e = Lit Int data Term f = | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig 5

  15. Compositional Data Types data Lit e = Lit Int data Exp = Lit Int | Add Exp Exp s n i o g | Mult Exp Exp n i s a r u t u c r e e r decompose data Sig e = Lit Int data Term f = | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig data Ops e = Add e e | Mult e e 5

  16. Compositional Data Types data Lit e = Lit Int data Exp = Lit Int | Add Exp Exp s n i o g | Mult Exp Exp n i s a r u t u c r e e r decompose data Sig e = Lit Int data Term f = Lit ⊕ Ops | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig data Ops e = Add e e | Mult e e 5

  17. Compositional Data Types data Lit e = Lit Int data Exp = Lit Int | Add Exp Exp s n i o g | Mult Exp Exp n i s a r u t u c r e e r decompose data Sig e = Lit Int data Term f = Lit ⊕ Ops ⊕ ... | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig data Ops e = Add e e | Mult e e 5

  18. Compositional Data Types data Lit e = Lit Int data Exp = Lit Int | Add Exp Exp s n i o g | Mult Exp Exp n i s a r u t u c r e e r decompose annotations data Sig e = Lit Int data Term f = Lit ⊕ Ops ⊕ ... | Add e e In ( f ( Term f )) | Mult e e combine type Exp = Term Sig data Ops e = Add e e | Mult e e 5

  19. Variable Binding A straightforward solution type Name = String data Lam e = Lam Name e data Var e = Var Name data App e = App e e 6

  20. Variable Binding A straightforward solution type Name = String data Lam e = Lam Name e type Sig = Lam ⊕ Var ⊕ App data Var e = Var Name type Lambda = Term Lam data App e = App e e 6

  21. Variable Binding A straightforward solution type Name = String data Lam e = Lam Name e type Sig = Lam ⊕ Var ⊕ App data Var e = Var Name type Lambda = Term Lam data App e = App e e Issues Definitions modulo α -equivalence Capture-avoiding substitutions Implementing embedded languages 6

  22. Variable Binding A straightforward solution type Name = String data Lam e = Lam Name e type Sig = Lam ⊕ Var ⊕ App data Var e = Var Name type Lambda = Term Lam data App e = App e e Issues Definitions modulo α -equivalence Capture-avoiding substitutions Implementing embedded languages Goal Use higher-order abstract syntax to leverage the variable binding mechanism of the host language. 6

  23. Higher-Order Abstract Syntax Explicit Variables type Name = String data Lam e = Lam Name e data Var e = Var Name data App e = App e e 7

  24. Higher-Order Abstract Syntax Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e 7

  25. Higher-Order Abstract Syntax Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e 7

  26. Higher-Order Abstract Syntax Lam "x" ( ... Var "x" ... ) Lam ( λ x → ... x ... ) Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e 7

  27. Higher-Order Abstract Syntax Lam "x" ( ... Var "x" ... ) Lam ( λ x → ... x ... ) Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e Issues inefficient and cumbersome recursion schemes (catamorphism needs an algebra and the inverse coalgebra) Full function space allows for exotic terms 7

  28. Higher-Order Abstract Syntax Lam "x" ( ... Var "x" ... ) Lam ( λ x → ... x ... ) Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e Issues inefficient and cumbersome recursion schemes (catamorphism needs an algebra and the inverse coalgebra) � Fegaras & Sheard (1996): parametric functions space Full function space allows for exotic terms 7

  29. Higher-Order Abstract Syntax Lam "x" ( ... Var "x" ... ) Lam ( λ x → ... x ... ) Explicit Variables Higher-Order Abstract Syntax type Name = String data Lam e = Lam Name e data Lam e = Lam ( e → e ) data Var e = Var Name data App e = App e e data App e = App e e Issues inefficient and cumbersome recursion schemes (catamorphism needs an algebra and the inverse coalgebra) � Fegaras & Sheard (1996): parametric functions space Full function space allows for exotic terms � Washburn & Weirich (2008): polymorphism & abstract type of terms 7

  30. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) 8

  31. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) 8

  32. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) type Term f = ∀ a . Trm f a 8

  33. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) | Var a type Term f = ∀ a . Trm f a 8

  34. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) | Var a newtype Term f = Term ( ∀ a . Trm f a ) 8

  35. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) | Var a newtype Term f = Term ( ∀ a . Trm f a ) Example data Sig a e = Lam ( a → e ) | App e e 8

  36. Parametric Higher-Order Abstract Syntax [Chlipala 2008] Idea Signature for lambda bindings: data Lam a e = Lam ( a → e ) Recursive construction of terms: data Trm f a = In ( f a ( Trm f a )) | Var a newtype Term f = Term ( ∀ a . Trm f a ) Example data Sig a e = Lam ( a → e ) | App e e e :: Term Sig e = Term $ Lam ( λ x → Var x ‘ App ‘ Var x ) 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend