~thon/thon

thon/thon.sml -rw-r--r-- 32.6 KiB
2c9c65d6Evan Bergeron Parse SUCC | LET | CASE | FN 3 months ago
                                                                                
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
(* thon - a small functional language *)
structure Thon : sig
              val parse : string -> Ast.exp
              val parseFile : string -> Ast.exp
              val typeof' : A.typ list -> 'b option list -> A.exp -> A.typ
              val typeof : A.exp -> A.typ
              (* val test : unit -> unit *)
              val eval : A.exp -> A.exp
              val isval : A.exp -> bool
              val step : A.exp -> A.exp
              val subst : A.exp -> A.exp -> A.exp
              val run : string -> A.exp
              val eraseNamesInTyp : A.typ -> A.typ
              val runFile : string -> A.exp
              val newRunFile : string -> A.exp
              val newParseFile : string -> A.exp
              val findParseErrors : string -> unit
              val elaborateDatatypes : A.exp -> A.exp
              val shiftDeBruijinIndicesInExp : int -> A.exp -> int -> A.exp
              val get : 'a list -> int -> 'a
              val istype : 'a option list -> A.typ -> bool
              val abstractOutType : string -> A.typ -> A.typ -> A.typ
              val substType : A.typ -> A.typ -> A.typ
              exception IllTyped
              exception IllTypedMsg of string
          end =
struct

exception IllTyped
exception IllTypedMsg of string
exception No
exception VarNotInContext
exception VarWithNegativeDeBruijinIndex of string * int
exception ClientTypeCannotEscapeClientScope
exception Unimplemented


fun get ctx i =
    case (List.findi (fn (j, _) => j = i) ctx) of
        NONE => (print (Int.toString i); raise VarNotInContext)
      | SOME (j,v) => v


