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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
(** {1 Unification and Matching} *)
module T = InnerTerm
module S = Subst
module US = Unif_subst
exception Fail
type unif_subst = Unif_subst.t
type subst = Subst.t
type term = InnerTerm.t
type ty = InnerTerm.t
type 'a sequence = ('a -> unit) -> unit
let section = Util.Section.make "unif"
let prof_unify = Util.mk_profiler "unify"
let prof_matching = Util.mk_profiler "matching"
let fail () = raise Fail
(** {2 Signatures} *)
module type S = Unif_intf.S
(** {2 Base (scoped terms)} *)
let occurs_check ~depth subst (v,sc_v) t =
let rec check ~depth (t,sc_t) = match T.ty t with
| T.NoType -> false
| T.HasType ty ->
check ~depth (ty, sc_t) ||
match T.view t with
| T.Var v' ->
(HVar.equal T.equal v v' && sc_v = sc_t)
||
begin match Subst.find subst (v',sc_t) with
| None -> false
| Some t' -> check ~depth t'
end
| T.DB i -> i>=depth
| T.Const _ -> false
| T.Bind (_, varty, t') ->
check ~depth (varty,sc_t) ||
check ~depth:(depth+1) (t',sc_t)
| T.AppBuiltin (_, l) -> check_l ~depth l sc_t
| T.App (hd, l) ->
check ~depth (hd,sc_t) ||
check_l ~depth l sc_t
and check_l ~depth l sc = match l with
| [] -> false
| [t] -> check ~depth (t,sc)
| t :: tail -> check ~depth (t,sc) || check_l ~depth tail sc
in
check ~depth t
let unif_array_com ?(size=`Same) subst ~op (a1,sc1) (a2,sc2) k =
let module BV = CCBV in
let rec iter2 subst bv i =
if i = Array.length a1
then k subst
else iter3 subst bv i 0
and iter3 subst bv i j =
if j = Array.length a2
then ()
else (
if not (BV.get bv j) then (
BV.set bv j;
op subst (a1.(i),sc1) (a2.(j),sc2)
(fun subst -> iter2 subst bv (i+1));
BV.reset bv j
);
iter3 subst bv i (j+1)
)
in
let size_ok = match size with
| `Same -> Array.length a1 = Array.length a2
| `Smaller -> Array.length a1 <= Array.length a2
in
if size_ok then (
let bv = BV.create ~size:(Array.length a1) false in
iter2 subst bv 0
)
let unif_list_com ?size subst ~op (l1,sc1) (l2,sc2) =
unif_array_com ?size subst ~op (Array.of_list l1,sc1) (Array.of_list l2,sc2)
let rec unif_list subst ~op (l1,sc1) (l2,sc2) k = match l1, l2 with
| [], [] -> k subst
| [], _ | _, [] -> ()
| x1 :: tail1, x2 :: tail2 ->
op subst (x1,sc1) (x2,sc2)
(fun subst -> unif_list subst ~op (tail1,sc1)(tail2,sc2) k)
let pair_lists_right f1 l1 f2 l2 : _ list * _ list =
let len1 = List.length l1 and len2 = List.length l2 in
if len1 = len2 then f1::l1, f2::l2
else if len1 < len2
then
let l2_1, l2_2 = CCList.take_drop (len2 - len1) l2 in
let f2' = T.app ~ty:(T.ty_exn f1) f2 l2_1 in
f1 :: l1, f2' :: l2_2
else
let l1_1, l1_2 = CCList.take_drop (len1 - len2) l1 in
(T.app ~ty:(T.ty_exn f2) f1 l1_1) :: l1_2, f2 :: l2
let pair_lists_left l1 ret1 l2 ret2 : _ list * _ list =
let len1 = List.length l1 and len2 = List.length l2 in
if len1 = len2 then ret1::l1, ret2::l2
else if len1 < len2
then (
let l2_1, l2_2 = CCList.take_drop len1 l2 in
let ret2' = T.arrow l2_2 ret2 in
ret1 :: l1, ret2' :: l2_1
) else (
let l1_1, l1_2 = CCList.take_drop len2 l1 in
let ret1' = T.arrow l1_2 ret1 in
ret1' :: l1_1, ret2 :: l2
)
(** During matching or variant checking, we need to {b protect} some variables
from being bound.
Typically, when matching [u] against pattern [t], we can bind variables
from [t] but not from [u] (since [u] must be preserved).
Two styles of protection exist:
- by explicit set of variables (when both [t] and [u] live in the
same scope)
- by scope: all variables in this scope are protected, EXCEPT
fresh variables. Fresh variables are not protected because
pattern unification/matching will move both terms into
the same (protected) scope, but we still need to bind
(freshly renamed) variables from the pattern.
*)
type protected =
| P_vars of T.VarSet.t
| P_scope of int
let pp_protected out = function
| P_scope c ->
Format.fprintf out "protect[%d]" c
| P_vars s ->
Format.fprintf out "protect{@[%a@]}@])" (T.VarSet.pp HVar.pp) s
(** The various operations to perform.
- unification
- matching (with some variables protected, see {!protected})
- variant checking (equality modulo renaming)
- equality (modulo some substitution)
*)
type op =
| O_unify
| O_match_protect of protected
| O_variant of protected
| O_equal
let pp_op out = function
| O_unify -> CCFormat.string out "unify"
| O_equal -> CCFormat.string out "equal"
| O_variant p -> Format.fprintf out "(@[variant %a@])" pp_protected p
| O_match_protect p -> Format.fprintf out "(@[match %a@])" pp_protected p
(** {2 Unary Unification} *)
module Inner = struct
type ty = T.t
type term = T.t
let bind ?(check=true) subst v t =
if check && occurs_check ~depth:0 subst v t
then fail()
else if S.mem subst v then fail()
else S.bind subst v t
let update ?(check=true) subst v t =
if check && occurs_check ~depth:0 subst v t
then fail()
else if not (S.mem subst v) then fail()
else S.update subst v t
let has_non_unifiable_type_or_is_prop (t:T.t): bool = match T.ty t with
| T.NoType -> false
| T.HasType ty -> T.type_is_prop ty || not (T.type_is_unifiable ty)
let restrict_to_scope subst(t,sc_t) ~into:scope =
let rec aux sc_t subst t : US.t * term = match T.ty t with
| T.NoType -> subst, t
| T.HasType ty ->
let subst, ty = aux sc_t subst ty in
begin match T.view t with
| T.Var v ->
begin match Subst.find (US.subst subst) (v,sc_t) with
| Some (u,sc_u) ->
if sc_t = scope
then
let subst, u' = aux sc_u subst u in
let subst = US.update subst (v,scope) (u', scope) in
subst, T.var v
else if T.is_var u && sc_u = scope
then
subst, u
else (
let v' = HVar.fresh ~ty () in
let subst, u' = aux sc_u subst u in
let subst = US.update subst (v,sc_t) (T.var v', scope) in
let subst = US.bind subst (v',scope) (u', scope) in
subst, T.var v'
)
| None ->
if sc_t = scope
then subst, T.var (HVar.cast ~ty v)
else (
let v' = HVar.fresh ~ty () in
US.bind subst (v,sc_t) (T.var v', scope), T.var v'
)
end
| T.App (f, l) ->
let subst, f = aux sc_t subst f in
let subst, l = CCList.fold_map (aux sc_t) subst l in
subst, T.app ~ty f l
| T.AppBuiltin (b,l) ->
let subst, l = CCList.fold_map (aux sc_t) subst l in
subst, T.app_builtin ~ty b l
| T.DB i -> subst, T.bvar ~ty i
| T.Const id -> subst, T.const ~ty id
| T.Bind (b, tyvar, body) ->
let subst, varty = aux sc_t subst tyvar in
let subst, body = aux sc_t subst body in
subst, T.bind ~ty ~varty b body
end
in
aux sc_t subst t
let rec whnf_deref_rec (subst:US.t) (t,sc_t) : US.t * T.t =
begin match T.view t with
| T.Var _ ->
let u, sc_u = US.deref subst (t,sc_t) in
assert (sc_t=sc_u || T.is_ground u);
if T.equal t u then subst, u
else whnf_deref_rec subst (u,sc_t)
| T.App (f0, l) ->
let subst, f = whnf_deref_rec subst (f0,sc_t) in
let t =
if T.equal f0 f then t else T.app ~ty:(T.ty_exn t) f l
in
let u = Lambda.Inner.whnf t in
if T.equal t u
then subst, t
else whnf_deref_rec subst (u,sc_t)
| _ -> subst, t
end
let whnf_deref subst t =
let subst, u = whnf_deref_rec subst t in
subst, u
module B_vars : sig
type t = private {
left: T.t DBEnv.t;
right: T.t DBEnv.t;
}
val empty : t
val make : T.t DBEnv.t -> T.t DBEnv.t -> t
val pp : t CCFormat.printer
end = struct
type t = {
left: T.t DBEnv.t;
right: T.t DBEnv.t;
}
let make left right =
assert (DBEnv.size left = DBEnv.size right);
{left; right}
let empty : t = make DBEnv.empty DBEnv.empty
let pp out (b:t) =
Format.fprintf out "{@[@[%a@] |@ @[%a@]@]}"
(DBEnv.pp T.pp) b.left
(DBEnv.pp T.pp) b.right
end
let distinct_term_l l : bool =
List.length l = (T.Set.of_list l |> T.Set.cardinal)
let distinct_bvar_l ~bvars l : bool =
let n = DBEnv.size bvars in
List.for_all
(fun t -> match T.view t with
| T.DB i -> i < n
| _ -> false)
l &&
distinct_term_l l
let distinct_ground_l l : bool =
List.for_all (fun t -> T.is_ground t ) l
&& distinct_term_l l
let lift_terms (l:T.t list) (rhs:T.t) : T.t =
assert (List.for_all T.is_ground l);
let vars =
List.map (fun t -> HVar.fresh ~ty:(T.ty_exn t) ()) l
in
let body =
let m =
List.map2 (fun t v -> t, T.var v) l vars
|> T.Map.of_list
in
T.replace_m rhs m
in
let res = T.fun_of_fvars vars body in
res
let env_l_dense (e:'a DBEnv.t) : 'a list =
DBEnv.to_list e
|> List.map
(function | Some x -> x | None -> assert false)
let fun_of_bvars ~bvars (l:T.t list) (t:T.t) : T.t =
assert (List.for_all T.is_bvar l);
let n = List.length l in
let env =
DBEnv.to_list_i bvars
|> CCList.filter_map
(function
| None -> None
| Some (i, _) -> match CCList.find_idx (T.is_bvar_i i) l with
| None -> None
| Some (j, t_bvar) ->
let ty = T.ty_exn t_bvar in
Some (i, T.bvar ~ty (n-j-1)))
|> DBEnv.of_list
in
T.DB.eval env t
|> T.fun_l (List.map T.ty_exn l)
let restrict_fun1
= fun subst ~op ~ty ~to_:subset ~scope (v,args) ->
assert (not (US.mem subst (v,scope)));
let n_old = List.length args in
let args =
List.filter
(fun t -> match T.view t with
| T.DB i -> DBEnv.mem subset i
| _ -> assert false)
args
in
let n = List.length args in
match op with
| O_match_protect _ ->
if n = n_old then subst
else fail()
| _ ->
let ty_fun = T.arrow (List.map T.ty_exn args) ty in
let f = HVar.fresh ~ty:ty_fun () in
let rhs =
T.app ~ty
(T.var f)
(List.mapi (fun i a -> T.bvar ~ty:(T.ty_exn a) (n-i-1)) args)
|> T.fun_l (List.map T.ty_exn args)
in
US.bind subst (v,scope) (rhs,scope)
let is_match_op = function O_match_protect _ -> true | _ -> false
let restrict_fun2
: unif_subst -> ty_ret:T.t -> bvars:B_vars.t -> scope:Scoped.scope ->
_ -> _ -> unif_subst
= fun subst ~ty_ret ~bvars ~scope (v1,l1) (v2,l2) ->
assert (not (HVar.equal T.equal v1 v2));
assert (not (US.mem subst (v1,scope)));
assert (not (US.mem subst (v2,scope)));
let inter =
List.filter
(fun t -> match T.view t with
| T.DB i ->
DBEnv.mem bvars.B_vars.left i &&
List.exists (T.is_bvar_i i) l2
| _ -> assert false)
l1
in
let ty_fun = T.arrow (List.map T.ty_exn inter) ty_ret in
let f = HVar.fresh ~ty:ty_fun () in
let mk_rhs l =
let n = List.length l in
let args =
List.map
(fun a ->
let i = CCList.find_idx (T.equal a) l |> CCOpt.get_exn |> fst in
T.bvar ~ty:(T.ty_exn a) (n-i-1))
inter
in
let body = T.app ~ty:ty_ret (T.var f) args in
T.fun_l (List.map T.ty_exn l) body
in
let rhs1 = mk_rhs l1 in
let rhs2 = mk_rhs l2 in
let subst = US.bind subst (v1,scope) (rhs1,scope) in
let subst = US.bind subst (v2,scope) (rhs2,scope) in
subst
let delay ~bvars ~tags subst t1 sc1 t2 sc2 =
if T.equal t1 t2 && sc1=sc2 then subst
else (
let u1 = T.fun_l (env_l_dense bvars.B_vars.left |> List.rev) t1 in
let u2 = T.fun_l (env_l_dense bvars.B_vars.right |> List.rev) t2 in
if T.DB.closed u1 && T.DB.closed u2 then (
US.add_constr (Unif_constr.make ~tags (u1,sc1)(u2,sc2)) subst
) else (
fail()
)
)
let rec unif_rec ~op ~root ~bvars subst t1s t2s : unif_subst =
let t1,sc1 = US.deref subst t1s
and t2,sc2 = US.deref subst t2s in
begin match T.ty t1, T.ty t2 with
| T.NoType, T.NoType ->
assert (t1 == t2 && t1 == T.tType);
subst
| T.NoType, _
| _, T.NoType -> fail()
| T.HasType ty1, T.HasType ty2 ->
let subst = unif_rec ~op ~root:true ~bvars subst (ty1,sc1) (ty2,sc2) in
unif_term ~op ~root ~bvars subst t1 sc1 t2 sc2
end
and unif_term ~op ~root ~bvars subst t1 sc1 t2 sc2 : unif_subst =
let view1 = T.view t1 and view2 = T.view t2 in
let delay() = delay ~bvars subst t1 sc1 t2 sc2 in
let is_whnf t sc_t = match T.view t with
| T.App (f, _) ->
not (T.is_lambda f) &&
not (T.is_var f && US.mem subst (T.as_var_exn f,sc_t))
| _ -> true
in
begin match view1, view2 with
| _ when sc1=sc2 && T.equal t1 t2 ->
subst
| T.Var _, _ when is_whnf t2 sc2 ->
Util.debugf ~section 40 "unifying vars: %a =?= %a" (fun k-> k T.pp t1 T.pp t2);
unif_vars ~op subst t1 sc1 t2 sc2
| _, T.Var _ when is_whnf t1 sc1 ->
unif_vars ~op subst t1 sc1 t2 sc2
| T.DB i, T.DB j -> if i = j then subst else raise Fail
| T.Const f, T.Const g ->
if ID.equal f g
then subst
else if op=O_unify && not root && has_non_unifiable_type_or_is_prop t1
then (
let tags = T.type_non_unifiable_tags (T.ty_exn t1) in
US.add_constr (Unif_constr.make ~tags (t1,sc1)(t2,sc2)) subst
)
else raise Fail
| T.App ({T.term=T.Const id1; _}, l1),
T.App ({T.term=T.Const id2; _}, l2) ->
if ID.equal id1 id2 &&
List.length l1 = List.length l2
then (
unif_list ~op ~bvars subst l1 sc1 l2 sc2
) else if op=O_unify && not root && has_non_unifiable_type_or_is_prop t1 then (
let tags = T.type_non_unifiable_tags (T.ty_exn t1) in
delay ~tags ()
) else fail()
| T.App ({T.term=(T.Var _ | T.DB _ | T.Bind (Binder.Lambda, _, _)); _}, _), _
| _, T.App ({T.term=(T.Var _ | T.DB _ | T.Bind (Binder.Lambda, _, _)); _}, _)
| T.Bind (Binder.Lambda, _, _), _
| _, T.Bind (Binder.Lambda, _, _) ->
Util.debugf ~section 40 "var_ho-unifying: %a =?= %a" (fun k-> k T.pp t1 T.pp t2);
begin match op with
| O_match_protect (P_scope sc2') | O_variant (P_scope sc2') ->
assert (sc2=sc2');
if sc1=sc2' then (
Util.debug ~section 40 "no renaming needed *";
unif_ho ~op ~root ~bvars subst t1 t2 ~scope:sc2
) else (
let subst, t1 = restrict_to_scope subst (t1,sc1) ~into:sc2 in
Util.debug ~section 40 "restricting scope *";
unif_ho ~op ~root ~bvars subst t1 t2 ~scope:sc2
)
| O_match_protect (P_vars _) | O_variant (P_vars _) | O_equal ->
Util.debug ~section 40 "protected with set *";
let subst, t1 = restrict_to_scope subst (t1,sc1) ~into:sc2 in
unif_ho ~op ~root ~bvars subst t1 t2 ~scope:sc2
| O_unify ->
let subst, t1 = restrict_to_scope subst (t1,sc1) ~into:sc2 in
let subst, t2 = restrict_to_scope subst (t2,sc2) ~into:sc2 in
unif_ho ~op ~root ~bvars subst t1 t2 ~scope:sc2
end
| T.AppBuiltin (Builtin.Arrow, ret1::args1),
T.AppBuiltin (Builtin.Arrow, ret2::args2) ->
let l1, l2 = pair_lists_left args1 ret1 args2 ret2 in
unif_list ~op ~bvars subst l1 sc1 l2 sc2
| T.Bind ((Binder.Forall | Binder.Exists | Binder.ForallTy) as b1, varty1, t1'),
T.Bind (b2, varty2, t2') when b1=b2 ->
let subst =
unif_rec ~op ~root:true ~bvars subst (varty1,sc1) (varty2,sc2)
in
unif_rec ~op ~root:false ~bvars
subst (t1',sc1) (t2',sc2)
| T.Bind ((Binder.Forall | Binder.Exists), _, _), _
| _, T.Bind ((Binder.Forall | Binder.Exists), _, _) ->
delay ~tags:[] ()
| T.AppBuiltin (Builtin.Int n1,[]),
T.AppBuiltin (Builtin.Int n2,[]) ->
if Z.equal n1 n2 then subst else raise Fail
| T.AppBuiltin (Builtin.Rat n1,[]),
T.AppBuiltin (Builtin.Rat n2,[]) ->
if Q.equal n1 n2 then subst else raise Fail
| T.AppBuiltin (Builtin.True, _), _
| T.AppBuiltin (Builtin.False, _), _ ->
if T.equal t1 t2 then subst else raise Fail
| _ when op=O_unify && not root && has_non_unifiable_type_or_is_prop t1 ->
let tags = T.type_non_unifiable_tags (T.ty_exn t1) in
delay ~tags ()
| T.AppBuiltin (s1,l1), T.AppBuiltin (s2, l2) when
Builtin.equal s1 s2 ->
unif_list ~op ~bvars subst l1 sc1 l2 sc2
| _, _ -> raise Fail
end
and unif_vars ~op subst t1 sc1 t2 sc2 : unif_subst =
let t1,t2 = CCPair.map_same (fun t -> ((Lambda.eta_reduce t) :> InnerTerm.t)) (Term.of_term_unsafe t1,Term.of_term_unsafe t2) in
begin match T.view t1, T.view t2, op with
| T.Var v1, T.Var v2, O_equal ->
if HVar.equal T.equal v1 v2 && sc1=sc2
then subst else fail()
| T.Var v1, T.Var v2, _
when HVar.equal T.equal v1 v2 && sc1=sc2 -> subst
| T.Var v1, _, O_match_protect (P_vars s) when T.VarSet.mem v1 s ->
assert (sc1=sc2);
fail()
| T.Var v1, _, O_match_protect (P_scope sc)
when sc1 = sc && not (HVar.is_fresh v1) ->
fail()
| T.Var v1, _, (O_unify | O_match_protect _) ->
if occurs_check ~depth:0 (US.subst subst) (v1,sc1) (t2,sc2)
then fail ()
else (
if US.mem subst (v1,sc1) then (
let derefed = CCOpt.get_exn @@ Subst.get_var (US.subst subst) (v1,sc1) in
let derefed = Scoped.map ((fun t -> ((Lambda.eta_reduce (Term.of_term_unsafe t)) :> InnerTerm.t))) derefed in
if snd @@ derefed == sc2 && T.equal (fst @@ derefed) t2 then subst else fail()
) else US.bind subst (v1,sc1) (t2,sc2)
)
| T.Var v1, T.Var _, O_variant (P_vars s) when not (T.VarSet.mem v1 s) ->
US.bind subst (v1,sc1) (t2,sc2)
| T.Var v1, T.Var _, O_variant (P_scope sc')
when sc1<>sc' || HVar.is_fresh v1 ->
US.bind subst (v1,sc1) (t2,sc2)
| _, T.Var v2, O_unify ->
if occurs_check ~depth:0 (US.subst subst) (v2,sc2) (t1,sc1)
then fail()
else (
if US.mem subst (v2,sc2) then (
let derefed = CCOpt.get_exn @@ Subst.get_var (US.subst subst) (v2,sc2) in
let derefed = Scoped.map ((fun t -> ((Lambda.eta_reduce (Term.of_term_unsafe t)) :> InnerTerm.t))) derefed in
if snd @@ derefed == sc1 && T.equal (fst @@ derefed) t1 then subst else fail()
) else US.bind subst (v2,sc2) (t1,sc1)
)
| _ ->
fail ()
end
and unif_list ~op ~bvars subst l1 sc1 l2 sc2 : unif_subst = match l1, l2 with
| [], [] -> subst
| _, []
| [], _ -> fail ()
| t1::l1', t2::l2' ->
let subst = unif_rec ~op ~root:false ~bvars subst (t1,sc1) (t2,sc2) in
unif_list ~op ~bvars subst l1' sc1 l2' sc2
and unif_ho ~op ~root ~bvars subst t1_0 t2_0 ~scope : unif_subst =
let subst, t1 = whnf_deref subst (t1_0,scope) in
let subst, t2 = whnf_deref subst (t2_0,scope) in
Util.debugf ~section 20
"(@[unif_ho@ :t1 `%a`@ :t1_nf `%a`@ :t2 `%a`@ :t2_nf `%a`@ \
:sc %d :subst %a@ :op %a@ :bvars %a@])@."
(fun k -> k T.pp t1_0 T.pp t1 T.pp t2_0 T.pp t2 scope US.pp subst pp_op op B_vars.pp bvars);
let f1, l1 = T.as_app t1 in
let f2, l2 = T.as_app t2 in
let delay() =
Util.debug ~section 5 "delaying\n";
delay ~bvars subst t1 scope t2 scope in
let same_rigid_head() =
if List.length l1 = List.length l2
then (
unif_list ~op ~bvars subst l1 scope l2 scope
) else if op=O_unify && not root && has_non_unifiable_type_or_is_prop t1 then (
let tags = T.type_non_unifiable_tags (T.ty_exn t1) in
delay ~tags ()
) else fail()
in
begin match T.view f1, T.view f2 with
| _ when T.equal f1 f2 -> same_rigid_head()
| T.Bind (Binder.Lambda, _, _),
T.Bind (Binder.Lambda, _, _) ->
assert (l1=[] && l2=[]);
let new_vars1, f1, new_vars2, f2 =
T.open_bind2 Binder.Lambda f1 f2
in
unif_rec ~op ~root:false
~bvars:(B_vars.make
(DBEnv.push_l_rev bvars.B_vars.left new_vars1)
(DBEnv.push_l_rev bvars.B_vars.right new_vars2))
subst (f1,scope) (f2,scope)
| T.Bind (Binder.Lambda, _, _), _ ->
assert (l1=[]);
let new_vars, f1 = T.open_bind Binder.Lambda f1 in
let n = List.length new_vars in
unif_rec ~op ~root
~bvars:(B_vars.make
(DBEnv.push_l_rev bvars.B_vars.left new_vars)
(DBEnv.push_l_rev bvars.B_vars.right new_vars))
subst
(f1,scope)
(T.app ~ty:(T.ty_exn f1)
(T.DB.shift n t2)
(List.mapi (fun i ty->T.bvar ~ty (n-i-1)) new_vars), scope)
| _, T.Bind (Binder.Lambda, _, _) ->
assert (l2=[]);
let new_vars, f2 = T.open_bind Binder.Lambda f2 in
let n = List.length new_vars in
unif_rec ~op ~root
~bvars:(B_vars.make
(DBEnv.push_l_rev bvars.B_vars.left new_vars)
(DBEnv.push_l_rev bvars.B_vars.right new_vars))
subst
(T.app ~ty:(T.ty_exn f2)
(T.DB.shift n t1)
(List.mapi (fun i ty -> T.bvar ~ty (n-i-1)) new_vars), scope)
(f2,scope)
| T.Const id1, T.Const id2 ->
if ID.equal id1 id2 then same_rigid_head()
else if op=O_unify && not root && has_non_unifiable_type_or_is_prop t1
then (
let tags = T.type_non_unifiable_tags (T.ty_exn t1) in
delay ~tags ()
)
else fail()
| T.DB i1, T.DB i2 ->
if i1=i2 then same_rigid_head() else fail()
| T.Var _, _ when l1=[] ->
unif_rec ~op ~bvars ~root subst (t1,scope) (t2, scope)
| _, T.Var _ when l2=[] ->
unif_rec ~op ~bvars ~root subst (t1,scope) (t2, scope)
| T.Var v1, T.Const _ ->
begin match op with
| O_match_protect (P_scope sc2')
when sc2' = scope && not (HVar.is_fresh v1) -> fail()
| O_match_protect (P_vars s) when T.VarSet.mem v1 s -> fail()
| O_unify | O_match_protect _ -> ()
| O_variant _ | O_equal -> fail()
end;
if distinct_bvar_l ~bvars:bvars.B_vars.left l1
&& CCList.subset ~eq:(=) (T.DB.unbound t2) (List.map T.as_bvar_exn l1)
then (
flex_rigid ~op ~bvars:bvars.B_vars.left subst f1 l1 t2 ~scope
) else if distinct_ground_l l1 then (
let t2 = lift_terms l1 t2 in
unif_rec ~op ~root ~bvars subst (f1,scope) (t2,scope)
) else if l2<>[] then (
let l1, l2 = pair_lists_right f1 l1 f2 l2 in
assert (T.expected_ty_vars (HVar.ty v1) = 0);
if T.expected_ty_vars (T.ty_exn (List.hd l2)) != 0 then fail();
unif_list ~op ~bvars subst l1 scope l2 scope
) else fail()
| T.Const _, T.Var v2 ->
if distinct_bvar_l ~bvars:bvars.B_vars.right l2
&& CCList.subset ~eq:(=) (T.DB.unbound t1) (List.map T.as_bvar_exn l2)
&& op=O_unify
then (
flex_rigid ~op ~bvars:bvars.B_vars.right subst f2 l2 t1 ~scope
) else if distinct_ground_l l2 && op=O_unify then (
let t1 = lift_terms l2 t1 in
unif_rec ~op ~root ~bvars subst (t1,scope) (f2,scope)
) else if l1<>[] then (
let l1, l2 = pair_lists_right f1 l1 f2 l2 in
assert (T.expected_ty_vars (HVar.ty v2) = 0);
if T.expected_ty_vars (T.ty_exn (List.hd l1)) != 0 then fail();
unif_list ~op ~bvars subst l1 scope l2 scope
) else fail()
| T.Var v1, T.Var v2
when op=O_unify &&
distinct_bvar_l ~bvars:bvars.B_vars.left l1 &&
distinct_bvar_l ~bvars:bvars.B_vars.right l2 ->
flex_flex_unif subst ~bvars ~ty_ret:(T.ty_exn t1) ~scope
v1 l1 v2 l2
| T.Var v1, T.Var v2
when is_match_op op &&
distinct_bvar_l ~bvars:bvars.B_vars.left l1 &&
distinct_bvar_l ~bvars:bvars.B_vars.right l2 &&
CCList.subset ~eq:T.equal l2 l1 ->
flex_flex_matching ~op subst ~bvars ~ty_ret:(T.ty_exn t2) ~scope v1 l1 v2 l2
| T.Var _, T.Var _ ->
let l1, l2 = pair_lists_right f1 l1 f2 l2 in
unif_list ~op ~bvars subst l1 scope l2 scope
| _ -> fail()
end
and flex_rigid ~bvars ~op subst f1 l1 t2 ~scope : unif_subst =
Util.debugf ~section 5
"(@[flex_rigid@ :subst %a@ `@[%a %a@]`@ :rhs `%a`@ :bvars %a@])"
(fun k->k US.pp subst T.pp f1 (Util.pp_list T.pp) l1 T.pp t2
(DBEnv.pp T.pp) bvars);
assert (l1<>[]);
assert (List.for_all T.is_bvar l1);
assert (T.is_var f1);
let rhs = fun_of_bvars ~bvars l1 t2 in
let subst =
unif_rec ~op ~root:true ~bvars:B_vars.empty
subst (f1,scope) (rhs,scope)
in
Util.debugf ~section 5 "(@[flex_rigid_bind@ :subst %a@])" (fun k->k US.pp subst);
Util.debugf ~section 5 "(@[proj@ :bvars %a@ :in `%a`@])"
(fun k->k (DBEnv.pp T.pp) bvars T.pp t2);
proj_fun ~bvars ~op subst (t2,scope)
and proj_fun ~op ~bvars subst (t,sc_t) : unif_subst =
let subst, t = whnf_deref subst (t,sc_t) in
let f, l = T.as_app t in
begin match T.view f with
| T.Const _ -> proj_fun_l ~ op ~bvars subst (l,sc_t)
| T.Bind (b, _, _) ->
assert (l=[]);
let new_vars, body = T.open_bind b f in
proj_fun ~op ~bvars:(DBEnv.push_l_rev bvars new_vars) subst (body,sc_t)
| T.App _ -> assert false
| T.AppBuiltin (_, l2) ->
proj_fun_l ~op ~bvars subst (l@l2,sc_t)
| T.DB i ->
if DBEnv.mem bvars i
then proj_fun_l ~op ~bvars subst (l,sc_t)
else fail()
| T.Var v ->
if l=[] then subst
else if List.for_all T.is_bvar l then (
restrict_fun1 ~op subst ~ty:(T.ty_exn t) ~to_:bvars ~scope:sc_t (v,l)
) else fail()
end
and proj_fun_l ~op ~bvars subst (l,sc) : unif_subst =
List.fold_left
(fun subst t -> proj_fun ~op ~bvars subst (t,sc))
subst l
and flex_flex_unif ~bvars subst ~ty_ret v1 l1 v2 l2 ~scope : unif_subst =
Util.debugf ~section 5
"(@[flex_flex@ `@[%a %a@]`[%d]@ `@[%a %a@]`[%d]@ :subst %a@ :bvars %a@])"
(fun k->k
HVar.pp v1 (Util.pp_list T.pp) l1 scope
HVar.pp v2 (Util.pp_list T.pp) l2 scope
US.pp subst B_vars.pp bvars);
let subst =
restrict_fun2 subst ~ty_ret ~bvars ~scope (v1,l1) (v2,l2)
in
subst
and flex_flex_matching ~op ~bvars subst ~ty_ret v1 l1 v2 l2 ~scope : unif_subst =
Util.debugf ~section 5
"(@[flex_flex_matching@ `@[%a %a@]`[%d]@ `@[%a %a@]`[%d]@ :subst %a@ :bvars %a@])"
(fun k->k HVar.pp v1 (Util.pp_list T.pp) l1 scope
HVar.pp v2 (Util.pp_list T.pp) l2 scope
US.pp subst B_vars.pp bvars);
begin match op with
| O_match_protect (P_scope sc')
when sc'=scope && not (HVar.is_fresh v1) -> fail()
| O_match_protect (P_vars s) when T.VarSet.mem v1 s -> fail()
| _ -> ()
end;
let n = List.length l1 in
let rhs =
T.app ~ty:ty_ret
(T.var v2)
(List.map
(fun a ->
let i = CCList.find_idx (T.equal a) l1 |> CCOpt.get_exn|>fst in
T.bvar ~ty:(T.ty_exn a) (n-i-1))
l2)
|> T.fun_l (List.map T.ty_exn l1)
in
let subst = US.bind subst (v1,scope) (rhs,scope) in
subst
and equal_ ~subst a b : bool =
try
let _ = unif_rec ~op:O_equal ~root:true ~bvars:B_vars.empty subst a b in
true
with Fail -> false
let unify_full ?(subst=US.empty) a b : unif_subst =
Util.with_prof prof_unify
(fun () -> unif_rec ~root:true ~op:O_unify ~bvars:B_vars.empty subst a b) ()
let unify_syn ?(subst=Subst.empty) a b : Subst.t =
let subst = US.of_subst subst in
let subst = unify_full ~subst a b in
if US.has_constr subst
then raise Fail
else US.subst subst
let matching ?(subst=Subst.empty) ~pattern b =
if Scoped.same_scope pattern b then invalid_arg "Unif.matching: same scopes";
let scope = Scoped.scope b in
Util.with_prof prof_matching
(fun () ->
let subst = US.of_subst subst in
let subst =
unif_rec subst pattern b
~root:true ~op:(O_match_protect (P_scope scope)) ~bvars:B_vars.empty
in
assert (not @@ US.has_constr subst);
US.subst subst)
()
let matching_same_scope
?(protect=Iter.empty) ?(subst=S.empty) ~scope ~pattern b =
let protect = Iter.append protect (T.Seq.vars b) in
let blocked = T.VarSet.of_seq protect in
Util.with_prof prof_matching
(fun () ->
let subst = US.of_subst subst in
let subst =
unif_rec
subst (Scoped.make pattern scope) (Scoped.make b scope)
~op:(O_match_protect (P_vars blocked)) ~root:true ~bvars:B_vars.empty
in
assert (not @@ US.has_constr subst);
US.subst subst)
()
let matching_adapt_scope ?protect ?subst ~pattern t =
if Scoped.same_scope pattern t
then matching_same_scope ?protect ?subst
~scope:(Scoped.scope t) ~pattern:(Scoped.get pattern) (Scoped.get t)
else matching ?subst ~pattern t
let variant ?(subst=Subst.empty) ((_,sc1)as a) ((t2,sc2) as b) =
let subst = US.of_subst subst in
let op =
let protect =
if sc1=sc2
then P_vars (T.Seq.vars t2 |> T.VarSet.of_seq)
else P_scope sc2
in
O_variant protect
in
let subst = unif_rec ~op ~root:true ~bvars:B_vars.empty subst a b in
assert (not @@ US.has_constr subst);
let subst = US.subst subst in
if Subst.is_renaming subst then subst else raise Fail
let equal ~subst a b =
let subst = US.of_subst subst in
try
let subst = unif_rec ~op:O_equal ~root:true ~bvars:B_vars.empty subst a b in
assert (not @@ US.has_constr subst);
true
with Fail -> false
let are_variant t1 t2 =
try
let _ = variant (Scoped.make t1 0) (Scoped.make t2 1) in
true
with Fail ->
false
let matches ~pattern t =
try
let _ = matching ~pattern:(Scoped.make pattern 0) (Scoped.make t 1) in
true
with Fail ->
false
let are_unifiable_full t1 t2 =
try
let _ = unify_full (Scoped.make t1 0) (Scoped.make t2 1) in
true
with Fail ->
false
let are_unifiable_syn t1 t2 =
try
let _ = unify_syn (Scoped.make t1 0) (Scoped.make t2 1) in
true
with Fail ->
false
end
(** {2 Specializations} *)
module Ty = struct
open Inner
type ty = Type.t
type term = Type.t
let bind =
(bind :> ?check:bool -> subst -> ty HVar.t Scoped.t -> term Scoped.t -> subst)
let update =
(update :> ?check:bool -> subst -> ty HVar.t Scoped.t -> term Scoped.t -> subst)
let unify_full =
(unify_full :> ?subst:unif_subst -> term Scoped.t -> term Scoped.t -> unif_subst)
let unify_syn =
(unify_syn :> ?subst:subst -> term Scoped.t -> term Scoped.t -> subst)
let matching =
(matching :> ?subst:subst ->
pattern:term Scoped.t -> term Scoped.t -> subst)
let matching_same_scope =
(matching_same_scope :> ?protect:(Type.t HVar.t Iter.t) -> ?subst:subst ->
scope:int -> pattern:term -> term -> subst)
let matching_adapt_scope =
(matching_adapt_scope :>
?protect:(Type.t HVar.t Iter.t) -> ?subst:subst ->
pattern:term Scoped.t -> term Scoped.t -> subst)
let variant =
(variant :> ?subst:subst -> term Scoped.t -> term Scoped.t -> subst)
let equal =
(equal :> subst:subst -> term Scoped.t -> term Scoped.t -> bool)
let are_unifiable_full =
(are_unifiable_full :> term -> term -> bool)
let are_unifiable_syn =
(are_unifiable_syn :> term -> term -> bool)
let matches =
(matches :> pattern:term -> term -> bool)
let are_variant =
(are_variant :> term -> term -> bool)
let type_is_unifiable = (T.type_is_unifiable :> term -> bool)
end
module FO = struct
open Inner
type ty = Type.t
type term = Term.t
let bind =
(bind :> ?check:bool -> subst -> ty HVar.t Scoped.t -> term Scoped.t -> subst)
let update =
(update :> ?check:bool -> subst -> ty HVar.t Scoped.t -> term Scoped.t -> subst)
let bind_or_update ?(check=true) (subst:subst) (var:ty HVar.t Scoped.t) t =
if S.mem subst (var :> InnerTerm.t HVar.t Scoped.t) then update ~check subst var t
else bind ~check subst var t
let unify_full ?(subst=US.empty) =
fun sc1 sc2 ->
let ta, sca = sc1 in
let tb, scb = sc2 in
if(not (Term.DB.is_closed ta) || not (Term.DB.is_closed tb)) then (
let sk_a, sk_a_subs = Term.DB.skolemize_loosely_bound ta in
let sk_b, sk_b_subs = Term.DB.skolemize_loosely_bound tb in
let res = (unify_full :> ?subst:unif_subst -> term Scoped.t -> term Scoped.t -> unif_subst)
~subst (Scoped.make sk_a sca) (Scoped.make sk_b scb) in
let sk_a_rev = Term.IntMap.fold (fun k v acc -> Term.Map.add v k acc) sk_a_subs Term.Map.empty in
let sk_b_rev = Term.IntMap.fold (fun k v acc -> Term.Map.add v k acc) sk_b_subs Term.Map.empty in
let sk_rev_union = Term.Map.union (fun _ _ _ -> raise (Invalid_argument "keys must be unique "))
sk_a_rev sk_b_rev in
let subst = Unif_subst.subst res in
let mapped = Subst.FO.map (fun t -> Term.DB.unskolemize sk_rev_union t) subst in
let res' = Unif_subst.make mapped (Unif_subst.constr_l res) in
res'
)
else (
(unify_full :> ?subst:unif_subst -> term Scoped.t -> term Scoped.t -> unif_subst)
~subst sc1 sc2
)
let unify_syn ?(subst=Subst.empty) =
fun sc1 sc2 ->
let ta, sca = sc1 in
let tb, scb = sc2 in
if(not (Term.DB.is_closed ta) || not (Term.DB.is_closed tb)) then (
let sk_a, sk_a_subs = Term.DB.skolemize_loosely_bound ta in
let sk_b, sk_b_subs = Term.DB.skolemize_loosely_bound tb in
let res = (unify_syn :> ?subst:subst -> term Scoped.t -> term Scoped.t -> subst)
~subst (Scoped.make sk_a sca) (Scoped.make sk_b scb) in
let sk_a_rev = Term.IntMap.fold (fun k v -> Term.Map.add v k) sk_a_subs Term.Map.empty in
let sk_b_rev = Term.IntMap.fold (fun k v -> Term.Map.add v k) sk_b_subs Term.Map.empty in
let sk_rev_union = Term.Map.union (fun _ _ _ -> raise (Invalid_argument "keys must be unique "))
sk_a_rev sk_b_rev in
let res = Subst.FO.map (Term.DB.unskolemize sk_rev_union) res in
res
)
else (
(unify_syn :> ?subst:subst -> term Scoped.t -> term Scoped.t -> subst)
~subst sc1 sc2
)
let matching =
(matching :> ?subst:subst ->
pattern:term Scoped.t -> term Scoped.t -> subst)
let matching_same_scope =
(matching_same_scope :> ?protect:(Type.t HVar.t Iter.t) -> ?subst:subst ->
scope:int -> pattern:term -> term -> subst)
let matching_adapt_scope =
(matching_adapt_scope :> ?protect:(Type.t HVar.t Iter.t) -> ?subst:subst ->
pattern:term Scoped.t -> term Scoped.t -> subst)
let variant =
(variant :> ?subst:subst -> term Scoped.t -> term Scoped.t -> subst)
let equal =
(equal :> subst:subst -> term Scoped.t -> term Scoped.t -> bool)
let are_unifiable_full =
(are_unifiable_full :> term -> term -> bool)
let are_unifiable_syn =
(are_unifiable_syn :> term -> term -> bool)
let matches =
(matches :> pattern:term -> term -> bool)
let are_variant =
(are_variant :> term -> term -> bool)
let anti_unify ?(cut=max_int) (t:term)(u:term): (term * term) list option =
let module T = Term in
let pairs = ref [] in
let len = ref 0 in
let rec aux t u = match T.view t, T.view u with
| _ when T.equal t u -> ()
| _ when not (Type.equal (T.ty t) (T.ty u)) ->
raise Exit
| _ when Type.equal (Type.returns (T.ty t)) Type.tType ->
raise Exit
| T.App (f, ts), T.App (g, us) when T.equal f g &&
List.length ts = List.length us ->
List.iter2 aux ts us
| _ ->
incr len;
if !len <= cut then (
pairs := (t, u) :: !pairs;
) else raise Exit
in
assert (not (T.equal t u));
try
aux t u;
Some !pairs
with Exit -> None
let pair_lists_ =
(pair_lists_right :> term -> term list -> term -> term list -> InnerTerm.t list * InnerTerm.t list)
let pair_lists f1 l1 f2 l2 =
let l1, l2 = pair_lists_ f1 l1 f2 l2 in
Term.of_term_unsafe_l l1, Term.of_term_unsafe_l l2
end