A Generic Programming Extension for Clean - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

A Generic Programming Extension for Clean

Description:

Functions are defined by induction on the structure of data types. ... Binary sums and products (in generic prelude) :: UNIT = UNIT :: PAIR a b = PAIR a b : ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 24
Provided by: artemal
Category:

less

Transcript and Presenter's Notes

Title: A Generic Programming Extension for Clean


1
A Generic Programming Extension for Clean
  • Artem Alimarine and Rinus Plasmeijer
  • University of Nijmegen

2
Contents (1)
  • Introduction to generic programming
  • Generics in Clean
  • Customized instances
  • Conclusion

3
Generic programming (1)
  • Functions are defined by induction on the
    structure of data types.
  • Defined once, specialized for an arbitrary data
    type.
  • Examples
  • Equality, lex. comparison, unification.
  • Mapping, zipping, folding.
  • Pretty printers and parsers.

4
Generic programming (2)
mapList f
List a
List b
fromList
toList
gmap f
Generic a
Generic b
toRose
fromRose
mapRose f
Rose a
Rose b
5
Generic type representation
  • Example types
  • List a Nil Cons a (List a)
  • Rose a Rose a (List (Rose a))
  • Tree a b Tip a Bin b (Tree a b) (Tree a b)
  • Binary sums and products (in generic prelude)
  • UNIT UNIT
  • PAIR a b PAIR a b
  • EITHER a b LEFT a RIGHT b
  • Generic type representations
  • ListG a EITHER UNIT (PAIR a (List a))
  • RoseG a PAIR a (List (Rose a))
  • TreeG a b EITHER a (PAIR b (PAIR
  • (Tree a b) (Tree a b)))

6
Generic equality
  • class eq t t t ? Bool
  • instance eq Int where
  • eq x y eqInt x y
  • instance eq UNIT where
  • eq x y True
  • instance eq (EITHER a b) eq a eq b where
  • eq (LEFT x) (LEFT y) eq x y
  • eq (RIGHT x) (RIGHT y) eq x y
  • eq _ _ False
  • instance eq (PAIR a b) eq a eq b where
  • eq (PAIR x1 x2) (PAIR y1 y2)
  • eq x1 y1 eq x2 y2

7
Specialization
  • instance eq (List a) eq a where
  • eq x y eq (fromList x) (fromList y)
  • instance eq (Rose a) eq a where
  • eq x y eq (fromRose x) (fromRose y)
  • instance eq (Tree a b) eq a eq b where
  • eq x y eq (fromTree x) (fromTree y)

8
Generic mapping
  • Mapping
  • class map t (a ? b) (t a) ? (t b)
  • Specialization for lists
  • instance map List where
  • map f x toList (map f (fromList x))
  • Problems
  • Instances on UNIT, PAIR and EITHER cannot be
    defined the kinds do not match
  • instance map UNIT where map f x x
  • This mapping function is defined only for kind
    ?.

9
Contents (2)
  • Introduction to Generic programming
  • Generics in Clean
  • Customized instances
  • Conclusion

10
Our approach
  • Based on two approaches
  • Derivable type classes of Hinze and P. Jones
  • Generic definition of class members
  • Limited to classes with class variables of kind
  • Kind-indexed types of Hinze
  • One generic function works for types of all kinds
  • Overloaded functions cannot be defined
    generically
  • One generic function works for all kinds
  • Overloaded functions can be defined generically

11
Instances to generate
  • instance map List generic
  • instance map Rose generic
  • instance map Tree generic
  • instance map (,) generic
  • instance map (,,) generic
  • Sequ a SequEmpty
  • SequZero (Sequ (a,a))
  • SequOne a (Sequ (a,a))
  • instance map Sequ generic

12
Definition of generic mapping
  • generic map a b a ? b
  • instance map Int where
  • map x x
  • instance map UNIT where
  • map x x
  • instance map PAIR where
  • map mapx mapy (PAIR x y)
  • PAIR (mapx x)(mapy y)
  • instance map EITHER where
  • map mapl mapr (LEFT x) LEFT (mapl x)
  • map mapl mapr (RIGHT x) RIGHT (mapr x)

