/
Check.cag
283 lines (224 loc) · 11.4 KB
/
Check.cag
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
%%[0
%include lhs2TeX.fmt
%include afp.fmt
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Type check as system F
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf) hs module {%{EH}Core.SysF.Check} import({%{EH}Base.Builtin},{%{EH}Base.Common},{%{EH}Opts},{%{EH}Ty},{%{EH}Error})
%%]
%%[(8 codegen coresysf) hs import({%{EH}Gam})
%%]
%%[(8 codegen coresysf) hs import({%{EH}Base.TermLike},{%{EH}AbstractCore},{%{EH}Core})
%%]
%%[(8 codegen coresysf) hs import({%{EH}LamInfo})
%%]
%%[(8 codegen coresysf) hs import({%{EH}Core.BindExtract},qualified {%{EH}Core.SysF.AsTy} as SysF)
%%]
%%[(8 codegen coresysf) hs import(qualified UHC.Util.FastSeq as Seq)
%%]
%%[(8 codegen coresysf) hs import(Data.Maybe, qualified Data.Map as Map, Control.Applicative)
%%]
%%[(8 codegen coresysf) hs import(UHC.Util.Pretty)
%%]
%%[(8 codegen coresysf).WRAPPER ag import({Core/AbsSyn},{Core/CommonBindNm},{Core/CommonMetaLev})
WRAPPER CodeAGItf
%%]
%%[(99 codegen coresysf)
-- PRAGMA strictcase
PRAGMA nocycle
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Haskell itf
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf) hs export(CheckEnv(..),emptyCheckEnv)
data CheckEnv
= CheckEnv
{ cenvLamMp :: LamMp
}
emptyCheckEnv
= CheckEnv
emptyLamMp
%%]
%%[(8 codegen coresysf) hs export(cmodSysfCheck)
cmodSysfCheck :: EHCOpts -> CheckEnv -> CModule -> ErrL
cmodSysfCheck opts env cmod
= Seq.toList $ errs_Syn_CodeAGItf t
where t = wrap_CodeAGItf
(sem_CodeAGItf (CodeAGItf_AGItf cmod))
(Inh_CodeAGItf
{ opts_Inh_CodeAGItf = opts
, env_Inh_CodeAGItf = env
})
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Global info
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf)
ATTR
AllCodeNT CodeAGItf
[ opts: EHCOpts
env : CheckEnv
| | ]
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% SELF
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf)
ATTR AllCodeNT [ | | self: SELF ]
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Type checking: environment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf) hs
type CheckLookup = MetaLev -> ACoreBindRef -> Maybe CExpr
chkLkup' :: err -> (MetaLev -> ACoreBindRef -> err) -> MetaLev -> ACoreBindRef -> CheckLookup -> (CExpr,err)
chkLkup' emperr mkerr mlev r lk
= maybe ( acoreExprErr ("chkLkup: " ++ show r)
, mkerr mlev r
)
(\e -> (e,emperr))
(lk mlev r)
chkLkup :: MetaLev -> ACoreBindRef -> CheckLookup -> (CExpr,ErrSq)
chkLkup = chkLkup' Seq.empty (\mlev r -> Seq.singleton $ rngLift emptyRange mkErr_NamesNotIntrod ("SysF@" ++ show mlev) [mkHNm r])
%%]
%%[(8 codegen coresysf)
ATTR
AllCodeNT
[ chkLkup: CheckLookup
| | ]
SEM CodeAGItf
| AGItf loc . chkLkup = \_ _ -> Nothing
%%]
Gather:
%%[(8 codegen coresysf)
ATTR AllBind AllPat [ | | gathGam USE {`gamUnion`} {emptyGam} : {SysF.SysfGam} ]
SEM CBound
| Val lhs . gathGam = gamMetaLevSingleton @mlevHere (acoreMkRef @lhs.nm) @expr.self -- (acoreMkAspRef @aspectKeyS @lhs.nm) @expr.self
%%]
Distribute & Bind:
%%[(8 codegen coresysf)
ATTR AllCodeNT [ gam: {SysF.SysfGam} | | ]
SEM CodeAGItf
| AGItf loc . gam = emptyGam
SEM CExpr
| Let loc . gam = @binds.gathGam `gamUnion` @lhs.gam
| Lam Arr body . gam = @bind.gathGam `gamUnion` @lhs.gam
SEM CAlt
| Alt expr . gam = @pat.gathGam `gamUnion` @lhs.gam
%%]
%%[(8888 codegen tycore)
ATTR AllValBind AllTyBind AllExprSeq1 AllPat Expr [ | | gathGam USE {`gamUnion`} {emptyEnv} : Env ]
SEM ValBind
| Val loc . gathGam = @bind.gathGam -- gamSingleton @nm @mlevHere @l1.t
SEM ExprSeq1
| L0Bind loc . gathGam = gamSingleton @nm @mlevHere @l1.t
| L1Bind loc . gathGam = gamSingleton @nm @mlevHere @l2.t
| L2Bind loc . gathGam = gamSingleton @nm @mlevHere @l3.t
SEM Expr
| * - Seq Seq1
lhs . gathGam = emptyEnv
SEM FldBind
| Fld loc . gathGam = gamSingleton @nm @mlevHere @ty.t
| Ty loc . gathGam = gamSingleton @nm @mlevHere @ki.t
SEM Pat
| Var loc . gathGam = gamSingleton @nm @mlevHere @ty.t
%%]
Use:
%%[(8 codegen coresysf)
SEM CExpr
| Var loc . (st,undefNmMp) = let lk mlev r
= gamLookupMetaLev mlev r @lhs.gam
<|> fmap libindaspCore (lamMpLookupAsp (acbrefNm r) (acbaspkeyDefaultSysfTy mlev) (cenvLamMp @lhs.env))
in chkLkup' Map.empty (\l r -> Map.singleton l [r]) (@lhs.mlev+1) @ref lk
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Type checking: actual checking
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf)
ATTR CExpr CAlt [ | | st: {SysF.Ty} ]
SEM CExpr
| App loc . (st,mout) = case (@func.st,@arg.stBoundL) of
(CExpr_Arr abi r, (abo:_))
-> (SysF.moutCSubst mout `cSubstApp` r, mout)
where mout = SysF.matchRL0Bind @lhs.mlev abo abi
_ -> ( appDbg "SysF.Check.CExpr.App.res"
, SysF.emptyMatchOut {SysF.moutErrL = [rngLift emptyRange Err_TyCoreMatchClash (pp @func.st) (pp arr) Nothing Nothing]}
)
where arr = CExpr_Arr (CBind_Bind hsnWild @arg.stBoundL) (acoreVar hsnWild)
| Case loc . (st,moutL) = let match ts = filter SysF.moutHasErr $ zipWith (SysF.match @lhs.mlev) (tail ts) (init ts)
in case @alts.stL of
-- ts -> (t,match (t:ts))
[] -> (appDbg "SysF.Check.CExpr.Case.st",[])
ts@(t:_) -> (t,match ts)
| Lam loc . st = CExpr_Arr @bind.stBind @body.st
| Let loc . st = @body.st
| Int loc . st = SysF.sysftyInt
| Char loc . st = SysF.sysftyChar
| Dbg loc . st = @self
| Cast loc . st = @ty.self
| * - Lam Let Var Int Char Dbg App Ann Cast Case
loc . st = (appDbg "SysF.Check.CExpr.st" :: SysF.Ty)
ATTR CBind [ | | stBind: CBind ]
SEM CBind
| Bind loc . stBind = case @bindAspects.stBoundL of
(b:_) -> CBind_Bind @nm [b]
_ -> CBind_Bind @nm [CBound_Val acbaspkeyDefault @lhs.mlev CLbl_None $ appDbg "SysF.Check.CBind.Bind.bound"]
ATTR AllBound [ | | stBoundL USE {++} {[]} : {[CBound]} ]
SEM CBound
| Val loc . stBoundL = let t = if @mlev == metaLevVal then @expr.st else @expr.self
in [CBound_Val @aspectKeyS (@mlev+1) @lbl t]
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Lists of XX
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%[(8 codegen coresysf)
ATTR CAltL [ | | stL: {[SysF.Ty]} ]
%%]
%%[(8 codegen coresysf)
SEM CAltL
| Nil lhs . stL = []
| Cons lhs . stL = @hd.st : @tl.stL
%%]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Type checking: errors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
No errors == ok
%%[(8 codegen coresysf) hs
type UndefNmMp = Map.Map MetaLev [ACoreBindRef]
undefmpUnion :: UndefNmMp -> UndefNmMp -> UndefNmMp
undefmpUnion = Map.unionWith (++)
%%]
%%[(8 codegen coresysf)
ATTR AllNT [ | | undefNmMp USE {`undefmpUnion`} {Map.empty} : UndefNmMp ]
%%]
%%[(8 codegen coresysf)
ATTR AllNT CodeAGItf [ | | errs USE {`Seq.union`} {Seq.empty} : ErrSq ]
%%]
%%[(8 codegen coresysf)
SEM CBind
| Bind loc . nmErrs = Seq.unions
[ Seq.singleton $ rngLift emptyRange mkErr_NamesNotIntrod ("SysF@" ++ show l) (map mkHNm rs)
| (l,rs) <- Map.toList @bindAspects.undefNmMp
]
lhs . errs = if ehcOptCoreSysFCheckOnlyVal @lhs.opts && @bindAspects.gathMinMetaLev > @lhs.mlev
then Seq.empty
else let e = Seq.unions [@nmErrs,@bindAspects.errs]
in Seq.fromList $ rngLift emptyRange mkNestErr (pp @nm) $ Seq.toList e
-- . errs = Seq.empty
. undefNmMp = Map.empty
SEM CExpr
| App lhs . errs = Seq.unions [@func.errs,@arg.errs,SysF.moutErrs @mout]
| Case lhs . errs = Seq.unions ([@expr.errs,@alts.errs] ++ map SysF.moutErrs @moutL)
%%]
SEM CExpr
| Var lhs . errs = @nmErrs
in Seq.unions
[ @bind.errs, @l0.gathErrs
, Seq.fromList $ rngLift emptyRange mkNestErr (ppValBind @t) $ Seq.toList e
]
SEM Expr
| App lhs . errs = Seq.unions [@func.errs,@arg.errs,moutErrs @mout]
| Case lhs . errs = Seq.unions ([@expr.errs,@alts.errs] ++ map moutErrs @moutL)
| Cast lhs . errs = Seq.unions [@expr.errs,@ty.errs,@castErrs]
| Var Tup lhs . errs = @nmErrs