Introduction Generic Views on Data Types Problem Towards a solution Generic Views Detour into GH The influence of S. Holdermans 1 J. Jeuring 1 oh 2 A. Rodriguez 1 A. L¨ views Formal definition Validity Implementation 1 Department of Information and Computing Sciences, Utrecht University in Generic Haskell 2 Institut f¨ ur Informatik III, Universit¨ at Bonn Generic Haskell Adding a view Conclusions 8 th International Conference on Mathematics of Program Construction - 2006 1
Contents Introduction Problem Introduction Problem Towards a solution Towards a solution Generic Views Detour into GH Generic Views The influence of views Detour into GH Formal definition Validity The influence of views Implementation Formal definition in Generic Haskell Validity Generic Haskell Adding a view Conclusions Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 2
So many approaches to generic programming. . . Only in Haskell, we have: Introduction ◮ LIGD Problem Towards a solution ◮ EMGM Generic Views Detour into GH ◮ PolyP The influence of views Formal definition ◮ Regular Validity ◮ and much more... Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 3
So many approaches to generic programming. . . Only in Haskell, we have: Introduction ◮ LIGD Problem Towards a solution ◮ EMGM Generic Views Detour into GH ◮ PolyP The influence of views Formal definition ◮ Regular Validity ◮ and much more... Implementation in Generic Haskell Generic Haskell And they are NOT equivalent: Adding a view Conclusions ◮ Some generic functions are much easier to write in certain frameworks then in others. ◮ Some just cannot be written in some frameworks. ◮ Performance can also vary greatly (time and space)... 3
It’s not so chaotic, after all What all approaches have in common They all provide: Introduction ◮ A way to define generic functions by induction on the Problem Towards a solution structure of types Generic Views ◮ Some view types , over which the functions are defined. Detour into GH The influence of views Formal definition Validity Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 4
It’s not so chaotic, after all What all approaches have in common They all provide: Introduction ◮ A way to define generic functions by induction on the Problem Towards a solution structure of types Generic Views ◮ Some view types , over which the functions are defined. Detour into GH The influence of views Formal definition Where they differ Validity Implementation Each approach views the structure of datatypes differently: in Generic Haskell ◮ Using a certain set of view types Generic Haskell Adding a view ◮ With certain conversions to and from these view types Conclusions 4
It’s not so chaotic, after all What all approaches have in common They all provide: Introduction ◮ A way to define generic functions by induction on the Problem Towards a solution structure of types Generic Views ◮ Some view types , over which the functions are defined. Detour into GH The influence of views Formal definition Where they differ Validity Implementation Each approach views the structure of datatypes differently: in Generic Haskell ◮ Using a certain set of view types Generic Haskell Adding a view ◮ With certain conversions to and from these view types Conclusions This paper ◮ Makes the case for generic views on datatypes ◮ Defines formally what is a generic view ◮ Shows an implementation of views (Generic Haskell) 4
Why are generic views useful Introduction Problem Towards a solution Generic Views ◮ We will give examples of how the generic view used can Detour into GH The influence of influence the expressiveness and efficiency of a generic views Formal definition programming approach Validity Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 5
Why are generic views useful Introduction Problem Towards a solution Generic Views ◮ We will give examples of how the generic view used can Detour into GH The influence of influence the expressiveness and efficiency of a generic views Formal definition programming approach Validity Implementation in Generic Haskell ◮ But first we need to have a minimal understanding of Generic Haskell Adding a view Generic Haskell Conclusions 5
GH Basics Introduction Generic Haskell Problem Towards a solution ◮ Has a compiler which generates Haskell code Generic Views Detour into GH The influence of ◮ Uses sum-of-products view types views Formal definition Validity Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 6
GH Basics Introduction Generic Haskell Problem Towards a solution ◮ Has a compiler which generates Haskell code Generic Views Detour into GH The influence of ◮ Uses sum-of-products view types views Formal definition Validity Implementation Generic Haskell’s view types in Generic Haskell Generic Haskell 1 data Unit = Unit Adding a view 2 data a + b = I n l a | I n r b Conclusions 3 data a × b = a × b 6
GH Basics A generic function in Generic Haskell ◮ Is type-indexed Introduction ◮ Just like in LIGD, slightly different syntax Problem Towards a solution Generic Views Detour into GH The influence of views Formal definition Validity Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 7
GH Basics A generic function in Generic Haskell ◮ Is type-indexed Introduction ◮ Just like in LIGD, slightly different syntax Problem Towards a solution Generic Views Detour into GH An example GH generic function The influence of views Formal definition 1 c o l l e c t < Unit > Unit = [ ] Validity 2 c o l l e c t < a+b > ( I n l x ) = c o l l e c t < a > x Implementation in Generic 3 c o l l e c t < a+b > ( I n r y ) = c o l l e c t < b > y Haskell 4 c o l l e c t < a × b > ( x × y ) = c o l l e c t < a > x ++ c o l l e c t < b > y Generic Haskell Adding a view 5 c o l l e c t < Int > n = [ ] Conclusions 6 c o l l e c t < Char > c = [ ] 7
GH Basics A generic function in Generic Haskell ◮ Is type-indexed Introduction ◮ Just like in LIGD, slightly different syntax Problem Towards a solution Generic Views Detour into GH An example GH generic function The influence of views Formal definition 1 c o l l e c t < Unit > Unit = [ ] Validity 2 c o l l e c t < a+b > ( I n l x ) = c o l l e c t < a > x Implementation in Generic 3 c o l l e c t < a+b > ( I n r y ) = c o l l e c t < b > y Haskell 4 c o l l e c t < a × b > ( x × y ) = c o l l e c t < a > x ++ c o l l e c t < b > y Generic Haskell Adding a view 5 c o l l e c t < Int > n = [ ] Conclusions 6 c o l l e c t < Char > c = [ ] Type signature 1 c o l l e c t < a : : ∗ | c :: ∗ > : : ( c o l l e c t < a | c > ) ⇒ a → [ c ] 7
Local re-definitions Introduction Problem Generic functions can be specialized using local re-definitions of Towards a solution the form: Generic Views Detour into GH 1 l e t c o l l e c t < a > x = i f x > 0 then [ x ] e l s e [ ] The influence of views 2 i n c o l l e c t < [a] > Formal definition Validity Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 8
Local re-definitions Introduction Problem Generic functions can be specialized using local re-definitions of Towards a solution the form: Generic Views Detour into GH 1 l e t c o l l e c t < a > x = i f x > 0 then [ x ] e l s e [ ] The influence of views 2 i n c o l l e c t < [a] > Formal definition Validity Implementation in Generic Haskell The expression above has type signature: Generic Haskell Adding a view 1 Num a ⇒ [ a ] → [ a ] Conclusions 8
Default cases Introduction Problem Towards a solution A specific function can be extended into a generic function by Generic Views giving it a default case , like in the following example: Detour into GH The influence of views Formal definition 1 v a r c o l l e c t < Variable > v = [ v ] Validity 2 v a r c o l l e c t extends c o l l e c t Implementation where c o l l e c t as v a r c o l l e c t 3 in Generic Haskell Generic Haskell Adding a view Conclusions 9
Default cases Introduction Problem Towards a solution A specific function can be extended into a generic function by Generic Views giving it a default case , like in the following example: Detour into GH The influence of views Formal definition 1 v a r c o l l e c t < Variable > v = [ v ] Validity 2 v a r c o l l e c t extends c o l l e c t Implementation where c o l l e c t as v a r c o l l e c t 3 in Generic Haskell Generic Haskell Adding a view Conclusions The where clause renames a dependency in the default case. 9
GH final remarks Introduction Structure types Problem Towards a solution GH uses sum-of-products, right-associative. Therefore a List Generic Views Detour into GH representation type would look like: The influence of views Formal definition 1 data L i s t a = N i l | Cons a ( L i s t a ) Validity 2 type L ist ’ a = Unit + a × L i s t a Implementation in Generic Haskell Generic Haskell Adding a view Conclusions 10
Recommend
More recommend