1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ConstrainedClassMethods #-}
module HSVIS.AST where
import Data.Bifunctor (bimap, first, second)
import qualified Data.Kind as DK
import Data.List (intersperse)
import Data.List.NonEmpty (NonEmpty)
import Data.Proxy
import HSVIS.Pretty
-- | An AST type has a single stage argument.
type ASTType = DK.Type -> DK.Type
-- | Trees that Grow extension type family
type family X (f :: ASTType) s
-- | Additional constructors
data family E (f :: ASTType) s
class HasExt f where
type HasXField f :: Bool
type HasECon f :: Bool
type ContainsData f :: [ASTType]
extOf :: HasXField f ~ 'True => f s -> X f s
extMap :: Proxy f -> Proxy s1 -> Proxy s2
-> XMapperTelescope (f ': ContainsData f) s1 s2
(EMapperTelescope (f ': ContainsData f) s1 s2
(f s1 -> f s2))
type family If c a b where
If 'True a b = a
If 'False a b = b
type family XMapperTelescope fs s1 s2 a where
XMapperTelescope '[] s1 s2 a = a
XMapperTelescope (f ': fs) s1 s2 a =
If (HasXField f)
((X f s1 -> X f s2) -> XMapperTelescope fs s1 s2 a)
(XMapperTelescope fs s1 s2 a)
type family EMapperTelescope fs s1 s2 a where
EMapperTelescope '[] s1 s2 a = a
EMapperTelescope (f ': fs) s1 s2 a =
If (HasECon f)
(If (HasXField f) (X f s1 -> E f s1 -> f s2)
(E f s1 -> f s2)
-> EMapperTelescope fs s1 s2 a)
(EMapperTelescope fs s1 s2 a)
newtype Name = Name String
deriving (Show, Eq, Ord)
data Program s = Program [DataDef s] [FunDef s]
deriving instance (Show (DataDef s), Show (FunDef s)) => Show (Program s)
data DataDef s = DataDef (X DataDef s) Name [(X Type s, Name)] [(Name, [Type s])]
deriving instance (Show (X DataDef s), Show (X Type s), Show (E Type s)) => Show (DataDef s)
data FunDef s = FunDef (X FunDef s) Name (TypeSig s) (NonEmpty (FunEq s))
deriving instance (Show (X FunDef s), Show (TypeSig s), Show (FunEq s)) => Show (FunDef s)
data TypeSig s
= TypeSig (Type s)
| TypeSigExt !(E TypeSig s)
deriving instance (Show (E TypeSig s), Show (Type s)) => Show (TypeSig s)
data FunEq s = FunEq (X FunEq s) Name [Pattern s] (RHS s)
deriving instance (Show (X FunEq s), Show (Pattern s), Show (RHS s)) => Show (FunEq s)
data Kind s
= KType (X Kind s)
| KFun (X Kind s) (Kind s) (Kind s)
-- extension point
| KExt (X Kind s) !(E Kind s)
deriving instance (Show (X Kind s), Show (E Kind s)) => Show (Kind s)
deriving instance (Eq (X Kind s), Eq (E Kind s)) => Eq (Kind s)
deriving instance (Ord (X Kind s), Ord (E Kind s)) => Ord (Kind s)
data Type s
= TApp (X Type s) (Type s) [Type s]
| TTup (X Type s) [Type s]
| TList (X Type s) (Type s)
| TFun (X Type s) (Type s) (Type s)
| TCon (X Type s) Name
| TVar (X Type s) Name
-- extension point
| TExt (X Type s) !(E Type s)
deriving instance (Show (X Type s), Show (E Type s)) => Show (Type s)
data Pattern s
= PWildcard (X Pattern s)
| PVar (X Pattern s) Name
| PAs (X Pattern s) Name (Pattern s)
| PCon (X Pattern s) Name [Pattern s]
| POp (X Pattern s) (Pattern s) Operator (Pattern s)
| PList (X Pattern s) [Pattern s]
| PTup (X Pattern s) [Pattern s]
deriving instance (Show (X Pattern s)) => Show (Pattern s)
data RHS s
= Guarded (X RHS s) [(Expr s, Expr s)] -- currently not parsed
| Plain (X RHS s) (Expr s)
deriving instance (Show (X RHS s), Show (Expr s)) => Show (RHS s)
data Expr s
= ELit (X Expr s) Literal
| EVar (X Expr s) Name
| ECon (X Expr s) Name
| EList (X Expr s) [Expr s]
| ETup (X Expr s) [Expr s]
| EApp (X Expr s) (Expr s) [Expr s]
| EOp (X Expr s) (Expr s) Operator (Expr s)
| EIf (X Expr s) (Expr s) (Expr s) (Expr s)
| ECase (X Expr s) (Expr s) [(Pattern s, RHS s)]
| ELet (X Expr s) [FunDef s] (Expr s)
| EError (X Expr s)
deriving instance (Show (X Expr s), Show (Pattern s), Show (RHS s), Show (FunDef s)) => Show (Expr s)
data Literal = LInt Integer | LFloat Rational | LChar Char | LString String
deriving (Show)
data Operator = OAdd | OSub | OMul | ODiv | OMod | OEqu | OPow
| OCons
deriving (Show)
instance Pretty Name where
prettysPrec _ (Name n) = showString n
instance Pretty (E Kind s) => Pretty (Kind s) where
prettysPrec _ (KType _) = showString "Type"
prettysPrec d (KFun _ a b) = showParen (d > -1) $
prettysPrec 0 a . showString " -> " . prettysPrec (-1) b
prettysPrec d (KExt _ e) = prettysPrec d e
instance Pretty (E Type s) => Pretty (Type s) where
prettysPrec d (TApp _ a ts) = showParen (d > 10) $
prettysPrec 10 a . foldr (.) id [showString " " . prettysPrec 11 t | t <- ts]
prettysPrec _ (TTup _ ts) =
showString "(" . foldr (.) id (intersperse (showString ",") (map (prettysPrec 0) ts)) . showString ")"
prettysPrec _ (TList _ t) =
showString "[" . prettysPrec 0 t . showString "]"
prettysPrec d (TFun _ a b) = showParen (d > -1) $
prettysPrec 0 a . showString " -> " . prettysPrec (-1) b
prettysPrec _ (TCon _ n) = prettysPrec 11 n
prettysPrec _ (TVar _ n) = prettysPrec 11 n
prettysPrec d (TExt _ e) = prettysPrec d e
instance (Pretty (X Type s), Pretty (E Type s)) => Pretty (DataDef s) where
prettysPrec _ (DataDef _ name vars cons) =
showString "data " . prettysPrec 11 name
. foldr (.) id [showString " (" . prettysPrec 11 n . showString " :: " . prettysPrec 11 k . showString ")"
| (k, n) <- vars]
. showString " = "
. foldr (.) id (intersperse (showString " | ")
[prettysPrec 11 cname . foldr (.) id [showString " " . prettysPrec 11 t | t <- fields]
| (cname, fields) <- cons])
instance HasExt DataDef where
type HasXField DataDef = 'True
type HasECon DataDef = 'False
type ContainsData DataDef = '[Type]
extOf (DataDef x _ _ _) = x
extMap _ ps1 ps2 fdd fty gty (DataDef x n ps cs) =
DataDef (fdd x) n (map (first fty) ps) (map (second (map (extMap (Proxy @Type) ps1 ps2 fty gty))) cs)
instance HasExt FunDef where
type HasXField FunDef = 'True
type HasECon FunDef = 'False
type ContainsData FunDef = '[TypeSig, FunEq, Type, Pattern, RHS, Expr]
extOf (FunDef x _ _ _) = x
extMap _ ps1 ps2 ffd ffe fty fpa frh fex gts gty (FunDef x n sig es) =
FunDef (ffd x) n
(extMap (Proxy @TypeSig) ps1 ps2 fty gts gty sig)
(fmap (extMap (Proxy @FunEq) ps1 ps2 ffe ffd fty fpa frh fex gts gty) es)
instance HasExt TypeSig where
type HasXField TypeSig = 'False
type HasECon TypeSig = 'True
type ContainsData TypeSig = '[Type]
extOf _ = undefined
extMap _ ps1 ps2 fty _ gty (TypeSig ty) =
TypeSig (extMap (Proxy @Type) ps1 ps2 fty gty ty)
extMap _ _ _ _ gts _ (TypeSigExt e) = gts e
instance HasExt FunEq where
type HasXField FunEq = 'True
type HasECon FunEq = 'False
type ContainsData FunEq = '[FunDef, TypeSig, Type, Pattern, RHS, Expr]
extOf (FunEq x _ _ _) = x
extMap _ ps1 ps2 ffe ffd fty fpa frh fex gts gty (FunEq x n ps rhs) =
FunEq (ffe x) n (map (extMap (Proxy @Pattern) ps1 ps2 fpa) ps)
(extMap (Proxy @RHS) ps1 ps2 frh ffd ffe fty fpa fex gts gty rhs)
instance HasExt Kind where
type HasXField Kind = 'True
type HasECon Kind = 'True
type ContainsData Kind = '[]
extOf (KType x) = x
extOf (KFun x _ _) = x
extOf (KExt x _) = x
extMap _ _ _ f _ (KType x) = KType (f x)
extMap p ps1 ps2 f g (KFun x a b) = KFun (f x) (extMap p ps1 ps2 f g a) (extMap p ps1 ps2 f g b)
extMap _ _ _ _ g (KExt x a) = g x a
instance HasExt Type where
type HasXField Type = 'True
type HasECon Type = 'True
type ContainsData Type = '[]
extOf (TApp x _ _) = x
extOf (TTup x _) = x
extOf (TList x _) = x
extOf (TFun x _ _) = x
extOf (TCon x _) = x
extOf (TVar x _) = x
extOf (TExt x _) = x
extMap p ps1 ps2 f g (TApp x a b) = TApp (f x) (extMap p ps1 ps2 f g a) (map (extMap p ps1 ps2 f g) b)
extMap p ps1 ps2 f g (TTup x as) = TTup (f x) (map (extMap p ps1 ps2 f g) as)
extMap p ps1 ps2 f g (TList x a) = TList (f x) (extMap p ps1 ps2 f g a)
extMap p ps1 ps2 f g (TFun x a b) = TFun (f x) (extMap p ps1 ps2 f g a) (extMap p ps1 ps2 f g b)
extMap _ _ _ f _ (TCon x n) = TCon (f x) n
extMap _ _ _ f _ (TVar x n) = TVar (f x) n
extMap _ _ _ _ g (TExt x e) = g x e
instance HasExt Pattern where
type HasXField Pattern = 'True
type HasECon Pattern = 'False
type ContainsData Pattern = '[]
extOf (PWildcard x) = x
extOf (PVar x _) = x
extOf (PAs x _ _) = x
extOf (PCon x _ _) = x
extOf (POp x _ _ _) = x
extOf (PList x _) = x
extOf (PTup x _) = x
extMap _ _ _ f (PWildcard x) = PWildcard (f x)
extMap _ _ _ f (PVar x n) = PVar (f x) n
extMap p ps1 ps2 f (PAs x n a) = PAs (f x) n (extMap p ps1 ps2 f a)
extMap p ps1 ps2 f (PCon x n as) = PCon (f x) n (map (extMap p ps1 ps2 f) as)
extMap p ps1 ps2 f (POp x a n b) = POp (f x) (extMap p ps1 ps2 f a) n (extMap p ps1 ps2 f b)
extMap p ps1 ps2 f (PList x as) = PList (f x) (map (extMap p ps1 ps2 f) as)
extMap p ps1 ps2 f (PTup x as) = PTup (f x) (map (extMap p ps1 ps2 f) as)
instance HasExt RHS where
type HasXField RHS = 'True
type HasECon RHS = 'False
type ContainsData RHS = '[FunDef, TypeSig, FunEq, Type, Pattern, Expr]
extOf (Guarded x _) = x
extOf (Plain x _) = x
extMap _ ps1 ps2 frh ffd ffe fty fpa fex gts gty (Guarded x l) =
Guarded (frh x)
(map (bimap (extMap (Proxy @Expr) ps1 ps2 fex ffd ffe fty fpa frh gts gty)
(extMap (Proxy @Expr) ps1 ps2 fex ffd ffe fty fpa frh gts gty))
l)
extMap _ ps1 ps2 frh ffd ffe fty fpa fex gts gty (Plain x e) =
Plain (frh x) (extMap (Proxy @Expr) ps1 ps2 fex ffd ffe fty fpa frh gts gty e)
instance HasExt Expr where
type HasXField Expr = 'True
type HasECon Expr = 'False
type ContainsData Expr = '[FunDef, TypeSig, FunEq, Type, Pattern, RHS]
extOf (ELit x _) = x
extOf (EVar x _) = x
extOf (ECon x _) = x
extOf (EList x _) = x
extOf (ETup x _) = x
extOf (EApp x _ _) = x
extOf (EOp x _ _ _) = x
extOf (EIf x _ _ _) = x
extOf (ECase x _ _) = x
extOf (ELet x _ _) = x
extOf (EError x) = x
extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty = \case
ELit x l -> ELit (fex x) l
EVar x n -> EVar (fex x) n
ECon x n -> ECon (fex x) n
EList x es -> EList (fex x) (map (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty) es)
ETup x es -> ETup (fex x) (map (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty) es)
EApp x a es -> EApp (fex x) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty a) (map (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty) es)
EOp x a n b -> EOp (fex x) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty a) n (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty b)
EIf x a b c -> EIf (fex x) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty a) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty b) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty c)
ECase x a l -> ECase (fex x) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty a)
(map (bimap (extMap (Proxy @Pattern) ps1 ps2 fpa)
(extMap (Proxy @RHS) ps1 ps2 frh ffd ffe fty fpa fex gts gty))
l)
ELet x as b -> ELet (fex x) (map (extMap (Proxy @FunDef) ps1 ps2 ffd ffe fty fpa frh fex gts gty) as) (extMap p ps1 ps2 fex ffd ffe fty fpa frh gts gty b)
EError x -> EError (fex x)
|