-
Notifications
You must be signed in to change notification settings - Fork 1
/
desugar.mc
523 lines (491 loc) · 14.6 KB
/
desugar.mc
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
include "log.mc"
include "mexpr/ast.mc"
include "mexpr/ast-builder.mc"
include "mexpr/boot-parser.mc"
include "mexpr/keyword-maker.mc"
include "mexpr/type-check.mc"
include "mexpr/boot-parser.mc"
include "mexpr/builtin.mc"
include "./ast_gen.mc"
include "./parse.mc"
lang DAEParseDesugar = DAEAst
sem daeDesugarType : DAEType -> Type
sem daeDesugarType =
| FloatDAEType r -> TyFloat { info = r.info }
| IntDAEType r -> TyInt { info = r.info }
sem daeDesugarConst : DAEConst -> Const
sem daeDesugarConst =
| FloatDAEConst r -> CFloat { val = r.val.v }
| IntDAEConst r -> CInt { val = r.val.v }
| TrueDAEConst _ -> CBool { val = true }
| FalseDAEConst _ -> CBool { val = false }
| SinDAEConst _ -> CSin {}
| CosDAEConst _ -> CCos {}
| SqrtDAEConst _ -> CSqrt {}
| ExpDAEConst _ -> CExp {}
sem daeDesugarPat : DAEPat -> Pat
sem daeDesugarPat =
| TupleDAEPat r ->
let bindings =
map
(lam n.
PatNamed {
ident = PName n.v,
info = n.i,
ty = TyUnknown { info = n.i }
})
r.names
in
patTuple bindings r.info
sem daeDesugarBinOp : Info -> Const -> (DAEExpr, DAEExpr) -> Expr
sem daeDesugarBinOp info c =
| (left, right) ->
let ty = TyUnknown { info = info } in
TmApp {
lhs = TmApp {
lhs = TmConst { val = c, info = info, ty = ty },
rhs = daeDesugarExpr left,
ty = ty,
info = info
},
rhs = daeDesugarExpr right,
ty = ty,
info = info
}
sem daeDesugarUnOp : Info -> Const -> DAEExpr -> Expr
sem daeDesugarUnOp info c =
| right ->
let ty = TyUnknown { info = info } in
TmApp {
lhs = TmConst { val = c, info = info, ty = ty },
rhs = daeDesugarExpr right,
ty = ty,
info = info
}
sem daeDesugarExpr : DAEExpr -> Expr
sem daeDesugarExpr =
| VarDAEExpr r ->
TmVar {
ident = r.name.v,
info = r.info,
ty = TyUnknown { info = r.info },
frozen = false
}
| AbsDAEExpr r ->
let tyParam = TyUnknown { info = r.name.i } in
let ty = TyUnknown { info = r.info } in
TmLam {
ident = r.name.v,
tyAnnot = tyParam,
tyParam = tyParam,
body = daeDesugarExpr r.body,
ty = ty,
info = r.info
}
| AppDAEExpr r -> TmApp {
lhs = daeDesugarExpr r.fn,
rhs = daeDesugarExpr r.arg,
ty = TyUnknown { info = r.info },
info = r.info
}
| AppDAEExpr (r & {fn = ConstDAEExpr { val = PredDAEConst _ }}) ->
let ty = TyUnknown { info = r.info } in
TmApp {
lhs = TmApp {
lhs = TmConst { val = CSubi (), info = r.info, ty = ty },
rhs = daeDesugarExpr r.arg,
ty = ty,
info = r.info
},
rhs = TmConst { val = CInt { val = 1 }, info = r.info, ty = ty },
ty = ty,
info = r.info
}
-- | AppDAEExpr (r & {fn = ConstDAEExpr { val = ExpDAEConst _ }}) ->
-- let ty = TyUnknown { info = r.info } in
-- TmApp {
-- lhs = TmApp {
-- lhs = TmConst { val = CSubi (), info = r.info, ty = ty },
-- rhs = daeDesugarExpr r.arg,
-- ty = ty,
-- info = r.info
-- },
-- rhs = TmConst { val = CInt { val = 1 }, info = r.info, ty = ty },
-- ty = ty,
-- info = r.info
-- }
| TupleDAEExpr r ->
tmTuple r.info (TyUnknown { info = r.info }) (map daeDesugarExpr r.exprs)
| ProjDAEExpr r ->
let ty = TyUnknown { info = r.info } in
let x = nameNoSym "x" in
TmMatch {
target = daeDesugarExpr r.expr,
pat = patTuple [
PatNamed {
ident = PName x,
info = r.info,
ty = ty
}] r.info,
thn = TmVar {
ident = x,
info = r.info,
ty = ty,
frozen = false
},
els = TmNever { info = r.info, ty = ty },
ty = ty,
info = r.info
}
| ConstDAEExpr r -> TmConst {
val = daeDesugarConst r.val,
ty = TyUnknown { info = r.info },
info = r.info
}
| AddDAEExpr r -> daeDesugarBinOp r.info (CAddf ()) (r.left, r.right)
| SubDAEExpr r -> daeDesugarBinOp r.info (CSubf ()) (r.left, r.right)
| MulDAEExpr r -> daeDesugarBinOp r.info (CMulf ()) (r.left, r.right)
| DivDAEExpr r -> daeDesugarBinOp r.info (CDivf ()) (r.left, r.right)
| LtDAEExpr r -> daeDesugarBinOp r.info (CLti ()) (r.left, r.right)
| LtfDAEExpr r -> daeDesugarBinOp r.info (CLtf ()) (r.left, r.right)
| EqDAEExpr r -> daeDesugarBinOp r.info (CEqi ()) (r.left, r.right)
| NegDAEExpr r -> daeDesugarUnOp r.info (CNegf ()) r.right
| MatchInDAEExpr r ->
let ty = TyUnknown { info = r.info } in
TmMatch {
target = daeDesugarExpr r.target,
pat = daeDesugarPat r.pat,
thn = daeDesugarExpr r.body,
els = TmNever { info = r.info, ty = ty },
ty = ty,
info = r.info
}
| IfDAEExpr r ->
let ty = TyUnknown { info = r.info } in
TmMatch {
target = daeDesugarExpr r.pred,
pat = PatBool { val = true, info = r.info, ty = ty },
thn = daeDesugarExpr r.thn,
els = daeDesugarExpr r.els,
ty = ty,
info = r.info
}
| LetDAEExpr r ->
let ty = TyUnknown { info = r.info } in
TmLet {
ident = r.name.v,
tyAnnot = ty,
tyBody = ty,
body = daeDesugarExpr r.arg,
inexpr = daeDesugarExpr r.body,
ty = ty,
info = r.info
}
| RecLetDAEExpr r ->
let ty = TyUnknown { info = r.info } in
let binding = {
ident = r.name.v,
tyAnnot = ty,
tyBody = ty,
body = daeDesugarExpr r.arg,
info = r.info
} in
TmRecLets {
bindings = [binding],
inexpr = daeDesugarExpr r.body,
ty = ty,
info = r.info
}
| PrimDAEExpr r ->
recursive let recur = lam order. lam expr.
switch expr
case VarDAEExpr varr then TmDVar {
ident = varr.name.v,
order = order,
info = r.info,
ty = TyUnknown { info = r.info }
}
case PrimDAEExpr primr then
recur (succ order) primr.left
case _ then error "daeDesugarExpr: Undefined"
end
in
recur 1 r.left
sem daeDesugarTop : DAETop -> Expr
sem daeDesugarTop =
| LetDAETop r -> nulet_ r.name.v (daeDesugarExpr r.arg)
| RecLetDAETop r -> nureclets_ [(r.name.v, daeDesugarExpr r.arg)]
sem daeDesugarVars : DAEVar -> [(Name, Type)]
sem daeDesugarVars =
| VarsDAEVar r ->
let ty = daeDesugarType r.ty in
map (lam name. (name.v, ty)) r.names
sem daeDesugarEqn : DAEEqn -> Expr
sem daeDesugarEqn =
| EqnDAEEqn r -> subf_ (daeDesugarExpr r.left) (daeDesugarExpr r.right)
-- Assumes a well-formed DAE program.
sem daeDesugarProg : DAEProg -> TmDAERec
sem daeDesugarProg =
| ProgDAEProg r ->
let bindings = bindall_ (snoc (map daeDesugarTop r.tops) unit_) in
let vars = join (map daeDesugarVars r.vars) in
let ieqns = map daeDesugarEqn r.ieqns in
let eqns = map daeDesugarEqn r.eqns in
let out = daeDesugarExpr r.output in
{
bindings = bindings,
vars = vars,
ieqns = ieqns,
eqns = eqns,
out = out,
info = r.info
}
-- sem daeResugarExpr : Expr -> Option DAEExpr
-- sem daeResugarExpr =
-- | TmVar r ->
-- let i = r.info in
-- Some (VarDAEExpr { info = i, name = { i = i, v = r.ident } })
-- | TmLam r ->
-- let i = r.info in
-- optionBind (daeResugarExpr r.body) (lam body.
-- Some
-- (AbsDAEExpr { body = body, info = i, name = { i = i, v = r.ident } }))
-- | TmApp {
-- lhs = TmApp {
-- lhs = TmConst { val = c, info = info },
-- rhs = left
-- },
-- rhs = right,
-- info = info
-- } ->
-- optionBind (daeResugarExpr left) (lam left.
-- optionBind (daeResugarExpr right) (lam right.
-- daeResugarBinOp { info = info, left = left, right = right } c))
-- | TmApp {
-- lhs = TmConst { val = c, info = info },
-- rhs = right,
-- info = info
-- } ->
-- optionBind (daeResugarExpr right) (lam right.
-- daeResugarUnOp { info = info, right = right } c)
-- | TmApp r ->
-- optionBind (daeResugarExpr r.lhs) (lam lhs.
-- optionBind (daeResugarExpr r.rhs) (lam rhs.
-- Some (AppDAEExpr { fn = lhs, arg = rhs, info = r.info })))
-- | TmRecord r ->
-- optionMap
-- (lam exprs. TupleDAEExpr { info = r.info, exprs = exprs })
-- ((optionCompose
-- (optionMapM daeResugarExpr)
-- record2tuple)
-- r.bindings)
-- | TmConst r ->
-- optionBind (daeResugarConst r.info r.val) (lam val.
-- Some (ConstDAEExpr { val = val, info = r.info }))
-- | TmLet r ->
-- let i = r.info in
-- optionBind (daeResugarExpr r.body) (lam arg.
-- optionBind (daeResugarExpr r.inexpr) (lam body.
-- Some
-- (LetDAEExpr {
-- arg = arg,
-- body = body,
-- info = i,
-- name = { i = i, v = r.ident }
-- })))
-- | TmRecLets (r & { bindings = [binding] }) ->
-- optionBind (daeResugarExpr binding.body) (lam arg.
-- optionBind (daeResugarExpr r.inexpr) (lam body.
-- Some
-- (RecLetDAEExpr {
-- arg = arg,
-- body = body,
-- info = r.info,
-- name = { i = r.info, v = binding.ident }
-- })))
-- | TmMatch {
-- info = info,
-- target = target,
-- pat = pat & !(PatBool { val = true }),
-- thn = thn,
-- els = TmNever _
-- } ->
-- optionBind (daeResugarExpr target) (lam target.
-- optionBind (daeResugarPat pat) (lam pat.
-- optionBind (daeResugarExpr thn) (lam body.
-- Some
-- (MatchInDAEExpr {
-- pat = pat,
-- body = body,
-- info = info,
-- target = target}))))
-- | TmMatch {
-- info = info,
-- target = target,
-- pat = pat & PatRecord {bindings = bindings},
-- thn = thn & TmVar {ident = exprName},
-- els = TmNever _
-- } ->
-- optionBind (daeResugarExpr target) (lam target.
-- match matchIsProj bindings exprName with Some fieldLabel then
-- let str = sidToString fieldLabel in
-- if forAll isDigit str then
-- let n = string2int str in
-- Some
-- (ProjDAEExpr {
-- expr = target,
-- label = { v = n, i = info },
-- info = info
-- })
-- else None ()
-- else
-- optionBind (daeResugarPat pat) (lam pat.
-- optionBind (daeResugarExpr thn) (lam body.
-- Some
-- (MatchInDAEExpr {
-- pat = pat,
-- body = body,
-- info = info,
-- target = target}))))
-- | TmMatch {
-- info = info,
-- target = target,
-- pat = PatBool { val = true },
-- thn = thn,
-- els = els & !TmNever _
-- } ->
-- optionBind (daeResugarExpr target) (lam pred.
-- optionBind (daeResugarExpr thn) (lam thn.
-- optionBind (daeResugarExpr els) (lam els.
-- Some
-- (IfDAEExpr {
-- pred = pred,
-- thn = thn,
-- els = els,
-- info = info}))))
-- | _ -> None ()
-- sem daeResugarUnOp
-- : {info : Info, right : DAEExpr} -> Const -> Option DAEExpr
-- sem daeResugarUnOp r =
-- | CNegf _ ->
-- Some
-- (SubDAEExpr {
-- left = ConstDAEExpr {
-- val = FloatDAEConst { val = { i = r.info, v = 0. }, info = r.info },
-- info = r.info
-- },
-- right = r.right,
-- info = r.info
-- })
-- | CSin _ -> daeResugarElemFunction r.info "sin" r.right
-- | CCos _ -> daeResugarElemFunction r.info "cos" r.right
-- | _ -> None ()
-- sem daeResugarElemFunction : Info -> String -> DAEExpr -> Option DAEExpr
-- sem daeResugarElemFunction info name =| arg ->
-- Some
-- (AppDAEExpr {
-- fn = VarDAEExpr {
-- info = info,
-- name = { i = info, v = nameNoSym name }
-- },
-- arg = arg,
-- info = info
-- })
-- sem daeResugarBinOp
-- : {info : Info, left : DAEExpr, right : DAEExpr} -> Const -> Option DAEExpr
-- sem daeResugarBinOp r =
-- | CAddf _ -> Some (AddDAEExpr r)
-- | CSubf _ -> Some (SubDAEExpr r)
-- | CMulf _ -> Some (MulDAEExpr r)
-- | CDivf _ -> Some (DivDAEExpr r)
-- | _ -> None ()
-- sem daeResugarConst : Info -> Const -> Option DAEConst
-- sem daeResugarConst info =
-- | CFloat r ->
-- Some (FloatDAEConst { val = { i = info, v = r.val }, info = info })
-- | CInt r ->
-- Some (IntDAEConst { val = { i = info, v = r.val }, info = info })
-- | CBool r ->
-- if r.val then
-- Some (TrueDAEConst { info = info })
-- else
-- Some (FalseDAEConst { info = info })
-- | _ -> None ()
-- sem daeResugarPat : Pat -> Option DAEPat
-- sem daeResugarPat =
-- | PatRecord r ->
-- optionMap (lam names. TupleDAEPat { info = r.info, names = names })
-- ((optionCompose
-- (optionMapM
-- (lam p.
-- match p with PatNamed {ident = PName v, info = i} then
-- Some { i = i, v = v }
-- else None ()))
-- record2tuple)
-- r.bindings)
-- | _ -> None ()
end
lang TestLang = DAEParseAnalysis + DAEParseDesugar end
mexpr
use TestLang in
let parseDAEProg = lam prog.
let prog = daeParseExn "internal" prog in
logMsg logLevel.debug
(lam. strJoin "\n" ["Input program:", daeProgToString prog]);
prog
in
let parseDAEExprExn = lam prog. typeCheck (symbolize (parseDAEExprExn prog)) in
logSetLogLevel logLevel.error;
let prog = parseDAEProg "
let mul = lam x. lam y. x*y end
let pow2 = lam x. mul x x end
variables
x, vx, y, vy, h : Float
init
x = 1.;
vy' = 0. - 1.
equations
vx = x';
vy = y';
vx' = mul x h;
vy' = mul y h - 1.;
pow2 x + pow2 y = pow2 1.
output
{x, vx, vx'}
"
in
let expectedExpr = parseDAEExprExn "
let mul = lam x. lam y. mulf x y in
let pow2 = lam x. mul x x in
lam x : Float.
lam vx : Float.
lam y : Float.
lam vy : Float.
lam h : Float.
{
ieqns =
(subf x 1.,
subf (prim 1 vy) (subf 0. 1.)),
eqns =
(subf vx (prim 1 x),
subf vy (prim 1 y),
subf (prim 1 vx) (mul x h),
subf (prim 1 vy) (subf (mul y h) 1.),
subf (addf (pow2 x) (pow2 y)) (pow2 1.)),
out =
(
x,
vx,
prim 1 vx
)
}
"
in
let daer = daeDesugarProg prog in
match typeCheck (symbolize (TmDAE daer)) with TmDAE daer then
logSetLogLevel logLevel.error;
logMsg logLevel.debug
(lam. strJoin "\n" ["Output program:", expr2str (TmDAE daer)]);
utest TmDAE daer with TmDAE (_tmToTmDAERec expectedExpr) using eqExpr in
()
else error "impossible"