@@ -57,6 +57,9 @@ type direction = Forward | Backward
57
57
58
58
(* This module defines type-classes *)
59
59
type typeclass = {
60
+ (* Universe quantification *)
61
+ cl_univs : Univ.AUContext .t ;
62
+
60
63
(* The class implementation *)
61
64
cl_impl : global_reference ;
62
65
@@ -111,23 +114,11 @@ let new_instance cl info glob poly impl =
111
114
let classes : typeclasses ref = Summary. ref Refmap. empty ~name: " classes"
112
115
let instances : instances ref = Summary. ref Refmap. empty ~name: " instances"
113
116
114
- let typeclass_univ_instance (cl ,u' ) =
115
- let subst =
116
- let u =
117
- match cl.cl_impl with
118
- | ConstRef c ->
119
- let cb = Global. lookup_constant c in
120
- Univ.AUContext. instance (Declareops. constant_polymorphic_context cb)
121
- | IndRef c ->
122
- let mib,oib = Global. lookup_inductive c in
123
- Univ.AUContext. instance (Declareops. inductive_polymorphic_context mib)
124
- | _ -> Univ.Instance. empty
125
- in Array. fold_left2 (fun subst u u' -> Univ.LMap. add u u' subst)
126
- Univ.LMap. empty (Univ.Instance. to_array u) (Univ.Instance. to_array u')
127
- in
128
- let subst_ctx = Context.Rel. map (subst_univs_level_constr subst) in
129
- { cl with cl_context = fst cl.cl_context, subst_ctx (snd cl.cl_context);
130
- cl_props = subst_ctx cl.cl_props}, u'
117
+ let typeclass_univ_instance (cl , u ) =
118
+ assert (Univ.AUContext. size cl.cl_univs == Univ.Instance. length u);
119
+ let subst_ctx c = Context.Rel. map (subst_instance_constr u) c in
120
+ { cl with cl_context = on_snd subst_ctx cl.cl_context;
121
+ cl_props = subst_ctx cl.cl_props}
131
122
132
123
let class_info c =
133
124
try Refmap. find c ! classes
@@ -185,29 +176,43 @@ let subst_class (subst,cl) =
185
176
do_subst_ctx ctx in
186
177
let do_subst_projs projs = List. smartmap (fun (x , y , z ) ->
187
178
(x, y, Option. smartmap do_subst_con z)) projs in
188
- { cl_impl = do_subst_gr cl.cl_impl;
179
+ { cl_univs = cl.cl_univs;
180
+ cl_impl = do_subst_gr cl.cl_impl;
189
181
cl_context = do_subst_context cl.cl_context;
190
182
cl_props = do_subst_ctx cl.cl_props;
191
183
cl_projs = do_subst_projs cl.cl_projs;
192
184
cl_strict = cl.cl_strict;
193
185
cl_unique = cl.cl_unique }
194
186
187
+ (* * FIXME: share this with Cooking somewhere in a nicely packed API *)
188
+ let lift_abstract_context subst abs_ctx auctx =
189
+ let open Univ in
190
+ let len = LMap. cardinal subst in
191
+ let rec gen_subst i acc =
192
+ if i < 0 then acc
193
+ else
194
+ let acc = LMap. add (Level. var i) (Level. var (i + len)) acc in
195
+ gen_subst (pred i) acc
196
+ in
197
+ let subst = gen_subst (AUContext. size auctx - 1 ) subst in
198
+ let auctx = Univ. subst_univs_level_abstract_universe_context subst auctx in
199
+ subst, AUContext. union abs_ctx auctx
200
+
195
201
let discharge_class (_ ,cl ) =
196
202
let repl = Lib. replacement_context () in
197
203
let rel_of_variable_context ctx = List. fold_right
198
204
( fun (decl ,_ ) (ctx' , subst ) ->
199
205
let decl' = decl |> NamedDecl. map_constr (substn_vars 1 subst) |> NamedDecl. to_rel_decl in
200
206
(decl' :: ctx', NamedDecl. get_id decl :: subst)
201
207
) ctx ([] , [] ) in
202
- let discharge_rel_context subst n rel =
208
+ let discharge_rel_context ( subst , usubst ) n rel =
203
209
let rel = Context.Rel. map (Cooking. expmod_constr repl) rel in
204
- let ctx, _ =
205
- List. fold_right
206
- (fun decl (ctx , k ) ->
207
- RelDecl. map_constr (substn_vars k subst) decl :: ctx, succ k
208
- )
209
- rel ([] , n)
210
- in ctx
210
+ let fold decl (ctx , k ) =
211
+ let map c = subst_univs_level_constr usubst (substn_vars k subst c) in
212
+ RelDecl. map_constr map decl :: ctx, succ k
213
+ in
214
+ let ctx, _ = List. fold_right fold rel ([] , n) in
215
+ ctx
211
216
in
212
217
let abs_context cl =
213
218
match cl.cl_impl with
@@ -229,10 +234,12 @@ let discharge_class (_,cl) =
229
234
if cl_impl' == cl.cl_impl then cl else
230
235
let ctx, usubst, uctx = abs_context cl in
231
236
let ctx, subst = rel_of_variable_context ctx in
232
- let context = discharge_context ctx subst cl.cl_context in
233
- let props = discharge_rel_context subst (succ (List. length (fst cl.cl_context))) cl.cl_props in
237
+ let usubst, cl_univs' = lift_abstract_context usubst uctx cl.cl_univs in
238
+ let context = discharge_context ctx (subst, usubst) cl.cl_context in
239
+ let props = discharge_rel_context (subst, usubst) (succ (List. length (fst cl.cl_context))) cl.cl_props in
234
240
let discharge_proj (x , y , z ) = x, y, Option. smartmap Lib. discharge_con z in
235
- { cl_impl = cl_impl';
241
+ { cl_univs = cl_univs';
242
+ cl_impl = cl_impl';
236
243
cl_context = context;
237
244
cl_props = props;
238
245
cl_projs = List. smartmap discharge_proj cl.cl_projs;
0 commit comments