Skip to content

Commit 3a0b543

Browse files
committed
Evar maps contain econstrs.
We bootstrap the circular evar_map <-> econstr dependency by moving the internal EConstr.API module to Evd.MiniEConstr. Then we make the Evd functions use econstr.
1 parent f3b84cf commit 3a0b543

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+462
-468
lines changed

dev/doc/changes.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## Changes between Coq 8.8 and Coq 8.9
2+
3+
### ML API
4+
5+
Proof engine
6+
7+
More functions have been changed to use `EConstr`, notably the
8+
functions in `Evd`.
9+
110
## Changes between Coq 8.7 and Coq 8.8
211

312
### Bug tracker

dev/top_printers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ let ppproofview p =
181181
pp(pr_enum Goal.pr_goal gls ++ fnl () ++ Termops.pr_evar_map (Some 1) sigma)
182182

183183
let ppopenconstr (x : Evd.open_constr) =
184-
let (evd,c) = x in pp (Termops.pr_evar_map (Some 2) evd ++ envpp pr_constr_env c)
184+
let (evd,c) = x in pp (Termops.pr_evar_map (Some 2) evd ++ envpp pr_econstr_env c)
185185
(* spiwack: deactivated until a replacement is found
186186
let pppftreestate p = pp(print_pftreestate p)
187187
*)

engine/eConstr.ml

Lines changed: 13 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -13,138 +13,8 @@ open Util
1313
open Names
1414
open Constr
1515
open Context
16-
open Evd
17-
18-
module API :
19-
sig
20-
module ESorts :
21-
sig
22-
type t
23-
val make : Sorts.t -> t
24-
val kind : Evd.evar_map -> t -> Sorts.t
25-
val unsafe_to_sorts : t -> Sorts.t
26-
end
27-
module EInstance :
28-
sig
29-
type t
30-
val make : Univ.Instance.t -> t
31-
val kind : Evd.evar_map -> t -> Univ.Instance.t
32-
val empty : t
33-
val is_empty : t -> bool
34-
val unsafe_to_instance : t -> Univ.Instance.t
35-
end
36-
type t
37-
val kind : Evd.evar_map -> t -> (t, t, ESorts.t, EInstance.t) Constr.kind_of_term
38-
val kind_upto : Evd.evar_map -> constr -> (constr, types, Sorts.t, Univ.Instance.t) Constr.kind_of_term
39-
val kind_of_type : Evd.evar_map -> t -> (t, t) Term.kind_of_type
40-
val whd_evar : Evd.evar_map -> t -> t
41-
val of_kind : (t, t, ESorts.t, EInstance.t) Constr.kind_of_term -> t
42-
val of_constr : Constr.t -> t
43-
val to_constr : ?abort_on_undefined_evars:bool -> evar_map -> t -> Constr.t
44-
val unsafe_to_constr : t -> Constr.t
45-
val unsafe_eq : (t, Constr.t) eq
46-
val of_named_decl : (Constr.t, Constr.types) Context.Named.Declaration.pt -> (t, t) Context.Named.Declaration.pt
47-
val unsafe_to_named_decl : (t, t) Context.Named.Declaration.pt -> (Constr.t, Constr.types) Context.Named.Declaration.pt
48-
val unsafe_to_rel_decl : (t, t) Context.Rel.Declaration.pt -> (Constr.t, Constr.types) Context.Rel.Declaration.pt
49-
val of_rel_decl : (Constr.t, Constr.types) Context.Rel.Declaration.pt -> (t, t) Context.Rel.Declaration.pt
50-
val to_rel_decl : Evd.evar_map -> (t, t) Context.Rel.Declaration.pt -> (Constr.t, Constr.types) Context.Rel.Declaration.pt
51-
end =
52-
struct
53-
54-
module ESorts =
55-
struct
56-
type t = Sorts.t
57-
let make s = s
58-
let kind sigma = function
59-
| Sorts.Type u -> Sorts.sort_of_univ (Evd.normalize_universe sigma u)
60-
| s -> s
61-
let unsafe_to_sorts s = s
62-
end
6316

