-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevd.ml
1778 lines (1493 loc) · 57.4 KB
/
evd.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
(************************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2015 *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
open Pp
open Errors
open Util
open Names
open Nameops
open Term
open Vars
open Termops
open Environ
open Globnames
(** Generic filters *)
module Filter :
sig
type t
val equal : t -> t -> bool
val identity : t
val filter_list : t -> 'a list -> 'a list
val filter_array : t -> 'a array -> 'a array
val extend : int -> t -> t
val compose : t -> t -> t
val apply_subfilter : t -> bool list -> t
val restrict_upon : t -> int -> (int -> bool) -> t option
val map_along : (bool -> 'a -> bool) -> t -> 'a list -> t
val make : bool list -> t
val repr : t -> bool list option
end =
struct
type t = bool list option
(** We guarantee through the interface that if a filter is [Some _] then it
contains at least one [false] somewhere. *)
let identity = None
let rec equal l1 l2 = match l1, l2 with
| [], [] -> true
| h1 :: l1, h2 :: l2 ->
(if h1 then h2 else not h2) && equal l1 l2
| _ -> false
let equal l1 l2 = match l1, l2 with
| None, None -> true
| Some _, None | None, Some _ -> false
| Some l1, Some l2 -> equal l1 l2
let rec is_identity = function
| [] -> true
| true :: l -> is_identity l
| false :: _ -> false
let normalize f = if is_identity f then None else Some f
let filter_list f l = match f with
| None -> l
| Some f -> CList.filter_with f l
let filter_array f v = match f with
| None -> v
| Some f -> CArray.filter_with f v
let rec extend n l =
if n = 0 then l
else extend (pred n) (true :: l)
let extend n = function
| None -> None
| Some f -> Some (extend n f)
let compose f1 f2 = match f1 with
| None -> f2
| Some f1 ->
match f2 with
| None -> None
| Some f2 -> normalize (CList.filter_with f1 f2)
let apply_subfilter_array filter subfilter =
(** In both cases we statically know that the argument will contain at
least one [false] *)
match filter with
| None -> Some (Array.to_list subfilter)
| Some f ->
let len = Array.length subfilter in
let fold b (i, ans) =
if b then
let () = assert (0 <= i) in
(pred i, Array.unsafe_get subfilter i :: ans)
else
(i, false :: ans)
in
Some (snd (List.fold_right fold f (pred len, [])))
let apply_subfilter filter subfilter =
apply_subfilter_array filter (Array.of_list subfilter)
let restrict_upon f len p =
let newfilter = Array.init len p in
if Array.for_all (fun id -> id) newfilter then None
else
Some (apply_subfilter_array f newfilter)
let map_along f flt l =
let ans = match flt with
| None -> List.map (fun x -> f true x) l
| Some flt -> List.map2 f flt l
in
normalize ans
let make l = normalize l
let repr f = f
end
(* The kinds of existential variables are now defined in [Evar_kinds] *)
(* The type of mappings for existential variables *)
module Dummy = struct end
module Store = Store.Make(Dummy)
type evar = Term.existential_key
let string_of_existential evk = "?X" ^ string_of_int (Evar.repr evk)
type evar_body =
| Evar_empty
| Evar_defined of constr
type evar_info = {
evar_concl : constr;
evar_hyps : named_context_val;
evar_body : evar_body;
evar_filter : Filter.t;
evar_source : Evar_kinds.t Loc.located;
evar_candidates : constr list option; (* if not None, list of allowed instances *)
evar_extra : Store.t }
let make_evar hyps ccl = {
evar_concl = ccl;
evar_hyps = hyps;
evar_body = Evar_empty;
evar_filter = Filter.identity;
evar_source = (Loc.ghost,Evar_kinds.InternalHole);
evar_candidates = None;
evar_extra = Store.empty
}
let instance_mismatch () =
anomaly (Pp.str "Signature and its instance do not match")
let evar_concl evi = evi.evar_concl
let evar_filter evi = evi.evar_filter
let evar_body evi = evi.evar_body
let evar_context evi = named_context_of_val evi.evar_hyps
let evar_filtered_context evi =
Filter.filter_list (evar_filter evi) (evar_context evi)
let evar_hyps evi = evi.evar_hyps
let evar_filtered_hyps evi = match Filter.repr (evar_filter evi) with
| None -> evar_hyps evi
| Some filter ->
let rec make_hyps filter ctxt = match filter, ctxt with
| [], [] -> empty_named_context_val
| false :: filter, _ :: ctxt -> make_hyps filter ctxt
| true :: filter, decl :: ctxt ->
let hyps = make_hyps filter ctxt in
push_named_context_val decl hyps
| _ -> instance_mismatch ()
in
make_hyps filter (evar_context evi)
let evar_env evi = Global.env_of_context evi.evar_hyps
let evar_filtered_env evi = match Filter.repr (evar_filter evi) with
| None -> evar_env evi
| Some filter ->
let rec make_env filter ctxt = match filter, ctxt with
| [], [] -> reset_context (Global.env ())
| false :: filter, _ :: ctxt -> make_env filter ctxt
| true :: filter, decl :: ctxt ->
let env = make_env filter ctxt in
push_named decl env
| _ -> instance_mismatch ()
in
make_env filter (evar_context evi)
let map_evar_body f = function
| Evar_empty -> Evar_empty
| Evar_defined d -> Evar_defined (f d)
let map_evar_info f evi =
{evi with
evar_body = map_evar_body f evi.evar_body;
evar_hyps = map_named_val f evi.evar_hyps;
evar_concl = f evi.evar_concl;
evar_candidates = Option.map (List.map f) evi.evar_candidates }
let evar_ident_info evi =
match evi.evar_source with
| _,Evar_kinds.ImplicitArg (c,(n,Some id),b) -> id
| _,Evar_kinds.VarInstance id -> id
| _,Evar_kinds.GoalEvar -> Id.of_string "Goal"
| _ ->
let env = reset_with_named_context evi.evar_hyps (Global.env()) in
Namegen.id_of_name_using_hdchar env evi.evar_concl Anonymous
(* This exception is raised by *.existential_value *)
exception NotInstantiatedEvar
(* Note: let-in contributes to the instance *)
let evar_instance_array test_id info args =
let len = Array.length args in
let rec instrec filter ctxt i = match filter, ctxt with
| [], [] ->
if Int.equal i len then []
else instance_mismatch ()
| false :: filter, _ :: ctxt ->
instrec filter ctxt i
| true :: filter, (id,_,_ as d) :: ctxt ->
if i < len then
let c = Array.unsafe_get args i in
if test_id d c then instrec filter ctxt (succ i)
else (id, c) :: instrec filter ctxt (succ i)
else instance_mismatch ()
| _ -> instance_mismatch ()
in
match Filter.repr (evar_filter info) with
| None ->
let map i (id,_,_ as d) =
if (i < len) then
let c = Array.unsafe_get args i in
if test_id d c then None else Some (id,c)
else instance_mismatch ()
in
List.map_filter_i map (evar_context info)
| Some filter ->
instrec filter (evar_context info) 0
let make_evar_instance_array info args =
evar_instance_array (fun (id,_,_) -> isVarId id) info args
let instantiate_evar_array info c args =
let inst = make_evar_instance_array info args in
match inst with
| [] -> c
| _ -> replace_vars inst c
module StringOrd = struct type t = string let compare = String.compare end
module UNameMap = struct
include Map.Make(StringOrd)
let union s t =
if s == t then s
else
merge (fun k l r ->
match l, r with
| Some _, _ -> l
| _, _ -> r) s t
end
(* 2nd part used to check consistency on the fly. *)
type evar_universe_context =
{ uctx_names : Univ.Level.t UNameMap.t * string Univ.LMap.t;
uctx_local : Univ.universe_context_set; (** The local context of variables *)
uctx_univ_variables : Universes.universe_opt_subst;
(** The local universes that are unification variables *)
uctx_univ_algebraic : Univ.universe_set;
(** The subset of unification variables that
can be instantiated with algebraic universes as they appear in types
and universe instances only. *)
uctx_universes : Univ.universes; (** The current graph extended with the local constraints *)
uctx_initial_universes : Univ.universes; (** The graph at the creation of the evar_map *)
}
let empty_evar_universe_context =
{ uctx_names = UNameMap.empty, Univ.LMap.empty;
uctx_local = Univ.ContextSet.empty;
uctx_univ_variables = Univ.LMap.empty;
uctx_univ_algebraic = Univ.LSet.empty;
uctx_universes = Univ.initial_universes;
uctx_initial_universes = Univ.initial_universes }
let evar_universe_context_from e =
let u = universes e in
{empty_evar_universe_context with
uctx_universes = u; uctx_initial_universes = u}
let is_empty_evar_universe_context ctx =
Univ.ContextSet.is_empty ctx.uctx_local &&
Univ.LMap.is_empty ctx.uctx_univ_variables
let union_evar_universe_context ctx ctx' =
if ctx == ctx' then ctx
else if is_empty_evar_universe_context ctx' then ctx
else
let local = Univ.ContextSet.union ctx.uctx_local ctx'.uctx_local in
let names = UNameMap.union (fst ctx.uctx_names) (fst ctx'.uctx_names) in
let names_rev = Univ.LMap.union (snd ctx.uctx_names) (snd ctx'.uctx_names) in
{ uctx_names = (names, names_rev);
uctx_local = local;
uctx_univ_variables =
Univ.LMap.subst_union ctx.uctx_univ_variables ctx'.uctx_univ_variables;
uctx_univ_algebraic =
Univ.LSet.union ctx.uctx_univ_algebraic ctx'.uctx_univ_algebraic;
uctx_initial_universes = ctx.uctx_initial_universes;
uctx_universes =
if local == ctx.uctx_local then ctx.uctx_universes
else
let cstrsr = Univ.ContextSet.constraints ctx'.uctx_local in
Univ.merge_constraints cstrsr ctx.uctx_universes }
(* let union_evar_universe_context_key = Profile.declare_profile "union_evar_universe_context";; *)
(* let union_evar_universe_context = *)
(* Profile.profile2 union_evar_universe_context_key union_evar_universe_context;; *)
type 'a in_evar_universe_context = 'a * evar_universe_context
let evar_universe_context_set ctx = ctx.uctx_local
let evar_universe_context_constraints ctx = snd ctx.uctx_local
let evar_context_universe_context ctx = Univ.ContextSet.to_context ctx.uctx_local
let evar_universe_context_of ctx = { empty_evar_universe_context with uctx_local = ctx }
let evar_universe_context_subst ctx = ctx.uctx_univ_variables
let instantiate_variable l b v =
(* let b = Univ.subst_large_constraint (Univ.Universe.make l) Univ.type0m_univ b in *)
(* if Univ.univ_depends (Univ.Universe.make l) b then *)
(* error ("Occur-check in universe variable instantiation") *)
(* else *) v := Univ.LMap.add l (Some b) !v
exception UniversesDiffer
let process_universe_constraints univs vars alg cstrs =
let vars = ref vars in
let normalize = Universes.normalize_universe_opt_subst vars in
let rec unify_universes fo l d r local =
let l = normalize l and r = normalize r in
if Univ.Universe.equal l r then local
else
let varinfo x =
match Univ.Universe.level x with
| None -> Inl x
| Some l -> Inr (l, Univ.LMap.mem l !vars, Univ.LSet.mem l alg)
in
if d == Universes.ULe then
if Univ.check_leq univs l r then
(** Keep Prop/Set <= var around if var might be instantiated by prop or set
later. *)
if Univ.Universe.is_level l then
match Univ.Universe.level r with
| Some r ->
Univ.Constraint.add (Option.get (Univ.Universe.level l),Univ.Le,r) local
| _ -> local
else local
else
match Univ.Universe.level r with
| None -> error ("Algebraic universe on the right")
| Some rl ->
if Univ.Level.is_small rl then
let levels = Univ.Universe.levels l in
Univ.LSet.fold (fun l local ->
if Univ.Level.is_small l || Univ.LMap.mem l !vars then
Univ.enforce_eq (Univ.Universe.make l) r local
else raise (Univ.UniverseInconsistency (Univ.Le, Univ.Universe.make l, r, None)))
levels local
else
Univ.enforce_leq l r local
else if d == Universes.ULub then
match varinfo l, varinfo r with
| (Inr (l, true, _), Inr (r, _, _))
| (Inr (r, _, _), Inr (l, true, _)) ->
instantiate_variable l (Univ.Universe.make r) vars;
Univ.enforce_eq_level l r local
| Inr (_, _, _), Inr (_, _, _) ->
unify_universes true l Universes.UEq r local
| _, _ -> assert false
else (* d = Universes.UEq *)
match varinfo l, varinfo r with
| Inr (l', lloc, _), Inr (r', rloc, _) ->
let () =
if lloc then
instantiate_variable l' r vars
else if rloc then
instantiate_variable r' l vars
else if not (Univ.check_eq univs l r) then
(* Two rigid/global levels, none of them being local,
one of them being Prop/Set, disallow *)
if Univ.Level.is_small l' || Univ.Level.is_small r' then
raise (Univ.UniverseInconsistency (Univ.Eq, l, r, None))
else
if fo then
raise UniversesDiffer
in
Univ.enforce_eq_level l' r' local
| _, _ (* One of the two is algebraic or global *) ->
if Univ.check_eq univs l r then local
else raise (Univ.UniverseInconsistency (Univ.Eq, l, r, None))
in
let local =
Universes.Constraints.fold (fun (l,d,r) local -> unify_universes false l d r local)
cstrs Univ.Constraint.empty
in
!vars, local
let add_constraints_context ctx cstrs =
let univs, local = ctx.uctx_local in
let cstrs' = Univ.Constraint.fold (fun (l,d,r) acc ->
let l = Univ.Universe.make l and r = Univ.Universe.make r in
let cstr' =
if d == Univ.Lt then (Univ.Universe.super l, Universes.ULe, r)
else (l, (if d == Univ.Le then Universes.ULe else Universes.UEq), r)
in Universes.Constraints.add cstr' acc)
cstrs Universes.Constraints.empty
in
let vars, local' =
process_universe_constraints ctx.uctx_universes
ctx.uctx_univ_variables ctx.uctx_univ_algebraic
cstrs'
in
{ ctx with uctx_local = (univs, Univ.Constraint.union local local');
uctx_univ_variables = vars;
uctx_universes = Univ.merge_constraints cstrs ctx.uctx_universes }
(* let addconstrkey = Profile.declare_profile "add_constraints_context";; *)
(* let add_constraints_context = Profile.profile2 addconstrkey add_constraints_context;; *)
let add_universe_constraints_context ctx cstrs =
let univs, local = ctx.uctx_local in
let vars, local' =
process_universe_constraints ctx.uctx_universes
ctx.uctx_univ_variables ctx.uctx_univ_algebraic
cstrs
in
{ ctx with uctx_local = (univs, Univ.Constraint.union local local');
uctx_univ_variables = vars;
uctx_universes = Univ.merge_constraints local' ctx.uctx_universes }
(* let addunivconstrkey = Profile.declare_profile "add_universe_constraints_context";; *)
(* let add_universe_constraints_context = *)
(* Profile.profile2 addunivconstrkey add_universe_constraints_context;; *)
(*******************************************************************)
(* Metamaps *)
(*******************************************************************)
(* Constraints for existential variables *)
(*******************************************************************)
type 'a freelisted = {
rebus : 'a;
freemetas : Int.Set.t }
(* Collects all metavars appearing in a constr *)
let metavars_of c =
let rec collrec acc c =
match kind_of_term c with
| Meta mv -> Int.Set.add mv acc
| _ -> fold_constr collrec acc c
in
collrec Int.Set.empty c
let mk_freelisted c =
{ rebus = c; freemetas = metavars_of c }
let map_fl f cfl = { cfl with rebus=f cfl.rebus }
(* Status of an instance found by unification wrt to the meta it solves:
- a supertype of the meta (e.g. the solution to ?X <= T is a supertype of ?X)
- a subtype of the meta (e.g. the solution to T <= ?X is a supertype of ?X)
- a term that can be eta-expanded n times while still being a solution
(e.g. the solution [P] to [?X u v = P u v] can be eta-expanded twice)
*)
type instance_constraint = IsSuperType | IsSubType | Conv
let eq_instance_constraint c1 c2 = c1 == c2
(* Status of the unification of the type of an instance against the type of
the meta it instantiates:
- CoerceToType means that the unification of types has not been done
and that a coercion can still be inserted: the meta should not be
substituted freely (this happens for instance given via the
"with" binding clause).
- TypeProcessed means that the information obtainable from the
unification of types has been extracted.
- TypeNotProcessed means that the unification of types has not been
done but it is known that no coercion may be inserted: the meta
can be substituted freely.
*)
type instance_typing_status =
CoerceToType | TypeNotProcessed | TypeProcessed
(* Status of an instance together with the status of its type unification *)
type instance_status = instance_constraint * instance_typing_status
(* Clausal environments *)
type clbinding =
| Cltyp of Name.t * constr freelisted
| Clval of Name.t * (constr freelisted * instance_status) * constr freelisted
let map_clb f = function
| Cltyp (na,cfl) -> Cltyp (na,map_fl f cfl)
| Clval (na,(cfl1,pb),cfl2) -> Clval (na,(map_fl f cfl1,pb),map_fl f cfl2)
(* name of defined is erased (but it is pretty-printed) *)
let clb_name = function
Cltyp(na,_) -> (na,false)
| Clval (na,_,_) -> (na,true)
(***********************)
module Metaset = Int.Set
module Metamap = Int.Map
let metamap_to_list m =
Metamap.fold (fun n v l -> (n,v)::l) m []
(*************************)
(* Unification state *)
type conv_pb = Reduction.conv_pb
type evar_constraint = conv_pb * Environ.env * constr * constr
module EvMap = Evar.Map
type evar_map = {
(** Existential variables *)
defn_evars : evar_info EvMap.t;
undf_evars : evar_info EvMap.t;
evar_names : Id.t EvMap.t * existential_key Idmap.t;
(** Universes *)
universes : evar_universe_context;
(** Conversion problems *)
conv_pbs : evar_constraint list;
last_mods : Evar.Set.t;
(** Metas *)
metas : clbinding Metamap.t;
(** Interactive proofs *)
effects : Declareops.side_effects;
future_goals : Evar.t list; (** list of newly created evars, to be
eventually turned into goals if not solved.*)
principal_future_goal : Evar.t option; (** if [Some e], [e] must be
contained
[future_goals]. The evar
[e] will inherit
properties (now: the
name) of the evar which
will be instantiated with
a term containing [e]. *)
}
(*** Lifting primitive from Evar.Map. ***)
let add_name_newly_undefined naming evk evi (evtoid,idtoev) =
let id = match naming with
| Misctypes.IntroAnonymous ->
let id = evar_ident_info evi in
Namegen.next_ident_away_from id (fun id -> Idmap.mem id idtoev)
| Misctypes.IntroIdentifier id ->
let id' =
Namegen.next_ident_away_from id (fun id -> Idmap.mem id idtoev) in
if not (Names.Id.equal id id') then
user_err_loc
(Loc.ghost,"",str "Already an existential evar of name " ++ pr_id id);
id'
| Misctypes.IntroFresh id ->
Namegen.next_ident_away_from id (fun id -> Idmap.mem id idtoev) in
(EvMap.add evk id evtoid, Idmap.add id evk idtoev)
let add_name_undefined naming evk evi (evtoid,idtoev as evar_names) =
if EvMap.mem evk evtoid then
evar_names
else
add_name_newly_undefined naming evk evi evar_names
let remove_name_defined evk (evtoid,idtoev) =
let id = EvMap.find evk evtoid in
(EvMap.remove evk evtoid, Idmap.remove id idtoev)
let remove_name_possibly_already_defined evk evar_names =
try remove_name_defined evk evar_names
with Not_found -> evar_names
let rename evk id evd =
let (evtoid,idtoev) = evd.evar_names in
let id' = EvMap.find evk evtoid in
if Idmap.mem id idtoev then anomaly (str "Evar name already in use");
{ evd with evar_names =
(EvMap.add evk id evtoid (* overwrite old name *),
Idmap.add id evk (Idmap.remove id' idtoev)) }
let reassign_name_defined evk evk' (evtoid,idtoev) =
let id = EvMap.find evk evtoid in
(EvMap.add evk' id (EvMap.remove evk evtoid),
Idmap.add id evk' (Idmap.remove id idtoev))
let add d e i = match i.evar_body with
| Evar_empty ->
let evar_names = add_name_undefined Misctypes.IntroAnonymous e i d.evar_names in
{ d with undf_evars = EvMap.add e i d.undf_evars; evar_names }
| Evar_defined _ ->
let evar_names = remove_name_possibly_already_defined e d.evar_names in
{ d with defn_evars = EvMap.add e i d.defn_evars; evar_names }
let remove d e =
let undf_evars = EvMap.remove e d.undf_evars in
let defn_evars = EvMap.remove e d.defn_evars in
{ d with undf_evars; defn_evars; }
let find d e =
try EvMap.find e d.undf_evars
with Not_found -> EvMap.find e d.defn_evars
let find_undefined d e = EvMap.find e d.undf_evars
let mem d e = EvMap.mem e d.undf_evars || EvMap.mem e d.defn_evars
(* spiwack: this function loses information from the original evar_map
it might be an idea not to export it. *)
let to_list d =
(* Workaround for change in Map.fold behavior in ocaml 3.08.4 *)
let l = ref [] in
EvMap.iter (fun evk x -> l := (evk,x)::!l) d.defn_evars;
EvMap.iter (fun evk x -> l := (evk,x)::!l) d.undf_evars;
!l
let undefined_map d = d.undf_evars
let drop_all_defined d = { d with defn_evars = EvMap.empty }
(* spiwack: not clear what folding over an evar_map, for now we shall
simply fold over the inner evar_map. *)
let fold f d a =
EvMap.fold f d.defn_evars (EvMap.fold f d.undf_evars a)
let fold_undefined f d a = EvMap.fold f d.undf_evars a
let raw_map f d =
let f evk info =
let ans = f evk info in
let () = match info.evar_body, ans.evar_body with
| Evar_defined _, Evar_empty
| Evar_empty, Evar_defined _ ->
anomaly (str "Unrespectful mapping function.")
| _ -> ()
in
ans
in
let defn_evars = EvMap.smartmapi f d.defn_evars in
let undf_evars = EvMap.smartmapi f d.undf_evars in
{ d with defn_evars; undf_evars; }
let raw_map_undefined f d =
let f evk info =
let ans = f evk info in
let () = match ans.evar_body with
| Evar_defined _ ->
anomaly (str "Unrespectful mapping function.")
| _ -> ()
in
ans
in
{ d with undf_evars = EvMap.smartmapi f d.undf_evars; }
let is_evar = mem
let is_defined d e = EvMap.mem e d.defn_evars
let is_undefined d e = EvMap.mem e d.undf_evars
let existential_value d (n, args) =
let info = find d n in
match evar_body info with
| Evar_defined c ->
instantiate_evar_array info c args
| Evar_empty ->
raise NotInstantiatedEvar
let existential_opt_value d ev =
try Some (existential_value d ev)
with NotInstantiatedEvar -> None
let existential_type d (n, args) =
let info =
try find d n
with Not_found ->
anomaly (str "Evar " ++ str (string_of_existential n) ++ str " was not declared") in
instantiate_evar_array info info.evar_concl args
let add_constraints d c =
{ d with universes = add_constraints_context d.universes c }
let add_universe_constraints d c =
{ d with universes = add_universe_constraints_context d.universes c }
(*** /Lifting... ***)
(* evar_map are considered empty disregarding histories *)
let is_empty d =
EvMap.is_empty d.defn_evars &&
EvMap.is_empty d.undf_evars &&
List.is_empty d.conv_pbs &&
Metamap.is_empty d.metas
let cmap f evd =
{ evd with
metas = Metamap.map (map_clb f) evd.metas;
defn_evars = EvMap.map (map_evar_info f) evd.defn_evars;
undf_evars = EvMap.map (map_evar_info f) evd.defn_evars
}
(* spiwack: deprecated *)
let create_evar_defs sigma = { sigma with
conv_pbs=[]; last_mods=Evar.Set.empty; metas=Metamap.empty }
(* spiwack: tentatively deprecated *)
let create_goal_evar_defs sigma = { sigma with
(* conv_pbs=[]; last_mods=Evar.Set.empty; metas=Metamap.empty } *)
metas=Metamap.empty }
let empty = {
defn_evars = EvMap.empty;
undf_evars = EvMap.empty;
universes = empty_evar_universe_context;
conv_pbs = [];
last_mods = Evar.Set.empty;
metas = Metamap.empty;
effects = Declareops.no_seff;
evar_names = (EvMap.empty,Idmap.empty); (* id<->key for undefined evars *)
future_goals = [];
principal_future_goal = None;
}
let from_env ?ctx e =
match ctx with
| None -> { empty with universes = evar_universe_context_from e }
| Some ctx -> { empty with universes = ctx }
let has_undefined evd = not (EvMap.is_empty evd.undf_evars)
let evars_reset_evd ?(with_conv_pbs=false) ?(with_univs=true) evd d =
let conv_pbs = if with_conv_pbs then evd.conv_pbs else d.conv_pbs in
let last_mods = if with_conv_pbs then evd.last_mods else d.last_mods in
let universes =
if not with_univs then evd.universes
else union_evar_universe_context evd.universes d.universes
in
{ evd with
metas = d.metas;
last_mods; conv_pbs; universes }
let merge_universe_context evd uctx' =
{ evd with universes = union_evar_universe_context evd.universes uctx' }
let set_universe_context evd uctx' =
{ evd with universes = uctx' }
let add_conv_pb ?(tail=false) pb d =
if tail then {d with conv_pbs = d.conv_pbs @ [pb]}
else {d with conv_pbs = pb::d.conv_pbs}
let evar_source evk d = (find d evk).evar_source
let evar_ident evk evd =
try EvMap.find evk (fst evd.evar_names)
with Not_found ->
(* Unnamed (non-dependent) evar *)
add_suffix (Id.of_string "X") (string_of_int (Evar.repr evk))
let evar_key id evd =
Idmap.find id (snd evd.evar_names)
let define_aux def undef evk body =
let oldinfo =
try EvMap.find evk undef
with Not_found ->
if EvMap.mem evk def then
anomaly ~label:"Evd.define" (Pp.str "cannot define an evar twice")
else
anomaly ~label:"Evd.define" (Pp.str "cannot define undeclared evar")
in
let () = assert (oldinfo.evar_body == Evar_empty) in
let newinfo = { oldinfo with evar_body = Evar_defined body } in
EvMap.add evk newinfo def, EvMap.remove evk undef
(* define the existential of section path sp as the constr body *)
let define evk body evd =
let (defn_evars, undf_evars) = define_aux evd.defn_evars evd.undf_evars evk body in
let last_mods = match evd.conv_pbs with
| [] -> evd.last_mods
| _ -> Evar.Set.add evk evd.last_mods
in
let evar_names = remove_name_defined evk evd.evar_names in
{ evd with defn_evars; undf_evars; last_mods; evar_names }
let evar_declare hyps evk ty ?(src=(Loc.ghost,Evar_kinds.InternalHole))
?(filter=Filter.identity) ?candidates ?(store=Store.empty)
?(naming=Misctypes.IntroAnonymous) evd =
let () = match Filter.repr filter with
| None -> ()
| Some filter ->
assert (Int.equal (List.length filter) (List.length (named_context_of_val hyps)))
in
let evar_info = {
evar_hyps = hyps;
evar_concl = ty;
evar_body = Evar_empty;
evar_filter = filter;
evar_source = src;
evar_candidates = candidates;
evar_extra = store; }
in
let evar_names = add_name_newly_undefined naming evk evar_info evd.evar_names in
{ evd with undf_evars = EvMap.add evk evar_info evd.undf_evars; evar_names }
let restrict evk evk' filter ?candidates evd =
let evar_info = EvMap.find evk evd.undf_evars in
let evar_info' =
{ evar_info with evar_filter = filter;
evar_candidates = candidates;
evar_extra = Store.empty } in
let evar_names = reassign_name_defined evk evk' evd.evar_names in
let ctxt = Filter.filter_list filter (evar_context evar_info) in
let id_inst = Array.map_of_list (fun (id,_,_) -> mkVar id) ctxt in
let body = mkEvar(evk',id_inst) in
let (defn_evars, undf_evars) = define_aux evd.defn_evars evd.undf_evars evk body in
{ evd with undf_evars = EvMap.add evk' evar_info' undf_evars;
defn_evars; evar_names }
let downcast evk ccl evd =
let evar_info = EvMap.find evk evd.undf_evars in
let evar_info' = { evar_info with evar_concl = ccl } in
{ evd with undf_evars = EvMap.add evk evar_info' evd.undf_evars }
(* extracts conversion problems that satisfy predicate p *)
(* Note: conv_pbs not satisying p are stored back in reverse order *)
let extract_conv_pbs evd p =
let (pbs,pbs1) =
List.fold_left
(fun (pbs,pbs1) pb ->
if p pb then
(pb::pbs,pbs1)
else
(pbs,pb::pbs1))
([],[])
evd.conv_pbs
in
{evd with conv_pbs = pbs1; last_mods = Evar.Set.empty},
pbs
let extract_changed_conv_pbs evd p =
extract_conv_pbs evd (fun pb -> p evd.last_mods pb)
let extract_all_conv_pbs evd =
extract_conv_pbs evd (fun _ -> true)
let loc_of_conv_pb evd (pbty,env,t1,t2) =
match kind_of_term (fst (decompose_app t1)) with
| Evar (evk1,_) -> fst (evar_source evk1 evd)
| _ ->
match kind_of_term (fst (decompose_app t2)) with
| Evar (evk2,_) -> fst (evar_source evk2 evd)
| _ -> Loc.ghost
(** The following functions return the set of evars immediately
contained in the object *)
(* excluding defined evars *)
let evar_list c =
let rec evrec acc c =
match kind_of_term c with
| Evar (evk, _ as ev) -> ev :: acc
| _ -> fold_constr evrec acc c in
evrec [] c
let evars_of_term c =
let rec evrec acc c =
match kind_of_term c with
| Evar (n, l) -> Evar.Set.add n (Array.fold_left evrec acc l)
| _ -> fold_constr evrec acc c
in
evrec Evar.Set.empty c
let evars_of_named_context nc =
List.fold_right (fun (_, b, t) s ->
Option.fold_left (fun s t ->
Evar.Set.union s (evars_of_term t))
(Evar.Set.union s (evars_of_term t)) b)
nc Evar.Set.empty
let evars_of_filtered_evar_info evi =
Evar.Set.union (evars_of_term evi.evar_concl)
(Evar.Set.union
(match evi.evar_body with
| Evar_empty -> Evar.Set.empty
| Evar_defined b -> evars_of_term b)
(evars_of_named_context (evar_filtered_context evi)))
(**********************************************************)
(* Side effects *)
let emit_side_effects eff evd =
{ evd with effects = Declareops.union_side_effects eff evd.effects; }
let drop_side_effects evd =
{ evd with effects = Declareops.no_seff; }
let eval_side_effects evd = evd.effects
(* Future goals *)
let declare_future_goal evk evd =
{ evd with future_goals = evk::evd.future_goals }
let declare_principal_goal evk evd =
match evd.principal_future_goal with
| None -> { evd with
future_goals = evk::evd.future_goals;
principal_future_goal=Some evk; }
| Some _ -> Errors.error "Only one main subgoal per instantiation."
let future_goals evd = evd.future_goals
let principal_future_goal evd = evd.principal_future_goal
let reset_future_goals evd =
{ evd with future_goals = [] ; principal_future_goal=None }
let restore_future_goals evd gls pgl =
{ evd with future_goals = gls ; principal_future_goal = pgl }
(**********************************************************)
(* Sort variables *)
type rigid =
| UnivRigid
| UnivFlexible of bool (** Is substitution by an algebraic ok? *)
let univ_rigid = UnivRigid
let univ_flexible = UnivFlexible false
let univ_flexible_alg = UnivFlexible true
let evar_universe_context d = d.universes
let universe_context_set d = d.universes.uctx_local
let universe_context evd =
Univ.ContextSet.to_context evd.universes.uctx_local
let universe_subst evd =
evd.universes.uctx_univ_variables
let merge_uctx rigid uctx ctx' =
let open Univ in
let uctx =
match rigid with
| UnivRigid -> uctx
| UnivFlexible b ->
let levels = ContextSet.levels ctx' in
let fold u accu =
if LMap.mem u accu then accu
else LMap.add u None accu
in
let uvars' = LSet.fold fold levels uctx.uctx_univ_variables in
if b then
{ uctx with uctx_univ_variables = uvars';
uctx_univ_algebraic = LSet.union uctx.uctx_univ_algebraic levels }
else { uctx with uctx_univ_variables = uvars' }
in
let uctx_local = ContextSet.append ctx' uctx.uctx_local in
let uctx_universes = merge_constraints (ContextSet.constraints ctx') uctx.uctx_universes in
{ uctx with uctx_local; uctx_universes }
let merge_context_set rigid evd ctx' =
{evd with universes = merge_uctx rigid evd.universes ctx'}
let merge_uctx_subst uctx s =
{ uctx with uctx_univ_variables = Univ.LMap.subst_union uctx.uctx_univ_variables s }
let merge_universe_subst evd subst =
{evd with universes = merge_uctx_subst evd.universes subst }
let with_context_set rigid d (a, ctx) =
(merge_context_set rigid d ctx, a)