13
Translation kind indexed classes
  • Generic definition
  • generic map a b a ? b
  • Translated to a kind-indexed family of classes
  • class map t t ? t
  • class map? t (a ? b) (t a) ? t b
  • class map?? t
  • (a1?b1) (a2?b2) (t a1 a2) ? t b1 b2
  • Each class has
  • One class variable
  • One class member with the member type derived
    from the generic type

14
Translation instances are bound to classes
  • Instances of the generic definition
  • instance map Int where map x x
  • instance map UNIT where map x x
  • instance map?? PAIR where
  • map?? mapx mapy (PAIR x y)
  • PAIR (mapx x)(mapy y)
  • instance map?? EITHER where
  • map?? mapl mapr (LEFT x) LEFT (mapl x)
  • map?? mapl mapr (RIGHT x) RIGHT (mapr x)
  • Instances to generate
  • instance map? List generic
  • instance map?? Tree generic

15
Translation instance for lists
  • Generic representation of lists
  • ListG a EITHER UNIT (PAIR a (List a))
  • Instance to generate
  • instance map? List generic
  • Generated instance for lists
  • instance map? List where
  • map? f x toList (mapListG f (fromList x))
  • mapListG (a ? b) ? (ListG a) ? ListG b
  • mapListG f map?? map (map?? f (map?
    f))
  • After resolving overloading
  • mapListG f mapEITHER mapUNIT (mapPAIR f
    (mapList f))

16
Translation instance for trees
  • Generic representation of trees
  • TreeG a b EITHER a (PAIR b
  • (PAIR (Tree a b) (Tree a b)))
  • Generated instance for trees
  • instance map?? Tree where
  • map?? f1 f2 x
  • toTree (mapTreeG f1 f2 (fromTree x))
  • mapTreeG (a1?b1) (a2?b2) ? (TreeG a1 a2)
  • ? TreeG b1 b2
  • mapTreeG f1 f2 map?? f1 (map?? f2
  • (map?? (map?? f1 f2) (map?? f1 f2)))
  • After resolving overloading
  • mapTreeG f1 f2 mapEITHER f1 (mapPAIR f2
  • (mapPAIR (mapTree f1 f2) (mapTree f1 f2)))

17
Applying map
  • Overloaded fmap and bimap can be defined in terms
    of generic map
  • fmap (a ? b) (t a) ? (t b) map? t
  • fmap f x map? f x
  • bimap (a ? b) (c ? d) (t a c) ? (t b d)
  • map?? t
  • bimap f g x map?? f g x
  • Kind index must be specified
  • mapinc1 map? inc
  • mapinc2 map?? inc

18
Contents (3)
  • Introduction to Generic programming
  • Generics in Clean
  • Customized instances
  • Conclusion

19
Customized instances
  • Allow for mixing of
  • Generic behavior
  • Specific behavior
  • The user can specify an instance in terms of the
    generated instance

20
Collecting free variables
  • Expr Lambda Var Expr App Expr Expr
  • generic vars a ? Var
  • instance vars UNIT where vars x
  • instance vars EITHER where
  • vars vl vr (LEFT x) vl x
  • vars vl vr (RIGHT x) vr x
  • instance vars PAIR where
  • vars vx vy (PAIR x y) removeDup (vx x vy y)
  • instance vars Var where vars v v
  • instance vars Expr where
  • vars (Lambda arg expr)
  • removeMember arg (vars expr)
  • vars x varsgeneric x

21
Contents (4)
  • Introduction to Generic programming
  • Generics in Clean
  • Customized instances
  • Conclusion

22
Contributions
  • Kind-indexed classes
  • One generic function works for all kinds
  • Overloaded functions can be defined generically
  • Customized instances provide a way to combine
  • Generic behavior
  • Specific behavior

23
Future work
  • Optimization
  • Types of higher-order kinds
  • Constructor information
  • Uniqueness typing
  • Array types
  • Explicit recursion in the generic representation
    of types
Write a Comment
User Comments (0)
About PowerShow.com