-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathholes.sc
More file actions
815 lines (709 loc) · 24.6 KB
/
holes.sc
File metadata and controls
815 lines (709 loc) · 24.6 KB
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Simple typechecker for a dependently typed language with dependent product
// (pi) types and type-in-type (U). NbE is used for congruence checking.
// Holes (`_` terms) can be solved for using basic pattern unification.
//> using scala 3
//> using dep com.lihaoyi::pprint:0.8.1
import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer
final case class Meta(val id: Int, private var _solution: Option[Val]):
final def solution = _solution
final def solve(v: Val) = _solution = Some(v)
class MetaGenerator(private var lastId: Int = 0):
private var allMetas: ArrayBuffer[Meta] = ArrayBuffer.empty
final def apply(): Meta =
val (newLastId, res) = (lastId + 1, new Meta(lastId, None))
allMetas.addOne(res)
lastId = newLastId
res
final def metas(): Vector[Meta] = allMetas.toVector
enum BD:
case Bound, Defined
enum Term:
case TVar(idx: Int)
case TApp(fun: Term, arg: Term)
case TAbs(name: String, body: Term)
case TPi(name: String, ty: Term, body: Term)
case TLet(name: String, ty: Term, is: Term, in: Term)
case TU
case TMeta(meta: Meta)
case TInsertedMeta(meta: Meta, bds: Vector[BD])
final def eval(env: Env = Vector.empty): Val = this match
case TVar(idx) => env(env.length - 1 - idx)
case TApp(fun, arg) => fun.eval(env)(arg.eval(env))
case TAbs(name, body) => Val.VAbs(Closure(name, env, body))
case TPi(name, ty, body) =>
Val.VPi(ty.eval(env), Closure(name, env, body))
case TLet(_, _, is, in) => in.eval(env :+ is.eval(env))
case TU => Val.VU
case TMeta(meta) => Val.meta(meta)
case TInsertedMeta(meta, bds) => Val.meta(meta).applyBD(env, bds)
final def nf(env: Env = Vector.empty): Term =
this.eval(env).quote(env.length)
type Env = Vector[Val]
final case class Closure(val name: String, val env: Env, val tm: Term):
final def apply(arg: Val): Val =
tm.eval(env :+ arg)
enum Val:
case VAbs(closure: Closure)
case VPi(ty: Val, closure: Closure)
case VU
case VStuck(head: Head, spine: Spine)
final def force(): Val = this match
case VStuck(Head.HMeta(meta), spine) => {
meta.solution.match
case Some(solution) => solution(spine).force()
case None => this
}
case _ => this
final def apply(arg: Val): Val = this match
case VAbs(closure) => closure(arg)
case VStuck(head, spine) => VStuck(head, spine :+ Elim.EApp(arg))
case _ => ???
final def elim(elim: Elim): Val = elim match
case Elim.EApp(arg) => this(arg)
/** Break down the spine after making progress on the head */
@tailrec
final def apply(spine: Spine): Val = (this, spine) match
case (VStuck(head, leftSpine), rightSpine) =>
VStuck(head, leftSpine ++ rightSpine)
case (v, elim +: spine) => this.elim(elim)(spine)
case (v, _) => v
/** Apply all bound variables from the environment */
final def applyBD(env: Env, bds: Vector[BD]): Val =
env.reverseIterator
.zip(bds.reverseIterator)
.filter(_._2 == BD.Bound)
.map(_._1)
.foldLeft(this)(_(_))
final def quote(envSize: Int = 0): Term = this.force() match
case VAbs(closure) =>
Term.TAbs(
closure.name,
closure(Val.bound(envSize)).quote(envSize + 1)
)
case VPi(ty, closure) =>
Term.TPi(
closure.name,
ty.quote(envSize),
closure.apply(Val.bound(envSize)).quote(envSize + 1)
)
case VStuck(head, spine) =>
val quotedHead = head match
case Head.HMeta(meta) => Term.TMeta(meta)
case Head.HNeutral(neutral) => Term.TVar(envSize - 1 - neutral)
spine.foldLeft(quotedHead)((head, elim) =>
elim match
case Elim.EApp(arg) => Term.TApp(head, arg.quote(envSize))
)
case VU => Term.TU
object Val:
final def meta(meta: Meta): Val = VStuck(Head.HMeta(meta), Vector.empty)
final def bound(lvl: Int): Val = VStuck(Head.HNeutral(lvl), Vector.empty)
enum Head:
case HNeutral(lvl: Int)
case HMeta(meta: Meta)
type Spine = Vector[Elim]
enum Elim:
case EApp(arg: Val)
class TermBuilder(private val builder: (Int, MetaGenerator) => Term):
final def build(): Term = builder(0, MetaGenerator())
final def apply(args: TermBuilder*): TermBuilder =
TermBuilder { (lvl: Int, metaGen: MetaGenerator) =>
args.foldLeft(this.builder(lvl, metaGen))((fun, arg) =>
Term.TApp(fun, arg.builder(lvl, metaGen))
)
}
final def ->:(arg: TermBuilder): TermBuilder =
TermBuilder.pi("_", arg)(_ => this)
final def eval(): Term = this.build().nf()
object TermBuilder:
private final def level(level: Int) =
TermBuilder((varLvl: Int, _) => Term.TVar(varLvl - 1 - level))
final def U: TermBuilder = TermBuilder((_, _) => Term.TU)
final def hole: TermBuilder =
TermBuilder((_, metaGen: MetaGenerator) => Term.TMeta(metaGen()))
final def pi(name: String, ty: TermBuilder)(
body: TermBuilder => TermBuilder
): TermBuilder =
TermBuilder((bindLvl: Int, metaGen: MetaGenerator) =>
Term.TPi(
name,
ty.builder(bindLvl, metaGen),
body(level(bindLvl)).builder(bindLvl + 1, metaGen)
)
)
final def let(name: String, ty: TermBuilder, is: TermBuilder)(
in: TermBuilder => TermBuilder
): TermBuilder =
TermBuilder((bindLvl, metaGen) =>
Term.TLet(
name,
ty.builder(bindLvl, metaGen),
is.builder(bindLvl, metaGen),
in(level(bindLvl)).builder(bindLvl + 1, metaGen)
)
)
final def let(name: String, is: TermBuilder)(
in: TermBuilder => TermBuilder
): TermBuilder = let(name, hole, is)(in)
final def lam(name: String)(body: TermBuilder => TermBuilder): TermBuilder =
TermBuilder((bindLvl: Int, metaGen: MetaGenerator) =>
Term.TAbs(
name,
body(level(bindLvl)).builder(bindLvl + 1, metaGen)
)
)
final def lam(name1: String, name2: String)(
body: (TermBuilder, TermBuilder) => TermBuilder
): TermBuilder =
lam(name1)(x => lam(name2)(y => body(x, y)))
final def lam(name1: String, name2: String, name3: String)(
body: (TermBuilder, TermBuilder, TermBuilder) => TermBuilder
): TermBuilder =
lam(name1)(x => lam(name2)(y => lam(name3)(z => body(x, y, z))))
final def lam(name1: String, name2: String, name3: String, name4: String)(
body: (TermBuilder, TermBuilder, TermBuilder, TermBuilder) => TermBuilder
): TermBuilder =
lam(name1)(p =>
lam(name2)(q => lam(name3)(r => lam(name4)(s => body(p, q, r, s))))
)
object EvalExamples:
import TermBuilder._
final def nat(x: Int): TermBuilder =
TermBuilder.lam("s")(s =>
TermBuilder.lam("z")(z => 1.to(x).foldLeft(z)((t, _) => s(t)))
)
final def zero: TermBuilder = lam("s", "z")((_, z) => z)
final def inc: TermBuilder = lam("a", "s", "z")((a, s, z) => s(a(s, z)))
final def add: TermBuilder =
lam("a", "b", "s", "z")((a, b, s, z) => a(s, b(s, z)))
final def mul: TermBuilder =
lam("a", "b", "s", "z")((a, b, s, z) => a(b(s), z))
final def pair: TermBuilder = lam("a", "b", "s")((a, b, s) => s(a, b))
final def tt: TermBuilder = lam("a", "_")((a, _) => a)
final def ff: TermBuilder = lam("_", "b")((_, b) => b)
final def fst: TermBuilder = lam("p")(_(tt))
final def snd: TermBuilder = lam("p")(_(ff))
final def pred: TermBuilder =
lam("n")(n =>
fst(n(lam("p")(p => pair(snd(p), inc(snd(p)))), pair(zero, zero)))
)
final def sub: TermBuilder =
lam("a", "b")((a, b) => b(pred, a))
/* Church-encoded arithmetic expressions in lambda calculus */
final def mkOp(
op: ((TermBuilder, TermBuilder, TermBuilder)) => TermBuilder
): TermBuilder =
lam("e1", "e2")((e1, e2) =>
lam("fAdd", "fSub", "fMul", "fN")((fAdd, fSub, fMul, fN) =>
op((fAdd, fSub, fMul))(
e1(fAdd, fSub, fMul, fN),
e2(fAdd, fSub, fMul, fN)
)
)
)
final def mkAdd = mkOp(_._1)
final def mkSub = mkOp(_._2)
final def mkMul = mkOp(_._3)
final def liftN =
lam("n")(n => lam("_", "_", "_", "fN")((_, _, _, fN) => fN(n)))
final class TypecheckingFailedException() extends Exception
def unify(v1: Val, v2: Val, envSize: Int): Unit =
/* Simple untyped pattern unification */
def solve(meta: Meta, sp: Spine, rhs: Val, envSize: Int): Unit =
case class InvertedSpine(
val srcEnvSize: Int,
val targetEnvSize: Int = 0,
val map: Map[Int, Int] = Map.empty,
val nonlinear: Set[Int] = Set.empty
):
def extend(srcLvl: Int): InvertedSpine =
if (nonlinear.contains(srcLvl) || map.contains(srcLvl))
InvertedSpine(
srcEnvSize,
targetEnvSize + 1,
map - srcLvl,
nonlinear + srcLvl
)
else
InvertedSpine(
srcEnvSize,
targetEnvSize + 1,
map + (srcLvl -> targetEnvSize),
nonlinear
)
def lift(): InvertedSpine = InvertedSpine(
srcEnvSize + 1,
targetEnvSize + 1,
map + (srcEnvSize -> targetEnvSize),
nonlinear
)
def getVarAtlvl(srcLvl: Int): Term.TVar = Term.TVar(
targetEnvSize - 1 - map.getOrElse(
srcLvl,
throw TypecheckingFailedException()
)
)
/* Returns a mapping with de-brujin level equivalents in empty context. */
/* Maps to None if there were multiple occurances of the variable in the spine. */
@tailrec
def invertSpine(
sp: Spine,
acc: InvertedSpine
): InvertedSpine =
sp match
case (Elim.EApp(v) +: sp) =>
v.force() match
case Val.VStuck(Head.HNeutral(n), IndexedSeq()) =>
invertSpine(sp, acc.extend(n))
case _ => throw TypecheckingFailedException()
case IndexedSeq() => acc
case _ => throw TypecheckingFailedException()
def rename(
v: Val,
meta: Meta,
invertedSpine: InvertedSpine
): Term = v.force() match
case Val.VAbs(closure) =>
Term.TAbs(
closure.name,
rename(
closure(Val.bound(envSize)),
meta,
invertedSpine.lift()
)
)
case Val.VPi(ty, closure) =>
Term.TPi(
closure.name,
rename(ty, meta, invertedSpine),
rename(
closure(Val.bound(envSize)),
meta,
invertedSpine.lift()
)
)
case Val.VU => Term.TU
case Val.VStuck(hd, sp) =>
sp.foldLeft(
hd match
case Head.HNeutral(lvl) =>
invertedSpine.getVarAtlvl(lvl)
case Head.HMeta(innerMeta) =>
if innerMeta.id == meta.id then
throw TypecheckingFailedException()
else Term.TMeta(meta)
)((head, elim) =>
elim match
case Elim.EApp(arg) =>
Term.TApp(head, rename(arg, meta, invertedSpine))
)
meta.solve(
1.to(sp.length)
.reverseIterator
.foldLeft(
rename(
rhs,
meta,
invertSpine(sp, InvertedSpine(srcEnvSize = envSize))
)
)((r, n) => Term.TAbs(s"t$n", r))
.eval() // Doesn't actually reduce anything
)
@tailrec
def unifySpines(sp1: Spine, sp2: Spine, envSize: Int): Unit =
(sp1, sp2) match
case (Elim.EApp(v1) +: sp1, Elim.EApp(v2) +: sp2) =>
unify(v1, v2, envSize); unifySpines(sp1, sp2, envSize)
case (IndexedSeq(), IndexedSeq()) => ()
case _ => throw TypecheckingFailedException()
def unify(v1: Val, v2: Val, envSize: Int): Unit =
(v1.force(), v2.force()) match
case (Val.VAbs(c1), Val.VAbs(c2)) =>
unify(c1(Val.bound(envSize)), c2(Val.bound(envSize)), envSize + 1)
/* Dealing with eta-equality */
case (Val.VAbs(c1), t2) =>
unify(c1(Val.bound(envSize)), t2(Val.bound(envSize)), envSize + 1)
case (t1, Val.VAbs(c2)) =>
unify(t1(Val.bound(envSize)), c2(Val.bound(envSize)), envSize + 1)
case (Val.VU, Val.VU) => ()
case (Val.VPi(argTy1, bodyTyClosure1), Val.VPi(argTy2, bodyTyClosure2)) =>
unify(argTy1, argTy2, envSize)
unify(
bodyTyClosure1(Val.bound(envSize)),
bodyTyClosure2(Val.bound(envSize)),
envSize + 1
)
case (Val.VStuck(hd1, sp1), Val.VStuck(hd2, sp2)) if hd1 == hd2 =>
unifySpines(sp1, sp2, envSize)
case (Val.VStuck(Head.HMeta(m1), sp1), rhs) =>
solve(m1, sp1, rhs, envSize)
case (lhs, Val.VStuck(Head.HMeta(m2), sp2)) =>
solve(m2, sp2, lhs, envSize)
case _ =>
throw TypecheckingFailedException()
unify(v1, v2, envSize)
enum Raw:
case RVar(name: String)
case RApp(fun: Raw, arg: Raw)
case RAbs(name: String, body: Raw)
case RPi(name: String, ty: Raw, body: Raw)
case RLet(name: String, ty: Raw, is: Raw, in: Raw)
case RU
case RHole
def apply(args: Raw*): Raw =
args.foldLeft(this)(RApp(_, _))
def ->:(lhs: Raw): Raw =
RPi("_", lhs, this)
object Raw:
def lam(name: String)(f: Raw => Raw): Raw =
RAbs(name, f(RVar(name)))
def lam(name1: String, name2: String)(f: (Raw, Raw) => Raw): Raw =
RAbs(name1, RAbs(name2, f(RVar(name1), RVar(name2))))
def lam(name1: String, name2: String, name3: String)(
f: (Raw, Raw, Raw) => Raw
): Raw =
RAbs(
name1,
RAbs(name2, RAbs(name3, f(RVar(name1), RVar(name2), RVar(name3))))
)
def lam(name1: String, name2: String, name3: String, name4: String)(
f: (Raw, Raw, Raw, Raw) => Raw
): Raw =
RAbs(
name1,
RAbs(
name2,
RAbs(
name3,
RAbs(name4, f(RVar(name1), RVar(name2), RVar(name3), RVar(name4)))
)
)
)
def pi(name: String, ty: Raw)(f: Raw => Raw): Raw =
RPi(name, ty, f(RVar(name)))
def pi(name1: String, name2: String, ty: Raw)(f: (Raw, Raw) => Raw): Raw =
RPi(name1, ty, RPi(name2, ty, f(RVar(name1), RVar(name2))))
def lets(defs: (String, Raw, Raw)*)(t: Raw): Raw =
defs.foldRight(t)((let, in) => RLet(let._1, let._2, let._3, in))
def let(name: String, ty: Raw, is: Raw)(f: Raw => Raw): Raw =
RLet(name, ty, is, f(RVar(name)))
def hole = RHole
def U = RU
trait TypecheckTrace:
def checkBegin(term: Raw, ty: Val, envSize: Int): Unit = ()
def inferBegin(term: Raw): Unit = ()
def unifyBegin(val1: Val, val2: Val, envSize: Int): Unit = ()
def checkEnd(res: Term): Unit = ()
def inferEnd(res: Term, ty: Val, envSize: Int): Unit = ()
def unifyEnd(): Unit = ()
final def wrapCheck(term: Raw, ty: Val, envSize: Int)(ret: => (Term)): Term =
checkBegin(term, ty, envSize)
val res = ret
checkEnd(res)
res
final def wrapInfer(term: Raw, envSize: Int)(
ret: => (Term, Val)
): (Term, Val) =
inferBegin(term)
val (res, ty) = ret
inferEnd(res, ty, envSize)
(res, ty)
final def doUnify(val1: Val, val2: Val, envSize: Int): Unit =
unifyBegin(val1, val2, envSize)
unify(val1, val2, envSize)
unifyEnd()
class NoOpTypecheckTrace() extends TypecheckTrace
class VerboseTypecheckTrace(
stream: java.io.PrintStream = System.err,
private var indentLevel: Int = 0
) extends TypecheckTrace:
def printPrefix(): Unit =
stream.print(" " * indentLevel)
override def checkBegin(term: Raw, ty: Val, envSize: Int): Unit =
printPrefix()
stream.println(s"check: $term |- ${ty.quote(envSize)}? {")
indentLevel += 1
override def inferBegin(term: Raw): Unit =
printPrefix()
stream.println(s"infer: $term? {")
indentLevel += 1
override def unifyBegin(val1: Val, val2: Val, envSize: Int): Unit =
printPrefix()
stream.print(s"unify: ${val1.quote(envSize)} == ${val2.quote(envSize)}? ")
override def checkEnd(res: Term): Unit =
indentLevel -= 1
printPrefix()
stream.println(s"} yes, elaborated to $res")
override def inferEnd(res: Term, ty: Val, envSize: Int): Unit =
indentLevel -= 1
printPrefix()
stream.println(s"} elaborated to $res, type ${ty.quote(envSize)}")
override def unifyEnd(): Unit =
stream.println(" yes!")
class Context(
val env: Env = Vector.empty,
val bds: Vector[BD] = Vector.empty,
val tys: Vector[Val] = Vector.empty,
val names: Vector[String] = Vector.empty,
val nameToLvl: Map[String, Int] = Map.empty,
val trace: TypecheckTrace = NoOpTypecheckTrace(),
val metaGen: MetaGenerator = MetaGenerator()
):
def metas(): Vector[Meta] = metaGen.metas()
def extend(name: String, ty: Val, bd: BD, v: Val): Context =
Context(
env :+ v,
bds :+ bd,
tys :+ ty,
names :+ name,
nameToLvl + (name -> env.length),
trace,
metaGen
)
def bindAtEnd(name: String, ty: Val): Context =
extend(name, ty, BD.Bound, newAtEnd)
def defineAtEnd(name: String, ty: Val, v: Val): Context =
extend(name, ty, BD.Defined, v)
def envSize = env.length
def newAtEnd = Val.bound(envSize)
def getLvl(name: String) =
nameToLvl.get(name).getOrElse(throw TypecheckingFailedException())
def inferVar(name: String): (Term, Val) =
val lvl = getLvl(name)
(Term.TVar(envSize - 1 - lvl), tys(lvl))
def insertedMeta(): Term.TInsertedMeta =
Term.TInsertedMeta(metaGen(), bds)
def infer(term: Raw): (Term, Val) = trace.wrapInfer(term, envSize)(term match
case Raw.RVar(name) => inferVar(name)
case Raw.RApp(fun, arg) =>
val (elaboratedFun, funTy) = infer(fun)
// LHS needs to be a Pi type. We are interested in its components -
// type of the argument and closure for the return type
val (argTy, bodyTyClosure) = funTy.force() match
case Val.VPi(argTy, bodyTyClosure) =>
(argTy, bodyTyClosure)
case Val.VU | Val.VAbs(_) => throw TypecheckingFailedException()
case Val.VStuck(hd, sp) =>
// This might potentially resolve to a Pi type later, so we need
// to create two metas for two components of the Pi type, hoping
// that we don't make a mistake
val argTy = insertedMeta().eval(env)
(argTy, Closure("x", env, bindAtEnd("x", argTy).insertedMeta()))
val elaboratedArg = check(arg, argTy)
(
Term.TApp(elaboratedFun, elaboratedArg),
bodyTyClosure(elaboratedArg.eval(env))
)
case Raw.RAbs(name, body) =>
val argTy = insertedMeta().eval(env)
val (elaboratedBody, bodyTy) =
bindAtEnd(name, argTy).infer(body)
(
Term.TAbs(name, elaboratedBody),
Val.VPi(argTy, Closure(name, env, bodyTy.quote(envSize + 1)))
)
case Raw.RPi(name, argTy, retTy) =>
val elaboratedArgTy = check(argTy, Val.VU)
val elaboratedRetTy =
bindAtEnd(name, elaboratedArgTy.eval(env)).check(retTy, Val.VU)
(Term.TPi(name, elaboratedArgTy, elaboratedRetTy), Val.VU)
case Raw.RLet(name, defTy, is, in) =>
val elaboratedTy = check(defTy, Val.VU)
val evaluatedTy = elaboratedTy.eval(env)
val elaboratedIs = check(is, evaluatedTy)
val evaluatedIs = elaboratedIs.eval(env)
val (elaboratedIn, inTy) =
defineAtEnd(name, evaluatedTy, evaluatedIs).infer(in)
(Term.TLet(name, elaboratedTy, elaboratedIs, elaboratedIn), inTy)
case Raw.RU => (Term.TU, Val.VU)
case Raw.RHole =>
val tMeta = insertedMeta()
(tMeta, Val.meta(tMeta.meta))
)
def check(term: Raw, ty: Val): Term =
trace.wrapCheck(term, ty, envSize)((term, ty.force()) match
case (Raw.RAbs(name, body), Val.VPi(argTy, closure)) =>
val bodyTy = closure(newAtEnd)
Term.TAbs(
name,
bindAtEnd(name, argTy).check(body, bodyTy)
)
case (Raw.RLet(name, defTy, is, in), ty) =>
val elaboratedTy = check(defTy, Val.VU)
val evaluatedTy = elaboratedTy.eval(env)
val elaboratedIs = check(is, evaluatedTy)
val evaluatedIs = elaboratedIs.eval(env)
val elaboratedIn =
defineAtEnd(name, evaluatedTy, evaluatedIs).check(in, ty)
Term.TLet(name, elaboratedTy, elaboratedIs, elaboratedIn)
case (Raw.RHole, ty) => insertedMeta()
case _ =>
val (elaborated, actualTy) = infer(term)
trace.doUnify(actualTy, ty, envSize)
elaborated
)
def elab(term: Raw): (Term, Term) =
val (elaboratedTerm, termTy) = infer(term)
(elaboratedTerm, termTy.quote(0))
object TypecheckExamples:
import Raw._
/* Examples from https://github.com/AndrasKovacs/elaboration-zoo/blob/master/02-typecheck-closures-debruijn/Main.hs */
def church(n: Int) = RAbs(
"N",
RAbs(
"s",
RAbs("z", 1.to(n).foldLeft(RVar("z"))((arg, _) => RApp(RVar("s"), arg)))
)
)
def ex0 =
let("id", pi("A", U)(A => A ->: A), lam("A", "x")((A, x) => x)) { id =>
let("foo", U, U)(foo => let("bar", U, id(id))(bar => id))
}
def ex1 =
let("id", pi("A", U)(A => A ->: A), lam("A", "x")((A, x) => x)) { id =>
let(
"const",
pi("A", "B", U)((A, B) => A ->: (B ->: A)),
lam("A", "B", "x", "y")((A, B, x, y) => x)
) { const =>
id(pi("A", "B", U)((A, B) => A ->: (B ->: A)), const)
}
}
def ex2 =
let("Nat", U, pi("N", U)(N => (N ->: N) ->: N ->: N)) { Nat =>
let("five", Nat, church(5)) { five =>
let(
"add",
Nat ->: Nat ->: Nat,
lam("a", "b")((a, b) =>
lam("N", "s", "z")((N, s, z) => a(N, s, b(N, s, z)))
)
) { add =>
let(
"mul",
Nat ->: Nat ->: Nat,
lam("a", "b")((a, b) =>
lam("N", "s", "z")((N, s, z) => a(N, b(N, s), z))
)
) { mul =>
let("ten", Nat, add(five, five)) { ten =>
let("hundred", Nat, mul(ten, ten)) { hundred =>
let("thousand", Nat, mul(ten, hundred)) { thousand => thousand }
}
}
}
}
}
}
/* https://github.com/AndrasKovacs/elaboration-zoo/blob/master/03-holes/example.txt */
def ex03Holes = lets(
("id", pi("A", hole)(A => A ->: A), lam("A", "x")((A, x) => x)),
(
"List",
U ->: U,
lam("A")(A => pi("L", hole)(L => (A ->: L ->: L) ->: L ->: L))
),
(
"nil",
pi("A", hole)(A => RVar("List")(A)),
lam("A", "L", "cons", "nil")((A, L, cons, nil) => nil)
),
(
"cons",
pi("A", hole)(A => A ->: RVar("List")(A) ->: RVar("List")(A)),
lam("A", "x", "xs")((A, x, xs) =>
lam("L", "cons", "nil")((L, cons, nil) => cons(x, xs(hole, cons, nil)))
)
),
(
"Bool",
U,
pi("B", hole)(B => B ->: B ->: B)
),
(
"true",
RVar("Bool"),
lam("B", "t", "f")((B, t, f) => t)
),
(
"false",
RVar("Bool"),
lam("B", "t", "f")((B, t, f) => f)
),
(
"not",
RVar("Bool") ->: RVar("Bool"),
lam("b", "B", "t", "f")((b, B, t, f) => b(B, f, t))
),
(
"list1",
RVar("List")(RVar("Bool")),
RVar("cons")(
hole,
RVar("id")(hole, RVar("true")),
RVar("nil")(hole)
)
),
(
"Eq",
pi("A", hole)(A => A ->: A ->: U),
lam("A", "x", "y")((A, x, y) => pi("P", A ->: U)(P => P(x) ->: P(y)))
),
(
"refl",
pi("A", hole)(A => pi("x", A)(x => RVar("Eq")(A, x, x))),
lam("A", "x", "P", "px")((A, x, P, px) => px)
),
(
"list1",
RVar("List")(RVar("Bool")),
RVar("cons")(
hole,
RVar("true"),
RVar("cons")(hole, RVar("false"), RVar("nil")(hole))
)
),
("Nat", U, pi("N", U)(N => (N ->: N) ->: N ->: N)),
("five", RVar("Nat"), church(5)),
(
"add",
RVar("Nat") ->: RVar("Nat") ->: RVar("Nat"),
lam("a", "b")((a, b) =>
lam("N", "s", "z")((N, s, z) => a(N, s, b(N, s, z)))
)
),
(
"mul",
RVar("Nat") ->: RVar("Nat") ->: RVar("Nat"),
lam("a", "b")((a, b) => lam("N", "s", "z")((N, s, z) => a(N, b(N, s), z)))
),
("ten", RVar("Nat"), RVar("add")(RVar("five"), RVar("five"))),
("hundred", RVar("Nat"), RVar("mul")(RVar("ten"), RVar("ten"))),
("thousand", RVar("Nat"), RVar("mul")(RVar("ten"), RVar("hundred"))),
(
"eqTest",
RVar("Eq")(hole, RVar("hundred"), RVar("hundred")),
RVar("refl")(hole, hole)
)
)(U)
val example: Raw =
if args.length == 0 then TypecheckExamples.ex1
else
args(0) match
case "ex0" => TypecheckExamples.ex0
case "ex1" => TypecheckExamples.ex1
case "ex2" => TypecheckExamples.ex2
case "holes" => TypecheckExamples.ex03Holes
case _ =>
println("usage: PROG [ex0|ex1|ex2|ex3]")
System.exit(1)
???
println("\nSource:")
pprint.pprintln(example, width = 120, height = 1000000)
println("\nElaborated:")
pprint.pprintln(
Context().elab(example),
width = 120,
height = 1000000
)