fun istype typeCtx A.Nat = true
  | istype typeCtx A.Unit = true
  | istype typeCtx (A.TypVar (name, i)) = i < (length typeCtx)
  | istype typeCtx (A.Arr(d, c)) = (istype typeCtx d) andalso (istype typeCtx c)
  | istype typeCtx (A.Prod(l, r)) = (istype typeCtx l) andalso (istype typeCtx r)
  | istype typeCtx (A.Plus(l, r)) = (istype typeCtx l) andalso (istype typeCtx r)
  | istype typeCtx (A.All (name, t')) = istype (NONE::typeCtx) t'
  | istype typeCtx (A.Some (name, t')) = istype (NONE::typeCtx) t'
  | istype typeCtx (A.TyRec (name, t')) = istype (NONE::typeCtx) t'


fun substType' src A.Nat bindingDepth = A.Nat
  | substType' src A.Unit bindingDepth = A.Unit
  | substType' src (A.TypVar (name, n)) bindingDepth =
    if n = bindingDepth then src else
    if n > bindingDepth then A.TypVar (name, n-1) else
    (A.TypVar (name, n))
  | substType' src (A.Arr(t, t')) bindingDepth = A.Arr((substType' src t bindingDepth),
                                                       (substType' src t' bindingDepth))
  | substType' src (A.Prod(l, r)) bindingDepth = A.Prod((substType' src l bindingDepth),
                                                        (substType' src r bindingDepth))
  | substType' src (A.Plus(l, r)) bindingDepth = A.Plus((substType' src l bindingDepth),
                                                        (substType' src r bindingDepth))
  | substType' src (A.All (name, t)) bindingDepth =
    A.All(name, substType' src t (bindingDepth + 1))
  | substType' src (A.Some (name, t)) bindingDepth =
    A.Some(name, substType' src t (bindingDepth + 1))
  | substType' src (A.TyRec (name, t)) bindingDepth =
    A.TyRec(name, substType' src t (bindingDepth + 1))


fun substType src dst = substType' src dst 0


(* Turns search to A.Var bindingDepth
 *
 * DEVNOTE: assumes the caller will place the result underneath a type
 * variable binding site.
 *
 * Remarkably similar to substType - might be able to dedup. This needs
 * to track bindingDepth though and subst in A.TypVar of the appropriate
 * depth.
 *)
fun abstractOutType' name search t bindingDepth =
    if t = search then (A.TypVar (name, bindingDepth)) else
    case t
     of A.Nat => A.Nat
      | A.Unit => A.Unit
      | A.TypVar (name, n)  => A.TypVar (name, n)
      | A.Arr(d, c) => A.Arr((abstractOutType' name search d bindingDepth),
                          (abstractOutType' name search c bindingDepth))
      | A.Prod(l, r) => A.Prod((abstractOutType' name search l bindingDepth),
                           (abstractOutType' name search r bindingDepth))
      | A.Plus(l, r) => A.Plus((abstractOutType' name search l bindingDepth),
                           (abstractOutType' name search r bindingDepth))
      | A.All (name, t') => A.All(name, abstractOutType' name search t' (bindingDepth+1))
      | A.Some (name, t') => A.Some(name, abstractOutType' name search t' (bindingDepth+1))
      | A.TyRec (name, t') => A.TyRec(name, abstractOutType' name search t' (bindingDepth+1))


fun abstractOutType name search t = abstractOutType' name search t 0


fun decrDeBruijinIndices t =
    case t of
        A.Nat => A.Nat
      | A.Unit => A.Unit
      | A.TypVar (name, i) => if (i-1) < 0 then raise ClientTypeCannotEscapeClientScope
                    else A.TypVar (name, i -1)
      | A.Arr(d, c) => A.Arr(decrDeBruijinIndices d, decrDeBruijinIndices c)
      | A.Prod(l, r) => A.Prod(decrDeBruijinIndices l, decrDeBruijinIndices r)
      | A.Plus(l, r) => A.Plus(decrDeBruijinIndices l, decrDeBruijinIndices r)
      | A.All (name, t') => A.All(name, decrDeBruijinIndices t')
      | A.Some (name, t') => A.Some(name, decrDeBruijinIndices t')
      | A.TyRec (name, t') => A.TyRec(name, decrDeBruijinIndices t')


fun shiftDeBruijinIndicesInExp shift dst bindingDepth =
    case dst
     of  A.Zero => A.Zero
       | A.TmUnit => A.TmUnit
       | A.Var (name, n)  => if n >= bindingDepth then A.Var(name, n+shift) else
                             A.Var(name, n)
       | A.Succ e2 => A.Succ (shiftDeBruijinIndicesInExp shift e2 bindingDepth)
       | A.ProdLeft e => A.ProdLeft (shiftDeBruijinIndicesInExp shift e bindingDepth)
       | A.ProdRight e => A.ProdRight (shiftDeBruijinIndicesInExp shift e bindingDepth)
       | A.PlusLeft (t, e) => A.PlusLeft (t, shiftDeBruijinIndicesInExp shift e bindingDepth)
       | A.PlusRight (t, e) => A.PlusRight (t, shiftDeBruijinIndicesInExp shift e bindingDepth)
       | A.Case(c, lname, l, rname, r) =>
         A.Case(shiftDeBruijinIndicesInExp shift c bindingDepth,
                lname, shiftDeBruijinIndicesInExp shift l (bindingDepth+1),
                rname, shiftDeBruijinIndicesInExp shift r (bindingDepth+1))
       | A.Fn (argName, t, f) => A.Fn(argName, t, (shiftDeBruijinIndicesInExp shift f (bindingDepth+1)))
       | A.Let (varname, vartype, varval, varscope) =>
         A.Let(varname,
               vartype,
               (shiftDeBruijinIndicesInExp shift varval (bindingDepth)),
               (shiftDeBruijinIndicesInExp shift varscope (bindingDepth+1)))
       | A.App (f, n) => A.App((shiftDeBruijinIndicesInExp shift f bindingDepth), (shiftDeBruijinIndicesInExp shift n bindingDepth))
       | A.Ifz (i, t, prev, e) => A.Ifz(shiftDeBruijinIndicesInExp shift i bindingDepth,
                                        shiftDeBruijinIndicesInExp shift t bindingDepth,
                                        prev,
                                        shiftDeBruijinIndicesInExp shift e (bindingDepth+1)) (* binds *)
       | A.Rec (i, baseCase, prevCaseName, recCase) =>
         A.Rec(shiftDeBruijinIndicesInExp shift i bindingDepth,
               shiftDeBruijinIndicesInExp shift baseCase bindingDepth,
               prevCaseName, shiftDeBruijinIndicesInExp shift recCase (bindingDepth+1))
       | A.Fix (name, t, e) =>
         A.Fix(name, t, shiftDeBruijinIndicesInExp shift e (bindingDepth+1)) (* binds *)
       | A.TypFn (name, e) => A.TypFn (name, shiftDeBruijinIndicesInExp shift e bindingDepth) (* abstracts over types, not exps *)
       | A.TypApp (appType, e) => A.TypApp(appType, shiftDeBruijinIndicesInExp shift e bindingDepth)
       | A.Impl(reprType, pkgImpl, t) => A.Impl(reprType, shiftDeBruijinIndicesInExp shift pkgImpl bindingDepth, t)
       | A.Use (pkg, clientName, typeName, client) =>
         A.Use(shiftDeBruijinIndicesInExp shift pkg bindingDepth,
               clientName,
               typeName,
               shiftDeBruijinIndicesInExp shift client (bindingDepth+1))
       | A.Pair (l, r) => A.Pair (shiftDeBruijinIndicesInExp shift l bindingDepth, shiftDeBruijinIndicesInExp shift r bindingDepth)
       | A.Fold(t, e') => A.Fold(t, (shiftDeBruijinIndicesInExp shift e' (bindingDepth))) (* binds a typ var *)
       | A.Unfold(e') => A.Unfold(shiftDeBruijinIndicesInExp shift e' (bindingDepth))



(* Just substitute the srcType in everywhere you see a A.TypVar bindingDepth *)
fun substTypeInExp' srcType dstExp bindingDepth =
    case dstExp
     of  A.Zero => A.Zero
       | A.Var (name, i) => A.Var (name, i)
       | A.TmUnit => A.TmUnit
       | A.Succ e2 => A.Succ (substTypeInExp' srcType e2 bindingDepth)
       | A.ProdLeft e => A.ProdLeft (substTypeInExp' srcType e bindingDepth)
       | A.ProdRight e => A.ProdRight (substTypeInExp' srcType e bindingDepth)
       | A.PlusLeft (t, e) => A.PlusLeft (t, substTypeInExp' srcType e bindingDepth)
       | A.PlusRight (t, e) => A.PlusRight (t, substTypeInExp' srcType e bindingDepth)
       | A.Case(c, lname, l, rname, r) =>
            A.Case(substTypeInExp' srcType c bindingDepth,
                   lname,
                   substTypeInExp' srcType l bindingDepth,
                   rname,
                   substTypeInExp' srcType r bindingDepth)
       | A.Fn (argName, argType, funcBody) =>
            A.Fn(argName, (substType' srcType argType bindingDepth),
                substTypeInExp' srcType funcBody bindingDepth)
       | A.Let (varname, vartype, varval, varscope) =>
            A.Let(varname, (substType' srcType vartype bindingDepth),
                  substTypeInExp' srcType varval bindingDepth,
                  substTypeInExp' srcType varscope bindingDepth
                 )
       | A.App (f, n) =>
            A.App (substTypeInExp' srcType f bindingDepth,
                 substTypeInExp' srcType n bindingDepth)
       | A.Ifz (i, t, prev, e) =>
            A.Ifz(substTypeInExp' srcType i bindingDepth,
                  substTypeInExp' srcType t bindingDepth,
                  prev,
                  substTypeInExp' srcType e bindingDepth)
       | A.Pair (l, r) =>
            A.Pair (substTypeInExp' srcType l bindingDepth,
                   substTypeInExp' srcType r bindingDepth)
       | A.Rec (i, baseCase, prevCaseName, recCase) =>
            A.Rec(substTypeInExp' srcType i bindingDepth,
                substTypeInExp' srcType baseCase bindingDepth,
                prevCaseName, substTypeInExp' srcType recCase bindingDepth)
       | A.Fix (name, t, e) =>
         A.Fix(name,
               substType' srcType t bindingDepth,
               substTypeInExp' srcType e bindingDepth)
       | A.TypFn (name, e) => A.TypFn(name, substTypeInExp' srcType e (bindingDepth+1)) (* binds type var *)
       | A.TypApp (appType, e) =>
            A.TypApp(substType' srcType appType bindingDepth,
                   substTypeInExp' srcType e bindingDepth)
       | A.Impl(reprType, pkgImpl, pkgType) =>
            A.Impl(substType' srcType reprType bindingDepth,
                 substTypeInExp' srcType pkgImpl bindingDepth,
                 substType' srcType pkgType bindingDepth)
       | A.Use (pkg, clientName, typeName, client) =>
            A.Use(substTypeInExp' srcType pkg bindingDepth,
                  clientName,
                  typeName,
                  substTypeInExp' srcType client (bindingDepth+1)) (*binds type var*)
       | A.Fold(t, e') => A.Fold(substType' srcType t bindingDepth,
                             substTypeInExp' srcType e' (bindingDepth+1)) (* binds typ var *)
       | A.Unfold(e') => A.Unfold(substTypeInExp' srcType e' bindingDepth)


fun setDeBruijnIndexInType t varnames typnames =
    let fun find name names =
        (case List.findi (fn (_, n : string) => n = name) names
         of NONE => NONE
          | SOME (i, _) => SOME i)
    in
    case t
     of  A.Nat => A.Nat
       | A.Unit => A.Unit
       | A.TypVar (name, i) =>
         (case find name typnames of
             NONE => (print ("unknown type var: "^ name); raise VarNotInContext)
           | SOME i => A.TypVar (name, i))
       | A.Arr(d, c) =>
            A.Arr(setDeBruijnIndexInType d varnames typnames,
                  setDeBruijnIndexInType c varnames typnames)
       | A.Prod(l, r) =>
            A.Prod(setDeBruijnIndexInType l varnames typnames,
                   setDeBruijnIndexInType r varnames typnames)
       | A.Plus(l, r) =>
            A.Plus(setDeBruijnIndexInType l varnames typnames,
                   setDeBruijnIndexInType r varnames typnames)
       | A.All (name, t') =>
            A.All(name, setDeBruijnIndexInType t' varnames (name::typnames))
       | A.Some (name, t') =>
            A.Some(name, setDeBruijnIndexInType t' varnames (name::typnames))
       | A.TyRec (name, t') =>
            A.TyRec(name, setDeBruijnIndexInType t' varnames (name::typnames))
       end


fun setDeBruijnIndex e varnames typnames =
    let fun find name names =
        (case List.findi (fn (_, n : string) => n = name) names
         of NONE => NONE
          | SOME (i, _) => SOME i)
    in
    case e
     of  A.Zero => e
       | A.TmUnit => e
       | A.Var (n, i) =>
         (case find n varnames of
             NONE => (print ("unknown var: "^ n); raise VarNotInContext)
           | SOME i => A.Var (n, i))
       | A.Fn(name, argType, funcBody) =>
         A.Fn(name,
               setDeBruijnIndexInType argType varnames typnames,
               setDeBruijnIndex funcBody (name::varnames) typnames)
       | A.Let (varname, vartype, varval, varscope) =>
         A.Let(varname,
               setDeBruijnIndexInType vartype varnames typnames,
               (setDeBruijnIndex varval (varnames) typnames),
               setDeBruijnIndex varscope (varname::varnames) typnames)
       | A.Succ e2 => A.Succ (setDeBruijnIndex e2 varnames typnames)
       | A.ProdLeft e => A.ProdLeft (setDeBruijnIndex e varnames typnames)
       | A.ProdRight e => A.ProdRight (setDeBruijnIndex e varnames typnames)
       | A.PlusLeft (t, e) =>
            A.PlusLeft(setDeBruijnIndexInType t varnames typnames,
                       setDeBruijnIndex e varnames typnames)
       | A.PlusRight (t, e) =>
            A.PlusRight(setDeBruijnIndexInType t varnames typnames,
                        setDeBruijnIndex e varnames typnames)
       | A.App (f, n) => A.App (setDeBruijnIndex f varnames typnames,
                                setDeBruijnIndex n varnames typnames)
       | A.Ifz (i, t, prev, e) => A.Ifz (setDeBruijnIndex i varnames typnames,
                                   setDeBruijnIndex t varnames typnames,
                                   prev,
                                   setDeBruijnIndex e (prev::varnames) typnames)
       | A.Pair (l, r) => A.Pair (setDeBruijnIndex l varnames typnames,
                                    setDeBruijnIndex r varnames typnames)
       | A.Rec (i, baseCase, prevCaseName, recCase) =>
            A.Rec (setDeBruijnIndex i varnames typnames,
                   setDeBruijnIndex baseCase varnames typnames,
                   prevCaseName, (setDeBruijnIndex recCase (prevCaseName::varnames) typnames))
       | A.Fix(name, t, e) =>
         A.Fix(name,
               setDeBruijnIndexInType t varnames typnames,
               setDeBruijnIndex e (name::varnames) typnames)
       | A.Case (c, lname, l, rname, r) => A.Case(
            setDeBruijnIndex c varnames typnames,
            lname,
            setDeBruijnIndex l (lname::varnames) typnames,
            rname,
            setDeBruijnIndex r (rname::varnames) typnames)
       | A.Use (pkg, clientName, typeName, client) => A.Use (
            setDeBruijnIndex pkg varnames typnames,
            clientName,
            typeName,
            setDeBruijnIndex client (clientName::varnames) (typeName::typnames))
       | A.TypApp (appType, e) =>
            A.TypApp (setDeBruijnIndexInType appType varnames typnames,
                      setDeBruijnIndex e varnames typnames)
       | A.TypFn (name, e) => A.TypFn (name, setDeBruijnIndex e varnames (name::typnames))
       | A.Fold(A.TyRec(name, t) (*declared type*), e'(* binds a typ var *)) =>
         A.Fold (A.TyRec(name, setDeBruijnIndexInType t varnames (name::typnames)),
                 setDeBruijnIndex e' varnames typnames)
       | A.Unfold(e') =>
            A.Unfold (setDeBruijnIndex e' varnames typnames)
       | A.Impl (reprType, pkgImpl, pkgType) =>
            A.Impl(setDeBruijnIndexInType reprType varnames typnames,
                   setDeBruijnIndex pkgImpl varnames typnames,
                   setDeBruijnIndexInType pkgType varnames typnames)
       | A.Data(dname, lname, ltyp, rname, rtyp, exp) =>
         A.Data(dname,
                lname,
                (* binds a typ var*)
                setDeBruijnIndexInType ltyp varnames (dname::typnames),
                rname,
                (* binds a typ var*)
                setDeBruijnIndexInType rtyp varnames (dname::typnames),
                (* binds lname and rname and dname *)
                setDeBruijnIndex exp (("expose" ^ dname)::rname::lname::varnames) (dname::typnames)
               )
       | _ => raise Unimplemented (* TODO *)
end

fun elaborateDatatype e =
    case e of
        A.Data(dataname, lname, ltyp, rname, rtyp, exp) =>
        let
            val datanameimpl = dataname ^ "Impl"
            val withType = A.TyRec(dataname, A.Plus(ltyp, rtyp))
            (* dataname is not bound here - the recursive reference is bound to the abstract
             * type bound in the Some *)
            val tInLtyp = substType (A.TypVar("t", 0)) ltyp
            val tInRtyp = substType (A.TypVar("t", 0)) rtyp
            val exposeFnType = A.Arr(A.TypVar("t", 0),
                                     A.Plus(tInLtyp, tInRtyp))
            val exposeFn = A.Fn(dataname ^ "exp", withType, A.Unfold(A.Var(dataname ^ "exp", 0)))
            val pkgType = A.Some("t", (*arbitrary name ok here *)
                                 A.Prod(A.Prod(A.Arr(tInLtyp, A.TypVar("t", 0)),
                                               A.Arr(tInRtyp, A.TypVar("t", 0))),
                                 exposeFnType)
                                )
            val lfn = A.Fn("foo", substType withType ltyp,
                           A.Fold(withType, A.PlusLeft(
                                      A.Plus(substType withType ltyp,
                                             substType withType rtyp),
                                      A.Var("foo", 0)
                                  )))
            val rfn = A.Fn("natAndNatList", substType withType rtyp,
                           A.Fold(withType, A.PlusRight(
                                      A.Plus(substType withType ltyp,
                                             substType withType rtyp),
                                      A.Var("natAndNatList", 0)
                                  )))
            val dtval = A.Impl(withType,
                               A.Pair(A.Pair(lfn, rfn), exposeFn),
                               pkgType)
            val useExp =
                A.Use(A.Var(datanameimpl, 0), "li", dataname,
                A.Let(lname, (A.Arr(ltyp, A.TypVar(dataname, 0))), A.ProdLeft(A.ProdLeft(A.Var("li", 0))),
               (A.Let(rname, (A.Arr(rtyp, A.TypVar(dataname, 0))), A.ProdRight(A.ProdLeft(A.Var("li", 1))),
                A.Let("expose" ^ dataname,
                      (A.Arr(A.TypVar(dataname, 0), A.Plus(ltyp, rtyp))),
                      A.ProdRight(A.Var("li", 2)),
                (* We've already bound term vars lname and rname, and type var dname.
                 *
                 * We have package impl to bind still, the use impl pkg exp to bind,
                 * the use impl pkg typ var is already bound as dname, and lname and
                 * rname are already bound.
                 *
                 * Just bump by two. Just a term rewrite, no new type variables are
                 * created in this rewrite.
                 *
                 * Need to bind impl and use vars. (Which are bound immediately before
                 * the two sides of the datatype declaration).
                 *)
                (shiftDeBruijinIndicesInExp 3 exp 3))))))
        in
            A.Let(datanameimpl, pkgType, dtval, useExp)
        end
      | _ => e


fun elaborateDatatypes e = A.expMap elaborateDatatype e


fun substTypeInExp srcType dstExp = substTypeInExp' srcType dstExp 0


fun eraseNamesInTyp t =
    let fun erase t =
            (case t of
                 A.TypVar(_, i) => A.TypVar("", i)
               | A.All(_, t') => A.All("", t')
               | A.Some(_, t') => A.Some("", t')
               | A.TyRec(_, t') => A.TyRec("", t')
               | _ => t
            )
    in A.typMap erase t end


fun typeEq (t : A.typ) (t' : A.typ) = ((eraseNamesInTyp t) = (eraseNamesInTyp t'))


fun typeof' ctx typCtx A.Zero = A.Nat
  | typeof' ctx typCtx A.TmUnit = A.Unit
  | typeof' ctx typCtx (A.Var (name, i)) =
    if i < 0 then raise VarWithNegativeDeBruijinIndex(name, i) else get ctx i
  | typeof' ctx typCtx (A.Succ e2) = (typeof' ctx typCtx e2)
  | typeof' ctx typCtx (A.ProdLeft e) = let val A.Prod(l, r) = (typeof' ctx typCtx e) in l end
  | typeof' ctx typCtx (A.ProdRight e) = let val A.Prod(l, r) = (typeof' ctx typCtx e) in r end
  | typeof' ctx typCtx (A.PlusLeft (t, e)) =
    let val A.Plus(l, r) = t in
        if not (typeEq l (typeof' ctx typCtx e)) then
            raise IllTypedMsg "Sum type annotation does not match deduced type"
        else
            A.Plus(l, r)
    end
  | typeof' ctx typCtx (A.PlusRight (t, e)) =
    let val A.Plus(l, r) = t in
        if not (typeEq r (typeof' ctx typCtx e)) then
            raise IllTypedMsg "Sum type annotation does not match deduced type"
        else
            A.Plus(l, r)
    end
  | typeof' ctx typCtx (A.Case (c, lname, l, rname, r)) =
    let val A.Plus(lt, rt) = typeof' ctx typCtx c
        (* both bind a term var *)
        val typeofLeftBranch = typeof' (lt::ctx) typCtx l
        val typeofRightBranch= typeof' (rt::ctx) typCtx r
    in
        if not (typeEq (typeofLeftBranch) (typeofRightBranch)) then
            raise IllTypedMsg "Case statement branches types do not agree"
        else
            typeofLeftBranch
    end
  | typeof' ctx typCtx (A.Fn (argName, argType, funcBody)) =
    if not (istype typCtx argType) then raise IllTypedMsg "Function arg type is not a type."
    else A.Arr (argType, typeof' (argType::ctx) typCtx funcBody)
  | typeof' ctx typCtx (A.Let (varname, vartype, varval, varscope)) =
    if not (istype typCtx vartype) then
        raise IllTypedMsg "Let var type is not a type"
    else if not (typeEq (typeof' ctx typCtx varval) vartype) then
        raise IllTypedMsg "Let var type is not equal to deduced var value type."
    else
        typeof' (vartype::ctx) typCtx varscope
  | typeof' ctx typCtx (A.App (f, n)) =
    let val A.Arr (d, c) = typeof' ctx typCtx f
        val argType = typeof' ctx typCtx n
    in
        if not (typeEq d argType) then raise IllTyped
        else c
    end
  | typeof' ctx typCtx (A.Ifz (i, t, prev, e)) =
    let val Nat = typeof' ctx typCtx i
        val thenType = typeof' ctx typCtx t
        val elseType = typeof' (Nat::ctx) typCtx e
    in
        if not (typeEq thenType elseType) then raise IllTyped
        else thenType
    end
  | typeof' ctx typCtx (A.Pair (l, r)) = A.Prod(typeof' ctx typCtx l, typeof' ctx typCtx r)
  | typeof' ctx typCtx (A.Rec (i, baseCase, prevCaseName, recCase)) =
    let val A.Nat = typeof' ctx typCtx i
        val t = typeof' ctx typCtx baseCase
        val t2 = typeof' (t::ctx) typCtx recCase
    in
        if not (typeEq t t2) then raise IllTyped else t
    end
  | typeof' ctx typCtx (A.Fix (name, typ, e)) = typeof' (typ::ctx) typCtx e
  | typeof' ctx typCtx (A.TypFn (name, e)) = A.All(name, typeof' ctx (NONE::typCtx) e)
  | typeof' ctx typCtx (A.TypApp (appType, e)) =
    if not (istype typCtx appType) then raise IllTyped else
    let val A.All(name, t) = typeof' ctx typCtx e
    in
        substType appType t
    end
  | typeof' ctx typCtx (A.Impl (reprType, pkgImpl, pkgType)) =
    if not (istype [] reprType) then raise IllTyped else
    (* pkgType : [reprType/A.TypVar 0](t') *)
    let val deducedPkgType = typeof' ctx (NONE::typCtx) pkgImpl
        val A.Some(name, pkgType') = pkgType
    in
        if not (typeEq (abstractOutType name reprType deducedPkgType)
                       (abstractOutType name reprType pkgType')) then
            raise IllTyped
        else
            pkgType
    end
  | typeof' ctx typCtx (A.Use (pkg, clientName, typeName, client)) =
    let val A.Some(name, r) = typeof' ctx typCtx pkg
        (* binds BOTH a A.TypVar and a exp A.Var *)
        val clientType = typeof' (r::ctx) (NONE::typCtx) client
        val resType = decrDeBruijinIndices clientType
    in
        if not (istype typCtx resType) then raise IllTyped else
        resType
    end
  | typeof' ctx typCtx (A.Fold(A.TyRec(name, t) (*declared type*), e'(* binds a typ var *))) =
    let val deduced = typeof' ctx (NONE::typCtx) e'
        val absDeduced = A.TyRec(name, abstractOutType name (A.TyRec(name, t)) (deduced))
        val absT = abstractOutType name (A.TyRec(name, t)) (A.TyRec(name, t))
    in
        if not (typeEq absDeduced (A.TyRec(name, t))) then raise IllTyped
        else A.TyRec(name, t)
    end
  | typeof' ctx typCtx (A.Fold(_ , e'(* binds a typ var *))) =
    raise IllTypedMsg "Fold type argument must be a recursive type"
  | typeof' ctx typCtx (A.Unfold(e')) =
    let val A.TyRec(name, t) = typeof' ctx typCtx e' in
        substType (A.TyRec(name, t)) t
    end


fun typeof e = typeof' [] [] e


fun isval e =
    case e of
        A.Zero => true
      | A.TmUnit => true
      | A.Succ(n) => isval n
      | A.Fn(_, _, _) => true
      | A.Let(_, _, _, _) => false
      | A.Pair(l, r) => (isval l) andalso (isval r)
      | A.TypFn (_, _)  => true
      | A.Impl(_, pkgImpl, _) => isval pkgImpl
      | A.PlusLeft(_, e') => isval e'
      | A.PlusRight(_, e') => isval e'
      | A.Fold(t, e') => isval e'
      | _ => false


fun subst' src dst bindingDepth =
    case dst
     of  A.Zero => A.Zero
       | A.TmUnit => A.TmUnit
       | A.Var (name, n)  => if n = bindingDepth then src else
                   if n > bindingDepth then A.Var(name, n-1) else
                   A.Var(name, n)
       | A.Succ e2 => A.Succ (subst' src e2 bindingDepth)
       | A.ProdLeft e => A.ProdLeft (subst' src e bindingDepth)
       | A.ProdRight e => A.ProdRight (subst' src e bindingDepth)
       | A.PlusLeft (t, e) => A.PlusLeft (t, subst' src e bindingDepth)
       | A.PlusRight (t, e) => A.PlusRight (t, subst' src e bindingDepth)
       | A.Case(c, lname, l, rname, r) =>
            A.Case(subst' src c bindingDepth,
                   lname, subst' src l (bindingDepth+1),
                   rname, subst' src r (bindingDepth+1))
       | A.Fn (argName, t, f) => A.Fn(argName, t, (subst' src f (bindingDepth+1)))
       | A.Let (varname, vartype, varval, varscope) =>
            A.Let(varname,
                  vartype,
                  (subst' src varval (bindingDepth)),
                  (subst' src varscope (bindingDepth+1)))
       | A.App (f, n) => A.App((subst' src f bindingDepth), (subst' src n bindingDepth))
       | A.Ifz (i, t, prev, e) => A.Ifz(subst' src i bindingDepth,
                                  subst' src t bindingDepth,
                                  prev,
                                  subst' src e (bindingDepth+1)) (* binds *)
       | A.Rec (i, baseCase, prevCaseName, recCase) =>
            A.Rec(subst' src i bindingDepth,
                subst' src baseCase bindingDepth,
                prevCaseName, subst' src recCase (bindingDepth+1))
       | A.Fix (name, t, e) =>
         A.Fix(name, t, subst' src e (bindingDepth+1)) (* binds *)
       | A.TypFn (name, e) => A.TypFn (name, subst' src e bindingDepth) (* abstracts over types, not exps *)
       | A.TypApp (appType, e) => A.TypApp(appType, subst' src e bindingDepth)
       | A.Impl(reprType, pkgImpl, t) => A.Impl(reprType, subst' src pkgImpl bindingDepth, t)
       | A.Use (pkg, clientName, typeName, client) =>
         A.Use(subst' src pkg bindingDepth,
               clientName,
               typeName,
               subst' src client (bindingDepth+1))
       | A.Pair (l, r) => A.Pair (subst' src l bindingDepth, subst' src r bindingDepth)
       | A.Fold(t, e') => A.Fold(t, (subst' src e' (bindingDepth))) (* binds a typ var *)
       | A.Unfold(e') => A.Unfold(subst' src e' (bindingDepth))


fun subst src dst = subst' src dst 0


fun step e =
    let val _ = typeof' [] [] e in
    if isval e then e else
    case e of
        A.Succ(n) => if not (isval n) then A.Succ(step n) else e
      | A.ProdLeft n  => if not (isval n) then A.ProdLeft(step n) else
                   let val A.Pair(l, r) = n in l end
      | A.ProdRight n  => if not (isval n) then A.ProdRight(step n) else
                    let val A.Pair(l, r) = n in r end
      | A.Pair(l, r) => if not (isval l) then A.Pair(step l, r) else
                       if not (isval r) then A.Pair(l, step r) else
                       e
      | A.App(f, n) => if not (isval f) then A.App(step f, n)
                     else (if not (isval n) then A.App(f, step n)
                           else let val A.Fn(argName, t, f') = f
                           in
                               (* plug `n` into `f'` *)
                               subst n f'
                           end
                          )
      | A.Ifz(i, t, prev, e) =>
            if not (isval i) then A.Ifz(step i, t, prev, e)
            else (case i of
                      A.Zero => t
                    | A.Succ i' => subst i' e
                    | _ => raise IllTypedMsg "ifz conditional must be an integer")
      (* BUG? should this eval varval before subst? should it eval varscope before subst? *)
      | A.Let (varname, vartype, varval, varscope) => subst varval varscope
      | A.Var (name, x) => (if x < 0 then raise VarNotInContext else A.Var (name, x))
      | A.Rec (A.Zero, baseCase, prevCaseName, recCase) => baseCase
      | A.Rec (A.Succ(i), baseCase, prevCaseName, recCase) =>
            (* Doesn't evaluate recursive call if not required. *)
            subst (A.Rec(i, baseCase, prevCaseName, recCase)) recCase
      | A.Rec (i, baseCase, prevCaseName, recCase) =>
            if not (isval i) then
                A.Rec(step i, baseCase, prevCaseName, recCase)
            else raise No
      | A.Fix(name, t, body) => subst e body
      | A.TypFn (name, e') => raise No (* Already isval *)
      | A.TypApp (t, e') =>
            if not (isval e') then (A.TypApp (t, step e'))
            else
                let val A.TypFn(name, e'') = e' in
                    substTypeInExp t e''
                end
      | A.Impl(reprType, pkgImpl, pkgType) =>
            if not (isval pkgImpl) then A.Impl(reprType, step pkgImpl, pkgType) else
            if not (isval e) then raise No else
            e
      | A.Use (pkg, clientName, typeName, client) =>
            if not (isval pkg) then A.Use (step pkg, clientName, typeName, client) else
            (* Note that there's no abstract type at runtime. *)
           (case pkg of
                A.Impl(reprType', pkgImpl', _) =>
                    subst pkgImpl' (substTypeInExp reprType' client)
              | _ => raise No
           )
      | A.PlusLeft (t, e') =>
            if not (isval e) then A.PlusLeft(t, step e')
            else e
      | A.PlusRight (t, e') =>
            if not (isval e) then A.PlusRight(t, step e')
            else e
      | A.Case (c, lname, l, rname, r) =>
        if not (isval c) then A.Case(step c, lname, l, rname, r)
        else (
            case c of
                 A.PlusLeft(_, e) => subst e l
               | A.PlusRight(_, e) => subst e r
               | _ => raise IllTyped
        )
      | A.Fold (t, e') => if not (isval e') then A.Fold(t, step e')
                        else (let val true = isval e in e end)
      | A.Unfold e' => if not (isval e') then A.Unfold (step e')
                     else (let val A.Fold(t, e'') = e' in e'' end)
      | _ => if (isval e) then e else raise No
    end


fun parse s =
    let val ast : A.exp = Parse.parse s
    in
        setDeBruijnIndex ast [] []
    end

fun parseFile filename =
    let val ast : A.exp = Parse.parseFile filename
    in
        setDeBruijnIndex ast [] []
    end

fun newParseFile filename =
    let val ast : A.exp = NewParse.parseFile filename
    in
        setDeBruijnIndex ast [] []
    end

fun findParseErrors filename =
    let val _ = parseFile filename
    in
        ()
    end

fun eval e = if isval e then e else eval (step e)

fun run s =
    let val e' = parse s
        val e = elaborateDatatypes e'
    in
        if isval e then e else eval (step e)
    end

fun runFile s =
    let val e' = parseFile s
        val e = elaborateDatatypes e'
    in
        if isval e then e else eval (step e)
    end

fun newRunFile s =
    let val e' = newParseFile s
        val e = elaborateDatatypes e'
    in
        if isval e then e else eval (step e)
    end

end (* structure Thon *)