-
Notifications
You must be signed in to change notification settings - Fork 16
/
Refiner.ml
1456 lines (1295 loc) · 47.9 KB
/
Refiner.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
open Containers
open Basis
open Monads
open Bwd
open Bwd.Infix
open CodeUnit
module D = Domain
module S = Syntax
module Env = RefineEnv
module Err = RefineError
module RM = RefineMonad
module RMU = Monad.Util (RM)
module T = Tactic
module Splice = Splice
module TB = TermBuilder
module Sem = Semantics
module Qu = Quote
open Monad.Notation (RM)
module MU = Monad.Util (RM)
exception CJHM
type ('a, 'b) quantifier = 'a -> Ident.t * (T.var -> 'b) -> 'b
module Probe : sig
val probe_chk : string option -> T.Chk.tac -> T.Chk.tac
val probe_boundary : T.Chk.tac -> T.Chk.tac -> T.Chk.tac
val probe_syn : string option -> T.Syn.tac -> T.Syn.tac
val probe_goal_chk : ((Ident.t * S.tp) list -> S.tp -> unit RM.m) -> T.Chk.tac -> T.Chk.tac
val probe_goal_syn : ((Ident.t * S.tp) list -> S.tp -> unit RM.m) -> T.Syn.tac -> T.Syn.tac
(** Run the first tactic, and if the boundary is not satisfied, run the second tactic family at the term produced by the first tactic. *)
val try_with_boundary : T.Chk.tac -> (S.t -> T.Chk.tac) -> T.Chk.tac
end =
struct
let probe_goal_chk k tac =
T.Chk.brule ~name:"probe_goal_chk" @@ fun (tp, phi, clo) ->
let* s = T.Chk.brun tac (tp, phi, clo) in
let+ () =
let* stp = RM.quote_tp @@ D.Sub (tp, phi, clo) in
let* env = RM.read in
let cells = Env.locals env in
RM.globally @@
let* ctx = RM.destruct_cells @@ BwdLabels.to_list cells in
k ctx stp
in
s
let probe_goal_syn k tac =
T.Syn.rule ~name:"probe_goal_syn" @@
let* s, tp = T.Syn.run tac in
let+ () =
let* stp = RM.quote_tp tp in
let* env = RM.read in
let cells = Env.locals env in
RM.globally @@
let* ctx = RM.destruct_cells @@ BwdLabels.to_list cells in
k ctx stp
in
s, tp
let probe_chk name tac =
probe_goal_chk (RM.print_state name) tac
let probe_syn name tac =
probe_goal_syn (RM.print_state name) tac
let probe_boundary probe tac =
T.Chk.brule ~name:"probe_boundary" @@ fun (tp, phi, clo) ->
let* probe_tm = T.Chk.run probe tp in
let* () = RM.print_boundary probe_tm tp phi clo in
T.Chk.brun tac (tp, phi, clo)
let try_with_boundary tac backup =
T.Chk.brule ~name:"try_with_boundary" @@ fun (tp, phi, tm_clo) ->
let* tm = T.Chk.brun tac (tp, phi, tm_clo) in
let* bdry_sat = RM.boundary_satisfied tm tp phi tm_clo in
match bdry_sat with
| `BdryUnsat -> T.Chk.brun (backup tm) (tp, phi, tm_clo)
| `BdrySat -> RM.ret tm
end
module Hole : sig
val silent_hole : string option -> T.Chk.tac
val unleash_hole : string option -> T.Chk.tac
val silent_syn_hole : string option -> T.Syn.tac
val unleash_syn_hole : string option -> T.Syn.tac
end =
struct
let assert_hole_possible tp =
RM.lift_cmp @@ Sem.whnf_tp_ tp |>>
function
| D.TpDim | D.TpCof | D.TpPrf _ ->
let* ttp = RM.quote_tp tp in
RM.with_pp @@ fun ppenv ->
RM.refine_err @@ Err.HoleNotPermitted (ppenv, ttp)
| _ -> RM.ret ()
let make_hole name (tp, phi, clo) : D.cut m =
let* () = assert_hole_possible tp in
let* env = RM.read in
let cells = Env.locals env in
RM.globally @@
let* sym =
let* tp = RM.multi_pi (BwdLabels.to_list cells) @@ RM.quote_tp @@ D.Sub (tp, phi, clo) in
let* vtp = RM.lift_ev @@ Sem.eval_tp tp in
let ident =
match name with
| None -> Ident.anon
| Some str -> Ident.machine @@ "?" ^ str
in
RM.add_global ~unfolder:None ~shadowing:true ident vtp
in
let* () = RM.add_hole (tp, phi, clo) in
let cut = RM.multi_ap cells (D.Global sym, []) in
RM.ret (D.UnstableCut (cut, D.KSubOut (phi, clo)), [])
let silent_hole name : T.Chk.tac =
T.Chk.brule ~name:"silent_hole" @@ fun (tp, phi, clo) ->
let* cut = make_hole name (tp, phi, clo) in
RM.quote_cut cut
let unleash_hole name : T.Chk.tac =
Probe.probe_chk name @@
T.Chk.brule ~name:"unleash_hole" @@ fun (tp, phi, clo) ->
let* cut = make_hole name (tp, phi, clo) in
RM.quote_cut cut
let silent_syn_hole name : T.Syn.tac =
T.Syn.rule ~name:"silent_syn_hole" @@
let* cut = make_hole name @@ (D.Univ, CofBuilder.bot, D.Clo (S.tm_abort, {tpenv = Emp; conenv = Emp})) in
let tp = D.ElCut cut in
let+ tm = tp |> T.Chk.run @@ unleash_hole name in
tm, tp
let unleash_syn_hole name : T.Syn.tac =
Probe.probe_syn name @@
T.Syn.rule ~name:"unleash_syn_hole" @@
let* cut = make_hole name @@ (D.Univ, CofBuilder.bot, D.Clo (S.tm_abort, {tpenv = Emp; conenv = Emp})) in
let tp = D.ElCut cut in
let+ tm = tp |> T.Chk.run @@ unleash_hole name in
tm, tp
end
module Sub =
struct
let formation (tac_base : T.Tp.tac) (tac_phi : T.Chk.tac) (tac_tm : T.var -> T.Chk.tac) : T.Tp.tac =
T.Tp.rule ~name:"Sub.formation" @@
let* base = T.Tp.run tac_base in
let* vbase = RM.lift_ev @@ Sem.eval_tp base in
let* phi = T.Chk.run tac_phi D.TpCof in
let+ tm =
let* vphi = RM.lift_ev @@ Sem.eval_cof phi in
T.abstract (D.TpPrf vphi) @@ fun prf ->
vbase |> T.Chk.run @@ tac_tm prf
in
S.Sub (base, phi, tm)
let intro (tac : T.Chk.tac) : T.Chk.tac =
T.Chk.brule ~name:"Sub.intro" @@
function
| D.Sub (tp_a, phi_a, clo_a), phi_sub, clo_sub ->
let phi = CofBuilder.join [phi_a; phi_sub] in
let* partial =
RM.lift_cmp @@ Sem.splice_tm @@
Splice.cof phi_a @@ fun phi_a ->
Splice.cof phi_sub @@ fun phi_sub ->
Splice.clo clo_a @@ fun fn_a ->
Splice.clo clo_sub @@ fun fn_sub ->
Splice.term @@ TB.lam @@ fun _ ->
TB.cof_split
[phi_a, TB.ap fn_a [TB.prf];
phi_sub, TB.sub_out @@ TB.ap fn_sub [TB.prf]]
in
let+ tm = T.Chk.brun tac (tp_a, phi, D.un_lam partial) in
S.SubIn tm
| tp, _, _ ->
RM.expected_connective `Sub tp
let elim (tac : T.Syn.tac) : T.Syn.tac =
T.Syn.rule ~name:"Sub.elim" @@
let* tm, subtp = T.Syn.run tac in
match subtp with
| D.Sub (tp, _, _) ->
RM.ret (S.SubOut tm, tp)
| tp ->
RM.expected_connective `Sub tp
end
module Dim =
struct
let formation : T.Tp.tac =
T.Tp.virtual_rule ~name:"Dim.formation" @@
RM.ret S.TpDim
let dim0 : T.Chk.tac =
T.Chk.rule ~name:"Dim.dim0" @@
function
| D.TpDim ->
RM.ret S.Dim0
| tp ->
RM.expected_connective `Dim tp
let dim1 : T.Chk.tac =
T.Chk.rule ~name:"Dim.dim1" @@
function
| D.TpDim ->
RM.ret S.Dim1
| tp ->
RM.expected_connective `Dim tp
let literal : int -> T.Chk.tac =
function
| 0 -> dim0
| 1 -> dim1
| n ->
T.Chk.rule ~name:"Dim.literal" @@ fun _ ->
RM.refine_err @@ Err.ExpectedDimensionLiteral n
end
module Cof =
struct
let formation : T.Tp.tac =
T.Tp.virtual_rule ~name:"Cof.formation" @@
RM.ret S.TpCof
let expected_cof =
RM.expected_connective `Cof
let eq tac0 tac1 =
T.Chk.rule ~name:"Cof.eq" @@
function
| D.TpCof ->
let+ r0 = T.Chk.run tac0 D.TpDim
and+ r1 = T.Chk.run tac1 D.TpDim in
S.CofBuilder.eq r0 r1
| tp ->
expected_cof tp
let le tac0 tac1 =
T.Chk.rule ~name:"Cof.le" @@
function
| D.TpCof ->
let+ r0 = T.Chk.run tac0 D.TpDim
and+ r1 = T.Chk.run tac1 D.TpDim in
S.CofBuilder.le r0 r1
| tp ->
expected_cof tp
let join tacs =
T.Chk.rule ~name:"Cof.join" @@
function
| D.TpCof ->
let+ phis = MU.map (fun t -> T.Chk.run t D.TpCof) tacs in
S.CofBuilder.join phis
| tp ->
expected_cof tp
let meet tacs =
T.Chk.rule ~name:"Cof.meet" @@
function
| D.TpCof ->
let+ phis = MU.map (fun t -> T.Chk.run t D.TpCof) tacs in
S.CofBuilder.meet phis
| tp ->
expected_cof tp
let boundary tac = join [eq tac Dim.dim0; eq tac Dim.dim1]
let assert_true vphi =
RM.lift_cmp @@ CmpM.test_sequent [] vphi |>> function
| true -> RM.ret ()
| false ->
RM.with_pp @@ fun ppenv ->
let* tphi = RM.quote_cof vphi in
RM.refine_err @@ Err.ExpectedTrue (ppenv, tphi)
module Split : sig
type branch_tac = {cof : T.Chk.tac; bdy : T.var -> T.Chk.tac}
val split : branch_tac list -> T.Chk.tac
end =
struct
type branch_tac = {cof : T.Chk.tac; bdy : T.var -> T.Chk.tac}
type branch_tac' = {cof : D.cof; tcof : S.t; bdy : T.var -> T.Chk.tac}
type branch = {cof : D.cof; tcof : S.t; fn : D.con; bdy : S.t}
let rec gather_branches (branches : branch_tac list) : (D.cof * branch_tac' list) m =
match branches with
| [] -> RM.ret (CofBuilder.bot, [])
| branch :: branches ->
let* tphi = T.Chk.run branch.cof D.TpCof in
let* vphi = RM.lift_ev @@ Sem.eval_cof tphi in
let+ psi, tacs = gather_branches branches in
CofBuilder.join [vphi; psi], {cof = vphi; tcof = tphi; bdy = branch.bdy} :: tacs
let splice_split branches =
let phis, fns = List.split branches in
RM.lift_cmp @@ Sem.splice_tm @@
Splice.cons (List.map D.cof_to_con phis) @@ fun phis ->
Splice.cons fns @@ fun fns ->
Splice.term @@ TB.lam @@ fun _ ->
TB.cof_split @@ List.combine phis @@ List.map (fun fn -> TB.ap fn [TB.prf]) fns
module State =
struct
open Bwd.Infix
type t =
{disj : D.cof;
fns : (D.cof * D.con) bwd;
acc : (S.t * S.t) bwd}
let init : t =
{disj = CofBuilder.bot;
fns = Emp;
acc = Emp}
let append : t -> branch -> t =
fun state branch ->
{disj = CofBuilder.join [state.disj; branch.cof];
fns = state.fns <: (branch.cof, branch.fn);
acc = state.acc <: (branch.tcof, branch.bdy)}
end
let split (branches : branch_tac list) : T.Chk.tac =
T.Chk.brule ~name:"Split.split" @@ fun (tp, psi, psi_clo) ->
let* disjunction, tacs = gather_branches branches in
let* () = assert_true disjunction in
let step : State.t -> branch_tac' -> State.t m =
fun state branch ->
let* bdy =
let psi' = CofBuilder.join [state.disj; psi] in
let* psi'_fn = splice_split @@ (psi, D.Lam (Ident.anon, psi_clo)) :: BwdLabels.to_list state.fns in
T.abstract (D.TpPrf branch.cof) @@ fun prf ->
T.Chk.brun (branch.bdy prf) (tp, psi', D.un_lam psi'_fn)
in
let+ fn = RM.lift_ev @@ Sem.eval (S.Lam (Ident.anon, bdy)) in
State.append state {cof = branch.cof; tcof = branch.tcof; fn; bdy}
in
let rec fold : State.t -> branch_tac' list -> S.t m =
fun state ->
function
| [] ->
RM.ret @@ S.CofSplit (BwdLabels.to_list state.acc)
| tac :: tacs ->
let* state = step state tac in
fold state tacs
in
fold State.init tacs
end
include Split
end
module Prf =
struct
let formation tac_phi =
T.Tp.virtual_rule ~name:"Prf.formation" @@
let+ phi = T.Chk.run tac_phi D.TpCof in
S.TpPrf phi
let intro =
T.Chk.brule ~name:"Prf.intro" @@
function
| D.TpPrf phi, _, _ ->
let+ () = Cof.assert_true phi in
S.Prf
| tp, _, _ ->
RM.expected_connective `Prf tp
end
module Pi =
struct
let formation : (T.Tp.tac, T.Tp.tac) quantifier =
fun tac_base (nm, tac_fam) ->
T.Tp.rule ~name:"Pi.formation" @@
let* base = T.Tp.run_virtual tac_base in
let* vbase = RM.lift_ev @@ Sem.eval_tp base in
let+ fam = T.abstract ~ident:nm vbase @@ fun var -> T.Tp.run @@ tac_fam var in
S.Pi (base, nm, fam)
let intro ?(ident = Ident.anon) (tac_body : T.var -> T.Chk.tac) : T.Chk.tac =
T.Chk.brule ~name:"Pi.intro" @@
function
| D.Pi (base, _, fam), phi, phi_clo ->
T.abstract ~ident base @@ fun var ->
let* fib = RM.lift_cmp @@ Sem.inst_tp_clo fam @@ T.Var.con var in
let+ tm = T.Chk.brun (tac_body var) (fib, phi, D.un_lam @@ D.compose (D.Lam (Ident.anon, D.apply_to (T.Var.con var))) @@ D.Lam (Ident.anon, phi_clo)) in
S.Lam (ident, tm)
| tp, _, _ ->
RM.expected_connective `Pi tp
let apply tac_fun tac_arg : T.Syn.tac =
T.Syn.rule ~name:"Pi.apply" @@
let* tfun, tp = T.Syn.run tac_fun in
match tp with
| D.Pi (base, _, fam) ->
let* targ = T.Chk.run tac_arg base in
let+ fib =
let* varg = RM.lift_ev @@ Sem.eval targ in
RM.lift_cmp @@ Sem.inst_tp_clo fam varg
in
S.Ap (tfun, targ), fib
| _ ->
Format.printf "Bad apply!! %a@." D.pp_tp tp;
RM.expected_connective `Pi tp
end
module Sg =
struct
let formation : (T.Tp.tac, T.Tp.tac) quantifier =
fun tac_base (nm, tac_fam) ->
T.Tp.rule ~name:"Sg.formation" @@
let* base = T.Tp.run tac_base in
let* vbase = RM.lift_ev @@ Sem.eval_tp base in
let+ fam = T.abstract ~ident:nm vbase @@ fun var -> T.Tp.run @@ tac_fam var in
S.Sg (base, nm, fam)
let intro (tac_fst : T.Chk.tac) (tac_snd : T.Chk.tac) : T.Chk.tac =
T.Chk.brule ~name:"Sg.intro" @@
function
| D.Sg (base, _, fam), phi, phi_clo ->
let* tfst = T.Chk.brun tac_fst (base, phi, D.un_lam @@ D.compose D.fst @@ D.Lam (Ident.anon, phi_clo)) in
let+ tsnd =
let* vfst = RM.lift_ev @@ Sem.eval tfst in
let* fib = RM.lift_cmp @@ Sem.inst_tp_clo fam vfst in
T.Chk.brun tac_snd (fib, phi, D.un_lam @@ D.compose D.snd @@ D.Lam (Ident.anon, phi_clo))
in
S.Pair (tfst, tsnd)
| tp , _, _ ->
RM.expected_connective `Sg tp
let pi1 tac : T.Syn.tac =
T.Syn.rule ~name:"Sg.pi1" @@
let* tpair, tp = T.Syn.run tac in
match tp with
| D.Sg (base, _, _) ->
RM.ret (S.Fst tpair, base)
| _ ->
RM.expected_connective `Sg tp
let pi2 tac : T.Syn.tac =
T.Syn.rule ~name:"Sg.pi2" @@
let* tpair, tp = T.Syn.run tac in
match tp with
| D.Sg (_, _, fam) ->
let+ fib =
let* vfst = RM.lift_ev @@ Sem.eval @@ S.Fst tpair in
RM.lift_cmp @@ Sem.inst_tp_clo fam vfst
in
S.Snd tpair, fib
| _ ->
RM.expected_connective `Sg tp
end
module Univ =
struct
let formation : T.Tp.tac =
T.Tp.rule ~name:"Univ.formation" @@
RM.ret S.Univ
let univ_tac : string -> (D.tp -> S.t RM.m) -> T.Chk.tac =
fun nm m ->
T.Chk.rule ~name:nm @@
function
| D.Univ -> m D.Univ
| tp ->
RM.expected_connective `Univ tp
let univ : T.Chk.tac =
univ_tac "Univ.univ" @@ fun _ ->
RM.ret S.CodeUniv
let nat : T.Chk.tac =
univ_tac "Univ.nat" @@ fun _ -> RM.ret S.CodeNat
let circle : T.Chk.tac =
univ_tac "Univ.circle" @@ fun _ -> RM.ret S.CodeCircle
let quantifier (tac_base : T.Chk.tac) (tac_fam : T.Chk.tac) =
fun univ ->
let* base = T.Chk.run tac_base univ in
let* vbase = RM.lift_ev @@ Sem.eval base in
let* famtp =
RM.lift_cmp @@
Sem.splice_tp @@
Splice.con vbase @@ fun base ->
Splice.tp univ @@ fun univ ->
Splice.term @@ TB.pi (TB.el base) @@ fun _ -> univ
in
let+ fam = T.Chk.run tac_fam famtp in
base, fam
let pi tac_base tac_fam : T.Chk.tac =
univ_tac "Univ.pi" @@ fun univ ->
let+ tp, fam = quantifier tac_base tac_fam univ in
S.CodePi (tp, fam)
let sg tac_base tac_fam : T.Chk.tac =
univ_tac "Univ.sg" @@ fun univ ->
let+ tp, fam = quantifier tac_base tac_fam univ in
S.CodeSg (tp, fam)
(* Quote a domain code signature to a syntax code signature, while optionally patching/renaming its fields
[patch_tacs] is a function from fields to an optional tactic that will definitionally constrain the value of that field.
`Patch will make the field an extension type, while `Subst will simply drop the field after instantiating it to the patch
[renaming] is a function from fields to an optional new name for that field
*)
let quote_kan_tele_hooks
(tele : D.kan_tele)
~(patch_tacs : Ident.t -> [`Patch of T.Chk.tac | `Subst of T.Chk.tac] option)
~(renaming : Ident.t -> Ident.t option)
(univ : D.tp) : _ m =
let rec go = function
| D.KEmpty -> RM.ret S.KEmpty
| D.KCell (lbl, code, tele) ->
let lbl = Option.value ~default:lbl (renaming lbl) in
match patch_tacs lbl with
| Some (`Subst tac) ->
let* tp = RM.lift_cmp @@ Sem.do_el code in
let* patch = T.Chk.run tac tp in
let* vpatch = RM.eval patch in
RM.lift_cmp @@ Sem.inst_kan_tele_clo tele vpatch |>> go
| Some (`Patch tac) ->
let* tp = RM.lift_cmp @@ Sem.do_el code in
let* patch = T.Chk.run tac tp in
let* vpatch = RM.eval patch in
let* patched_code =
RM.lift_cmp @@
Sem.splice_tm @@
Splice.con code @@ fun code ->
Splice.con vpatch @@ fun patch ->
Splice.term @@
TB.code_ext 0 code TB.top @@ TB.lam @@ fun _ -> patch
in
let* patched_tp = RM.lift_cmp @@ Sem.do_el patched_code in
let* qpatched_code = RM.quote_con univ patched_code in
RM.abstract (lbl :> Ident.t) patched_tp @@ fun _ ->
let* tele = RM.lift_cmp @@ Sem.inst_kan_tele_clo tele vpatch in
let+ qtele = go tele in
S.KCell (lbl, qpatched_code, qtele)
| None ->
let* qcode = RM.quote_con univ code in
let* tp = RM.lift_cmp @@ Sem.do_el code in
RM.abstract (lbl :> Ident.t) tp @@ fun x ->
let+ qtele = RM.lift_cmp @@ Sem.inst_kan_tele_clo tele x |>> go in
S.KCell (lbl, qcode, qtele)
in
go tele
let quote_kan_tele sign univ = quote_kan_tele_hooks sign ~patch_tacs:(fun _ -> None) ~renaming:(fun _ -> None) univ
let patch_fields sign patch_tacs univ = quote_kan_tele_hooks sign ~patch_tacs ~renaming:(fun _ -> None) univ
let rename_kan_tele sign renaming univ = quote_kan_tele_hooks sign ~renaming ~patch_tacs:(fun _ -> None) univ
(** Abstract over all the variables in a code signature *)
let abstract_kan_tele sign k =
let rec go vars = function
| D.KEmpty -> k (Bwd.to_list vars)
| D.KCell (lbl, code, tele) ->
let* tp = RM.lift_cmp @@ Sem.do_el code in
RM.abstract lbl tp @@ fun x ->
let* sign = RM.lift_cmp @@ Sem.inst_kan_tele_clo tele x in
go (vars <: x) sign
in
go Emp sign
let signature (tac : T.KanTele.tac) : T.Chk.tac =
univ_tac "Univ.signature" @@ fun univ ->
let+ tele = T.KanTele.run tac univ in
S.CodeSignature tele
let patch (sig_tac : T.Chk.tac) (tacs : Ident.t -> [`Patch of T.Chk.tac | `Subst of T.Chk.tac] option) : T.Chk.tac =
univ_tac "Univ.patch" @@ fun univ ->
let* code = T.Chk.run sig_tac univ in
let* vcode = RM.lift_ev @@ Sem.eval code in
let* tp = RM.lift_cmp @@ Sem.do_el vcode in
let* whnf_tp = RM.lift_cmp @@ Sem.whnf_tp_ tp in
match whnf_tp with
| D.ElStable (`Signature sign) ->
let+ sign = patch_fields sign tacs univ in
S.CodeSignature sign
| _ ->
RM.expected_connective `Signature whnf_tp
let total (vtele : D.kan_tele) (vfam : D.con) : T.Chk.tac =
univ_tac "Univ.total" @@ fun univ ->
let* qtele = quote_kan_tele vtele univ in
abstract_kan_tele vtele @@ fun vars ->
Debug.print "Taking total space of family: %a@." D.pp_con vfam;
let lbls = D.kan_tele_lbls vtele in
let strct = D.Struct (D.Fields (List.combine lbls vars)) in
let* fib =
RM.lift_cmp @@ Sem.splice_tm @@
Splice.con vfam @@ fun fam ->
Splice.con strct @@ fun strct ->
Splice.term @@
TB.el_out @@ TB.ap fam [TB.el_in strct]
in
let+ qfib = RM.quote_con D.Univ fib in
S.CodeSignature (S.append_kan_tele qtele (S.KCell (`User ["fib"], qfib, S.KEmpty)))
let ext (n : int) (tac_fam : T.Chk.tac) (tac_cof : T.Chk.tac) (tac_bdry : T.Chk.tac) : T.Chk.tac =
univ_tac "Univ.ext" @@ fun univ ->
let* tcof =
let* tp_cof_fam = RM.lift_cmp @@ Sem.splice_tp @@ Splice.term @@ TB.cube n @@ fun _ -> TB.tp_cof in
RM.globally @@ T.Chk.run tac_cof tp_cof_fam
in
let* cof = RM.lift_ev @@ EvM.drop_all_cons @@ Sem.eval tcof in
let* tfam =
let* tp_fam = RM.lift_cmp @@ Sem.splice_tp @@ Splice.tp univ @@ fun univ -> Splice.term @@ TB.cube n @@ fun _ -> univ in
T.Chk.run tac_fam tp_fam
in
let+ tbdry =
let* fam = RM.lift_ev @@ Sem.eval tfam in
let* tp_bdry =
RM.lift_cmp @@ Sem.splice_tp @@
Splice.con cof @@ fun cof ->
Splice.con fam @@ fun fam ->
Splice.term @@
TB.cube n @@ fun js ->
TB.pi (TB.tp_prf @@ TB.ap cof js) @@ fun _ ->
TB.el @@ TB.ap fam js
in
T.Chk.run tac_bdry tp_bdry
in
S.CodeExt (n, tfam, `Global tcof, tbdry)
let is_nullary_ext = function
| D.ElStable (`Ext (0,_ ,`Global (Cof cof), _)) ->
let* cof = RM.lift_cmp @@ Sem.cof_con_to_cof cof in
RM.lift_cmp @@ CmpM.test_sequent [] cof
| _ -> RM.ret false
let infer_nullary_ext : T.Chk.tac =
T.Chk.rule ~name:"Univ.infer_nullary_ext" @@ function
| ElStable (`Ext (0,code ,`Global (Cof cof),bdry)) ->
let* cof = RM.lift_cmp @@ Sem.cof_con_to_cof cof in
let* () = Cof.assert_true cof in
let* tp = RM.lift_cmp @@
Sem.splice_tp @@
Splice.con code @@ fun code ->
Splice.term @@ TB.el code
in
let* tm = RM.lift_cmp @@
Sem.splice_tm @@
Splice.con bdry @@ fun bdry ->
Splice.term @@
TB.ap bdry [TB.prf]
in
let+ ttm = RM.lift_qu @@ Qu.quote_con tp tm in
S.ElIn (S.SubIn ttm)
| tp -> RM.expected_connective `ElExt tp
let code_v (tac_dim : T.Chk.tac) (tac_pcode: T.Chk.tac) (tac_code : T.Chk.tac) (tac_pequiv : T.Chk.tac) : T.Chk.tac =
univ_tac "Univ.code_v" @@ fun _univ ->
let* r = T.Chk.run tac_dim D.TpDim in
let* vr : D.dim =
let* vr_con = RM.lift_ev @@ Sem.eval r in
RM.lift_cmp @@ Sem.con_to_dim vr_con
in
let* pcode =
let tp_pcode = D.Pi (D.TpPrf (CofBuilder.eq0 vr), Ident.anon, D.const_tp_clo D.Univ) in
T.Chk.run tac_pcode tp_pcode
in
let* code = T.Chk.run tac_code D.Univ in
let+ pequiv =
T.Chk.run tac_pequiv @<<
let* vpcode = RM.lift_ev @@ Sem.eval pcode in
let* vcode = RM.lift_ev @@ Sem.eval code in
RM.lift_cmp @@
Sem.splice_tp @@
Splice.Macro.tp_pequiv_in_v ~r:vr ~pcode:vpcode ~code:vcode
in
S.CodeV (r, pcode, code, pequiv)
let coe (tac_fam : T.Chk.tac) (tac_src : T.Chk.tac) (tac_trg : T.Chk.tac) (tac_tm : T.Chk.tac) : T.Syn.tac =
T.Syn.rule ~name:"Univ.coe" @@
let* piuniv =
RM.lift_cmp @@
Sem.splice_tp @@
Splice.term @@
TB.pi TB.tp_dim @@ fun _i ->
TB.univ
in
let* fam = T.Chk.run tac_fam piuniv in
let* src = T.Chk.run tac_src D.TpDim in
let* trg = T.Chk.run tac_trg D.TpDim in
let* fam_src = RM.lift_ev @@ Sem.eval_tp @@ S.El (S.Ap (fam, src)) in
let+ tm = T.Chk.run tac_tm fam_src
and+ fam_trg = RM.lift_ev @@ Sem.eval_tp @@ S.El (S.Ap (fam, trg)) in
S.Coe (fam, src, trg, tm), fam_trg
let hcom_bdy_tp tp r phi =
RM.lift_cmp @@
Sem.splice_tp @@
Splice.con r @@ fun src ->
Splice.con phi @@ fun cof ->
Splice.tp tp @@ fun vtp ->
Splice.term @@
TB.pi TB.tp_dim @@ fun i ->
TB.pi (TB.tp_prf (TB.join [TB.eq i src; cof])) @@ fun _ ->
vtp
let hcom (tac_code : T.Chk.tac) (tac_src : T.Chk.tac) (tac_trg : T.Chk.tac) (tac_cof : T.Chk.tac) (tac_tm : T.Chk.tac) : T.Syn.tac =
T.Syn.rule ~name:"Univ.hcom" @@
let* code = T.Chk.run tac_code D.Univ in
let* src = T.Chk.run tac_src D.TpDim in
let* trg = T.Chk.run tac_trg D.TpDim in
let* cof = T.Chk.run tac_cof D.TpCof in
let* vsrc = RM.lift_ev @@ Sem.eval src in
let* vcof = RM.lift_ev @@ Sem.eval cof in
let* vtp = RM.lift_ev @@ Sem.eval_tp @@ S.El code in
(* (i : dim) -> (_ : [i=src \/ cof]) -> A *)
let+ tm = T.Chk.run tac_tm @<< hcom_bdy_tp vtp vsrc vcof in
S.HCom (code, src, trg, cof, tm), vtp
let hcom_chk (tac_src : T.Chk.tac) (tac_trg : T.Chk.tac) (tac_tm : T.Chk.tac) : T.Chk.tac =
let as_code = function
| D.ElStable code -> RM.ret @@ D.StableCode code
| D.ElUnstable code -> RM.ret @@ D.UnstableCode code
| D.ElCut cut -> RM.ret @@ D.Cut { tp = D.Univ; cut }
| tp -> RM.expected_connective `El tp
in
let cool_hcom =
T.Chk.brule ~name:"Univ.hcom_chk" @@ fun (tp, phi, tm_clo) ->
let* tp = RM.lift_cmp @@ Sem.whnf_tp_ tp in
match tp with
| D.Sub (sub_tp, psi, _) ->
Debug.print "Sub type: %a@." D.pp_tp sub_tp;
let tac_code =
T.Chk.brule @@ fun (_, _, _) ->
let* vcode = as_code sub_tp in
RM.quote_con D.Univ vcode
in
let tac_cof =
T.Chk.brule @@ fun (_, _, _) ->
RM.quote_cof @@ D.Cof.join [phi; psi]
in
let hcom_tac =
Sub.intro @@
T.Chk.syn @@
hcom tac_code tac_src tac_trg tac_cof tac_tm in
T.Chk.brun hcom_tac (tp, phi, tm_clo)
| _ -> RM.expected_connective `Sub tp
in
Probe.try_with_boundary cool_hcom @@ fun tm ->
T.Chk.brule @@ fun (tp, phi, tm_clo) ->
let* () = RM.print_boundary tm tp phi tm_clo in
T.Chk.brun (Hole.silent_hole None) (tp, phi, tm_clo)
let com (tac_fam : T.Chk.tac) (tac_src : T.Chk.tac) (tac_trg : T.Chk.tac) (tac_cof : T.Chk.tac) (tac_tm : T.Chk.tac) : T.Syn.tac =
T.Syn.rule ~name:"Univ.com" @@
let* piuniv =
RM.lift_cmp @@
Sem.splice_tp @@
Splice.term @@
TB.pi TB.tp_dim @@ fun _i ->
TB.univ
in
let* fam = T.Chk.run tac_fam piuniv in
let* src = T.Chk.run tac_src D.TpDim in
let* trg = T.Chk.run tac_trg D.TpDim in
let* cof = T.Chk.run tac_cof D.TpCof in
let* vfam = RM.lift_ev @@ Sem.eval fam in
let* vsrc = RM.lift_ev @@ Sem.eval src in
let* vcof = RM.lift_ev @@ Sem.eval cof in
(* (i : dim) -> (_ : [i=src \/ cof]) -> A i *)
let+ tm =
T.Chk.run tac_tm @<<
RM.lift_cmp @@
Sem.splice_tp @@
Splice.con vfam @@ fun vfam ->
Splice.con vsrc @@ fun src ->
Splice.con vcof @@ fun cof ->
Splice.term @@
TB.pi TB.tp_dim @@ fun i ->
TB.pi (TB.tp_prf (TB.join [TB.eq i src; cof])) @@ fun _ ->
TB.el @@ TB.ap vfam [i]
and+ vfam_trg = RM.lift_ev @@ Sem.eval_tp @@ S.El (S.Ap (fam, trg)) in
S.Com (fam, src, trg, cof, tm), vfam_trg
end
module Telescope =
struct
let empty : T.Tele.tac =
T.Tele.rule ~name:"Tele.empty" @@
RM.ret @@ S.Empty
let cell tp_tac (ident, tele_tac) : T.Tele.tac =
T.Tele.rule ~name:"Tele.cell" @@
let* tp = T.Tp.run tp_tac in
let* vtp = RM.eval_tp tp in
T.abstract ~ident vtp @@ fun var ->
let+ tele = T.Tele.run (tele_tac var) in
S.Cell (ident, tp, tele)
let include_ (rename : Ident.t -> Ident.t option) (inc_tac : T.Tele.tac) (tele_tac : T.Var.tac list -> T.Tele.tac) : T.Tele.tac =
T.Tele.rule ~name:"Tele.include" @@
let* inc = T.Tele.run inc_tac in
let inc = S.rename_tele rename inc in
let* vinc = RM.lift_ev @@ Sem.eval_tele inc in
T.abstract_tele vinc @@ fun vars ->
let+ tele = T.Tele.run (tele_tac vars) in
S.append_tele inc tele
let el (tac : T.KanTele.tac) : T.Tele.tac =
T.Tele.rule ~name:"Tele.el" @@
let+ tele = T.KanTele.run tac D.Univ in
S.ElTele tele
end
module KanTelescope =
struct
let empty : T.KanTele.tac =
T.KanTele.rule ~name:"KanTele.empty" @@ fun _ ->
RM.ret @@ S.KEmpty
let cell code_tac (ident, tele_tac) : T.KanTele.tac =
T.KanTele.rule ~name:"KanTele.cell" @@ fun univ ->
let* code = T.Chk.run code_tac univ in
let* vcode = RM.eval code in
let* tp = RM.lift_cmp @@ Sem.do_el vcode in
T.abstract ~ident tp @@ fun var ->
let+ tele = T.KanTele.run (tele_tac var) univ in
S.KCell (ident, code, tele)
let include_ (rename : Ident.t -> Ident.t option) (inc_tac : T.KanTele.tac) (tele_tac : T.Var.tac list -> T.KanTele.tac) : T.KanTele.tac =
T.KanTele.rule ~name:"KanTele.include" @@ fun univ ->
let* inc = T.KanTele.run inc_tac univ in
let inc = S.rename_kan_tele rename inc in
let* vinc = RM.lift_ev @@ Sem.eval_kan_tele inc in
T.abstract_kan_tele vinc @@ fun vars ->
let+ tele = T.KanTele.run (tele_tac vars) univ in
S.append_kan_tele inc tele
end
module Signature =
struct
let formation (tele_tac : T.Tele.tac) : T.Tp.tac =
T.Tp.rule ~name:"Signature.formation" @@
let+ tele = T.Tele.run tele_tac
in S.Signature tele
(* Check that [tele0] is a prefix of [tele1], and return the rest of [tele1]
along with the quoted eta-expansion of [con], which has type [sign0]
*)
let equate_tele_prefix tele0 tele1 con ~renaming =
let rec go n projs tele0 tele1 =
match tele0, tele1 with
| D.Empty, _ ->
RM.ret (Bwd.to_list projs, tele1)
(* Matching fields *)
| D.Cell (lbl0, tp0, clo0), D.Cell (lbl1, tp1, clo1) when Ident.equal (Option.value ~default:lbl0 (renaming lbl0)) lbl1 ->
let* () = RM.equate_tp tp0 tp1 in
let* proj = RM.lift_cmp @@ Sem.do_proj con lbl0 n in
let* qproj = RM.quote_con tp0 proj in
let* tele0 = RM.lift_cmp @@ Sem.inst_tele_clo clo0 proj in
let* tele1 = RM.lift_cmp @@ Sem.inst_tele_clo clo1 proj in
go (n + 1) (projs <: (lbl0, qproj)) tele0 tele1
(* Extra field in included sign *)
| D.Cell (lbl0, _, clo0), _ ->
let* proj = RM.lift_cmp @@ Sem.do_proj con lbl0 n in
let* tele0 = RM.lift_cmp @@ Sem.inst_tele_clo clo0 proj in
go (n + 1) projs tele0 tele1
in go 0 Emp tele0 tele1
let intro_fields phi phi_clo (tele : D.tele) (tacs : [`Field of Ident.t * T.Chk.tac | `Include of T.Syn.tac * (Ident.t -> Ident.t option)] list) : S.fields m =
let rec go fields n tele tacs =
match tele, tacs with
(* The sig and struct labels line up, run the tactic and add the field *)
| D.Cell (lbl0, tp, clo), `Field (lbl1, tac) :: tacs when Ident.equal lbl0 lbl1 ->
let* tfield = T.Chk.brun tac (tp, phi, D.un_lam @@ D.compose (D.proj lbl0 n) @@ D.Lam (Ident.anon, phi_clo)) in
let* vfield = RM.lift_ev @@ Sem.eval tfield in
let* tele = RM.lift_cmp @@ Sem.inst_tele_clo clo vfield in
go (fields <: (lbl0, tfield)) (n + 1) tele tacs
(* The labels do not line up.
If the sig field is a nullary extension type then we fill it automatically
and continue with our list of struct tactics unchanged.
Otherwise we ignore the extra field
*)
| D.Cell (lbl, tp, clo), (`Field _ :: tacs as all_tacs) ->
Univ.is_nullary_ext tp |>> begin function
| true ->
let* tfield = T.Chk.brun Univ.infer_nullary_ext (tp, phi, D.un_lam @@ D.compose (D.proj lbl n) @@ D.Lam (Ident.anon, phi_clo)) in
let* vfield = RM.lift_ev @@ Sem.eval tfield in
let* tele = RM.lift_cmp @@ Sem.inst_tele_clo clo vfield in
go (fields <: (lbl, tfield)) (n + 1) tele all_tacs
| false ->
go fields n tele tacs
end
(* There are no more struct tactics.
If the sig field is a nullary extension type then we fill it automatically.
Otherwise the field is missing, so we unleash a hole for it
*)
| D.Cell (lbl,tp, clo), [] ->
let* tac = Univ.is_nullary_ext tp |>> function
| true -> RM.ret Univ.infer_nullary_ext
| false -> RM.ret @@ Hole.unleash_hole @@ Ident.to_string_opt lbl
in
let* tfield = T.Chk.brun tac (tp, phi, D.un_lam @@ D.compose (D.proj lbl n) @@ D.Lam (Ident.anon, phi_clo)) in
let* vfield = RM.lift_ev @@ Sem.eval tfield in
let* tele = RM.lift_cmp @@ Sem.inst_tele_clo clo vfield in
go (fields <: (lbl, tfield)) (n + 1) tele []
(* Including the fields of another struct *)
| tele, `Include (tac,renaming) :: tacs ->
let* tm,tp = T.Syn.run tac in
RM.lift_cmp @@ Sem.whnf_tp_ tp |>> begin function
| D.Signature inc_sign ->
let* vtm = RM.lift_ev @@ Sem.eval tm in
let* inc_fields, tele = equate_tele_prefix ~renaming inc_sign tele vtm in
go (Bwd.append fields inc_fields) (n + List.length inc_fields) tele tacs
| _ ->
RM.with_pp @@ fun ppenv ->
RM.refine_err @@ Err.ExpectedStructure (ppenv, tm)
end
(* There are no extra fields, we're done *)
| D.Empty, `Field _ :: _
| D.Empty,[] ->
RM.ret @@ Bwd.to_list fields
in
let+ fields = go Emp 0 tele tacs in
S.Fields fields
let intro (tacs : [`Field of Ident.t * T.Chk.tac | `Include of T.Syn.tac * (Ident.t -> Ident.t option)] list) : T.Chk.tac =
T.Chk.brule ~name:"Signature.intro" @@
function
| (D.Signature sign, phi, phi_clo) ->
let+ fields = intro_fields phi phi_clo sign tacs in
S.Struct fields
| (tp, _, _) -> RM.expected_connective `Signature tp
let proj_tp (tele : D.tele) (tstruct : S.t) (lbl : Ident.t) : (D.tp * int) m =
let rec go n =
function
| D.Cell (flbl, tp, _) when Ident.equal flbl lbl -> RM.ret (tp, n)
| D.Cell (flbl, __, clo) ->
let* vfield = RM.lift_ev @@ Sem.eval @@ S.Proj (tstruct, flbl, n) in
let* tele = RM.lift_cmp @@ Sem.inst_tele_clo clo vfield in
go (n + 1) tele
| D.Empty -> RM.expected_field tele tstruct lbl
in go 0 tele
let proj tac lbl : T.Syn.tac =
T.Syn.rule ~name:"Signature.proj" @@
let* tstruct, tp = T.Syn.run tac in
match tp with
| D.Signature sign ->
let+ (tp, n) = proj_tp sign tstruct lbl in
S.Proj (tstruct, lbl, n), tp
| _ -> RM.expected_connective `Signature tp
end
module El =
struct
let formation tac =
T.Tp.rule ~name:"El.formation" @@
let+ tm = T.Chk.run tac D.Univ in
S.El tm
let dest_el tp =
match tp with
| D.ElStable con ->
RM.lift_cmp @@ Sem.unfold_el con
| _ ->
RM.expected_connective `El tp
let intro tac =
T.Chk.brule ~name:"El.intro" @@ fun (tp, phi, clo) ->
let* unfolded = dest_el tp in
let+ tm = T.Chk.brun tac (unfolded, phi, D.un_lam @@ D.compose D.el_out @@ D.Lam (Ident.anon, clo)) in