Skip to content

Commit 71563eb

Browse files
committed
Safer API for constr_of_global, and getting rid of unsafe_constr_of_global.
1 parent 1014de5 commit 71563eb

13 files changed

+43
-89
lines changed

API/API.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -2684,10 +2684,8 @@ sig
26842684
val fresh_inductive_instance : Environ.env -> Names.inductive -> Term.pinductive Univ.in_universe_context_set
26852685
val new_Type : Names.DirPath.t -> Term.types
26862686
val type_of_global : Globnames.global_reference -> Term.types Univ.in_universe_context_set
2687-
val unsafe_type_of_global : Globnames.global_reference -> Term.types
26882687
val constr_of_global : Prelude.global_reference -> Term.constr
26892688
val new_univ_level : Names.DirPath.t -> Univ.Level.t
2690-
val unsafe_constr_of_global : Globnames.global_reference -> Term.constr Univ.in_universe_context
26912689
val new_sort_in_family : Sorts.family -> Sorts.t
26922690
val pr_with_global_universes : Univ.Level.t -> Pp.std_ppcmds
26932691
val pr_universe_opt_subst : universe_opt_subst -> Pp.std_ppcmds
@@ -2713,6 +2711,8 @@ sig
27132711
val env_of_context : Environ.named_context_val -> Environ.env
27142712
val is_polymorphic : Globnames.global_reference -> bool
27152713

2714+
val constr_of_global_in_context : Environ.env -> Globnames.global_reference -> Constr.t * Univ.AUContext.t
2715+
val type_of_global_in_context : Environ.env -> Globnames.global_reference -> Constr.t * Univ.AUContext.t
27162716
val type_of_global_unsafe : Globnames.global_reference -> Term.types
27172717

27182718
val current_dirpath : unit -> Names.DirPath.t

engine/termops.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ let collect_vars sigma c =
906906
aux Id.Set.empty c
907907

908908
let vars_of_global_reference env gr =
909-
let c, _ = Universes.unsafe_constr_of_global gr in
909+
let c, _ = Global.constr_of_global_in_context env gr in
910910
vars_of_global (Global.env ()) c
911911