64-
module EInstance =
65-
struct
66-
type t = Univ.Instance.t
67-
let make i = i
68-
let kind sigma i =
69-
if Univ.Instance.is_empty i then i
70-
else Evd.normalize_universe_instance sigma i
71-
let empty = Univ.Instance.empty
72-
let is_empty = Univ.Instance.is_empty
73-
let unsafe_to_instance t = t
74-
end
75-
76-
type t = Constr.t
77-
78-
let safe_evar_value sigma ev =
79-
try Some (Evd.existential_value sigma ev)
80-
with NotInstantiatedEvar | Not_found -> None
81-
82-
let rec whd_evar sigma c =
83-
match Constr.kind c with
84-
| Evar ev ->
85-
begin match safe_evar_value sigma ev with
86-
| Some c -> whd_evar sigma c
87-
| None -> c
88-
end
89-
| App (f, args) when isEvar f ->
90-
(** Enforce smart constructor invariant on applications *)
91-
let ev = destEvar f in
92-
begin match safe_evar_value sigma ev with
93-
| None -> c
94-
| Some f -> whd_evar sigma (mkApp (f, args))
95-
end
96-
| Cast (c0, k, t) when isEvar c0 ->
97-
(** Enforce smart constructor invariant on casts. *)
98-
let ev = destEvar c0 in
99-
begin match safe_evar_value sigma ev with
100-
| None -> c
101-
| Some c -> whd_evar sigma (mkCast (c, k, t))
102-
end
103-
| _ -> c
104-
105-
let kind sigma c = Constr.kind (whd_evar sigma c)
106-
let kind_upto = kind
107-
let kind_of_type sigma c = Term.kind_of_type (whd_evar sigma c)
108-
let of_kind = Constr.of_kind
109-
let of_constr c = c
110-
let unsafe_to_constr c = c
111-
let unsafe_eq = Refl
112-
113-
let to_constr ?(abort_on_undefined_evars=true) sigma c =
114-
let rec to_constr c = match Constr.kind c with
115-
| Evar ev ->
116-
begin match safe_evar_value sigma ev with
117-
| Some c -> to_constr c
118-
| None ->
119-
if abort_on_undefined_evars then
120-
anomaly ~label:"econstr" Pp.(str "grounding a non evar-free term")
121-
else
122-
Constr.map (fun c -> to_constr c) c
123-
end
124-
| Sort (Sorts.Type u) ->
125-
let u' = Evd.normalize_universe sigma u in
126-
if u' == u then c else mkSort (Sorts.sort_of_univ u')
127-
| Const (c', u) when not (Univ.Instance.is_empty u) ->
128-
let u' = Evd.normalize_universe_instance sigma u in
129-
if u' == u then c else mkConstU (c', u')
130-
| Ind (i, u) when not (Univ.Instance.is_empty u) ->
131-
let u' = Evd.normalize_universe_instance sigma u in
132-
if u' == u then c else mkIndU (i, u')
133-
| Construct (co, u) when not (Univ.Instance.is_empty u) ->
134-
let u' = Evd.normalize_universe_instance sigma u in
135-
if u' == u then c else mkConstructU (co, u')
136-
| _ -> Constr.map (fun c -> to_constr c) c
137-
in to_constr c
138-
139-
let of_named_decl d = d
140-
let unsafe_to_named_decl d = d
141-
let of_rel_decl d = d
142-
let unsafe_to_rel_decl d = d
143-
let to_rel_decl sigma d = Context.Rel.Declaration.map_constr (to_constr sigma) d
144-
145-
end
146-
147-
include API
17+
include Evd.MiniEConstr
14818

14919
type types = t
15020
type constr = t
@@ -387,8 +257,7 @@ let decompose_prod_n_assum sigma n c =
387257
in
388258
prodec_rec Context.Rel.empty n c
389259

