/
notation_ops.ml
1675 lines (1522 loc) · 74.5 KB
/
notation_ops.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
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
open Pp
open CErrors
open Util
open Names
open Nameops
open Constr
open Globnames
open Namegen
open Glob_term
open Glob_ops
open Mod_subst
open Notation_term
(**********************************************************************)
(* Utilities *)
let ldots_var = Id.of_string ".."
let rec alpha_var id1 id2 = function
| (i1,i2)::_ when Id.equal i1 id1 -> Id.equal i2 id2
| (i1,i2)::_ when Id.equal i2 id2 -> Id.equal i1 id1
| _::idl -> alpha_var id1 id2 idl
| [] -> Id.equal id1 id2
(* used to update the notation variable with the local variables used
in NList and NBinderList, since the iterator has its own variable *)
let replace_var i j var = j :: List.remove Id.equal i var
(* compare_glob_universe_instances true strictly_lt us1 us2 computes us1 <= us2,
compare_glob_universe_instances false strictly_lt us1 us2 computes us1 = us2.
strictly_lt will be set to true if any part is strictly less. *)
let compare_glob_universe_instances lt strictly_lt us1 us2 =
match us1, us2 with
| None, None -> true
| Some _, None -> strictly_lt := true; lt
| None, Some _ -> false
| Some l1, Some l2 ->
CList.for_all2eq (fun u1 u2 ->
match u1, u2 with
| UAnonymous {rigid=true}, UAnonymous {rigid=true} -> true
| UAnonymous {rigid=false}, UAnonymous {rigid=false} -> true
| UAnonymous _, UAnonymous _ -> false
| UNamed _, UAnonymous _ -> strictly_lt := true; lt
| UAnonymous _, UNamed _ -> false
| UNamed _, UNamed _ -> glob_level_eq u1 u2) l1 l2
(* Compute us1 <= us2, as a boolean *)
let compare_glob_universe_instances_le us1 us2 =
compare_glob_universe_instances true (ref false) us1 us2
(* When [lt] is [true], tell if [t1] is a strict refinement of [t2]
(this is a partial order, so returning [false] does not mean that
[t2] is finer than [t1]); when [lt] is false, tell if [t1] is the
same pattern as [t2] *)
let compare_notation_constr lt var_eq_hole (vars1,vars2) t1 t2 =
(* this is used to reason up to order of notation variables *)
let alphameta = ref [] in
(* this becomes true when at least one subterm is detected as strictly smaller *)
let strictly_lt = ref false in
(* this is the stack of inner of iter patterns for comparison with a
new iteration or the tail of a recursive pattern *)
let tail = ref [] in
let check_alphameta id1 id2 =
try if not (Id.equal (List.assoc id1 !alphameta) id2) then raise_notrace Exit
with Not_found ->
if (List.mem_assoc id1 !alphameta) then raise_notrace Exit;
alphameta := (id1,id2) :: !alphameta in
let check_eq_id (vars1,vars2) renaming id1 id2 =
let ismeta1 = List.mem_f Id.equal id1 vars1 in
let ismeta2 = List.mem_f Id.equal id2 vars2 in
match ismeta1, ismeta2 with
| true, true -> check_alphameta id1 id2
| false, false -> if not (alpha_var id1 id2 renaming) then raise_notrace Exit
| false, true ->
if not lt then raise_notrace Exit
else
(* a binder which is not bound in the notation can be
considered as strictly more precise since it prevents the
notation variables in its scope to be bound by this binder;
i.e. it is strictly more precise in the sense that it
covers strictly less patterns than a notation where the
same binder is bound in the notation; this is hawever
disputable *)
strictly_lt := true
| true, false -> if not lt then raise_notrace Exit in
let check_eq_name vars renaming na1 na2 =
match na1, na2 with
| Name id1, Name id2 -> check_eq_id vars renaming id1 id2; (id1,id2)::renaming
| Anonymous, Anonymous -> renaming
| Anonymous, Name _ when lt -> renaming
| _ -> raise_notrace Exit in
let rec aux (vars1,vars2 as vars) renaming t1 t2 = match t1, t2 with
| NVar id1, NVar id2 when id1 = ldots_var && id2 = ldots_var -> ()
| _, NVar id2 when lt && id2 = ldots_var -> tail := t1 :: !tail
| NVar id1, _ when lt && id1 = ldots_var -> tail := t2 :: !tail
| NVar id1, NVar id2 -> check_eq_id vars renaming id1 id2
| NHole _, NVar id2 when lt && List.mem_f Id.equal id2 vars2 -> ()
| NVar id1, NHole _ when lt && List.mem_f Id.equal id1 vars1 -> ()
| _, NVar id2 when lt && List.mem_f Id.equal id2 vars2 -> strictly_lt := true
| NRef (gr1,u1), NRef (gr2,u2) when GlobRef.equal gr1 gr2 && compare_glob_universe_instances lt strictly_lt u1 u2 -> ()
| NHole (_, _, _), NHole (_, _, _) -> () (* FIXME? *)
| _, NHole (_, _, _) when lt -> strictly_lt := true
| NList (i1, j1, iter1, tail1, b1), NList (i2, j2, iter2, tail2, b2)
| NBinderList (i1, j1, iter1, tail1, b1), NBinderList (i2, j2, iter2, tail2, b2) ->
if b1 <> b2 then raise_notrace Exit;
let vars1 = replace_var i1 j1 vars1 in
let vars2 = replace_var i2 j2 vars2 in
check_alphameta i1 i2; aux (vars1,vars2) renaming iter1 iter2; aux vars renaming tail1 tail2;
| NBinderList (i1, j1, iter1, tail1, b1), NList (i2, j2, iter2, tail2, b2)
| NList (i1, j1, iter1, tail1, b1), NBinderList (i2, j2, iter2, tail2, b2) ->
(* They may overlap on a unique iteration of them *)
let vars1 = replace_var i1 j1 vars1 in
let vars2 = replace_var i2 j2 vars2 in
aux (vars1,vars2) renaming iter1 iter2;
aux vars renaming tail1 tail2
| t1, NList (i2, j2, iter2, tail2, b2)
| t1, NBinderList (i2, j2, iter2, tail2, b2) when lt ->
(* checking if t1 is a finite iteration of the pattern *)
let vars2 = replace_var i2 j2 vars2 in
aux (vars1,vars2) renaming t1 iter2;
let t1 = List.hd !tail in
tail := List.tl !tail;
(* either matching a new iteration, or matching the tail *)
(try aux vars renaming t1 tail2 with Exit -> aux vars renaming t1 t2)
| NList (i1, j1, iter1, tail1, b1), t2
| NBinderList (i1, j1, iter1, tail1, b1), t2 when lt ->
(* we see the NList as a single iteration *)
let vars1 = replace_var i1 j1 vars1 in
aux (vars1,vars2) renaming iter1 t2;
let t2 = match !tail with
| t::rest -> tail := rest; t
| _ -> (* ".." is in a discarded fine-grained position *) raise_notrace Exit in
(* it had to be a single iteration of iter1 *)
aux vars renaming tail1 t2
| NApp (t1, a1), NApp (t2, a2) -> aux vars renaming t1 t2; List.iter2 (aux vars renaming) a1 a2
| NProj ((cst1,u1), l1, a1), NProj ((cst2,u2), l2, a2)
when GlobRef.equal (GlobRef.ConstRef cst1) (GlobRef.ConstRef cst2) && compare_glob_universe_instances lt strictly_lt u1 u2 ->
List.iter2 (aux vars renaming) l1 l2; aux vars renaming a1 a2
| NLambda (na1, t1, u1), NLambda (na2, t2, u2)
| NProd (na1, t1, u1), NProd (na2, t2, u2) ->
(match t1, t2 with
| None, None -> ()
| Some _, None -> if lt then strictly_lt := true
| Some t1, Some t2 -> aux vars renaming t1 t2
| None, Some _ -> raise_notrace Exit);
let renaming = check_eq_name vars renaming na1 na2 in
aux vars renaming u1 u2
| NLetIn (na1, b1, t1, u1), NLetIn (na2, b2, t2, u2) ->
aux vars renaming b1 b2;
Option.iter2 (aux vars renaming) t1 t2;(* TODO : subtyping? *)
let renaming = check_eq_name vars renaming na1 na2 in
aux vars renaming u1 u2
| NCases (_, o1, r1, p1), NCases (_, o2, r2, p2) -> (* FIXME? *)
let check_pat (p1, t1) (p2, t2) =
if not (List.equal cases_pattern_eq p1 p2) then raise_notrace Exit; (* TODO: subtyping and renaming *)
aux vars renaming t1 t2
in
let eqf renaming (t1, (na1, o1)) (t2, (na2, o2)) =
aux vars renaming t1 t2;
let renaming = check_eq_name vars renaming na1 na2 in
let eq renaming (i1, n1) (i2, n2) =
if not (Ind.CanOrd.equal i1 i2) then raise_notrace Exit;
List.fold_left2 (check_eq_name vars) renaming n1 n2 in
Option.fold_left2 eq renaming o1 o2 in
let renaming = List.fold_left2 eqf renaming r1 r2 in
Option.iter2 (aux vars renaming) o1 o2;
List.iter2 check_pat p1 p2
| NLetTuple (nas1, (na1, o1), t1, u1), NLetTuple (nas2, (na2, o2), t2, u2) ->
aux vars renaming t1 t2;
let renaming = check_eq_name vars renaming na1 na2 in
Option.iter2 (aux vars renaming) o1 o2;
let renaming' = List.fold_left2 (check_eq_name vars) renaming nas1 nas2 in
aux vars renaming' u1 u2
| NIf (t1, (na1, o1), u1, r1), NIf (t2, (na2, o2), u2, r2) ->
aux vars renaming t1 t2;
aux vars renaming u1 u2;
aux vars renaming r1 r2;
let renaming = check_eq_name vars renaming na1 na2 in
Option.iter2 (aux vars renaming) o1 o2
| NRec (_, ids1, ts1, us1, rs1), NRec (_, ids2, ts2, us2, rs2) -> (* FIXME? *)
let eq renaming (na1, o1, t1) (na2, o2, t2) =
Option.iter2 (aux vars renaming) o1 o2;
aux vars renaming t1 t2;
check_eq_name vars renaming na1 na2
in
let renaming = Array.fold_left2 (fun r id1 id2 -> check_eq_id vars r id1 id2; (id1,id2)::r) renaming ids1 ids2 in
let renamings = Array.map2 (List.fold_left2 eq renaming) ts1 ts2 in
Array.iter3 (aux vars) renamings us1 us2;
Array.iter3 (aux vars) (Array.map ((@) renaming) renamings) rs1 rs2
| NSort s1, NSort s2 when glob_sort_eq s1 s2 -> ()
| NCast (c1, k1, t1), NCast (c2, k2, t2) ->
aux vars renaming c1 c2;
if not (cast_kind_eq k1 k2) then raise_notrace Exit;
aux vars renaming t1 t2
| NInt i1, NInt i2 when Uint63.equal i1 i2 -> ()
| NFloat f1, NFloat f2 when Float64.equal f1 f2 -> ()
| NArray(t1,def1,ty1), NArray(t2,def2,ty2) ->
Array.iter2 (aux vars renaming) t1 t2;
aux vars renaming def1 def2;
aux vars renaming ty1 ty2
| (NRef _ | NVar _ | NApp _ | NProj _ | NHole _ | NList _ | NLambda _ | NProd _
| NBinderList _ | NLetIn _ | NCases _ | NLetTuple _ | NIf _
| NRec _ | NSort _ | NCast _ | NInt _ | NFloat _ | NArray _), _ -> raise_notrace Exit in
try
let _ = aux (vars1,vars2) [] t1 t2 in
if not lt then
(* Check that order of notation metavariables does not matter *)
List.iter2 check_alphameta vars1 vars2;
not lt || var_eq_hole || !strictly_lt
with Exit | (* Option.iter2: *) Option.Heterogeneous | Invalid_argument _ -> false
let eq_notation_constr vars t1 t2 = t1 == t2 || compare_notation_constr false false vars t1 t2
(* "strictly finer" is the order generated by constructed-node <= var/hole *)
let strictly_finer_notation_constr vars t1 t2 = compare_notation_constr true false vars t1 t2
(* "finer" is the order also including var = hole and hole = var *)
let finer_notation_constr vars t1 t2 = compare_notation_constr true true vars t1 t2
let adjust_application c1 c2 =
match c1, c2 with
| NApp (t1, a1), (NList (_,_,NApp (_, a2),_,_) | NBinderList (_,_,NApp (_, a2),_,_) | NApp (_, a2)) when List.length a1 >= List.length a2 ->
NApp (t1, List.firstn (List.length a2) a1)
| NApp (t1, a1), _ ->
t1
| _ -> c1
let strictly_finer_interpretation_than (vars1,c1) (vars2,c2) =
let c1 = adjust_application c1 c2 in
strictly_finer_notation_constr (List.map fst vars1, List.map fst vars2) c1 c2
let finer_interpretation_than (vars1,c1) (vars2,c2) =
let c1 = adjust_application c1 c2 in
finer_notation_constr (List.map fst vars1, List.map fst vars2) c1 c2
(**********************************************************************)
(* Re-interpret a notation as a glob_constr, taking care of binders *)
let name_to_ident = function
| Anonymous -> CErrors.user_err Pp.(str "This expression should be a simple identifier.")
| Name id -> id
let to_id g e id = let e,na = g e (Name id) in e,name_to_ident na
let product_of_cases_patterns patl =
List.fold_right (fun patl restl ->
List.flatten (List.map (fun p -> List.map (fun rest -> p::rest) restl) patl))
patl [[]]
let rec cases_pattern_fold_map ?loc g e = DAst.with_val (function
| PatVar na ->
let e',disjpat,na' = g e na in
e', (match disjpat with
| None -> [DAst.make ?loc @@ PatVar na']
| Some ((_,disjpat),_) -> disjpat)
| PatCstr (cstr,patl,na) ->
let e',disjpat,na' = g e na in
if disjpat <> None then user_err (Pp.str "Unable to instantiate an \"as\" clause with a pattern.");
let e',patl' = List.fold_left_map (cases_pattern_fold_map ?loc g) e patl in
(* Distribute outwards the inner disjunctive patterns *)
let disjpatl' = product_of_cases_patterns patl' in
e', List.map (fun patl' -> DAst.make ?loc @@ PatCstr (cstr,patl',na')) disjpatl'
)
let subst_binder_type_vars l = function
| Evar_kinds.BinderType (Name id) ->
let id =
try match DAst.get (Id.List.assoc id l) with GVar id' -> id' | _ -> id
with Not_found -> id in
Evar_kinds.BinderType (Name id)
| e -> e
let rec subst_glob_vars l gc = DAst.map (function
| GVar id as r -> (try DAst.get (Id.List.assoc id l) with Not_found -> r)
| GProd (Name id,bk,t,c) ->
let id =
try match DAst.get (Id.List.assoc id l) with GVar id' -> id' | _ -> id
with Not_found -> id in
GProd (Name id,bk,subst_glob_vars l t,subst_glob_vars l c)
| GLambda (Name id,bk,t,c) ->
let id =
try match DAst.get (Id.List.assoc id l) with GVar id' -> id' | _ -> id
with Not_found -> id in
GLambda (Name id,bk,subst_glob_vars l t,subst_glob_vars l c)
| GHole (x,naming,arg) -> GHole (subst_binder_type_vars l x,naming,arg)
| _ -> DAst.get (map_glob_constr (subst_glob_vars l) gc) (* assume: id is not binding *)
) gc
type 'a binder_status_fun = {
no : 'a -> 'a;
restart_prod : 'a -> 'a;
restart_lambda : 'a -> 'a;
switch_prod : 'a -> 'a;
switch_lambda : 'a -> 'a;
slide : 'a -> 'a;
}
let default_binder_status_fun = {
no = (fun x -> x);
restart_prod = (fun x -> x);
restart_lambda = (fun x -> x);
switch_prod = (fun x -> x);
switch_lambda = (fun x -> x);
slide = (fun x -> x);
}
let test_implicit_argument_mark bk =
if not (Glob_ops.binding_kind_eq bk Explicit) then
user_err (Pp.str "Unexpected implicit argument mark.")
let test_pattern_cast = function
| None -> ()
| Some t -> user_err ?loc:t.CAst.loc (Pp.str "Unsupported pattern cast.")
let protect g e na =
let e',disjpat,na,bk,t = g e na None in
if disjpat <> None then user_err (Pp.str "Unsupported substitution of an arbitrary pattern.");
test_implicit_argument_mark bk;
test_pattern_cast t;
e',na
let set_anonymous_type na = function
| None -> DAst.make @@ GHole (Evar_kinds.BinderType na, IntroAnonymous, None)
| Some t -> t
let apply_cases_pattern_term ?loc (ids,disjpat) tm c =
let eqns = List.map (fun pat -> (CAst.make ?loc (ids,[pat],c))) disjpat in
DAst.make ?loc @@ GCases (Constr.LetPatternStyle, None, [tm,(Anonymous,None)], eqns)
let apply_cases_pattern ?loc (ids_disjpat,id) c =
apply_cases_pattern_term ?loc ids_disjpat (DAst.make ?loc (GVar id)) c
let glob_constr_of_notation_constr_with_binders ?loc g f ?(h=default_binder_status_fun) e nc =
let lt x = DAst.make ?loc x in lt @@ match nc with
| NVar id -> GVar id
| NApp (a,args) -> let e = h.no e in DAst.get (mkGApp (f e a) (List.map (f e) args))
| NProj (p,args,c) -> let e = h.no e in GProj (p, List.map (f e) args, f e c)
| NList (x,y,iter,tail,swap) ->
let t = f e tail in let it = f e iter in
let innerl = (ldots_var,t)::(if swap then [y, lt @@ GVar x] else []) in
let inner = lt @@ GApp (lt @@ GVar (ldots_var),[subst_glob_vars innerl it]) in
let outerl = (ldots_var,inner)::(if swap then [] else [y, lt @@ GVar x]) in
DAst.get (subst_glob_vars outerl it)
| NBinderList (x,y,iter,tail,swap) ->
let t = f e tail in let it = f e iter in
let innerl = (ldots_var,t)::(if swap then [y, lt @@ GVar x] else []) in
let inner = lt @@ GApp (lt @@ GVar ldots_var,[subst_glob_vars innerl it]) in
let outerl = (ldots_var,inner)::(if swap then [] else [y, lt @@ GVar x]) in
DAst.get (subst_glob_vars outerl it)
| NLambda (na,ty,c) ->
let e = h.switch_lambda e in
let ty = Option.map (f (h.restart_prod e)) ty in
let e',disjpat,na',bk,ty = g e na ty in
GLambda (na',bk,set_anonymous_type na ty,Option.fold_right (apply_cases_pattern ?loc) disjpat (f e' c))
| NProd (na,ty,c) ->
let e = h.switch_prod e in
let ty = Option.map (f (h.restart_prod e)) ty in
let e',disjpat,na',bk,ty = g e na ty in
GProd (na',bk,set_anonymous_type na ty,Option.fold_right (apply_cases_pattern ?loc) disjpat (f e' c))
| NLetIn (na,b,t,c) ->
let t = Option.map (f (h.restart_prod e)) t in
let e',disjpat,na,bk,t = g e na t in
test_implicit_argument_mark bk;
(match disjpat with
| None -> GLetIn (na,f (h.restart_lambda e) b,t,f e' c)
| Some (disjpat,_id) -> test_pattern_cast t; DAst.get (apply_cases_pattern_term ?loc disjpat (f e b) (f e' c)))
| NCases (sty,rtntypopt,tml,eqnl) ->
let e = h.no e in
let e',tml' = List.fold_right (fun (tm,(na,t)) (e',tml') ->
let e',t' = match t with
| None -> e',None
| Some (ind,nal) ->
let e',nal' = List.fold_right (fun na (e',nal) ->
let e',na' = protect g e' na in
e',na'::nal) nal (e',[]) in
e',Some (CAst.make ?loc (ind,nal')) in
let e',na' = protect g e' na in
(e',(f e tm,(na',t'))::tml')) tml (e,[]) in
let fold (idl,e) na =
let (e,disjpat,na,bk,t) = g e na None in
test_implicit_argument_mark bk;
test_pattern_cast t;
((Name.cons na idl,e),disjpat,na) in
let eqnl' = List.map (fun (patl,rhs) ->
let ((idl,e),patl) =
List.fold_left_map (cases_pattern_fold_map ?loc fold) ([],e) patl in
let disjpatl = product_of_cases_patterns patl in
List.map (fun patl -> CAst.make (idl,patl,f e rhs)) disjpatl) eqnl in
GCases (sty,Option.map (f e') rtntypopt,tml',List.flatten eqnl')
| NLetTuple (nal,(na,po),b,c) ->
let e = h.no e in
let e',nal = List.fold_left_map (protect g) e nal in
let e'',na = protect g e na in
GLetTuple (nal,(na,Option.map (f e'') po),f e b,f e' c)
| NIf (c,(na,po),b1,b2) ->
let e = h.no e in
let e',na = protect g e na in
GIf (f e c,(na,Option.map (f e') po),f e b1,f e b2)
| NRec (fk,idl,dll,tl,bl) ->
let e = h.no e in
let e,dll = Array.fold_left_map (List.fold_left_map (fun e (na,oc,b) ->
let e,na = protect g e na in
(e,(na,Explicit,Option.map (f e) oc,f e b)))) e dll in
let e',idl = Array.fold_left_map (to_id (protect g)) e idl in
GRec (fk,idl,dll,Array.map (f e) tl,Array.map (f e') bl)
| NCast (c,k,t) -> GCast (f e c, k, f (h.slide e) t)
| NSort x -> GSort x
| NHole (x, naming, arg) -> GHole (x, naming, arg)
| NRef (x,u) -> GRef (x,u)
| NInt i -> GInt i
| NFloat f -> GFloat f
| NArray (t,def,ty) -> GArray(None, Array.map (f e) t, f e def, f e ty)
let glob_constr_of_notation_constr ?loc x =
let rec aux () x =
glob_constr_of_notation_constr_with_binders ?loc (fun () id t -> ((),None,id,Explicit,t)) aux () x
in aux () x
(******************************************************************************)
(* Translating a glob_constr into a notation, interpreting recursive patterns *)
let print_parentheses = ref false
type found_variables = {
vars : Id.t list;
recursive_term_vars : (Id.t * Id.t) list;
recursive_binders_vars : (Id.t * Id.t) list;
}
let add_id r id = r := { !r with vars = id :: (!r).vars }
let add_name r = function Anonymous -> () | Name id -> add_id r id
let mkNApp1 (g,a) =
match g with
(* Ensure flattening of nested applicative nodes *)
| NApp (g,args') -> NApp (g,args'@[a])
| _ -> NApp (g,[a])
let is_gvar id c = match DAst.get c with
| GVar id' -> Id.equal id id'
| _ -> false
let split_at_recursive_part c =
let sub = ref None in
let rec aux c =
let loc0 = c.CAst.loc in
match DAst.get c with
| GApp (f, c::l) when is_gvar ldots_var f -> (* *)
let loc = f.CAst.loc in
begin match !sub with
| None ->
let () = sub := Some c in
begin match l with
| [] -> DAst.make ?loc @@ GVar ldots_var
| _ :: _ -> DAst.make ?loc:loc0 @@ GApp (DAst.make ?loc @@ GVar ldots_var, l)
end
| Some _ ->
(* Not narrowed enough to find only one recursive part *)
raise Not_found
end
| _ -> map_glob_constr aux c in
let outer_iterator = aux c in
match !sub with
| None -> (* No recursive pattern found *) raise Not_found
| Some c ->
match DAst.get outer_iterator with
| GVar v when Id.equal v ldots_var -> (* Not enough context *) raise Not_found
| _ -> outer_iterator, c
let subtract_loc loc1 loc2 =
let l1 = fst (Option.cata Loc.unloc (0,0) loc1) in
let l2 = fst (Option.cata Loc.unloc (0,0) loc2) in
Some (Loc.make_loc (l1,l2-1))
let check_is_hole id t = match DAst.get t with GHole _ -> () | _ ->
user_err ?loc:(loc_of_glob_constr t)
(strbrk "In recursive notation with binders, " ++ Id.print id ++
strbrk " is expected to come without type.")
let check_pair_matching ?loc x y x' y' revert revert' =
if not (Id.equal x x' && Id.equal y y' && revert = revert') then
let x,y = if revert then y,x else x,y in
let x',y' = if revert' then y',x' else x',y' in
(* This is a case where one would like to highlight two locations! *)
user_err ?loc
(strbrk "Found " ++ Id.print x ++ strbrk " matching " ++ Id.print y ++
strbrk " while " ++ Id.print x' ++ strbrk " matching " ++ Id.print y' ++
strbrk " was first found.")
let pair_equal eq1 eq2 (a,b) (a',b') = eq1 a a' && eq2 b b'
let mem_recursive_pair (x,y) l = List.mem_f (pair_equal Id.equal Id.equal) (x,y) l
type recursive_pattern_kind =
| RecursiveTerms of bool (* in reverse order *)
| RecursiveBinders of bool (* in reverse order *)
let compare_recursive_parts recvars found f f' (iterator,subc) =
let diff = ref None in
let terminator = ref None in
let rec aux c1 c2 = match DAst.get c1, DAst.get c2 with
| GVar v, term when Id.equal v ldots_var ->
(* We found the pattern *)
assert (match !terminator with None -> true | Some _ -> false);
terminator := Some c2;
true
| GApp (f,l1), GApp (term, l2) ->
begin match DAst.get f with
| GVar v when Id.equal v ldots_var ->
(* We found the pattern, but there are extra arguments *)
(* (this allows e.g. alternative (recursive) notation of application) *)
assert (match !terminator with None -> true | Some _ -> false);
terminator := Some term;
List.for_all2eq aux l1 l2
| _ -> mk_glob_constr_eq aux c1 c2
end
| GVar x, GVar y
when mem_recursive_pair (x,y) recvars || mem_recursive_pair (y,x) recvars ->
(* We found the position where it differs *)
let revert = mem_recursive_pair (y,x) recvars in
let x,y = if revert then y,x else x,y in
begin match !diff with
| None ->
let () = diff := Some (x, y, RecursiveTerms revert) in
true
| Some (x', y', RecursiveTerms revert')
| Some (x', y', RecursiveBinders revert') ->
check_pair_matching ?loc:c1.CAst.loc x y x' y' revert revert';
true
end
| GLambda (Name x,_,t_x,c), GLambda (Name y,_,t_y,term)
| GProd (Name x,_,t_x,c), GProd (Name y,_,t_y,term)
when mem_recursive_pair (x,y) recvars || mem_recursive_pair (y,x) recvars ->
(* We found a binding position where it differs *)
check_is_hole x t_x;
check_is_hole y t_y;
let revert = mem_recursive_pair (y,x) recvars in
let x,y = if revert then y,x else x,y in
begin match !diff with
| None ->
let () = diff := Some (x, y, RecursiveBinders revert) in
aux c term
| Some (x', y', RecursiveBinders revert') ->
check_pair_matching ?loc:c1.CAst.loc x y x' y' revert revert';
true
| Some (x', y', RecursiveTerms revert') ->
(* Recursive binders have precedence: they can be coerced to
terms but not reciprocally *)
check_pair_matching ?loc:c1.CAst.loc x y x' y' revert revert';
let () = diff := Some (x, y, RecursiveBinders revert) in
true
end
| _ ->
mk_glob_constr_eq aux c1 c2 in
if aux iterator subc then
match !diff with
| None ->
let loc1 = loc_of_glob_constr iterator in
let loc2 = loc_of_glob_constr (Option.get !terminator) in
(* Here, we would need a loc made of several parts ... *)
user_err ?loc:(subtract_loc loc1 loc2)
(str "Both ends of the recursive pattern are the same.")
| Some (x,y,RecursiveTerms revert) ->
(* By arbitrary convention, we use the second variable of the pair
as the place-holder for the iterator *)
let iterator =
f' (if revert then iterator else subst_glob_vars [x, DAst.make @@ GVar y] iterator) in
(* found variables have been collected by compare_constr *)
found := { !found with vars = List.remove Id.equal y (!found).vars;
recursive_term_vars = List.add_set (pair_equal Id.equal Id.equal) (x,y) (!found).recursive_term_vars };
NList (x,y,iterator,f (Option.get !terminator),revert)
| Some (x,y,RecursiveBinders revert) ->
let iterator =
f' (if revert then iterator else subst_glob_vars [x, DAst.make @@ GVar y] iterator) in
(* found have been collected by compare_constr *)
found := { !found with vars = List.remove Id.equal y (!found).vars;
recursive_binders_vars = List.add_set (pair_equal Id.equal Id.equal) (x,y) (!found).recursive_binders_vars };
NBinderList (x,y,iterator,f (Option.get !terminator),revert)
else
raise Not_found
let notation_constr_and_vars_of_glob_constr recvars a =
let found = ref { vars = []; recursive_term_vars = []; recursive_binders_vars = [] } in
let has_ltac = ref false in
(* Turn a glob_constr into a notation_constr by first trying to find a recursive pattern *)
let rec aux c =
let keepfound = !found in
(* n^2 complexity but small and done only once per notation *)
try compare_recursive_parts recvars found aux aux' (split_at_recursive_part c)
with Not_found ->
found := keepfound;
match DAst.get c with
| GApp (t, [_]) ->
begin match DAst.get t with
| GVar f when Id.equal f ldots_var ->
(* Fall on the second part of the recursive pattern w/o having
found the first part *)
let loc = t.CAst.loc in
user_err ?loc
(str "Cannot find where the recursive pattern starts.")
| _ -> aux' c
end
| _c ->
aux' c
and aux' x = DAst.with_val (function
| GVar id -> if not (Id.equal id ldots_var) then add_id found id; NVar id
| GApp (g,[]) -> NApp (aux g,[]) (* Encoding @foo *)
| GApp (g,args) ->
(* Treat applicative notes as binary nodes *)
let a,args = List.sep_last args in mkNApp1 (aux (DAst.make (GApp (g, args))), aux a)
| GProj (p,args,c) -> NProj (p, List.map aux args, aux c)
| GLambda (na,bk,ty,c) -> add_name found na; NLambda (na,aux_type ty,aux c)
| GProd (na,bk,ty,c) -> add_name found na; NProd (na,aux_type ty,aux c)
| GLetIn (na,b,t,c) -> add_name found na; NLetIn (na,aux b,Option.map aux t, aux c)
| GCases (sty,rtntypopt,tml,eqnl) ->
let f {CAst.v=(idl,pat,rhs)} = List.iter (add_id found) idl; (pat,aux rhs) in
NCases (sty,Option.map aux rtntypopt,
List.map (fun (tm,(na,x)) ->
add_name found na;
Option.iter
(fun {CAst.v=(_,nl)} -> List.iter (add_name found) nl) x;
(aux tm,(na,Option.map (fun {CAst.v=(ind,nal)} -> (ind,nal)) x))) tml,
List.map f eqnl)
| GLetTuple (nal,(na,po),b,c) ->
add_name found na;
List.iter (add_name found) nal;
NLetTuple (nal,(na,Option.map aux po),aux b,aux c)
| GIf (c,(na,po),b1,b2) ->
add_name found na;
NIf (aux c,(na,Option.map aux po),aux b1,aux b2)
| GRec (fk,idl,dll,tl,bl) ->
Array.iter (add_id found) idl;
let dll = Array.map (List.map (fun (na,bk,oc,b) ->
if bk != Explicit then
user_err Pp.(str "Binders marked as implicit not allowed in notations.");
add_name found na; (na,Option.map aux oc,aux b))) dll in
NRec (fk,idl,dll,Array.map aux tl,Array.map aux bl)
| GCast (c,k,t) -> NCast (aux c, k, aux t)
| GSort s -> NSort s
| GInt i -> NInt i
| GFloat f -> NFloat f
| GHole (w,naming,arg) ->
if arg != None then has_ltac := true;
NHole (w, naming, arg)
| GRef (r,u) -> NRef (r,u)
| GArray (_u,t,def,ty) -> NArray (Array.map aux t, aux def, aux ty)
| GEvar _ | GPatVar _ ->
user_err Pp.(str "Existential variables not allowed in notations.")
) x
and aux_type t = DAst.with_val (function
| GHole (Evar_kinds.BinderType _,IntroAnonymous,None) -> None
| _ -> Some (aux t)) t
in
let t = aux a in
(* Side effect *)
t, !found, !has_ltac
let check_variables_and_reversibility nenv
{ vars = found; recursive_term_vars = foundrec; recursive_binders_vars = foundrecbinding } =
let injective = ref [] in
let recvars = nenv.ninterp_rec_vars in
let fold _ y accu = Id.Set.add y accu in
let useless_vars = Id.Map.fold fold recvars Id.Set.empty in
let filter y _ = not (Id.Set.mem y useless_vars) in
let vars = Id.Map.filter filter nenv.ninterp_var_type in
let check_recvar x =
if Id.List.mem x found then
user_err (Id.print x ++
strbrk " should only be used in the recursive part of a pattern.") in
let check (x, y) = check_recvar x; check_recvar y in
let () = List.iter check foundrec in
let () = List.iter check foundrecbinding in
let check_bound x =
if not (Id.List.mem x found) then
if Id.List.mem_assoc x foundrec ||
Id.List.mem_assoc x foundrecbinding ||
Id.List.mem_assoc_sym x foundrec ||
Id.List.mem_assoc_sym x foundrecbinding
then
user_err Pp.(str
(Id.to_string x ^
" should not be bound in a recursive pattern of the right-hand side."))
else injective := x :: !injective
in
let check_pair s x y where =
if not (mem_recursive_pair (x,y) where) then
user_err (strbrk "in the right-hand side, " ++ Id.print x ++
str " and " ++ Id.print y ++ strbrk " should appear in " ++ str s ++
str " position as part of a recursive pattern.") in
let check_type x typ =
match typ with
| NtnInternTypeAny _ ->
begin
try check_pair "term" x (Id.Map.find x recvars) foundrec
with Not_found -> check_bound x
end
| NtnInternTypeOnlyBinder ->
begin
try check_pair "binding" x (Id.Map.find x recvars) foundrecbinding
with Not_found -> check_bound x
end in
Id.Map.iter check_type vars;
List.rev !injective
let notation_constr_of_glob_constr nenv a =
let recvars = Id.Map.bindings nenv.ninterp_rec_vars in
let a, found, has_ltac = notation_constr_and_vars_of_glob_constr recvars a in
let injective = check_variables_and_reversibility nenv found in
let status = if has_ltac then HasLtac else match injective with
| [] -> APrioriReversible
| l -> NonInjective l in
a, status
(**********************************************************************)
(* Substitution of kernel names, avoiding a list of bound identifiers *)
let notation_constr_of_constr avoiding t =
let t = EConstr.of_constr t in
let env = Global.env () in
let evd = Evd.from_env env in
let t = Detyping.detype Detyping.Now false avoiding env evd t in
let nenv = {
ninterp_var_type = Id.Map.empty;
ninterp_rec_vars = Id.Map.empty;
} in
notation_constr_of_glob_constr nenv t
let rec subst_pat subst pat =
match DAst.get pat with
| PatVar _ -> pat
| PatCstr (((kn,i),j),cpl,n) ->
let kn' = subst_mind subst kn
and cpl' = List.Smart.map (subst_pat subst) cpl in
if kn' == kn && cpl' == cpl then pat else
DAst.make ?loc:pat.CAst.loc @@ PatCstr (((kn',i),j),cpl',n)
let rec subst_notation_constr subst bound raw =
match raw with
| NRef (ref,u) ->
let ref',t = subst_global subst ref in
if ref' == ref then raw else (match t with
| None -> NRef (ref',u)
| Some t ->
fst (notation_constr_of_constr bound t.Univ.univ_abstracted_value))
| NVar _ -> raw
| NApp (r,rl) ->
let r' = subst_notation_constr subst bound r
and rl' = List.Smart.map (subst_notation_constr subst bound) rl in
if r' == r && rl' == rl then raw else
NApp(r',rl')
| NProj ((cst,u),rl,r) ->
let ref = GlobRef.ConstRef cst in
let ref',t = subst_global subst ref in
assert (t = None);
let rl' = List.Smart.map (subst_notation_constr subst bound) rl
and r' = subst_notation_constr subst bound r in
if ref' == ref && rl' == rl && r' == r then raw else
NProj ((destConstRef ref',u),rl',r')
| NList (id1,id2,r1,r2,b) ->
let r1' = subst_notation_constr subst bound r1
and r2' = subst_notation_constr subst bound r2 in
if r1' == r1 && r2' == r2 then raw else
NList (id1,id2,r1',r2',b)
| NLambda (n,r1,r2) ->
let r1' = Option.Smart.map (subst_notation_constr subst bound) r1
and r2' = subst_notation_constr subst bound r2 in
if r1' == r1 && r2' == r2 then raw else
NLambda (n,r1',r2')
| NProd (n,r1,r2) ->
let r1' = Option.Smart.map (subst_notation_constr subst bound) r1
and r2' = subst_notation_constr subst bound r2 in
if r1' == r1 && r2' == r2 then raw else
NProd (n,r1',r2')
| NBinderList (id1,id2,r1,r2,b) ->
let r1' = subst_notation_constr subst bound r1
and r2' = subst_notation_constr subst bound r2 in
if r1' == r1 && r2' == r2 then raw else
NBinderList (id1,id2,r1',r2',b)
| NLetIn (n,r1,t,r2) ->
let r1' = subst_notation_constr subst bound r1 in
let t' = Option.Smart.map (subst_notation_constr subst bound) t in
let r2' = subst_notation_constr subst bound r2 in
if r1' == r1 && t == t' && r2' == r2 then raw else
NLetIn (n,r1',t',r2')
| NCases (sty,rtntypopt,rl,branches) ->
let rtntypopt' = Option.Smart.map (subst_notation_constr subst bound) rtntypopt
and rl' = List.Smart.map
(fun (a,(n,signopt) as x) ->
let a' = subst_notation_constr subst bound a in
let signopt' = Option.map (fun ((indkn,i),nal as z) ->
let indkn' = subst_mind subst indkn in
if indkn == indkn' then z else ((indkn',i),nal)) signopt in
if a' == a && signopt' == signopt then x else (a',(n,signopt')))
rl
and branches' = List.Smart.map
(fun (cpl,r as branch) ->
let cpl' = List.Smart.map (subst_pat subst) cpl
and r' = subst_notation_constr subst bound r in
if cpl' == cpl && r' == r then branch else
(cpl',r'))
branches
in
if rtntypopt' == rtntypopt && rtntypopt == rtntypopt' &&
rl' == rl && branches' == branches then raw else
NCases (sty,rtntypopt',rl',branches')
| NLetTuple (nal,(na,po),b,c) ->
let po' = Option.Smart.map (subst_notation_constr subst bound) po
and b' = subst_notation_constr subst bound b
and c' = subst_notation_constr subst bound c in
if po' == po && b' == b && c' == c then raw else
NLetTuple (nal,(na,po'),b',c')
| NIf (c,(na,po),b1,b2) ->
let po' = Option.Smart.map (subst_notation_constr subst bound) po
and b1' = subst_notation_constr subst bound b1
and b2' = subst_notation_constr subst bound b2
and c' = subst_notation_constr subst bound c in
if po' == po && b1' == b1 && b2' == b2 && c' == c then raw else
NIf (c',(na,po'),b1',b2')
| NRec (fk,idl,dll,tl,bl) ->
let dll' =
Array.Smart.map (List.Smart.map (fun (na,oc,b as x) ->
let oc' = Option.Smart.map (subst_notation_constr subst bound) oc in
let b' = subst_notation_constr subst bound b in
if oc' == oc && b' == b then x else (na,oc',b'))) dll in
let tl' = Array.Smart.map (subst_notation_constr subst bound) tl in
let bl' = Array.Smart.map (subst_notation_constr subst bound) bl in
if dll' == dll && tl' == tl && bl' == bl then raw else
NRec (fk,idl,dll',tl',bl')
| NSort _ -> raw
| NInt _ -> raw
| NFloat _ -> raw
| NHole (knd, naming, solve) ->
let nknd = match knd with
| Evar_kinds.ImplicitArg (ref, i, b) ->
let nref, _ = subst_global subst ref in
if nref == ref then knd else Evar_kinds.ImplicitArg (nref, i, b)
| _ -> knd
in
let nsolve = Option.Smart.map (Genintern.generic_substitute subst) solve in
if nsolve == solve && nknd == knd then raw
else NHole (nknd, naming, nsolve)
| NCast (r1,k,t) ->
let r1' = subst_notation_constr subst bound r1 in
let t' = subst_notation_constr subst bound t in
if r1' == r1 && t' == t then raw else NCast(r1',k,t')
| NArray (t,def,ty) ->
let def' = subst_notation_constr subst bound def
and t' = Array.Smart.map (subst_notation_constr subst bound) t
and ty' = subst_notation_constr subst bound ty
in
if def' == def && t' == t && ty' == ty then raw else
NArray(t',def',ty')
let subst_interpretation subst (metas,pat) =
let bound = List.fold_left (fun accu (id, _) -> Id.Set.add id accu) Id.Set.empty metas in
(metas,subst_notation_constr subst bound pat)
(**********************************************************************)
(* Pattern-matching a [glob_constr] against a [notation_constr] *)
let abstract_return_type_context pi mklam tml rtno =
Option.map (fun rtn ->
let nal =
List.flatten (List.map (fun (_,(na,t)) ->
match t with Some x -> (pi x)@[na] | None -> [na]) tml) in
List.fold_right mklam nal rtn)
rtno
let abstract_return_type_context_glob_constr tml rtn =
abstract_return_type_context (fun {CAst.v=(_,nal)} -> nal)
(fun na c -> DAst.make @@
GLambda(na,Explicit,DAst.make @@ GHole(Evar_kinds.InternalHole,IntroAnonymous,None),c)) tml rtn
let abstract_return_type_context_notation_constr tml rtn =
abstract_return_type_context snd
(fun na c -> NLambda(na,None,c)) tml rtn
let rec push_pattern_binders vars pat =
match DAst.get pat with
| PatVar na -> Termops.add_vname vars na
| PatCstr (c, pl, na) -> List.fold_left push_pattern_binders (Termops.add_vname vars na) pl
let rec push_context_binders vars = function
| [] -> vars
| b :: bl ->
let vars = match DAst.get b with
| GLocalAssum (na,_,_) -> Termops.add_vname vars na
| GLocalPattern ((disjpat,ids),p,bk,t) -> List.fold_right Id.Set.add ids vars
| GLocalDef (na,_,_) -> Termops.add_vname vars na in
push_context_binders vars bl
let is_term_meta id metas =
try match Id.List.assoc id metas with _,(NtnTypeConstr | NtnTypeConstrList) -> true | _ -> false
with Not_found -> false
let is_onlybinding_strict_meta id metas =
try match Id.List.assoc id metas with _,NtnTypeBinder (NtnParsedAsPattern true) -> true | _ -> false
with Not_found -> false
let is_onlybinding_meta id metas =
try match Id.List.assoc id metas with _,NtnTypeBinder _ -> true | _ -> false
with Not_found -> false
let is_onlybinding_pattern_like_meta isvar id metas =
try match Id.List.assoc id metas with
| _,NtnTypeBinder (NtnBinderParsedAsConstr
(AsNameOrPattern | AsStrictPattern)) -> true
| _,NtnTypeBinder (NtnParsedAsPattern strict) -> not (strict && isvar)
| _,NtnTypeBinder NtnParsedAsBinder -> not isvar
| _ -> false
with Not_found -> false
let is_bindinglist_meta id metas =
try match Id.List.assoc id metas with _,NtnTypeBinderList -> true | _ -> false
with Not_found -> false
exception No_match
let alpha_rename alpmetas v =
if alpmetas == [] then v
else try rename_glob_vars alpmetas v with UnsoundRenaming -> raise No_match
let add_env (vars,(alp,alpmetas)) (terms,termlists,binders,binderlists) var v =
(* Check that no capture of binding variables occur *)
(* [alp] is used when matching a pattern "fun x => ... x ... ?var ... x ..."
with an actual term "fun z => ... z ..." when "x" is not bound in the
notation, as in "Notation "'twice_upto' y" := (fun x => x + x + y)". Then
we keep (z,x) in alp, and we have to check that what the [v] which is bound
to [var] does not contain z *)
if not (Id.equal ldots_var var) &&
List.exists (fun (id,_) -> occur_glob_constr id v) alp then raise No_match;
(* [alpmetas] is used when matching a pattern "fun x => ... x ... ?var ... x ..."
with an actual term "fun z => ... z ..." when "x" is bound in the
notation and the name "x" cannot be changed to "z", e.g. because
used at another occurrence, as in "Notation "'lam' y , P & Q" :=
((fun y => P),(fun y => Q))". Then, we keep (z,y) in alpmetas, and we
have to check that "fun z => ... z ..." denotes the same term as
"fun x => ... x ... ?var ... x" up to alpha-conversion when [var]
is instantiated by [v];
Currently, we fail, but, eventually, [x] in [v] could be replaced by [x],
and, in match_, when finding "x" in subterm, failing because of a capture,
and, in match_, when finding "z" in subterm, replacing it with "x",
and, in an even further step, being even more robust, independent of the order, so
that e.g. the notation for ex2 works on "x y |- ex2 (fun x => y=x) (fun y => x=y)"
by giving, say, "exists2 x0, y=x0 & x=x0", but this would typically require the
glob_constr_eq in bind_term_env to be postponed in match_notation_constr, and the
choice of exact variable be done there; but again, this would be a non-trivial
refinement *)
let v = alpha_rename alpmetas v in
(* TODO: handle the case of multiple occs in different scopes *)
((var,(vars,v))::terms,termlists,binders,binderlists)
let add_termlist_env (vars,(alp,alpmetas)) (terms,termlists,binders,binderlists) var vl =
if List.exists (fun (id,_) -> List.exists (occur_glob_constr id) vl) alp then raise No_match;
let vl = List.map (alpha_rename alpmetas) vl in
(terms,(var,(vars,vl))::termlists,binders,binderlists)
let add_binding_env (vars,alp) (terms,termlists,binders,binderlists) var v =
(* TODO: handle the case of multiple occs in different scopes *)
(terms,termlists,(var,(vars,v))::binders,binderlists)
let add_bindinglist_env (vars,alp) (terms,termlists,binders,binderlists) var bl =
(terms,termlists,binders,(var,(vars,bl))::binderlists)
let rec map_cases_pattern_name_left f = DAst.map (function
| PatVar na -> PatVar (f na)
| PatCstr (c,l,na) -> PatCstr (c,List.map_left (map_cases_pattern_name_left f) l,f na)
)
let rec fold_cases_pattern_eq f x p p' =
let loc = p.CAst.loc in
match DAst.get p, DAst.get p' with
| PatVar na, PatVar na' -> let x,na = f x na na' in x, DAst.make ?loc @@ PatVar na