-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtt.ml
1370 lines (1228 loc) · 45.8 KB
/
xtt.ml
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Aaaaaaa we're tryin' XTT *)
type name = string
type idx = int and lvl = int
module AST = struct
type ty = exp
and dim = exp
and exp =
| Var of name
| Let of name * exp * exp
| Annot of exp * ty
(* coe r r' i.A a *)
| Coe of { r: dim; r': dim; i: name; ty: ty; a: exp }
(* com<s> r r' i.A i.lhs i.rhs a *)
| Com of { s: dim; r: dim; r': dim
; ty: name * ty
; lhs: name * exp; rhs: name * exp; a: exp }
(* TODO: hcom *)
| Zero | One
(* Set and weird injectivity stuff *)
| Set (* Set : Set lol *)
| DomTy of exp
| CodTy of exp
| PathPTyLhs of exp
| PathPTyRhs of exp
| PathPTyPath of exp
| PathPLhs of exp
| PathPRhs of exp
(* Functions *)
| Pi of name * ty * ty
(* pathp i.A lhs rhs *)
| PathP of name * ty * exp * exp
(* lhs ≡ rhs *)
| Eq of exp * exp
| Lam of name * exp
| AnnLam of name * ty * exp
| App of exp * exp
(* Pairs *)
| Sigma of name * ty * ty
| Pair of exp * exp
| Fst of exp
| Snd of exp
| FstTy of exp
| SndTy of exp
end
module Core = struct
type 'a binds = 'a
type dim = Zero | DimVar of idx | One
type ty = tm
and tm =
| Var of idx
| Let of name * tm * tm binds
| Coe of dim * dim * name * ty binds * tm
| Com of { s: dim; r: dim; r': dim
; i: name; ty: ty binds
; lhs: tm binds; rhs: tm binds }
| HCom of { s: dim; r': dim; i: name; lhs: tm binds; rhs: tm binds }
| Abort
(* Set *)
| Set
| DomTy of tm
| CodTy of tm
| PathPTyLhs of tm
| PathPTyRhs of tm
| PathPTyPath of tm
| PathPLhs of tm
| PathPRhs of tm
(* Functions *)
| Pi of name * ty * ty binds
| Lam of name * tm binds
| App of tm * tm
(* Paths *)
| PathP of name * ty binds * tm * tm
| DimAbs of name * tm binds
| DimApp of tm * dim
(* Pairs *)
| Sigma of name * ty * ty binds
| Pair of tm * tm
| Fst of tm
| Snd of tm
end
module Domain = struct
type 'n dim = Zero | DimVar of (* n *) idx | One
module TypeNats : sig
type z
type 'n s
end = struct type z = unit and 'n s = unit end
include TypeNats
type 'n cond =
| DimEq of 'n dim * 'n dim
| TyEq of 'n dl * 'n dl
| Forall_i of 'n s cond
and 'n dl = 'n d lazy_t
and 'n d =
| DNe of 'n dne
| DSplit of 'n dim * 'n dl * 'n dl
| DIf of { cond: 'n cond; yes: 'n dl; no: 'n dl } (* unstable elements *)
| DSet
| DPi of 'n dl * 'n clos
| DLam of 'n clos
| DPathP of name * 'n s dl * 'n dl * 'n dl
| DDimAbs of name * 'n s dl
| DSigma of 'n dl * 'n clos
| DPair of 'n dl * 'n dl
and 'n dne =
| DVar of lvl * 'n dl
| DApp of 'n dne * 'n dl
| DCoe of { r: 'n dim; r': 'n dim
(* invariant: ty0 ≠ ty1 (or else it would compute) *)
; ty0: 'n dl; ty1: 'n dl
; a: 'n dl }
| DFst of 'n dne
| DSnd of 'n dne
and 'n env_item =
| Dim of 'n dim
| Val of 'n dl
and 'n env = 'n env_item list
and 'n clos = { name: name; env: 'n env; body: Core.tm }
module Sub : sig
type ('n, 'm) sub
val id : ('n, 'n) sub
val compose : ('m, 'l) sub -> ('n, 'm) sub -> ('n, 'l) sub
val shift_up : ('n, 'n s) sub
val app : 'n dim -> ('n s, 'n) sub
val extend : ('n, 'm) sub -> ('n s, 'm s) sub
val dim : ('n, 'm) sub -> 'n dim -> 'm dim
val d : ('n, 'm) sub -> 'n d -> 'm d
val dl : ('n, 'm) sub -> 'n dl -> 'm dl
val dne : ('n, 'm) sub -> 'n dne -> 'm dne
val cond : ('n, 'm) sub -> 'n cond -> 'm cond
val clos : ('n, 'm) sub -> 'n clos -> 'm clos
val env : ('n, 'm) sub -> 'n env -> 'm env
val env_item : ('n, 'm) sub -> 'n env_item -> 'm env_item
end = struct
type ('n, 'm) sub = idx -> 'm dim
let dim s = function
| Zero -> Zero
| One -> One
| DimVar i -> s i
let id x = DimVar x
let compose s s' i = dim s (s' i)
let shift_up i = DimVar (i+1)
let app x i = if i = 0 then x else DimVar(i-1)
let extend s i =
if i = 0 then DimVar 0 else dim shift_up (s (i-1))
let rec d : 'n 'm. ('n, 'm) sub -> 'n d -> 'm d
= fun s tm -> match tm with
| DNe ne -> DNe (dne s ne)
| DIf { cond = c; yes; no } ->
DIf { cond = cond s c; yes = dl s yes; no = dl s no }
| DSplit(i, l, r) ->
DSplit(dim s i, dl s l, dl s r)
| DSet -> DSet
| DPi(a, b) ->
DPi(dl s a, clos s b)
| DLam f ->
DLam (clos s f)
| DPathP(i, ty, lhs, rhs) ->
DPathP(i, dl (extend s) ty, dl s lhs, dl s rhs)
| DDimAbs(i, body) ->
DDimAbs(i, dl (extend s) body)
| DSigma(a, b) ->
DSigma(dl s a, clos s b)
| DPair(x, y) ->
DPair(dl s x, dl s y)
and dl : 'n 'm. ('n, 'm) sub -> 'n dl -> 'm dl
= fun s dl -> Lazy.map (d s) dl
and dne : 'n 'm. ('n, 'm) sub -> 'n dne -> 'm dne
= fun s ne -> match ne with
| DVar(lvl, ty) -> DVar(lvl, dl s ty)
| DApp(f, x) -> DApp(dne s f, dl s x)
| DCoe { r; r'; ty0; ty1; a } ->
DCoe { r = dim s r; r' = dim s r'
; ty0 = dl s ty0; ty1 = dl s ty1
; a = dl s a }
| DFst x -> DFst (dne s x)
| DSnd x -> DSnd (dne s x)
and cond : 'n 'm. ('n, 'm) sub -> 'n cond -> 'm cond
= fun s c -> match c with
| DimEq(i, j) -> DimEq(dim s i, dim s j)
| TyEq(a, b) -> TyEq(dl s a, dl s b)
| Forall_i c -> Forall_i (cond (extend s) c)
and env_item : 'n 'm. ('n, 'm) sub -> 'n env_item -> 'm env_item
= fun s i -> match i with
| Dim d -> Dim (dim s d)
| Val v -> Val (dl s v)
and env : 'n 'm. ('n, 'm) sub -> 'n env -> 'm env
= fun s env -> List.map (env_item s) env
and clos : 'n 'm. ('n, 'm) sub -> 'n clos -> 'm clos
= fun s c -> { name = c.name; env = env s c.env; body = c.body }
end
type ('n, 'm) sub = ('n, 'm) Sub.sub
end
module Eval : sig
open Domain
val lam_to_clos : 'n dl -> 'n clos
val ($) : 'n clos -> 'n dl -> 'n d
val type_of_ne : 'n dne -> 'n d
val app : 'n dl -> 'n dl -> 'n d
val dim_app : 'n dl -> 'n dim -> 'n d
val fst : 'n dl -> 'n d
val snd : 'n dl -> 'n d
val un_dim_abs : 'n dl -> 'n s d
val dom_ty : 'n d -> 'n d
val cod_ty : 'n d -> 'n d
val pathp_ty_lhs : 'n d -> 'n d
val pathp_ty_rhs : 'n d -> 'n d
val pathp_ty_path : 'n d -> 'n d
val pathp_lhs : 'n d -> 'n d
val pathp_rhs : 'n d -> 'n d
val fst_ty : 'n d -> 'n d
val snd_ty : 'n d -> 'n d
val eval : 'n env -> Core.tm -> 'n d
val eval_dim : 'n env -> Core.dim -> 'n dim
end = struct
open Domain
let bind_with_sub : 'n 'm. ('n, 'm) sub -> 'n d -> ('n d -> 'm d) -> 'm d
= fun s x f ->
let rec go = function
| DIf { cond; yes; no } ->
DIf { cond = Sub.cond s cond
; yes = Lazy.map go yes
; no = Lazy.map go no }
| DSplit(dim, l, r) ->
DSplit(Sub.dim s dim, Lazy.map go l, Lazy.map go r)
| x -> f x in
go x
let bind x f = bind_with_sub Sub.id x f
let lam_to_clos lam =
{ name = "x"
; env = [Val lam]
; body = App(Var 1, Var 0) }
(* also use internal injectivity to merge some things that are the same, so
coe can match on it *)
let rec bind_shift_down : 'n. 'n s d -> ('n s d -> 'n d) -> 'n d
= fun x f ->
let rec go = function
| DIf { cond; yes; no } ->
DIf { cond = Forall_i cond
; yes = Lazy.map go yes
; no = Lazy.map go no }
| DSplit(Zero, l, _) -> go (Lazy.force l) (* lol *)
| DSplit(One, _, r) -> go (Lazy.force r) (* lol *)
| DSplit(DimVar 0, l, r) ->
bind (Sub.d (Sub.app Zero) (Lazy.force l)) (fun l ->
bind (Sub.d (Sub.app Zero) (Lazy.force r)) (fun r ->
let tm =
DSplit(DimVar 0
, lazy (Sub.d Sub.shift_up l)
, lazy (Sub.d Sub.shift_up r)) in
match l, r with
| DSet, DSet -> f DSet
| DPi _, DPi _ ->
let dom = lazy (dom_ty tm) in
let cod = lazy (cod_ty tm) in
f (DPi(dom, lam_to_clos cod))
| DPathP(i, _, _, _), DPathP _ ->
let a = lazy (un_dim_abs (lazy (pathp_ty_path tm))) in
let lhs = lazy (pathp_lhs tm) in
let rhs = lazy (pathp_rhs tm) in
f (DPathP(i, a, lhs, rhs))
| DSigma _, DSigma _ ->
let fst = lazy (fst_ty tm) in
let snd = lazy (snd_ty tm) in
f (DSigma(fst, lam_to_clos snd))
| _ -> f tm))
| DSplit(DimVar n, l, r) ->
DSplit(DimVar (n-1), Lazy.map go l, Lazy.map go r)
| x -> f x in
go x
and ($) : 'n. 'n clos -> 'n dl -> 'n d
= fun clos x -> eval (Val x::clos.env) clos.body
and type_of_ne : 'n. 'n dne -> 'n d = function
| DVar(_, ty) -> Lazy.force ty
| DApp(f, x) -> begin
match type_of_ne f with
| DPi(_, b) -> b $ x
| _ -> failwith "internal type error"
end
| DCoe { r'; ty0; ty1; _ } ->
DSplit(r', ty0, ty1)
| DFst ne -> begin
match type_of_ne ne with
| DSigma(a, _) -> Lazy.force a
| _ -> failwith "internal type error"
end
| DSnd ne -> begin
match type_of_ne ne with
| DSigma(_, b) -> b $ lazy (DNe (DFst ne))
| _ -> failwith "internal type error"
end
and app : 'n. 'n dl -> 'n dl -> 'n d
= fun f x -> bind (Lazy.force f) (function
| DLam f -> f $ x
| DNe f -> DNe (DApp(f, x))
| _ -> failwith "internal type error")
and dim_app : 'n. 'n dl -> 'n dim -> 'n d
= fun f i -> bind (Lazy.force f) (function
| DDimAbs(_, x) -> Sub.d (Sub.app i) (Lazy.force x)
| DNe ne -> begin
match type_of_ne ne with
| DPathP(_, _, l, r) ->
DSplit(i, l, r)
| _ -> failwith "internal type error"
end
| _ -> failwith "internal type error")
and un_dim_abs : 'n. 'n dl -> 'n s d
= fun x -> match Lazy.force x with
| DIf { cond; yes; no } ->
DIf { cond = Sub.cond Sub.shift_up cond
; yes = lazy (un_dim_abs yes)
; no = lazy (un_dim_abs no) }
| DSplit(dim, l, r) ->
DSplit(Sub.dim Sub.shift_up dim
, lazy (un_dim_abs l)
, lazy (un_dim_abs r))
| DDimAbs(_, x) -> Lazy.force x
| DNe ne -> begin
match type_of_ne ne with
| DPathP(_, _, l, r) ->
DSplit(DimVar 0, Sub.dl Sub.shift_up l, Sub.dl Sub.shift_up r)
| _ -> failwith "internal type error"
end
| _ -> failwith "internal type error"
and fst : 'n. 'n dl -> 'n d
= fun x -> bind (Lazy.force x) (function
| DPair(x, _) -> Lazy.force x
| DNe ne -> DNe (DFst ne)
| _ -> failwith "internal type error")
and snd : 'n. 'n dl -> 'n d
= fun x -> bind (Lazy.force x) (function
| DPair(_, y) -> Lazy.force y
| DNe ne -> DNe (DSnd ne)
| _ -> failwith "internal type error")
and dom_ty : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPi(a, _) -> Lazy.force a
| _ -> failwith "internal type error")
and cod_ty : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPi(_, b) -> DLam b
| _ -> failwith "internal type error")
and pathp_ty_lhs : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPathP(_, a, _, _) -> Sub.d (Sub.app Zero) (Lazy.force a)
| _ -> failwith "internal type error")
and pathp_ty_rhs : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPathP(_, a, _, _) -> Sub.d (Sub.app One) (Lazy.force a)
| _ -> failwith "internal type error")
and pathp_ty_path : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPathP(i, a, _, _) -> DDimAbs(i, a)
| _ -> failwith "internal type error")
and pathp_lhs : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPathP(_, _, lhs, _) -> Lazy.force lhs
| _ -> failwith "internal type error")
and pathp_rhs : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DPathP(_, _, _, rhs) -> Lazy.force rhs
| _ -> failwith "internal type error")
and fst_ty : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DSigma(a, _) -> Lazy.force a
| _ -> failwith "internal type error")
and snd_ty : 'n. 'n d -> 'n d
= fun x -> bind x (function
| DSigma(_, b) -> DLam b
| _ -> failwith "internal type error")
and eval : 'n. 'n env -> Core.tm -> 'n d
= fun env tm -> match tm with
| Var idx -> begin
match List.nth env idx with
| Val v -> Lazy.force v
| Dim _ -> failwith "internal scoping error"
end
| Let(_, x, body) ->
eval (Val (lazy (eval env x))::env) body
| Coe(r, r', _, ty, a) ->
let env' = Dim (DimVar 0) :: Sub.env Sub.shift_up env in
let r, r' = eval_dim env r, eval_dim env r' in
let ty = lazy (eval env' ty) in
let a = lazy (eval env a) in
coe r r' ty a
| Com { s; r; r'; i = _; ty; lhs; rhs } ->
let env' = Dim (DimVar 0) :: Sub.env Sub.shift_up env in
let s, r, r' = eval_dim env s, eval_dim env r, eval_dim env r' in
let ty = lazy (eval env' ty) in
let lhs = lazy (eval env' lhs) in
let rhs = lazy (eval env' rhs) in
com s r r' ty lhs rhs
| HCom { s; r'; i = _; lhs; rhs } ->
let env' = Dim (DimVar 0) :: Sub.env Sub.shift_up env in
let s, r' = eval_dim env s, eval_dim env r' in
let lhs = lazy (eval env' lhs) in
let rhs = lazy (eval env' rhs) in
hcom s r' lhs rhs
| Abort -> failwith "unreachable"
| Set -> DSet
| DomTy x -> dom_ty (eval env x)
| CodTy x -> cod_ty (eval env x)
| PathPTyLhs x -> pathp_ty_lhs (eval env x)
| PathPTyRhs x -> pathp_ty_rhs (eval env x)
| PathPTyPath x -> pathp_ty_path (eval env x)
| PathPLhs x -> pathp_lhs (eval env x)
| PathPRhs x -> pathp_rhs (eval env x)
| Pi(name, a, b) ->
DPi(lazy (eval env a), { name; env; body = b})
| Lam(name, body) ->
DLam { name; env; body }
| App(f, x) ->
app (lazy (eval env f)) (lazy (eval env x))
| PathP(name, ty, lhs, rhs) ->
let env' = Dim (DimVar 0) :: Sub.env Sub.shift_up env in
DPathP( name
, lazy (eval env' ty)
, lazy (eval env lhs)
, lazy (eval env rhs))
| DimAbs(name, body) ->
let env' = Dim (DimVar 0) :: Sub.env Sub.shift_up env in
DDimAbs(name, lazy (eval env' body))
| DimApp(p, i) ->
dim_app (lazy (eval env p)) (eval_dim env i)
| Sigma(name, a, b) ->
DSigma(lazy (eval env a), { name; env; body = b })
| Pair(x, y) ->
DPair(lazy (eval env x), lazy (eval env y))
| Fst tm ->
fst (lazy (eval env tm))
| Snd tm ->
snd (lazy (eval env tm))
and eval_dim : 'n. 'n env -> Core.dim -> 'n dim
= fun env d -> match d with
| Zero -> Zero
| One -> One
| DimVar idx ->
match List.nth env idx with
| Dim d -> d
| Val _ -> failwith "internal scoping error"
(* How to coe r r' (i.A) a: (bottom of p.14)
(force A and) match A with
| Π(x:A) → B ⇒ λ x. coe r r' i.B(coe r' i (i.A) x) (a(coe r' r i.A x))
| Σ(x:A) × B ⇒ (coe r r' i.A a.0, coe r r' i.B(coe r i i.A a.0) a.1
| Path j.A x y ⇒ λ j. com<j> r r' i.A(j) i.x i.y i.a@j
| Bool ⇒ a
| Set ⇒ a
| i ? l : r ⇒ (a neutral term; BUT ALSO special rules)
| neutral ⇒ (a neutral term; BUT ALSO special rules)
Special rules for coe:
- when r = r', coe is identity
- REGULARITY: when i,j ⊢ A(i) = A(j), coe is identity
*)
and coe : 'n. 'n dim -> 'n dim -> 'n s dl -> 'n dl -> 'n d
= fun r r' ty x -> bind_shift_down (Lazy.force ty) (function
| DSet -> Lazy.force x
| DPi(a, b) ->
let a = lazy (DDimAbs("i", a)) in
let b = lazy (DDimAbs("i", lazy (DLam b))) in
(* indices: 1 2 3 4 5 *)
let env = [Dim r; Dim r'; Val a; Val b; Val x] in
(* λ x. coe r r' i.B(coe r' i (i.A) x) (a(coe r' r i.A x)) *)
let open Core in
let arg, r, r', _a, _b, x =
Var 0, DimVar 1, DimVar 2, Var 3, Var 4, Var 5 in
let body = Coe(r, r', "i",
begin
let i, arg, _r, r', _a, b, _x =
DimVar 0, Var 1, DimVar 2, DimVar 3, Var 4, Var 5, Var 6 in
App(DimApp(b, i), Coe(r', i, "i", DimApp(Var 5, DimVar 0), arg))
end,
App(x, Coe(r', r, "i", DimApp(Var 4, DimVar 0), arg))) in
DLam { name = "x"; env; body }
| DSigma(a, b) ->
(* I think this impl of coe means I need to have eta for pairs *)
let fst_x = lazy (fst x) in
let fst = lazy (coe r r' a fst_x) in
let shift: ('n, 'n s) sub = Sub.shift_up in
let shift' = Sub.extend shift in
let b_of_fst = lazy (b $ lazy
(coe (Sub.dim shift r) (DimVar 0) (Sub.dl shift' a)
(Sub.dl shift fst_x))) in
let snd = lazy (coe r r' b_of_fst (lazy (snd x))) in
DPair(fst, snd)
| DPathP(j, ty, lhs, rhs) ->
DDimAbs(j, lazy (
let shift: ('n, 'n s) sub = Sub.shift_up in
let shift' = Sub.extend shift in
let r, r' = Sub.dim shift r, Sub.dim shift r' in
let lhs, rhs = Sub.dl shift' lhs, Sub.dl shift' rhs in
(* let cap = lazy (dim_app *)
(* (Sub.dl (Sub.compose Sub.shift_up shift) a) (DimVar 1)) in *)
let swap: ('n s s, 'n s s) sub =
Sub.(compose (app (DimVar 1)) (extend shift')) in
let ty = Sub.dl swap ty in
com (DimVar 0) r r' ty lhs rhs))
| (DSplit _ | DNe _) as ty ->
let ty0 = lazy (Sub.d (Sub.app Zero) ty) in
let ty1 = lazy (Sub.d (Sub.app One) ty) in
let neutral = lazy (DNe (DCoe { r; r'; ty0; ty1; a = x })) in
DIf { cond = DimEq(r, r')
; yes = x
; no = lazy (DIf
{ cond = TyEq(ty0, ty1); yes = x; no = neutral }) }
| _ -> failwith "internal type error")
(* How to com<s> r r' (i.A) i.lhs i.rhs a:
hcom<s> r r' A(r')
i.(coe i r' (i.A) lhs)
i.(coe i r' (i.A) rhs)
(coe r r' (i.A) a)
*)
and com
: 'n. 'n dim -> 'n dim -> 'n dim -> 'n s dl -> 'n s dl -> 'n s dl -> 'n d
= fun s _r r' ty lhs rhs ->
let ty_up = Sub.dl Sub.shift_up ty in
let r'_up = Sub.dim Sub.shift_up r' in
let coerce_it x = lazy (coe (DimVar 0) r'_up ty_up x) in
let lhs', rhs' = coerce_it lhs, coerce_it rhs in
hcom s r' lhs' rhs'
(* How to hcom<s> r r' A i.lhs i.rhs a:
All that matters is the boundary behavior!
Just return DSplit s lhs(r') rhs(r')
Lmao
*)
and hcom : 'n. 'n dim -> 'n dim -> 'n s dl -> 'n s dl -> 'n d
= fun s r' lhs rhs ->
let lhs_r' = Sub.dl (Sub.app r') lhs in
let rhs_r' = Sub.dl (Sub.app r') rhs in
DSplit(s, lhs_r', rhs_r')
end
module Ctx : sig
open Domain
exception NotInScope of name
exception GotDimWantedVal
exception GotValWantedDim
type 'n t
val initial_ctx : z t
(* The length of the term context Γ and its names *)
val lvl : 'n t -> lvl
val tm_names : 'n t -> name list
(* The length of the dimension context Ψ is 'n, and these are its names *)
val dim_names : 'n t -> name list
val env : 'n t -> 'n env
val lookup_var : 'n t -> name -> idx * 'n dl
val lookup_dim : 'n t -> name -> idx
val with_defn : 'n t -> name -> 'n dl -> 'n dl -> 'n t
val with_var : 'n t -> name -> 'n dl -> 'n dl * 'n t
val with_dim_var : 'n t -> name -> 'n s dim * 'n s t
val with_eqn : 'n t -> 'n dim -> 'n dim -> 'n t option
val are_dims_eq : 'n t -> 'n dim -> 'n dim -> bool
end = struct
open Domain
exception NotInScope of name
exception GotDimWantedVal
exception GotValWantedDim
module DisjSets = struct
module IntMap = Map.Make (Int)
include IntMap
type t = int IntMap.t
let rec find i m =
let p = IntMap.find i m in
if p = i then i else find p m
let union i j m =
let i, j = find i m, find j m in
IntMap.add i j m
end
type 'n t =
{ lvl: int
; tm_names: name list
; dim_count: int
; dim_names: name list
; env: 'n env
; tys: (name * 'n env_item) list
; formula: DisjSets.t }
let initial_ctx =
{ lvl = 0
; tm_names = []
; dim_count = 0
; dim_names = []
; env = []
; tys = []
; formula = DisjSets.(empty |> add 0 0 |> add 1 1) }
let lvl ctx = ctx.lvl
let tm_names ctx = ctx.tm_names
let dim_names ctx = ctx.dim_names
let env ctx = ctx.env
let lookup_helper ctx name =
let rec go i = function
| [] -> raise (NotInScope name)
| (n,x)::_ when n = name -> i, x
| _::rest -> go (i+1) rest in
go 0 ctx.tys
let lookup_var ctx name = match lookup_helper ctx name with
| i, Val ty -> i, ty
| _, Dim _ -> raise GotDimWantedVal
let lookup_dim ctx name = match lookup_helper ctx name with
| i, Dim _ -> i
| _, Val _ -> raise GotValWantedDim
let with_defn ctx name ty value =
{ lvl = 1 + ctx.lvl
; tm_names = name :: ctx.tm_names
; dim_count = ctx.dim_count
; dim_names = ctx.dim_names
; env = Val value :: ctx.env
; tys = (name, Val ty) :: ctx.tys
; formula = ctx.formula }
let with_var ctx name ty =
let value = lazy (DNe (DVar(ctx.lvl, ty))) in
value, with_defn ctx name ty value
let dim_to_id ctx =
function
| Zero -> 0
| One -> 1
| DimVar idx -> 2 + ctx.dim_count - idx - 1
let with_dim_var ctx name =
let i = DimVar 0 in
let id = 2 + ctx.dim_count in
i, { lvl = ctx.lvl
; tm_names = ctx.tm_names
; dim_count = 1 + ctx.dim_count
; dim_names = name :: ctx.dim_names
; env = Dim i :: Sub.env Sub.shift_up ctx.env
; tys = (name, Dim i)
:: List.map (fun (n, it) -> n, Sub.env_item Sub.shift_up it) ctx.tys
; formula = DisjSets.add id id ctx.formula }
let is_consistent formula =
DisjSets.find 0 formula <> DisjSets.find 1 formula
let with_eqn ctx i j =
let i_id, j_id = dim_to_id ctx i, dim_to_id ctx j in
let formula' = DisjSets.union i_id j_id ctx.formula in
if is_consistent formula' then
Some { ctx with formula = formula' }
else
None
let are_dims_eq ctx i j =
let i_id, j_id = dim_to_id ctx i, dim_to_id ctx j in
DisjSets.find i_id ctx.formula = DisjSets.find j_id ctx.formula
end
module Conv : sig
open Domain
val force : 'n Ctx.t -> 'n dl -> 'n d
(* Conversion checking *)
val eq : 'n Ctx.t -> 'n dl -> 'n dl -> 'n dl -> bool
(* Subtype checking *)
val sub : 'n Ctx.t -> 'n dl -> 'n dl -> bool
end = struct
open Domain
(* Resolve DIf/DSplit conditions and apply INTERNAL INJECTIVITY! *)
let rec force : 'n. 'n Ctx.t -> 'n dl -> 'n d
= fun ctx x -> match Lazy.force x with
| DIf { cond; yes; no } ->
force ctx (if is_cond_true ctx cond then yes else no)
| DSplit(dim, l, r) -> begin
if Ctx.are_dims_eq ctx dim Zero then
force ctx l
else if Ctx.are_dims_eq ctx dim One then
force ctx r
else
let l, r = force ctx l, force ctx r in
let tm = DSplit(dim, lazy l, lazy r) in
match l, r with
| DNe l, DNe r when eq_ne ctx l r -> DNe l
| DSet, DSet -> DSet
| DPi _, DPi _ ->
let dom = lazy (Eval.dom_ty tm) in
let cod = lazy (Eval.cod_ty tm) in
DPi(dom, Eval.lam_to_clos cod)
| DPathP _, DPathP _ ->
let a = lazy (Eval.un_dim_abs (lazy (Eval.pathp_ty_path tm))) in
let lhs = lazy (Eval.pathp_lhs tm) in
let rhs = lazy (Eval.pathp_rhs tm) in
DPathP("i", a, lhs, rhs)
| DSigma _, DSigma _ ->
let fst = lazy (Eval.fst_ty tm) in
let snd = lazy (Eval.snd_ty tm) in
DSigma(fst, Eval.lam_to_clos snd)
(* TODO: extend this when extending the domain *)
| _ -> tm
end
| x -> x
and is_cond_true : 'n. 'n Ctx.t -> 'n cond -> bool
= fun ctx cond -> match cond with
| DimEq(i, j) -> Ctx.are_dims_eq ctx i j
| TyEq(x, y) -> eq ctx (lazy DSet) x y
| Forall_i cond ->
let _i, ctx' = Ctx.with_dim_var ctx "i" in
is_cond_true ctx' cond
and eq_ne : 'n. 'n Ctx.t -> 'n dne -> 'n dne -> bool
= fun ctx x y -> Option.is_some (conv_ne ctx x y)
(* Conversion checking of neutrals returns their type *)
and conv_ne : 'n. 'n Ctx.t -> 'n dne -> 'n dne -> 'n dl option
= fun ctx x y -> match x, y with
| DVar(x, ty), DVar(y, _) ->
if x = y then Some ty else None
| DApp(fx, arg_x), DApp(fy, arg_y) ->
Option.bind (Option.map Lazy.force (conv_ne ctx fx fy)) (function
| DPi(a, b) ->
let open Eval in
if eq ctx a arg_x arg_y then Some (lazy (b $ arg_x)) else None
| _ -> failwith "internal type error")
| DCoe { r = rx; r' = r'_x; ty0 = ty0_x; ty1 = ty1_x; a = ax }
, DCoe { r = ry; r' = r'_y; ty0 = ty0_y; ty1 = ty1_y; a = ay } ->
if not (Ctx.are_dims_eq ctx rx ry) then None
else if not (Ctx.are_dims_eq ctx r'_x r'_y) then None
else if not (eq ctx (lazy DSet) ty0_x ty0_y) then None
else if not (eq ctx (lazy DSet) ty1_x ty1_y) then None
else if not (eq ctx (lazy (DSplit(rx, ty0_x, ty1_x))) ax ay) then None
else Some (lazy (DSplit(r'_x, ty0_x, ty0_y)))
| DFst x, DFst y ->
Option.map (Lazy.map Eval.fst_ty) (conv_ne ctx x y)
| DSnd x, DSnd y ->
Option.map (fun ty ->
lazy Eval.(app (Lazy.map snd_ty ty) (lazy (DNe (DFst x)))))
(conv_ne ctx x y)
| _ -> None
(* hooray for *boundary separation* *)
and check_both : 'n Ctx.t -> 'n dim -> 'n dl -> 'n dl
-> ('n Ctx.t -> 'n dl -> bool) -> bool
= fun ctx dim l r k ->
let ctx_l = Option.get @@ Ctx.with_eqn ctx dim Zero in
let ctx_r = Option.get @@ Ctx.with_eqn ctx dim One in
k ctx_l l && k ctx_r r
and eq : 'n. 'n Ctx.t -> 'n dl -> 'n dl -> 'n dl -> bool
= fun ctx ty x y -> match force ctx ty with
| DNe _ as ty -> begin
match force ctx x, force ctx y with
| DSplit(dim, xl, xr), y ->
let ty, y = lazy ty, lazy y in
check_both ctx dim xl xr (fun ctx x -> eq ctx ty x y)
| x, DSplit(dim, yl, yr) ->
let ty, x = lazy ty, lazy x in
check_both ctx dim yl yr (fun ctx y -> eq ctx ty x y)
| DNe x, DNe y -> eq_ne ctx x y
| _ -> failwith "internal type error"
end
| DSplit(dim, ty_l, ty_r) ->
check_both ctx dim ty_l ty_r (fun ctx ty -> eq ctx ty x y)
| DSet -> begin
match force ctx x, force ctx y with
| DSplit(dim, xl, xr), y ->
let ty, y = lazy DSet, lazy y in
check_both ctx dim xl xr (fun ctx x -> eq ctx ty x y)
| x, DSplit(dim, yl, yr) ->
let ty, x = lazy DSet, lazy x in
check_both ctx dim yl yr (fun ctx y -> eq ctx ty x y)
| DNe x, DNe y -> eq_ne ctx x y
| DSet, DSet -> true
| DPi(xa, xb), DPi(ya, yb) ->
let open Eval in
eq ctx (lazy DSet) xa ya &&
let v, ctx' = Ctx.with_var ctx xb.name xa in
eq ctx' (lazy DSet) (lazy (xb $ v)) (lazy (yb $ v))
| DPathP(name, xa, xl, xr), DPathP(_, ya, yl, yr) ->
let _i, ctx' = Ctx.with_dim_var ctx name in
eq ctx' (lazy DSet) xa ya &&
let ty_l = Sub.dl (Sub.app Zero) xa in
let ty_r = Sub.dl (Sub.app One) xa in
eq ctx ty_l xl yl && eq ctx ty_r xr yr
| DSigma(xa, xb), DSigma(ya, yb) ->
let open Eval in
eq ctx (lazy DSet) xa ya &&
let v, ctx' = Ctx.with_var ctx xb.name xa in
eq ctx' (lazy DSet) (lazy (xb $ v)) (lazy (yb $ v))
| _ -> false
end
| DPi(a, b) ->
let v, ctx' = Ctx.with_var ctx b.name a in
let open Eval in
eq ctx' (lazy (b $ v)) (lazy (app x v)) (lazy (app y v))
| DPathP _ -> true (* lol definitional UIP *)
| DSigma(a, b) ->
let open Eval in
eq ctx a (lazy (fst x)) (lazy (fst y))
&& eq ctx (lazy (b $ x)) (lazy (snd x)) (lazy (snd y))
| _ -> failwith "internal error: not a type"
(* TODO *)
let sub : 'n. 'n Ctx.t -> 'n dl -> 'n dl -> bool
= fun ctx x y -> eq ctx (lazy DSet) x y
end
module Pretty : sig
open Domain
(* Given ctx ⊢ x : ty, show ctx ty x pretty-prints x *)
val show : 'n Ctx.t -> 'n dl -> 'n dl -> string
end = struct
open Domain
let ($) f x = lazy Eval.(f $ x)
let parens p s = if p then "(" ^ s ^ ")" else s
let rec go : 'n. bool -> 'n Ctx.t -> 'n dl -> 'n dl -> string
= fun p ctx ty x -> match Conv.force ctx x with
| DNe ne -> fst (go_ne p ctx ne)
| DSplit(dim, a, b) ->
let ctx_l = Option.get @@ Ctx.with_eqn ctx dim Zero in
let ctx_r = Option.get @@ Ctx.with_eqn ctx dim One in
let dim = show_dim ctx dim in
"[" ^ dim ^ "=0 ↦ " ^ go false ctx_l ty a ^ " | "
^ dim ^ "=1 ↦ " ^ go false ctx_r ty b ^ "]"
| DIf _ -> failwith "unreachable"
| DSet -> "Set"
| DPi(a, b) ->
let v, ctx' = Ctx.with_var ctx b.name a in
parens p @@
"Π (" ^ b.name ^ " : " ^ go false ctx ty a ^ ") → "
^ go false ctx' ty (b $ v)
| DLam f ->
let a, b = match Conv.force ctx ty with
| DPi(a, b) -> a, b
| _ -> failwith "internal type error" in
let v, ctx' = Ctx.with_var ctx f.name a in
parens p @@ "λ " ^ f.name ^ " → " ^ go false ctx' (b $ v) (f $ v)
| DPathP(i, ty, lhs, rhs) ->
let _, ctx' = Ctx.with_dim_var ctx i in
let lhs_ty = Sub.dl (Sub.app Zero) ty in
let rhs_ty = Sub.dl (Sub.app One) ty in
parens p @@
"pathp " ^ i ^ "." ^ go true ctx' (lazy DSet) ty
^ " " ^ go true ctx lhs_ty lhs ^ " " ^ go true ctx rhs_ty rhs
| DDimAbs(i, body) ->
let _, ctx' = Ctx.with_dim_var ctx i in
let ty = match Conv.force ctx ty with
| DPathP(_, ty, _, _) -> ty
| _ -> failwith "internal type error" in
parens p @@ "λ " ^ i ^ " → " ^ go false ctx' ty body
| DSigma(a, b) ->
let v, ctx' = Ctx.with_var ctx b.name a in
parens p @@
"Σ (" ^ b.name ^ " : " ^ go false ctx ty a ^ ") → "
^ go false ctx' ty (b $ v)
| DPair(x, y) ->
let a, b = match Conv.force ctx ty with
| DSigma(a, b) -> a, (b $ x)
| _ -> failwith "internal type error" in
"(" ^ go false ctx a x ^ ", " ^ go false ctx b y ^ ")"
and go_ne : 'n. bool -> 'n Ctx.t -> 'n dne -> string * 'n dl
= fun p ctx x -> match x with
| DVar(l, ty) -> List.nth (Ctx.tm_names ctx) (Ctx.lvl ctx - l - 1), ty
| DApp(f, x) ->
let f, fty = go_ne false ctx f in
let a, b = match Conv.force ctx fty with
| DPi(a, b) -> a, b
| _ -> failwith "internal type error" in
let str = parens p @@ f ^ " " ^ go true ctx a x in
str, (b $ x)
| DCoe { r; r'; ty0; ty1; a } ->
let a_ty = lazy (DSplit(r', ty0, ty1)) in
let res_ty = lazy (DSplit(r', ty0, ty1)) in
let whole_ty =
lazy (DSplit(DimVar 0
, Sub.dl Sub.shift_up ty0
, Sub.dl Sub.shift_up ty1)) in
let _, ctx' = Ctx.with_dim_var ctx "i" in
let str = parens p @@
"coe " ^ show_dim ctx r ^ " " ^ show_dim ctx r' ^ " i."
^ go true ctx' (lazy DSet) whole_ty ^ " " ^ go true ctx a_ty a in
str, res_ty
| DFst ne ->
let str, ty = go_ne true ctx ne in
let str = parens p @@ "fst " ^ str in
let ty = lazy (match Conv.force ctx ty with
| DSigma(a, _) -> Lazy.force a
| _ -> failwith "internal type error") in
str, ty
| DSnd ne ->
let str, ty = go_ne true ctx ne in
let str = parens p @@ "snd " ^ str in
let ty = lazy (match Conv.force ctx ty with
| DSigma(_, b) -> Eval.(b $ lazy (DNe (DFst ne)))
| _ -> failwith "internal type error") in
str, ty
and show_dim : 'n. 'n Ctx.t -> 'n dim -> string
= fun ctx d -> match d with
| Zero -> "0"
| One -> "1"
| DimVar idx -> List.nth (Ctx.dim_names ctx) idx
let show ctx ty x =
go false ctx ty x
end
module Tychk = struct
open Domain
exception TypeError of string
let check_eq ctx ty x y =
if not (Conv.eq ctx ty x y) then
let x = Pretty.show ctx ty x in
let y = Pretty.show ctx ty y in
raise (TypeError(x ^ " is different from " ^ y))
let check_sub ctx x y =
if not (Conv.sub ctx x y) then
let x = Pretty.show ctx (lazy DSet) x in
let y = Pretty.show ctx (lazy DSet) y in
raise (TypeError(x ^ " is not a subtype of " ^ y))
(* the main check/infer loop! *)
let rec check : 'n. 'n Ctx.t -> AST.exp -> 'n dl -> Core.tm =