StdGeneric.dcl 2.21 KB
Newer Older
1 2 3 4 5 6 7 8
definition module StdGeneric

// generic representation
:: UNIT = UNIT
:: EITHER a b = LEFT a | RIGHT b
:: PAIR a b = PAIR a b

// for constructor information
9
:: OBJECT a =: OBJECT a		// object marking
10
:: CONS a = CONS a 			// constructor marking
11
:: RECORD a = RECORD a 		// record marking
12
:: FIELD a =: FIELD a 		// record field marking
13

14 15 16 17 18 19
:: GenericTypeDefDescriptor =
	{ gtd_name  	:: String 
	, gtd_arity 	:: Int 
	, gtd_num_conses :: Int
	, gtd_conses 	:: [GenericConsDescriptor]
	}
20 21 22 23

:: GenericConsDescriptor = 
	{ gcd_name 		:: String
	, gcd_arity  	:: Int	
24 25 26 27
	, gcd_prio 		:: GenConsPrio				// priority and associativity
	, gcd_type_def	:: GenericTypeDefDescriptor // type def of the constructor
	, gcd_type 		:: GenType					// type of the constructor
	, gcd_index 	:: Int						// index of the contructor in the type def
28 29 30
	}
:: GenConsPrio = GenConsNoPrio | GenConsPrio GenConsAssoc Int
:: GenConsAssoc = GenConsAssocNone | GenConsAssocLeft | GenConsAssocRight   	
31 32 33 34 35 36 37 38 39

:: GenericRecordDescriptor = 
	{ grd_name 		:: String
	, grd_arity  	:: Int	
	, grd_type_arity:: Int 						// arity of the type
	, grd_type 		:: GenType					// type of the constructor
	, grd_fields 	:: [String]
	}

40 41 42
:: GenericFieldDescriptor = 
	{ gfd_name 	:: String
	, gfd_index :: Int							// index of the field in the record
43
	, gfd_cons 	:: GenericRecordDescriptor 		// the record constructor
44 45 46 47 48 49 50 51 52 53
	}

:: GenType 	= GenTypeCons String
			| GenTypeVar !Int
			| GenTypeApp GenType GenType
			| GenTypeArrow GenType GenType
	
// determine the path in the generic binary-sum-tree of a constructor
:: ConsPos = ConsLeft | ConsRight
getConsPath :: !GenericConsDescriptor -> [ConsPos]
54

55
// generic bidirectional mapping
56
generic bimap a b | bimap b a :: .a ->.b
57 58 59 60 61 62

derive bimap c
derive bimap PAIR
derive bimap EITHER
derive bimap OBJECT
derive bimap CONS
63
derive bimap RECORD
64 65 66
derive bimap FIELD
derive bimap (->)

67
// HACK: dictionaries for all generics.
68 69
// It works since all generic classes have only one method and do not inherit 
// from other classes
70
:: GenericDict a = { generic_dict :: !a }
71
:: GenericDict0 a = { generic_dict0 :: a }
72 73 74 75

// embedding-projection
:: Bimap a b = { map_to :: .(a -> b), map_from :: .(b -> a) }	// deprecated, no longer used
bimapId :: Bimap .a .a	// deprecated, no longer used