390-
let existential_type sigma (evk, args) =
391-
of_constr (existential_type sigma (evk, Array.map unsafe_to_constr args))
260+
let existential_type = Evd.existential_type
392261

393262
let map sigma f c = match kind sigma c with
394263
| (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
@@ -749,7 +618,7 @@ let universes_of_constr env sigma c =
749618
LSet.fold LSet.add (Universe.levels u) s
750619
| Evar (k, args) ->
751620
let concl = Evd.evar_concl (Evd.find sigma k) in
752-
fold sigma aux (aux s (of_constr concl)) c
621+
fold sigma aux (aux s concl) c
753622
| _ -> fold sigma aux s c
754623
in aux LSet.empty c
755624

@@ -907,6 +776,10 @@ let named_context e = cast_named_context (sym unsafe_eq) (named_context e)
907776
let val_of_named_context e = val_of_named_context (cast_named_context unsafe_eq e)
908777
let named_context_of_val e = cast_named_context (sym unsafe_eq) (named_context_of_val e)
909778

779+
let of_existential : Constr.existential -> existential =
780+
let gen : type a b. (a,b) eq -> 'c * b array -> 'c * a array = fun Refl x -> x in
781+
gen unsafe_eq
782+
910783
let lookup_rel i e = cast_rel_decl (sym unsafe_eq) (lookup_rel i e)
911784
let lookup_named n e = cast_named_decl (sym unsafe_eq) (lookup_named n e)
912785
let lookup_named_val n e = cast_named_decl (sym unsafe_eq) (lookup_named_val n e)
@@ -922,7 +795,7 @@ let map_rel_context_in_env f env sign =
922795

923796
let fresh_global ?loc ?rigid ?names env sigma reference =
924797
let (evd,t) = Evd.fresh_global ?loc ?rigid ?names env sigma reference in
925-
evd, of_constr t
798+
evd, t
926799

927800
let is_global sigma gr c =
928801
Globnames.is_global gr (to_constr sigma c)
@@ -934,5 +807,10 @@ let to_instance = EInstance.unsafe_to_instance
934807
let to_constr = unsafe_to_constr
935808
let to_rel_decl = unsafe_to_rel_decl
936809
let to_named_decl = unsafe_to_named_decl
810+
let to_named_context =
811+
let gen : type a b. (a, b) eq -> (a,a) Context.Named.pt -> (b,b) Context.Named.pt
812+
= fun Refl x -> x
813+
in
814+
gen unsafe_eq
937815
let eq = unsafe_eq
938816
end

engine/eConstr.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ open Names
1313
open Constr
1414
open Environ
1515

16-
type t
16+
type t = Evd.econstr
1717
(** Type of incomplete terms. Essentially a wrapper around {!Constr.t} ensuring
1818
that {!Constr.kind} does not observe defined evars. *)
1919

@@ -290,6 +290,7 @@ val is_global : Evd.evar_map -> Globnames.global_reference -> t -> bool
290290

291291
(** {5 Extra} *)
292292

293+
val of_existential : Constr.existential -> existential
293294
val of_named_decl : (Constr.t, Constr.types) Context.Named.Declaration.pt -> (t, types) Context.Named.Declaration.pt
294295
val of_rel_decl : (Constr.t, Constr.types) Context.Rel.Declaration.pt -> (t, types) Context.Rel.Declaration.pt
295296

@@ -308,6 +309,8 @@ sig
308309
val to_named_decl : (t, types) Context.Named.Declaration.pt -> (Constr.t, Constr.types) Context.Named.Declaration.pt
309310
(** Physical identity. Does not care for defined evars. *)
310311

312+
val to_named_context : (t, types) Context.Named.pt -> Context.Named.t
313+
311314
val to_sorts : ESorts.t -> Sorts.t
312315
(** Physical identity. Does not care for normalization. *)
313316

0 commit comments

Comments
 (0)