Skip to content

Commit 54a3bae

Browse files
committed
Adapt to PR #18903
1 parent acf29a3 commit 54a3bae

File tree

7 files changed

+156
-89
lines changed

7 files changed

+156
-89
lines changed

builtin-doc/coq-builtin.elpi

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,9 @@ external pred coq.univ.variable.of-term i:term, o:coq.univ.variable.set.
10401040

10411041
% -- Universe instance (for universe polymorphic global terms) ------
10421042

1043-
% As of today a universe polymorphic constant can only be instantiated
1044-
% with universe level variables. That is f@{Prop} is not valid, nor
1045-
% is f@{u+1}. One can only write f@{u} for any u.
1043+
% A universe polymorphic constant can be instantiated with universes.
10461044
%
1047-
% A univ-instance is morally a list of universe level variables,
1045+
% A univ-instance is morally a list of universes,
10481046
% but its list syntax is hidden in the terms. If you really need to
10491047
% craft or inspect one of these, the following APIs can help you.
10501048
%
@@ -1082,9 +1080,8 @@ external pred coq.univ-instance.unify-leq i:gref, i:univ-instance,
10821080

10831081
% Constraint between two universes level variables
10841082
kind univ-constraint type.
1085-
type lt univ.variable -> univ.variable -> univ-constraint.
1086-
type le univ.variable -> univ.variable -> univ-constraint.
1087-
type eq univ.variable -> univ.variable -> univ-constraint.
1083+
type le univ -> univ -> univ-constraint.
1084+
type eq univ -> univ -> univ-constraint.
10881085

10891086
% Variance of a universe level variable
10901087
kind univ-variance type.

