a generic programming extension for clean artem alimarine and rinus plasmeijer university of...

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

Post on 21-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

A Generic Programming Extension for Clean

Artem Alimarine and Rinus Plasmeijer

University of Nijmegen

26/09/2001 A Generic Programming Extension for Clean 2

Contents (1)

Introduction to generic programming Generics in Clean Customized instances Conclusion

26/09/2001 A Generic Programming Extension for Clean 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.

26/09/2001 A Generic Programming Extension for Clean 4

List a

Generic programming (2)

List b

Rose a Rose b

Generic a Generic b

mapRose f

toRosefromRose

gmap f

mapList f

fromList toList

26/09/2001 A Generic Programming Extension for Clean 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)))

26/09/2001 A Generic Programming Extension for Clean 6

Generic equalityclass 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 whereeq (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 whereeq (PAIR x1 x2) (PAIR y1 y2) =

eq x1 y1 && eq x2 y2

26/09/2001 A Generic Programming Extension for Clean 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)

26/09/2001 A Generic Programming Extension for Clean 8

Generic mappingMappingclass map t :: (a b) (t a) (t b)

Specialization for listsinstance 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 **.

26/09/2001 A Generic Programming Extension for Clean 9

Contents (2)

Introduction to Generic programming Generics in Clean Customized instances Conclusion

26/09/2001 A Generic Programming Extension for Clean 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 kindsOverloaded functions can be defined generically

26/09/2001 A Generic Programming Extension for Clean 11

Instances to generateinstance 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

26/09/2001 A Generic Programming Extension for Clean 12

Definition of generic mappinggeneric map a b :: a binstance 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 wheremap mapl mapr (LEFT x) = LEFT (mapl x)map mapl mapr (RIGHT x) = RIGHT (mapr x)

26/09/2001 A Generic Programming Extension for Clean 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 tclass map** t :: (a b) (t a) t bclass map*** t ::

(a1b1) (a2b2) (t a1 a2) t b1 b2

… Each class has

One class variable One class member with the member type derived from the

generic type

26/09/2001 A Generic Programming Extension for Clean 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 generateinstance map** List generic

instance map*** Tree generic

26/09/2001 A Generic Programming Extension for Clean 15

Translation: instance for lists

Generic representation of lists:: ListG a :== EITHER UNIT (PAIR a (List a))

Instance to generateinstance map** List generic

Generated instance for listsinstance map** List where

map** f x = toList (mapListG f (fromList x))

mapListG :: (a b) (ListG a) ListG bmapListG f = map*** map* (map*** f (map** f))

After resolving overloadingmapListG f = mapEITHER mapUNIT (mapPAIR f (mapList f))

26/09/2001 A Generic Programming Extension for Clean 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 treesinstance map*** Tree where

map*** f1 f2 x

= toTree (mapTreeG f1 f2 (fromTree x))

mapTreeG :: (a1b1) (a2b2) (TreeG a1 a2)

TreeG b1 b2

mapTreeG f1 f2 = map*** f1 (map*** f2

(map*** (map*** f1 f2) (map*** f1 f2)))

After resolving overloadingmapTreeG f1 f2 = mapEITHER f1 (mapPAIR f2

(mapPAIR (mapTree f1 f2) (mapTree f1 f2)))

26/09/2001 A Generic Programming Extension for Clean 17

Applying mapOverloaded fmap and bimap can be defined

in terms of generic mapfmap :: (a b) (t a) (t b) | map{|**|} tfmap f x = map{|**|} f x bimap :: (a b) (c d) (t a c) (t b d)

| map{|***|} tbimap f g x = map{|***|} f g x

Kind index must be specifiedmapinc1 = map{|**|} incmapinc2 = map{|***|} inc

26/09/2001 A Generic Programming Extension for Clean 18

Contents (3)

Introduction to Generic programmingGenerics in Clean Customized instances Conclusion

26/09/2001 A Generic Programming Extension for Clean 19

Customized instances

Allow for mixing of Generic behavior Specific behavior

The user can specify an instance in terms of the generated instance

26/09/2001 A Generic Programming Extension for Clean 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 = vars{|generic|} x

26/09/2001 A Generic Programming Extension for Clean 21

Contents (4)

Introduction to Generic programmingGenerics in CleanCustomized instances Conclusion

26/09/2001 A Generic Programming Extension for Clean 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

26/09/2001 A Generic Programming Extension for Clean 23

Future work

Optimization Types of higher-order kinds Constructor information Uniqueness typing Array types Explicit recursion in the generic representation of

types