912912
(* Tests whether [m] is a subterm of [t]:

engine/universes.ml

-66
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,6 @@ let fresh_instance_from ctx inst =
319319
let constraints = AUContext.instantiate inst ctx in
320320
inst, (ctx', constraints)
321321

322-
let unsafe_instance_from ctx =
323-
(Univ.AUContext.instance ctx, Univ.instantiate_univ_context ctx)
324-
325322
(** Fresh universe polymorphic construction *)
326323

327324
let fresh_constant_instance env c inst =
@@ -358,34 +355,6 @@ let fresh_constructor_instance env (ind,i) inst =
358355
let inst, ctx = fresh_instance_from (ACumulativityInfo.univ_context acumi) inst in
359356
(((ind,i),inst), ctx)
360357

361-
let unsafe_constant_instance env c =
362-
let cb = lookup_constant c env in
363-
match cb.Declarations.const_universes with
364-
| Declarations.Monomorphic_const _ ->
365-
((c,Instance.empty), UContext.empty)
366-
| Declarations.Polymorphic_const auctx ->
367-
let inst, ctx = unsafe_instance_from auctx in ((c, inst), ctx)
368-
369-
let unsafe_inductive_instance env ind =
370-
let mib, mip = Inductive.lookup_mind_specif env ind in
371-
match mib.Declarations.mind_universes with
372-
| Declarations.Monomorphic_ind _ -> ((ind,Instance.empty), UContext.empty)
373-
| Declarations.Polymorphic_ind auctx ->
374-
let inst, ctx = unsafe_instance_from auctx in ((ind,inst), ctx)
375-
| Declarations.Cumulative_ind acumi ->
376-
let inst, ctx = unsafe_instance_from (ACumulativityInfo.univ_context acumi) in
377-
((ind,inst), ctx)
378-
379-
let unsafe_constructor_instance env (ind,i) =
380-
let mib, mip = Inductive.lookup_mind_specif env ind in
381-
match mib.Declarations.mind_universes with
382-
| Declarations.Monomorphic_ind _ -> (((ind, i),Instance.empty), UContext.empty)
383-
| Declarations.Polymorphic_ind auctx ->
384-
let inst, ctx = unsafe_instance_from auctx in (((ind, i),inst), ctx)
385-
| Declarations.Cumulative_ind acumi ->
386-
let inst, ctx = unsafe_instance_from (ACumulativityInfo.univ_context acumi) in
387-
(((ind, i),inst), ctx)
388-
389358
open Globnames
390359

391360
let fresh_global_instance ?names env gr =
@@ -410,19 +379,6 @@ let fresh_inductive_instance env sp =
410379
let fresh_constructor_instance env sp =
411380
fresh_constructor_instance env sp None
412381

413-
let unsafe_global_instance env gr =
414-
match gr with
415-
| VarRef id -> mkVar id, UContext.empty
416-
| ConstRef sp ->
417-
let c, ctx = unsafe_constant_instance env sp in
418-
mkConstU c, ctx
419-
| ConstructRef sp ->
420-
let c, ctx = unsafe_constructor_instance env sp in
421-
mkConstructU c, ctx
422-
| IndRef sp ->
423-
let c, ctx = unsafe_inductive_instance env sp in
424-
mkIndU c, ctx
425-
426382
let constr_of_global gr =
427383
let c, ctx = fresh_global_instance (Global.env ()) gr in
428384
if not (Univ.ContextSet.is_empty ctx) then
@@ -437,9 +393,6 @@ let constr_of_global gr =
437393

438394
let constr_of_reference = constr_of_global
439395

440-
let unsafe_constr_of_global gr =
441-
unsafe_global_instance (Global.env ()) gr
442-
443396
let constr_of_global_univ (gr,u) =
444397
match gr with
445398
| VarRef id -> mkVar id
@@ -513,25 +466,6 @@ let type_of_reference env r =
513466

514467
let type_of_global t = type_of_reference (Global.env ()) t
515468

516-
let unsafe_type_of_reference env r =
517-
match r with
518-
| VarRef id -> Environ.named_type id env
519-
| ConstRef c ->
520-
let cb = Environ.lookup_constant c env in
521-
Typeops.type_of_constant_type env cb.const_type
522-
523-
| IndRef ind ->
524-
let (mib, oib as specif) = Inductive.lookup_mind_specif env ind in
525-
let (_, inst), _ = unsafe_inductive_instance env ind in
526-
Inductive.type_of_inductive env (specif, inst)
527-
528-
| ConstructRef (ind, _ as cstr) ->
529-
let (mib,oib as specif) = Inductive.lookup_mind_specif env (inductive_of_constructor cstr) in
530-
let (_, inst), _ = unsafe_inductive_instance env ind in
531-
Inductive.type_of_constructor (cstr,inst) specif
532-
533-
let unsafe_type_of_global t = unsafe_type_of_reference (Global.env ()) t
534-
535469
let fresh_sort_in_family env = function
536470
| InProp -> prop_sort, ContextSet.empty
537471
| InSet -> set_sort, ContextSet.empty

engine/universes.mli

-11
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,11 @@ val constr_of_global : Globnames.global_reference -> constr
189189
(** ** DEPRECATED ** synonym of [constr_of_global] *)
190190
val constr_of_reference : Globnames.global_reference -> constr
191191

192-
(** [unsafe_constr_of_global gr] turns [gr] into a constr, works on polymorphic
193-
references by taking the original universe instance that is not recorded
194-
anywhere. The constraints are forgotten as well. DO NOT USE in new code. *)
195-
val unsafe_constr_of_global : Globnames.global_reference -> constr in_universe_context
196-
197192
(** Returns the type of the global reference, by creating a fresh instance of polymorphic
198193
references and computing their instantiated universe context. (side-effect on the
199194
universe counter, use with care). *)
200195
val type_of_global : Globnames.global_reference -> types in_universe_context_set
201196

202-
(** [unsafe_type_of_global gr] returns [gr]'s type, works on polymorphic
203-
references by taking the original universe instance that is not recorded
204-
anywhere. The constraints are forgotten as well.
205-
USE with care. *)
206-
val unsafe_type_of_global : Globnames.global_reference -> types
207-
208197
(** Full universes substitutions into terms *)
209198

210199
val nf_evars_and_universes_opt_subst : (existential -> constr option) ->

library/global.ml

+19
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,25 @@ let type_of_global_unsafe r =
191191
let inst = Univ.AUContext.instance (Declareops.inductive_polymorphic_context mib) in
192192
Inductive.type_of_constructor (cstr,inst) specif
193193

194+
let constr_of_global_in_context env r =
195+
let open Constr in
196+
match r with
197+
| VarRef id -> mkVar id, Univ.AUContext.empty
198+
| ConstRef c ->
199+
let cb = Environ.lookup_constant c env in
200+
let univs = Declareops.constant_polymorphic_context cb in
201+
mkConstU (c, Univ.make_abstract_instance univs), univs
202+
| IndRef ind ->
203+
let (mib, oib as specif) = Inductive.lookup_mind_specif env ind in
204+
let univs = Declareops.inductive_polymorphic_context mib in
205+
mkIndU (ind, Univ.make_abstract_instance univs), univs
206+
| ConstructRef cstr ->
207+
let (mib,oib as specif) =
208+
Inductive.lookup_mind_specif env (inductive_of_constructor cstr)
209+
in
210+
let univs = Declareops.inductive_polymorphic_context mib in
211+
mkConstructU (cstr, Univ.make_abstract_instance univs), univs
212+
194213
let type_of_global_in_context env r =
195214
match r with
196215
| VarRef id -> Environ.named_type id env, Univ.AUContext.empty

library/global.mli

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ val is_polymorphic : Globnames.global_reference -> bool
122122
val is_template_polymorphic : Globnames.global_reference -> bool
123123
val is_type_in_type : Globnames.global_reference -> bool
124124

125+
val constr_of_global_in_context : Environ.env ->
126+
Globnames.global_reference -> Constr.types * Univ.AUContext.t
127+
(** Returns the type of the constant in its local universe
128+
context. The type should not be used without pushing it's universe
129+
context in the environmnent of usage. For non-universe-polymorphic
130+
constants, it does not matter. *)
131+
125132
val type_of_global_in_context : Environ.env ->
126133
Globnames.global_reference -> Constr.types * Univ.AUContext.t
127134
(** Returns the type of the constant in its local universe

plugins/funind/functional_principles_types.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ let build_case_scheme fa =
651651
(* in *)
652652
let funs =
653653
let (_,f,_) = fa in
654-
try fst (Universes.unsafe_constr_of_global (Smartlocate.global_with_alias f))
654+
try fst (Global.constr_of_global_in_context (Global.env ()) (Smartlocate.global_with_alias f))
655655
with Not_found ->
656656
user_err ~hdr:"FunInd.build_case_scheme"
657657
(str "Cannot find " ++ Libnames.pr_reference f) in

plugins/funind/recdef.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let type_of_const sigma t =
9090
|_ -> assert false
9191

9292
let constr_of_global x =
93-
fst (Universes.unsafe_constr_of_global x)
93+
fst (Global.constr_of_global_in_context (Global.env ()) x)
9494

9595
let constant sl s = constr_of_global (find_reference sl s)
9696

plugins/ssr/ssrvernac.ml4

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ let coerce_search_pattern_to_sort hpat =
337337
Pattern.PApp (fp, args') in
338338
let hr, na = splay_search_pattern 0 hpat in
339339
let dc, ht =
340-
Reductionops.splay_prod env sigma (EConstr.of_constr (Universes.unsafe_type_of_global hr)) in
340+
let hr, _ = Global.type_of_global_in_context (Global.env ()) hr (** FIXME *) in
341+
Reductionops.splay_prod env sigma (EConstr.of_constr hr) in
341342
let np = List.length dc in
342343
if np < na then CErrors.user_err (Pp.str "too many arguments in head search pattern") else
343344
let hpat' = if np = na then hpat else mkPApp hpat (np - na) [||] in

pretyping/classops.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ type coercion = {
403403
(* Computation of the class arity *)
404404

405405
let reference_arity_length ref =
406-
let t = Universes.unsafe_type_of_global ref in
406+
let t, _ = Global.type_of_global_in_context (Global.env ()) ref in
407407
List.length (fst (Reductionops.splay_arity (Global.env()) Evd.empty (EConstr.of_constr t))) (** FIXME *)
408408

409409
let projection_arity_length p =

pretyping/pretyping.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ let pretype_global ?loc rigid env evd gr us =
511511
match us with
512512
| None -> evd, None
513513
| Some l ->
514-
let _, ctx = Universes.unsafe_constr_of_global gr in
515-
let len = Univ.UContext.size ctx in
514+
let _, ctx = Global.constr_of_global_in_context env.ExtraEnv.env gr in
515+
let len = Univ.AUContext.size ctx in
516516
interp_instance ?loc evd ~len l
517517
in
518518
let (sigma, c) = Evd.fresh_global ?loc ~rigid ?names:instance env.ExtraEnv.env evd gr in

printing/prettyp.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,11 @@ let print_opaque_name qid =
798798
| IndRef (sp,_) ->
799799
print_inductive sp
800800
| ConstructRef cstr as gr ->
801-
let open EConstr in
802-
let ty = Universes.unsafe_type_of_global gr in
801+
let ty, ctx = Global.type_of_global_in_context env gr in
802+
let inst = Univ.AUContext.instance ctx in
803+
let ty = Vars.subst_instance_constr inst ty in
803804
let ty = EConstr.of_constr ty in
805+
let open EConstr in
804806
print_typed_value (mkConstruct cstr, ty)
805807
| VarRef id ->
806808
env |> lookup_named id |> NamedDecl.set_id id |> print_named_decl

tactics/tactics.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -5004,7 +5004,9 @@ let cache_term_by_tactic_then ~opaque ?(goal_type=None) id gk tac tacK =
50045004
in
50055005
let cst = Impargs.with_implicit_protection cst () in
50065006
(* let evd, lem = Evd.fresh_global (Global.env ()) evd (ConstRef cst) in *)
5007-
let lem, ctx = Universes.unsafe_constr_of_global (ConstRef cst) in
5007+
let lem, ctx = Global.constr_of_global_in_context (Global.env ()) (ConstRef cst) in
5008+
let inst = Univ.AUContext.instance ctx in
5009+
let lem = CVars.subst_instance_constr inst lem in
50085010
let lem = EConstr.of_constr lem in
50095011
let evd = Evd.set_universe_context evd ectx in
50105012
let open Safe_typing in

0 commit comments

Comments
 (0)