src/rocq_elpi_HOAS.ml

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let add_universe_constraint state c =
130130
let new_univ_level_variable ?(flexible=false) state =
131131
S.update_return (Option.get !pre_engine) state (fun ({ sigma } as e) ->
132132
(* ~name: really mean the universe level is a binder as in Definition f@{x} *)
133-
let rigidity = if flexible then UState.univ_flexible_alg else UState.univ_rigid in
133+
let rigidity = if flexible then UState.univ_flexible else UState.univ_rigid in
134134
let sigma, v = Evd.new_univ_level_variable ?name:None rigidity sigma in
135135
let u = Univ.Universe.make v in
136136
(*
@@ -210,13 +210,10 @@ let universe_constraint : Univ.univ_constraint API.Conversion.t =
210210
doc = "Constraint between two universes level variables";
211211
pp = (fun fmt _ -> Format.fprintf fmt "<todo>");
212212
constructors = [
213-
K("lt","",A(universe_level_variable,A(universe_level_variable,N)),
214-
B (fun u1 u2 -> (u1,Univ.Lt,u2)),
215-
M (fun ~ok ~ko -> function (l1,Univ.Lt,l2) -> ok l1 l2 | _ -> ko ()));
216-
K("le","",A(universe_level_variable,A(universe_level_variable,N)),
213+
K("le","",A(univ,A(univ,N)),
217214
B (fun u1 u2 -> (u1,Univ.Le,u2)),
218215
M (fun ~ok ~ko -> function (l1,Univ.Le,l2) -> ok l1 l2 | _ -> ko ()));
219-
K("eq","",A(universe_level_variable,A(universe_level_variable,N)),
216+
K("eq","",A(univ,A(univ,N)),
220217
B (fun u1 u2 -> (u1,Univ.Eq,u2)),
221218
M (fun ~ok ~ko -> function (l1,Univ.Eq,l2) -> ok l1 l2 | _ -> ko ()))
222219
]
@@ -269,7 +266,17 @@ let universe_decl_cumul : universe_decl_cumul API.Conversion.t =
269266
]
270267
} |> API.ContextualConversion.(!<)
271268

269+
let collapse_to_type_sigma sigma s =
270+
match s with
271+
| Sorts.QSort (q, u) ->
272+
let sigma = Evd.set_eq_qualities sigma (Sorts.Quality.QVar q) Sorts.Quality.qtype in
273+
sigma, Sorts.make Sorts.Quality.qtype u
274+
| x -> sigma, s
272275

276+
let collapse_to_type_state state s =
277+
S.update_return (Option.get !pre_engine) state (fun ({ sigma } as x) ->
278+
let sigma, s = collapse_to_type_sigma sigma s in
279+
{ x with sigma }, s)
273280

274281
(* All in one data structure to represent the Coq context and its link with
275282
the elpi one:
@@ -336,7 +343,7 @@ let default_options () = {
336343
keepunivs = None;
337344
redflags = None;
338345
no_tc = None;
339-
algunivs = None;
346+
algunivs = Some true;
340347
}
341348
let make_options ~hoas_holes ~local ~warn ~depr ~primitive ~failsafe ~ppwidth
342349
~pp ~pplevel ~using ~inline ~uinstance ~universe_decl ~reversible ~keepunivs
@@ -400,6 +407,7 @@ let sort : (Sorts.t, _ coq_context, API.Data.constraints) API.ContextualConversi
400407
| Sorts.SProp -> Format.fprintf fmt "SProp"
401408
| Sorts.QSort _ -> Format.fprintf fmt "QSort");
402409
embed = (fun ~depth { options } _ state s ->
410+
let state, s = collapse_to_type_state state s in
403411
match s with
404412
| Sorts.Prop -> state, E.mkConst propc, []
405413
| Sorts.SProp -> state, E.mkConst spropc, []
@@ -409,7 +417,7 @@ let sort : (Sorts.t, _ coq_context, API.Data.constraints) API.ContextualConversi
409417
| Sorts.Type u ->
410418
let state, u, gls = univ.embed ~depth state u in
411419
state, E.mkApp typc u [], gls
412-
| Sorts.QSort _ -> nYI "sort polymorphism");
420+
| Sorts.QSort (q, u) -> nYI ("sort polymorphism: " ^ Sorts.QVar.to_string q));
413421
readback = (fun ~depth { options } _ state t ->
414422
match E.look ~depth t with
415423
| E.Const c when c == propc -> state, Sorts.prop, []
@@ -526,14 +534,14 @@ let constructorina ~loc x = A.mkOpaque ~loc (constructorino x)
526534
let compare_instances x y =
527535
let qx, ux = UVars.Instance.to_array x
528536
and qy, uy = UVars.Instance.to_array y in
529-
Util.Compare.(compare [(CArray.compare Sorts.Quality.compare, qx, qy); (CArray.compare Univ.Level.compare, ux, uy)])
537+
Util.Compare.(compare [(CArray.compare Sorts.Quality.compare, qx, qy); (CArray.compare Univ.Universe.compare, ux, uy)])
530538

531539
let uinstancein, uinstanceino, isuinstance, uinstanceout, uinstance =
532540
let { CD.cin; cino; isc; cout }, uinstance = CD.declare {
533541
CD.name = "univ-instance";
534542
doc = "Universes level instance for a universe-polymorphic constant";
535543
pp = (fun fmt x ->
536-
let s = Pp.string_of_ppcmds (UVars.Instance.pr Sorts.QVar.raw_pr UnivNames.pr_level_with_global_universes x) in
544+
let s = Pp.string_of_ppcmds (UVars.Instance.pr Sorts.QVar.raw_pr (Univ.Universe.pr UnivNames.pr_level_with_global_universes) x) in
537545
Format.fprintf fmt "«%s»" s);
538546
compare = compare_instances;
539547
hash = UVars.Instance.hash;
@@ -1077,13 +1085,22 @@ let force_level_of_universe state u =
10771085
let w = Sorts.sort_of_univ v in
10781086
add_universe_constraint state (constraint_eq (Sorts.sort_of_univ u) w), l, v, w
10791087

1088+
[%%if coq = "9.1"]
1089+
let purge_algebraic_univs_sort state s = state, EConstr.ESorts.kind (S.get engine state).sigma s
1090+
[%%else]
10801091
let purge_algebraic_univs_sort state s =
10811092
let sigma = (S.get engine state).sigma in
10821093
match EConstr.ESorts.kind sigma s with
10831094
| Sorts.Type u | Sorts.QSort (_ , u) ->
10841095
let state, _, _, s = force_level_of_universe state u in
10851096
state, s
10861097
| x -> state, x
1098+
[%%endif]
1099+
1100+
let collapse_to_type state s =
1101+
S.update_return engine state (fun ({ sigma } as e) ->
1102+
let sigma, s = collapse_to_type_sigma sigma s in
1103+
{ e with sigma }, s)
10871104

10881105
let in_elpi_flex_sort t = E.mkApp sortc (E.mkApp typc t []) []
10891106
let in_elpiast_flex_sort ~loc t =
@@ -1094,7 +1111,9 @@ let sort = { sort with API.ContextualConversion.embed = (fun ~depth ctx csts sta
10941111
if ctx.options.algunivs = None || ctx.options.algunivs = Some false then
10951112
purge_algebraic_univs_sort state (EConstr.ESorts.make s)
10961113
else
1097-
state, s in
1114+
let state, s = collapse_to_type state s in
1115+
state, s
1116+
in
10981117
sort.API.ContextualConversion.embed ~depth ctx csts state s) }
10991118

11001119
let in_elpi_sort ~depth ctx csts state s =
@@ -1704,7 +1723,6 @@ let body_of_constant state c inst_opt = S.update_return engine state (fun x ->
17041723
let sigma = match priv with
17051724
| Opaqueproof.PrivateMonomorphic () -> sigma
17061725
| Opaqueproof.PrivatePolymorphic ctx ->
1707-
let ctx = Util.on_snd (Univ.subst_univs_level_constraints (snd (UVars.make_instance_subst inst))) ctx in
17081726
Evd.merge_context_set Evd.univ_rigid sigma ctx
17091727
in
17101728
{ x with sigma }, (Some (EConstr.of_constr bo), Some inst)
@@ -1844,8 +1862,8 @@ let analyze_scope ~depth coq_ctx args =
18441862
module UIM = F.Map(struct
18451863
type t = UVars.Instance.t
18461864
let compare = compare_instances
1847-
let show x = Pp.string_of_ppcmds @@ UVars.Instance.pr Sorts.QVar.raw_pr UnivNames.pr_level_with_global_universes x
1848-
let pp fmt x = Format.fprintf fmt "%a" Pp.pp_with (UVars.Instance.pr Sorts.QVar.raw_pr UnivNames.pr_level_with_global_universes x)
1865+
let show x = Pp.string_of_ppcmds @@ UVars.Instance.pr Sorts.QVar.raw_pr (Univ.Universe.pr UnivNames.pr_level_with_global_universes) x
1866+
let pp fmt x = Format.fprintf fmt "%a" Pp.pp_with (UVars.Instance.pr Sorts.QVar.raw_pr (Univ.Universe.pr UnivNames.pr_level_with_global_universes) x)
18491867
end)
18501868

18511869
let uim = S.declare_component ~name:"rocq-elpi:evar-univ-instance-map" ~descriptor:interp_state
@@ -1862,7 +1880,7 @@ let in_coq_poly_gref ~depth ~origin ~failsafe s t i =
18621880
s, u, []
18631881
with Not_found ->
18641882
let u, ctx = UnivGen.fresh_global_instance (get_global_env s) t in
1865-
let s = update_sigma s (fun sigma -> Evd.merge_sort_context_set UState.univ_flexible_alg sigma ctx) in
1883+
let s = update_sigma s (fun sigma -> Evd.merge_sort_context_set UState.univ_flexible sigma ctx) in
18661884
let u =
18671885
match C.kind u with
18681886
| C.Const (_, u) -> u
@@ -2947,7 +2965,7 @@ let universes_of_udecl state ({ UState.univdecl_instance ; univdecl_constraints
29472965
let used1 = univdecl_instance in
29482966
let used2 = List.map (fun (x,_,y) -> [x;y]) (Univ.Constraints.elements univdecl_constraints) in
29492967
let used = List.fold_right Univ.Level.Set.add used1 Univ.Level.Set.empty in
2950-
let used = List.fold_right Univ.Level.Set.add (List.flatten used2) used in
2968+
let used = List.fold_right (fun x acc -> Univ.Level.Set.union (Univ.Universe.levels x) acc) (List.flatten used2) used in
29512969
used
29522970

29532971
let name_universe_level = ref 0
@@ -2965,30 +2983,40 @@ let name_universe_level state l =
29652983
{ e with sigma }, id
29662984
)
29672985

2968-
2986+
[%%if coq = "9.1"]
2987+
let mk_universe_decl univdecl_extensible_instance univdecl_extensible_constraints univdecl_constraints univdecl_instance =
2988+
let open UState in
2989+
{ univdecl_qualities = [];
2990+
univdecl_extensible_instance;
2991+
univdecl_extensible_qualities = false;
2992+
univdecl_extensible_constraints;
2993+
univdecl_constraints;
2994+
univdecl_variances = None;
2995+
univdecl_instance; }
2996+
[%%else]
29692997
let mk_universe_decl univdecl_extensible_instance univdecl_extensible_constraints univdecl_constraints univdecl_instance =
29702998
let open UState in
29712999
{ univdecl_qualities = [];
29723000
univdecl_extensible_instance;
29733001
univdecl_extensible_qualities = false;
29743002
univdecl_extensible_constraints;
29753003
univdecl_constraints;
2976-
univdecl_instance}
3004+
univdecl_instance; }
3005+
[%%endif]
29773006

29783007
let poly_cumul_udecl_variance_of_options state options =
29793008
match options.universe_decl with
2980-
| NotUniversePolymorphic -> state, false, false, UState.default_univ_decl, [| |]
3009+
| NotUniversePolymorphic -> state, false, false, UState.default_univ_decl, None
29813010
| Cumulative ((univ_lvlt_var,univdecl_extensible_instance),(univdecl_constraints,univdecl_extensible_constraints)) ->
29823011
let univdecl_instance, variance = List.split univ_lvlt_var in
29833012
state, true, true,
29843013
mk_universe_decl univdecl_extensible_instance univdecl_extensible_constraints univdecl_constraints univdecl_instance,
2985-
Array.of_list variance
3014+
Some (Array.of_list variance)
29863015
| NonCumulative((univ_lvlt,univdecl_extensible_instance),(univdecl_constraints,univdecl_extensible_constraints)) ->
29873016
let univdecl_instance = univ_lvlt in
2988-
let variance = List.init (List.length univdecl_instance) (fun _ -> None) in
29893017
state, true, false,
29903018
mk_universe_decl univdecl_extensible_instance univdecl_extensible_constraints univdecl_constraints univdecl_instance,
2991-
Array.of_list variance
3019+
None
29923020

29933021
[%%if coq = "8.20"]
29943022
let comInductive_interp_mutual_inductive_constr ~cumulative ~poly ~template ~finite =
@@ -3004,6 +3032,20 @@ let comInductive_interp_mutual_inductive_constr ~cumulative ~poly ~template ~fin
30043032
}
30053033
in
30063034
ComInductive.interp_mutual_inductive_constr ~arities_explicit:[true] ~template_syntax:[SyntaxAllowsTemplatePoly] ~flags
3035+
[%%elif coq = "9.1"]
3036+
let comInductive_interp_mutual_inductive_constr ~cumulative ~poly ~template ~finite ~udecl ~variances =
3037+
let flags = {
3038+
ComInductive.poly;
3039+
cumulative;
3040+
template = Some false;
3041+
finite;
3042+
mode = None;
3043+
}
3044+
in
3045+
let udecl =
3046+
UState.{ udecl with univdecl_variances = variances }
3047+
in
3048+
ComInductive.interp_mutual_inductive_constr ~arities_explicit:[true] ~template_syntax:[SyntaxAllowsTemplatePoly] ~flags ~udecl
30073049
[%%else]
30083050
let comInductive_interp_mutual_inductive_constr ~cumulative ~poly ~template ~finite ~ctx_params ~env_ar_params =
30093051
let flags = {
@@ -3495,19 +3537,20 @@ let upoly_decl_of ~depth state ~loose_udecl mie =
34953537
let open Entries in
34963538
match mie.mind_entry_universes with
34973539
| Template_ind_entry _ -> nYI "template polymorphic inductives"
3498-
| Polymorphic_ind_entry uc ->
3499-
let qvars, vars = UVars.Instance.to_array @@ UVars.UContext.instance uc in
3540+
| Polymorphic_ind_entry (uc, variances) ->
3541+
let qvars, vars = UVars.LevelInstance.to_array @@ UVars.UContext.instance uc in
35003542
if not (CArray.is_empty qvars) then nYI "sort poly inductives"
35013543
else
35023544
let state, vars = CArray.fold_left_map (fun s l -> fst (name_universe_level s l), l) state vars in
35033545
let csts = UVars.UContext.constraints uc in
3504-
begin match mie.mind_entry_variance with
3505-
| None ->
3546+
begin match variances with
3547+
| None | Some Infer_variances ->
35063548
let state, up, gls = universe_decl.API.Conversion.embed ~depth state ((Array.to_list vars,loose_udecl),(csts,loose_udecl)) in
35073549
state, (fun i -> E.mkApp uideclc i [up]), gls
3508-
| Some variance ->
3550+
| Some (Check_variances variance) ->
3551+
let variance = UVars.Variances.repr variance in
35093552
assert(Array.length variance = Array.length vars);
3510-
let uv = Array.map2 (fun x y -> (x,y)) vars variance |> Array.to_list in
3553+
let uv = Array.map2 (fun x y -> (x,Some (UVars.VarianceOccurrence.typing_variances y))) vars variance |> Array.to_list in
35113554
let state, up, gls = universe_decl_cumul.API.Conversion.embed ~depth state ((uv,loose_udecl),(csts,loose_udecl)) in
35123555
state, (fun i -> E.mkApp uideclc i [up]), gls
35133556
end
@@ -3532,7 +3575,7 @@ let inductive_entry2lp ~depth coq_ctx constraints state ~loose_udecl e =
35323575
let state = match mie.mind_entry_universes with
35333576
| Template_ind_entry _ -> nYI "template polymorphic inductives"
35343577
| Monomorphic_ind_entry -> state
3535-
| Polymorphic_ind_entry cs -> S.update engine state (fun e ->
3578+
| Polymorphic_ind_entry (cs, _variances) -> S.update engine state (fun e ->
35363579
{ e with sigma = Evd.merge_context_set UState.univ_flexible e.sigma (snd (UVars.UContext.to_context_set cs)) }) (* ???? *) in
35373580
let state, upoly_decl_of, upoly_decl_gls = upoly_decl_of ~depth state ~loose_udecl mie in
35383581
let allparams = mie.mind_entry_params in
@@ -3588,7 +3631,7 @@ let record_entry2lp ~depth coq_ctx constraints state ~loose_udecl e =
35883631
let state = match mie.mind_entry_universes with
35893632
| Template_ind_entry _ -> nYI "template polymorphic inductives"
35903633
| Monomorphic_ind_entry -> state
3591-
| Polymorphic_ind_entry cs -> S.update engine state (fun e ->
3634+
| Polymorphic_ind_entry (cs, _variances) -> S.update engine state (fun e ->
35923635
{ e with sigma = Evd.merge_context_set UState.univ_flexible e.sigma (snd (UVars.UContext.to_context_set cs)) }) (* ???? *) in
35933636

35943637
let state, upoly_decl_of, upoly_decl_gls = upoly_decl_of ~depth state ~loose_udecl mie in
@@ -3657,7 +3700,7 @@ let record_entry2lp ~depth coq_ctx constraints state ~loose_udecl (decl:Record.R
36573700
let state = match mie.mind_entry_universes with
36583701
| Template_ind_entry _ -> nYI "template polymorphic inductives"
36593702
| Monomorphic_ind_entry -> state
3660-
| Polymorphic_ind_entry cs -> S.update engine state (fun e ->
3703+
| Polymorphic_ind_entry (cs, _variances) -> S.update engine state (fun e ->
36613704
{ e with sigma = Evd.merge_context_set UState.univ_flexible e.sigma (snd (UVars.UContext.to_context_set cs)) }) (* ???? *) in
36623705

36633706
let state, upoly_decl_of, upoly_decl_gls = upoly_decl_of ~depth state ~loose_udecl mie in

src/rocq_elpi_HOAS.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ val force_level_of_universe : state -> Univ.Universe.t -> state * Univ.Level.t *
344344
val purge_algebraic_univs_sort : state -> EConstr.ESorts.t -> state * Sorts.t
345345
val ideclc : constant
346346
val uideclc : constant
347-
val poly_cumul_udecl_variance_of_options : state -> options -> state * bool * bool * UState.universe_decl * Entries.variance_entry
347+
val poly_cumul_udecl_variance_of_options : state -> options -> state * bool * bool * UState.universe_decl * UVars.Variance.t option array option
348348
val merge_universe_context : state -> UState.t -> state
349349
val restricted_sigma_of : Univ.Level.Set.t -> state -> Evd.evar_map
350350
val universes_of_term : state -> EConstr.t -> Univ.Level.Set.t

src/rocq_elpi_arg_HOAS.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ let intern_record_decl glob_sign (it : raw_record_decl) = glob_sign, it
328328
let mkCLocalAssum x y z = Constrexpr.CLocalAssum(x,None,y,z)
329329
let dest_entry (_,_,_,_,x) = x
330330

331+
[%%if coq = "9.1"]
332+
let expr_Type_sort = Constrexpr_ops.expr_Type_sort UState.univ_flexible
333+
[%%else]
331334
let expr_Type_sort = Constrexpr_ops.expr_Type_sort
335+
[%%endif]
332336

333337
[%%if coq = "8.20" || coq = "9.0"]
334338
let raw_record_decl_to_glob_synterp ({ name; sort; parameters; constructor; fields; univpoly } : raw_record_decl_elpi) : glob_record_decl_elpi =

0 commit comments

Comments
 (0)