kinds
[clean-tests.git] / gengen / src / GenType.dcl
1 definition module GenType
2
3 import StdGeneric
4 from StdOverloaded import class ==, class toString
5
6 //* Auxiliary type to help with casting values, this is gone at runtime
7 :: Box b a =: Box b
8 derive bimap Box
9 unBox (Box b) :== b
10 box b :== Box b
11 reBox x :== box (unBox x)
12
13 //* Deeply embedded generic type representation
14 :: GType
15 = GTyBasic BasicType
16 | GTyRef String
17 | GTyArrow GType GType
18 | GTyArray ArrayType GType
19 | GTyUList UListType GType
20 | GTyUMaybe GType
21 | GTyUnit
22 | GTyEither GType GType
23 | GTyPair GType GType
24 | GTyCons GenericConsDescriptor GType
25 | GTyField GenericFieldDescriptor GType
26 | GTyObject GenericTypeDefDescriptor GType
27 | GTyRecord GenericRecordDescriptor GType
28
29 //* Type representation larded with the generic type information
30 :: Type
31 = TyBasic BasicType
32 | TyRef String
33 | TyArrow Type Type
34 | TyArray ArrayType Type
35 | TyUList UListType Type
36 | TyUMaybe Type
37 | TyNewType GenericTypeDefDescriptor GenericConsDescriptor Type
38 | TyObject GenericTypeDefDescriptor [(GenericConsDescriptor, [Type])]
39 | TyRecord GenericRecordDescriptor [(GenericFieldDescriptor, Type)]
40
41 //* Basic types
42 :: BasicType = BTInt | BTChar | BTReal | BTBool | BTDynamic | BTFile | BTWorld
43 //* Array kinds
44 :: ArrayType = AStrict | ALazy | AUnboxed | APacked
45 //* Unboxed list kinds
46 :: UListType = ULLazy | ULStrict
47 //* Kind of a type
48 :: Kind = KStar | (KArrow) infixr 1 Kind Kind
49
50 instance == GType, Type, BasicType, ArrayType, UListType, GenType, Kind
51 instance toString GType, Type, BasicType, ArrayType, UListType, GenType, Kind
52
53 /**
54 * Removes recursive types by replacing them with references
55 *
56 * @param gtype
57 * @result the main type
58 * @result the separated types grouped in strongly connected components
59 */
60 flattenGType :: GType -> [[GType]]
61
62 //* Convert a GType to a Type
63 gTypeToType :: GType -> Type
64
65 //* Extract the name of the type
66 typeName :: Type -> String
67
68 //* Extract the genType for a type
69 typeGenType :: Type -> [GenType]
70
71 //* Extract the kind of the type's constructors (see `{{typeGenType}}`)
72 genTypeKind :: [GenType] -> Kind
73 //* @type Type -> Kind
74 typeKind t :== genTypeKind (typeGenType t)
75
76 //* Predicate whether the outer type is a builtin type
77 class isBuiltin a :: a -> Bool
78 instance isBuiltin Type, GType
79
80 //* Predicate whether the outer type is a basic type
81 class isBasic a :: a -> Bool
82 instance isBasic Type, GType
83
84 //* Replace builtin constructors with their pretty names (e.g. _!Cons with [!])
85 class replaceBuiltins a :: a -> a
86 instance replaceBuiltins Type, GType, GenType
87
88 //* Creates a deep embedded generic representation of a type
89 generic gType a :: Box GType a
90 derive gType UNIT, EITHER, PAIR, CONS of gcd, FIELD of gfd, OBJECT of gtd, RECORD of grd
91 derive gType Int, Bool, Real, Char, World, File
92 derive gType (->)
93 derive gType ?#, ?, ?^
94 derive gType [], [! ], [ !], [!!], [#], [#!], {}, {!}, {#}, {32#}
95 derive gType (), (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)