-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathecMatching.ml
More file actions
1436 lines (1166 loc) · 44.1 KB
/
ecMatching.ml
File metadata and controls
1436 lines (1166 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
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
(* -------------------------------------------------------------------- *)
(* Expressions / formulas matching for tactics *)
(* -------------------------------------------------------------------- *)
(* -------------------------------------------------------------------- *)
open EcUtils
open EcMaps
open EcIdent
open EcEnv
open EcAst
open EcTypes
open EcModules
open EcFol
open EcGenRegexp
(* -------------------------------------------------------------------- *)
module Position = struct
type cp_match = [
| `If
| `While
| `Assign of lvmatch
| `AssignTuple of lvmatch
| `Sample of lvmatch
| `Call of lvmatch
| `Match
]
and lvmatch = [ `LvmNone | `LvmVar of EcTypes.prog_var ]
type cp_base = [
| `ByPos of int
| `ByMatch of int option * cp_match
]
type codepos_brsel = [`Cond of bool | `Match of EcSymbols.symbol]
type codepos1 = int * cp_base
type codepos = (codepos1 * codepos_brsel) list * codepos1
type codepos_range = codepos * [`Base of codepos | `Offset of codepos1]
type codeoffset1 = [`ByOffset of int | `ByPosition of codepos1]
let shift1 ~(offset : int) ((o, p) : codepos1) : codepos1 =
(o + offset, p)
let shift ~(offset : int) ((outp, p) : codepos) : codepos =
(outp, shift1 ~offset p)
let resolve_offset ~(base : codepos1) ~(offset : codeoffset1) : codepos1 =
match offset with
| `ByPosition pos -> pos
| `ByOffset off -> (off + fst base, snd base)
end
(* -------------------------------------------------------------------- *)
module Zipper = struct
open Position
exception InvalidCPos
module P = EcPath
type ('a, 'state) folder =
env -> 'a -> 'state -> instr -> 'state * instr list
type ('a, 'state) folder_l =
env -> 'a -> 'state -> instr list -> 'state * instr list
type spath_match_ctxt = {
locals : (EcIdent.t * ty) list;
prebr : ((EcIdent.t * ty) list * stmt) list;
postbr : ((EcIdent.t * ty) list * stmt) list;
}
type ipath =
| ZTop
| ZWhile of expr * spath
| ZIfThen of expr * spath * stmt
| ZIfElse of expr * stmt * spath
| ZMatch of expr * spath * spath_match_ctxt
and spath = (instr list * instr list) * ipath
type zipper = {
z_head : instr list; (* instructions on my left (rev) *)
z_tail : instr list; (* instructions on my right (me incl.) *)
z_path : ipath; (* path (zipper) leading to me *)
z_env : env option;
}
let cpos (i : int) : codepos1 = (0, `ByPos i)
let zipper ?env hd tl zpr = { z_head = hd; z_tail = tl; z_path = zpr; z_env = env; }
let find_by_cp_match
(env : EcEnv.env)
((i, cm) : int option * cp_match)
(s : stmt)
=
let rec progress (acc : instr list) (s : instr list) (i : int) =
if i <= 0 then
let shd = oget (List.Exceptionless.hd acc) in
let stl = oget (List.Exceptionless.tl acc) in
(stl, shd, s)
else
let ir, s =
match s with [] -> raise InvalidCPos | ir :: s -> (ir, s)
in
let i =
match ir.i_node, cm with
| Swhile _, `While -> i-1
| Sif _, `If -> i-1
| Smatch _, `Match -> i-1
| Scall (None, _, _), `Call `LvmNone -> i-1
| Scall (Some lv, _, _), `Call lvm
| Srnd (lv, _), `Sample lvm
| Sasgn (lv, _), `Assign lvm -> begin
match lv, lvm with
| _, `LvmNone -> i-1
| LvVar (pv, _), `LvmVar pvm
when EcReduction.EqTest.for_pv env pv pvm
-> i-1
| _ -> i
end
| Sasgn (lv, _), `AssignTuple lvm -> begin
match lv, lvm with
| LvTuple _, `LvmNone -> i-1
| LvTuple pvs, `LvmVar pvm
when List.exists (fun (pv, _) -> EcReduction.EqTest.for_pv env pv pvm) pvs
-> i-1
| _ -> i
end
| _ -> i
in progress (ir :: acc) s i
in
let i = odfl 1 i in if i = 0 then raise InvalidCPos;
let rev, i = (i < 0), abs i in
let s1, ir, s2 =
progress [] (if rev then List.rev s.s_node else s.s_node) i in
match rev with
| false -> (s1, ir, s2)
| true -> (s2, ir, s1)
type after = [`Yes | `No | `Auto]
let split_at_cp_base ~(after : after) (env : EcEnv.env) (cb : cp_base) (s : stmt) =
match cb with
| `ByPos i -> begin
let after =
match after with
| `Auto -> 0 <= i
| `Yes -> true
| `No -> false in
let i = if i < 0 then List.length s.s_node + i + 1 else i in
let i = i - if after then 0 else 1 in
try List.takedrop i s.s_node
with (Invalid_argument _ | Not_found) -> raise InvalidCPos
end
| `ByMatch (i, cm) ->
let (s1, i, s2) = find_by_cp_match env (i, cm) s in
match after with
| `No -> (List.rev s1, i :: s2)
| _ -> (List.rev_append s1 [i], s2)
let split_at_cpos1 ~after (env : EcEnv.env) ((ipos, cb) : codepos1) s =
let (s1, s2) = split_at_cp_base ~after env cb s in
let (s1, s2) =
match ipos with
| off when off > 0 ->
let (ss1, ss2) =
try List.takedrop off s2
with (Invalid_argument _ | Not_found) -> raise InvalidCPos in
(s1 @ ss1, ss2)
| off when off < 0 ->
let (ss1, ss2) =
try List.takedrop (List.length s1 + off) s1
with (Invalid_argument _ | Not_found) -> raise InvalidCPos in
(ss1, ss2 @ s2)
| _ -> (s1, s2)
in (s1, s2)
let find_by_cpos1 ?(rev = true) (env : EcEnv.env) (cpos1 : codepos1) (s : stmt) =
match split_at_cpos1 ~after:`No env cpos1 s with
| (s1, i :: s2) -> ((if rev then List.rev s1 else s1), i, s2)
| _ -> raise InvalidCPos
let offset_of_position (env : EcEnv.env) (cpos : codepos1) (s : stmt) =
let (s, _) = split_at_cpos1 ~after:`No env cpos s in
1 + List.length s
let zipper_at_nm_cpos1
(env : EcEnv.env)
((cp1, sub) : codepos1 * codepos_brsel)
(s : stmt)
(zpr : ipath)
: (ipath * stmt) * (codepos1 * codepos_brsel) * env
=
let (s1, i, s2) = find_by_cpos1 env cp1 s in
let zpr, env =
match i.i_node, sub with
| Swhile (e, sw), `Cond true ->
(ZWhile (e, ((s1, s2), zpr)), sw), env
| Sif (e, ifs1, ifs2), `Cond true ->
(ZIfThen (e, ((s1, s2), zpr), ifs2), ifs1), env
| Sif (e, ifs1, ifs2), `Cond false ->
(ZIfElse (e, ifs1, ((s1, s2), zpr)), ifs2), env
| Smatch (e, bs), `Match cn ->
let _, indt, _ = oget (EcEnv.Ty.get_top_decl e.e_ty env) in
let indt = oget (EcDecl.tydecl_as_datatype indt) in
let cnames = List.fst indt.tydt_ctors in
let ix, _ =
try List.findi (fun _ n -> EcSymbols.sym_equal cn n) cnames
with Not_found -> raise InvalidCPos
in
let prebr, (locals, body), postbr = List.pivot_at ix bs in
let env = EcEnv.Var.bind_locals locals env in
(ZMatch (e, ((s1, s2), zpr), { locals; prebr; postbr; }), body), env
| _ -> raise InvalidCPos
in zpr, ((0, `ByPos (1 + List.length s1)), sub), env
let zipper_of_cpos_r (env : EcEnv.env) ((nm, cp1) : codepos) (s : stmt) =
let ((zpr, s), env), nm =
List.fold_left_map
(fun ((zpr, s), env) nm1 -> let zpr, s, env = zipper_at_nm_cpos1 env nm1 s zpr in (zpr, env), s)
((ZTop, s), env) nm in
let s1, i, s2 = find_by_cpos1 env cp1 s in
let zpr = zipper ~env s1 (i :: s2) zpr in
(zpr, (nm, (0, `ByPos (1 + List.length s1))))
let zipper_of_cpos (env : EcEnv.env) (cp : codepos) (s : stmt) =
fst (zipper_of_cpos_r env cp s)
let zipper_of_cpos_range env cpr s =
let top, bot = cpr in
let zpr, (_, pos) = zipper_of_cpos_r env top s in
match bot with
| `Base cp -> begin
let zpr', (_, pos') = zipper_of_cpos_r env cp s in
(* The two positions should identify the same block *)
if zpr'.z_path <> zpr.z_path then
raise InvalidCPos;
(* The end position should be after the start *)
match pos, pos' with
| (_, `ByPos x), (_, `ByPos y) when x <= y ->
zpr, (0, `ByPos (y - x))
| _ -> raise InvalidCPos
end
| `Offset cp1 -> zpr, cp1
let zipper_and_split_of_cpos_range env cpr s =
let zpr, cp = zipper_of_cpos_range env cpr s in
match zpr.z_tail with
| [] -> raise InvalidCPos
| i :: tl ->
let s, tl = split_at_cpos1 ~after:`Auto env cp (stmt tl) in
(zpr, cp), ((i::s), tl)
let split_at_cpos1 env cpos1 s =
split_at_cpos1 ~after:`Auto env cpos1 s
let may_split_at_cpos1 ?(rev = false) env cpos1 s =
ofdfl
(fun () -> if rev then (s.s_node, []) else ([], s.s_node))
(omap ((split_at_cpos1 env)^~ s) cpos1)
let rec zip i ((hd, tl), ip) =
let s = stmt (List.rev_append hd (List.ocons i tl)) in
match ip with
| ZTop -> s
| ZWhile (e, sp) -> zip (Some (i_while (e, s))) sp
| ZIfThen (e, sp, se) -> zip (Some (i_if (e, s, se))) sp
| ZIfElse (e, se, sp) -> zip (Some (i_if (e, se, s))) sp
| ZMatch (e, sp, mpi) ->
zip (Some (i_match (e, List.rev_append mpi.prebr ((mpi.locals, s) :: mpi.postbr)))) sp
let zip zpr = zip None ((zpr.z_head, zpr.z_tail), zpr.z_path)
let after ~strict zpr =
let rec doit acc ip =
match ip with
| ZTop -> acc
| ZWhile (_, ((_, is), ip)) -> doit (is :: acc) ip
| ZIfThen (_, ((_, is), ip), _) -> doit (is :: acc) ip
| ZIfElse (_, _, ((_, is), ip)) -> doit (is :: acc) ip
| ZMatch (_, ((_, is), ip), _) -> doit (is :: acc) ip
in
let after =
match zpr.z_tail, strict with
| [] , _ -> doit [[]] zpr.z_path
| is , false -> doit [is] zpr.z_path
| _ :: is, true -> doit [is] zpr.z_path
in
List.rev after
let fold_range env cenv cpr f state s =
let (zpr, _), (s, tl) = zipper_and_split_of_cpos_range env cpr s in
let env = odfl env zpr.z_env in
let state', si' = f env cenv state s in
state', zip { zpr with z_tail = si' @ tl }
let map_range env cpr f s =
snd (fold_range env () cpr (fun env () _ si -> (), f env si) () s)
let fold env cenv cp f state s =
let f env cenv state si = f env cenv state (List.hd si) in
fold_range env cenv (cp, `Offset (cpos 0)) f state s
let map env cpos f s =
fst_map
Option.get
(fold env () cpos (fun _ () _ i -> fst_map some (f i)) None s)
end
(* -------------------------------------------------------------------- *)
type 'a evmap = {
ev_map : ('a option) Mid.t;
ev_unset : int;
}
module EV = struct
let empty : 'a evmap = {
ev_map = Mid.empty;
ev_unset = 0;
}
let add (x : ident) (m : 'a evmap) =
let chg = function Some _ -> assert false | None -> Some None in
let map = Mid.change chg x m.ev_map in
{ ev_map = map; ev_unset = m.ev_unset + 1; }
let mem (x : ident) (m : 'a evmap) =
EcUtils.is_some (Mid.find_opt x m.ev_map)
let set (x : ident) (v : 'a) (m : 'a evmap) =
let chg = function
| None | Some (Some _) -> assert false
| Some None -> Some (Some v)
in
{ ev_map = Mid.change chg x m.ev_map; ev_unset = m.ev_unset - 1; }
let get (x : ident) (m : 'a evmap) =
match Mid.find_opt x m.ev_map with
| None -> None
| Some None -> Some `Unset
| Some (Some a) -> Some (`Set a)
let isset (x : ident) (m : 'a evmap) =
match get x m with
| Some (`Set _) -> true
| _ -> false
let map (f : 'a -> 'a) (m : 'a evmap) =
{ ev_map = Mid.map (omap f) m.ev_map
; ev_unset = m.ev_unset }
let doget (x : ident) (m : 'a evmap) =
match get x m with
| Some (`Set a) -> a
| _ -> assert false
let of_idents (ids : ident list) : 'a evmap =
List.fold_left ((^~) add) empty ids
let fold (f : ident -> 'a -> 'b -> 'b) ev state =
Mid.fold
(fun x t s -> match t with Some t -> f x t s | None -> s)
ev.ev_map state
let filled (m : 'a evmap) = (m.ev_unset = 0)
end
(* -------------------------------------------------------------------- *)
type mevmap = {
evm_form : form evmap;
evm_mem : EcMemory.memory evmap;
evm_mod : EcPath.mpath evmap;
}
(* -------------------------------------------------------------------- *)
module MEV = struct
type item = [
| `Form of form
| `Mem of EcMemory.memory
| `Mod of EcPath.mpath
]
type kind = [ `Form | `Mem | `Mod ]
let empty : mevmap = {
evm_form = EV.empty;
evm_mem = EV.empty;
evm_mod = EV.empty;
}
let of_idents ids k =
match k with
| `Form -> { empty with evm_form = EV.of_idents ids }
| `Mem -> { empty with evm_mem = EV.of_idents ids }
| `Mod -> { empty with evm_mod = EV.of_idents ids }
let add x k m =
match k with
| `Form -> { m with evm_form = EV.add x m.evm_form }
| `Mem -> { m with evm_mem = EV.add x m.evm_mem }
| `Mod -> { m with evm_mod = EV.add x m.evm_mod }
let mem x k m =
match k with
| `Form -> EV.mem x m.evm_form
| `Mem -> EV.mem x m.evm_mem
| `Mod -> EV.mem x m.evm_mod
let set x v m =
match v with
| `Form v -> { m with evm_form = EV.set x v m.evm_form }
| `Mem v -> { m with evm_mem = EV.set x v m.evm_mem }
| `Mod v -> { m with evm_mod = EV.set x v m.evm_mod }
let get x k m =
let tx f = function `Unset -> `Unset | `Set x -> `Set (f x) in
match k with
| `Form -> omap (tx (fun x -> `Form x)) (EV.get x m.evm_form)
| `Mem -> omap (tx (fun x -> `Mem x)) (EV.get x m.evm_mem )
| `Mod -> omap (tx (fun x -> `Mod x)) (EV.get x m.evm_mod )
let isset x k m =
match k with
| `Form -> EV.isset x m.evm_form
| `Mem -> EV.isset x m.evm_mem
| `Mod -> EV.isset x m.evm_mod
let filled m =
EV.filled m.evm_form
&& EV.filled m.evm_mem
&& EV.filled m.evm_mod
let fold (f : _ -> item -> _ -> _) m v =
let v = EV.fold (fun x k v -> f x (`Form k) v) m.evm_form v in
let v = EV.fold (fun x k v -> f x (`Mem k) v) m.evm_mem v in
let v = EV.fold (fun x k v -> f x (`Mod k) v) m.evm_mod v in
v
let assubst ue ev env =
let subst = f_subst_init ~tu:(EcUnify.UniEnv.assubst ue) () in
let subst = EV.fold (fun x m s -> Fsubst.f_bind_mem s x m) ev.evm_mem subst in
let subst = EV.fold (fun x mp s -> EcFol.f_bind_mod s x mp env) ev.evm_mod subst in
let seen = ref Sid.empty in
let rec for_ident x binding subst =
if Sid.mem x !seen then subst else begin
seen := Sid.add x !seen;
match binding with None -> subst | Some f ->
let subst =
Mid.fold2_inter (fun x bdx _ -> for_ident x bdx)
ev.evm_form.ev_map f.f_fv subst in
Fsubst.f_bind_local subst x (Fsubst.f_subst subst f)
end
in
Mid.fold_left
(fun acc x bd -> for_ident x bd acc)
subst ev.evm_form.ev_map
end
(* -------------------------------------------------------------------- *)
exception MatchFailure
type fmoptions = {
fm_delta : bool;
fm_conv : bool;
fm_horder : bool;
}
let fmsearch =
{ fm_delta = false;
fm_conv = false;
fm_horder = true ; }
let fmrigid = {
fm_delta = false;
fm_conv = true ;
fm_horder = true ; }
let fmdelta = {
fm_delta = true ;
fm_conv = true ;
fm_horder = true ; }
let fmnotation = {
fm_delta = false;
fm_conv = false;
fm_horder = false; }
(* -------------------------------------------------------------------- *)
(* Rigid unification *)
let f_match_core opts hyps (ue, ev) f1 f2 =
let ue = EcUnify.UniEnv.copy ue in
let ev = ref ev in
let iscvar = function
| { f_node = Flocal x } -> is_none (EV.get x !ev.evm_form)
| _ -> false
in
let conv =
match opts.fm_conv with
| true -> EcReduction.is_conv ~ri:EcReduction.full_compat hyps
| false -> EcReduction.is_alpha_eq hyps
in
let rec doit env ((subst, mxs) as ilc) f1 f2 =
let failure =
let oue, oev = (EcUnify.UniEnv.copy ue, !ev) in
fun () ->
EcUnify.UniEnv.restore ~dst:ue ~src:oue; ev := oev;
raise MatchFailure
in
let norm (f : form) =
let f = Fsubst.f_subst subst f in
let f = Fsubst.f_subst (MEV.assubst ue !ev env) f in
f
in
let var_form_match ((x, xty) : ident * ty) (f : form) =
match EV.get x !ev.evm_form with
| None -> assert false
| Some `Unset ->
let f = norm f in
if not (Mid.set_disjoint mxs f.f_fv) then
failure ();
begin
try EcUnify.unify env ue xty f.f_ty
with EcUnify.UnificationFailure _ -> failure ();
end;
ev := { !ev with evm_form = EV.set x f !ev.evm_form }
| Some (`Set a) -> begin
let f = norm f in
if not (conv f a) then
doit env ilc a f
else
try EcUnify.unify env ue xty f.f_ty
with EcUnify.UnificationFailure _ -> failure ()
end
in
let ho_match (ho, args, hoty) (sbj : form) =
if
not (Mid.mem ho mxs)
&& (EV.get ho !ev.evm_form = Some `Unset)
&& not (List.is_empty args)
&& List.for_all iscvar args
then begin
let oargs = List.map destr_local args in
if not (List.is_unique ~eq:id_equal oargs) then
failure ();
let xsubst, bindings =
List.map_fold
(fun xsubst x ->
let x, xty = (destr_local x, x.f_ty) in
let nx = EcIdent.fresh x in
let xsubst =
Mid.find_opt x mxs
|> omap (fun y -> Fsubst.f_bind_rename xsubst y nx xty)
|> odfl xsubst
in (xsubst, (nx, GTty xty)))
Fsubst.f_subst_id args in
let sbj = norm (Fsubst.f_subst xsubst sbj) in
if not (Mid.set_disjoint mxs sbj.f_fv) then
failure ();
begin
let fty = toarrow (List.map f_ty args) sbj.f_ty in
try EcUnify.unify env ue hoty fty
with EcUnify.UnificationFailure _ -> failure ();
end;
let sbj = f_lambda bindings sbj in
ev := { !ev with evm_form = EV.set ho sbj !ev.evm_form }
end else
failure ()
in
try
match f1.f_node, f2.f_node with
| Flocal x1, Flocal x2 when Mid.mem x1 mxs -> begin
if not (id_equal (oget (Mid.find_opt x1 mxs)) x2) then
failure ();
try EcUnify.unify env ue f1.f_ty f2.f_ty
with EcUnify.UnificationFailure _ -> failure ()
end
| Flocal x1, Flocal x2 when id_equal x1 x2 -> begin
try EcUnify.unify env ue f1.f_ty f2.f_ty
with EcUnify.UnificationFailure _ -> failure ()
end
| Flocal x, _ when EV.mem x !ev.evm_form ->
var_form_match (x, f1.f_ty) f2
| _, Flocal y when EV.mem y !ev.evm_form ->
var_form_match (y, f2.f_ty) f1
| Fapp (f1, fs1), Fapp (f2, fs2) ->
doit_args env ilc (f1::fs1) (f2::fs2)
| Fquant (b1, q1, f1), Fquant (b2, q2, f2) when b1 = b2 ->
let n1, n2 = List.length q1, List.length q2 in
let q1, r1 = List.split_at (min n1 n2) q1 in
let q2, r2 = List.split_at (min n1 n2) q2 in
let (env, subst, mxs) = doit_bindings env (subst, mxs) q1 q2 in
doit env (subst, mxs) (f_quant b1 r1 f1) (f_quant b2 r2 f2)
| Fquant _, Fquant _ ->
failure ();
| Fpvar (pv1, m1), Fpvar (pv2, m2) ->
let pv1 = EcEnv.NormMp.norm_pvar env pv1 in
let pv2 = EcEnv.NormMp.norm_pvar env pv2 in
if not (EcTypes.pv_equal pv1 pv2) then
failure ();
doit_mem env mxs m1 m2
| Fif (c1, t1, e1), Fif (c2, t2, e2) ->
List.iter2 (doit env ilc) [c1; t1; e1] [c2; t2; e2]
| Fmatch (b1, fs1, ty1), Fmatch (b2, fs2, ty2) -> begin
(try EcUnify.unify env ue ty1 ty2
with EcUnify.UnificationFailure _ -> failure ());
if List.length fs1 <> List.length fs2 then
failure ();
List.iter2 (doit env ilc) (b1 :: fs1) (b2 :: fs2)
end
| Fint i1, Fint i2 ->
if not (EcBigInt.equal i1 i2) then failure ();
| Fglob (mp1, me1), Fglob (mp2, me2) ->
if not (EcIdent.id_equal mp1 mp2) then
failure ();
doit_mem env mxs me1 me2
| Ftuple fs1, Ftuple fs2 ->
if List.length fs1 <> List.length fs2 then
failure ();
List.iter2 (doit env ilc) fs1 fs2
| Fproj (f1, i), Fproj (f2, j) ->
if i <> j then failure () else doit env ilc f1 f2
| Fop (op1, tys1), Fop (op2, tys2) -> begin
if not (EcPath.p_equal op1 op2) then
failure ();
try List.iter2 (EcUnify.unify env ue) tys1 tys2
with EcUnify.UnificationFailure _ -> failure ()
end
| FhoareF hf1, FhoareF hf2 -> begin
if not (EcReduction.EqTest.for_xp env hf1.hf_f hf2.hf_f) then
failure ();
let subst =
if id_equal hf1.hf_m hf2.hf_m then
subst
else
Fsubst.f_bind_mem subst hf1.hf_m hf2.hf_m in
assert (not (Mid.mem hf1.hf_m mxs) && not (Mid.mem hf2.hf_m mxs));
let mxs = Mid.add hf1.hf_m hf2.hf_m mxs in
let poe2 =
EcAst.POE.map2_pre
(fun a b -> (a, b))
(POE.destruct (hf_po hf1).hsi_inv)
(POE.destruct (hf_po hf2).hsi_inv)
in
let lpoe2 = EcAst.POE.to_list_pre poe2 in
let lf1 = List.map fst lpoe2 in
let lf2 = List.map snd lpoe2 in
List.iter2 (doit env (subst, mxs))
((hf_pr hf1).inv :: lf1) ((hf_pr hf2).inv :: lf2)
end
| FbdHoareF hf1, FbdHoareF hf2 -> begin
if not (EcReduction.EqTest.for_xp env hf1.bhf_f hf2.bhf_f) then
failure ();
if hf1.bhf_cmp <> hf2.bhf_cmp then
failure ();
let subst =
if id_equal hf1.bhf_m hf2.bhf_m then
subst
else
Fsubst.f_bind_mem subst hf1.bhf_m hf2.bhf_m in
assert (not (Mid.mem hf1.bhf_m mxs) && not (Mid.mem hf2.bhf_m mxs));
let mxs = Mid.add hf1.bhf_m hf2.bhf_m mxs in
List.iter2 (doit env (subst, mxs))
[(bhf_pr hf1).inv; (bhf_po hf1).inv; (bhf_bd hf1).inv]
[(bhf_pr hf2).inv; (bhf_po hf2).inv; (bhf_bd hf2).inv]
end
| FequivF hf1, FequivF hf2 -> begin
if not (EcReduction.EqTest.for_xp env hf1.ef_fl hf2.ef_fl) then
failure ();
if not (EcReduction.EqTest.for_xp env hf1.ef_fr hf2.ef_fr) then
failure();
let subst =
if id_equal hf1.ef_ml hf2.ef_ml then
subst
else
Fsubst.f_bind_mem subst hf1.ef_ml hf2.ef_ml in
assert (not (Mid.mem hf1.ef_ml mxs) && not (Mid.mem hf2.ef_ml mxs));
let mxs = Mid.add hf1.ef_ml hf2.ef_ml mxs in
let subst =
if id_equal hf1.ef_mr hf2.ef_mr then
subst
else
Fsubst.f_bind_mem subst hf1.ef_mr hf2.ef_mr in
assert (not (Mid.mem hf1.ef_mr mxs) && not (Mid.mem hf2.ef_mr mxs));
let mxs = Mid.add hf1.ef_mr hf2.ef_mr mxs in
List.iter2
(doit env (subst, mxs))
[(ef_pr hf1).inv; (ef_po hf1).inv] [(ef_pr hf2).inv; (ef_po hf2).inv]
end
| Fpr pr1, Fpr pr2 -> begin
if not (EcReduction.EqTest.for_xp env pr1.pr_fun pr2.pr_fun) then
failure ();
doit_mem env mxs pr1.pr_mem pr2.pr_mem;
doit env (subst, mxs) pr1.pr_args pr2.pr_args;
let ev1, ev2 = pr1.pr_event, pr2.pr_event in
let subst =
if id_equal ev1.m ev2.m then
subst
else
Fsubst.f_bind_mem subst ev1.m ev2.m in
assert (not (Mid.mem ev1.m mxs) && not (Mid.mem ev2.m mxs));
let mxs = Mid.add ev1.m ev2.m mxs in
doit env (subst, mxs) ev1.inv ev2.inv;
end
| _, _ -> failure ()
with MatchFailure ->
let try_betared () =
let f1' = f_betared f1 in
let f2' = f_betared f2 in
if f1 == (*phy*) f1' && f2 ==(*phy*) f2' then
failure ();
doit env (subst, mxs) f1' f2' in
let try_horder () =
if not opts.fm_horder then
failure ();
let doit (f1 : form) (f2 : form) =
match destr_app f1 with
| { f_node = Flocal ho; f_ty = hoty }, args
when not (List.is_empty args)
->
ho_match (ho, args, hoty) f2
| _ ->
failure ()
in
try
doit f1 f2
with MatchFailure -> doit f2 f1
in
let try_delta () =
if not opts.fm_delta then
failure ();
match fst_map f_node (destr_app f1),
fst_map f_node (destr_app f2)
with
| (Flocal x1, args1), _ when LDecl.can_unfold x1 hyps ->
doit_lreduce env ((doit env ilc)^~ f2) f1.f_ty x1 args1
| _, (Flocal x2, args2) when LDecl.can_unfold x2 hyps ->
doit_lreduce env (doit env ilc f1) f2.f_ty x2 args2
| (Fop (op1, tys1), args1), _ when EcEnv.Op.reducible env op1 ->
doit_reduce env ((doit env ilc)^~ f2) f1.f_ty op1 tys1 args1
| _, (Fop (op2, tys2), args2) when EcEnv.Op.reducible env op2 ->
doit_reduce env (doit env ilc f1) f2.f_ty op2 tys2 args2
| _, _ -> failure ()
in
let default () =
if not (conv (norm f1) (norm f2)) then
failure ()
in
List.find_map_opt
(fun doit ->
try Some (doit ()) with MatchFailure -> None)
[try_betared; try_horder; try_delta; default]
|> oget ~exn:MatchFailure
and doit_args env ilc fs1 fs2 =
if List.length fs1 <> List.length fs2 then
raise MatchFailure;
List.iter2 (doit env ilc) fs1 fs2
and doit_reduce env cb ty op tys args =
let reduced =
try f_app (EcEnv.Op.reduce env op tys) args ty
with NotReducible -> raise MatchFailure in
cb (odfl reduced (EcReduction.h_red_opt EcReduction.beta_red hyps reduced))
and doit_lreduce _env cb ty x args =
let reduced =
try f_app (LDecl.unfold x hyps) args ty
with LookupFailure _ -> raise MatchFailure in
cb (odfl reduced (EcReduction.h_red_opt EcReduction.beta_red hyps reduced))
and doit_mem _env mxs m1 m2 =
if not (EcMemory.mem_equal m1 m2) then begin
match EV.get m1 !ev.evm_mem with
| None ->
raise MatchFailure
| Some `Unset ->
if Mid.mem m2 mxs then
raise MatchFailure;
ev := { !ev with evm_mem = EV.set m1 m2 !ev.evm_mem }
| Some (`Set m1) ->
if not (EcMemory.mem_equal m1 m2) then
raise MatchFailure
end
and doit_bindings env (subst, mxs) q1 q2 =
let doit_binding (env, subst, mxs) (x1, gty1) (x2, gty2) =
let gty2 = Fsubst.gty_subst subst gty2 in
assert (not (Mid.mem x1 mxs) && not (Mid.mem x2 mxs));
let env, subst =
match gty1, gty2 with
| GTty ty1, GTty ty2 ->
begin
try EcUnify.unify env ue ty1 ty2
with EcUnify.UnificationFailure _ -> raise MatchFailure
end;
let subst =
if id_equal x1 x2
then subst
else Fsubst.f_bind_rename subst x2 x1 ty2
and env = EcEnv.Var.bind_local x1 ty1 env in
(env, subst)
| GTmem mt1, GTmem mt2 ->
let on_ty ty1 ty2 =
try EcUnify.unify env ue ty1 ty2; true
with EcUnify.UnificationFailure _ -> false in
if not (EcMemory.mt_equal_gen on_ty mt1 mt2) then raise MatchFailure;
let subst =
if id_equal x1 x2
then subst
else Fsubst.f_bind_mem subst x2 x1
in (env, subst)
| GTmodty p1, GTmodty p2 ->
if not (NormMp.mod_type_equiv env p1 p2) then
raise MatchFailure;
let subst =
if id_equal x1 x2
then subst
else Fsubst.f_bind_absmod subst x2 x1
and env = EcEnv.Mod.bind_local x1 p1 env in
(env, subst)
| _, _ -> raise MatchFailure
in
(env, subst, Mid.add x1 x2 mxs)
in
List.fold_left2 doit_binding (env, subst, mxs) q1 q2
in
doit (EcEnv.LDecl.toenv hyps) (Fsubst.f_subst_id, Mid.empty) f1 f2;
(ue, !ev)
let f_match opts hyps (ue, ev) f1 f2 =
let (ue, ev) = f_match_core opts hyps (ue, ev) f1 f2 in
if not (MEV.filled ev) then
raise MatchFailure;
let clue =
try EcUnify.UniEnv.close ue
with EcUnify.UninstantiateUni -> raise MatchFailure
in
(ue, clue, ev)
(* -------------------------------------------------------------------- *)
type ptnpos1 = [`Select of int | `Sub of ptnpos]
and ptnpos = ptnpos1 Mint.t
type occ = [`Inclusive | `Exclusive] * Sint.t
exception InvalidPosition
exception InvalidOccurence
module FPosition = struct
type select = [`Accept of int | `Continue]
(* ------------------------------------------------------------------ *)
let empty : ptnpos = Mint.empty
(* ------------------------------------------------------------------ *)
let is_empty (p : ptnpos) = Mint.is_empty p
(* ------------------------------------------------------------------ *)
let rec tostring (p : ptnpos) =
let items = Mint.bindings p in
let items =
List.map
(fun (i, p) -> Printf.sprintf "%d[%s]" i (tostring1 p))
items
in
String.concat ", " items
(* ------------------------------------------------------------------ *)
and tostring1 = function
| `Select i when i < 0 -> "-"
| `Select i -> Printf.sprintf "-(%d)" i
| `Sub p -> tostring p
(* ------------------------------------------------------------------ *)
let occurences =
let rec doit1 n p =
match p with
| `Select _ -> n+1
| `Sub p -> doit n p
and doit n (ps : ptnpos) =
Mint.fold (fun _ p n -> doit1 n p) ps n
in
fun p -> doit 0 p
(* ------------------------------------------------------------------ *)
let filter ((mode, s) : occ) =
let rec doit1 n p =
match p with
| `Select _ -> begin
match mode with
| `Inclusive -> (n+1, if Sint.mem n s then Some p else None )
| `Exclusive -> (n+1, if Sint.mem n s then None else Some p)
end
| `Sub p -> begin
match doit n p with
| (n, sub) when Mint.is_empty sub -> (n, None)
| (n, sub) -> (n, Some (`Sub sub))
end
and doit n (ps : ptnpos) =
Mint.mapi_filter_fold (fun _ p n -> doit1 n p) ps n