Skip to content

Commit c0bb832

Browse files
authored
Merge pull request #134 from SkySkimmer/template-entry-qvar
Adapt to coq/coq#20178 (kernel template data has qvars)
2 parents aac0331 + 7cc7ab5 commit c0bb832

File tree

1 file changed

+76
-61
lines changed

1 file changed

+76
-61
lines changed

src/parametricity.ml

+76-61
Original file line numberDiff line numberDiff line change
@@ -1054,13 +1054,7 @@ open Declarations
10541054

10551055
let get_arity t =
10561056
let decls, s = Term.decompose_prod_decls t in
1057-
match Constr.kind s with
1058-
| Sort (Type s) ->
1059-
begin match Univ.Universe.level s with
1060-
| None -> None
1061-
| Some l -> Some (decls, l)
1062-
end
1063-
| _ -> None
1057+
decls
10641058

10651059
(* Workaround to ensure that template universe levels are linear *)
10661060
let fix_template_params order evdr env temp b params =
@@ -1070,76 +1064,91 @@ let fix_template_params order evdr env temp b params =
10701064
| [] ->
10711065
let () = assert (List.is_empty params) in
10721066
umap, []
1073-
| false :: templ ->
1067+
| None :: templ ->
10741068
let decls, params = List.chop (order + 1) params in
10751069
let umap, params = freshen umap templ params in
10761070
umap, decls @ params
1077-
| true :: templ ->
1071+
| Some bind_sort :: templ ->
10781072
let decls, params = List.chop (order + 1) params in
10791073
let umap, params = freshen umap templ params in
10801074
let rel, pdecls = match decls with
10811075
| rel :: decls -> rel, decls
10821076
| _ -> assert false
10831077
in
1084-
let u0 = match get_arity (Context.Rel.Declaration.get_type rel) with
1085-
| None -> assert false
1086-
| Some (_, l) -> l
1087-
in
1088-
let umap, (ur, repl) = match Univ.Level.Map.find_opt u0 umap with
1089-
| None ->
1090-
let ur = UnivGen.fresh_level () in
1091-
let r = List.init order (fun _ -> UnivGen.fresh_level ()) in
1092-
let r = (ur, r) in
1093-
Univ.Level.Map.add u0 r umap, r
1094-
| Some r -> umap, r
1078+
let q0, u0 = Inductive.Template.bind_kind bind_sort in
1079+
let umap, urrepl = match u0 with
1080+
| None -> umap, None
1081+
| Some u0 -> match Int.Map.find_opt u0 umap with
1082+
| None ->
1083+
let ur = UnivGen.fresh_level () in
1084+
let r = List.init order (fun _ -> UnivGen.fresh_level ()) in
1085+
Int.Map.add u0 (ur,r) umap, Some (ur, r)
1086+
| Some r -> umap, Some r
10951087
in
10961088
let map pdecl u = match pdecl with
10971089
| Context.Rel.Declaration.LocalDef _ -> assert false
10981090
| Context.Rel.Declaration.LocalAssum (na, pdecl) ->
1099-
match get_arity pdecl with
1100-
| Some (ctx, _) ->
1101-
let u = Univ.Universe.make u in
1102-
let pdecl = Term.it_mkProd_or_LetIn (Constr.mkType u) ctx in
1103-
Context.Rel.Declaration.LocalAssum (na, pdecl)
1104-
| None -> assert false
1091+
let ctx, _ = Term.decompose_prod_decls pdecl in
1092+
let s = match u with
1093+
| Some u ->
1094+
let u = Univ.Universe.make u in
1095+
begin match bind_sort with
1096+
| Sorts.QSort (q,_) -> Sorts.qsort q u
1097+
| Type _ -> Sorts.sort_of_univ u
1098+
| SProp | Prop | Set -> assert false
1099+
end
1100+
| None -> bind_sort
1101+
in
1102+
let pdecl = Term.it_mkProd_or_LetIn (Constr.mkSort s) ctx in
1103+
Context.Rel.Declaration.LocalAssum (na, pdecl)
1104+
in
1105+
let ur, repl = match urrepl with
1106+
| None -> None, List.make order None
1107+
| Some (ur, repl) -> Some ur, List.map (fun r -> Some r) repl
11051108
in
11061109
let rel = map rel ur in
11071110
let pdecls = List.map2 map pdecls repl in
11081111
umap, rel :: pdecls @ params
11091112
in
1110-
let umap, decls = freshen Univ.Level.Map.empty templ params in
1113+
let umap, decls = freshen Int.Map.empty templ params in
11111114
(* Add corresponding declaration and constraints for each new level *)
1112-
let fold_univs u0 accu = match Univ.Level.Map.find_opt u0 umap with
1113-
| None -> assert false (* unbound template level *)
1114-
| Some (ur, repl) ->
1115-
let accu = Univ.Level.Set.add ur accu in
1116-
let fold accu u = Univ.Level.Set.add u accu in
1117-
List.fold_left fold accu repl
1115+
let map_univs u0 =
1116+
match Univ.Level.var_index u0 with
1117+
| None -> assert false
1118+
| Some u0 ->
1119+
match Int.Map.find_opt u0 umap with
1120+
| None -> assert false (* unbound template level *)
1121+
| Some (ur, repl) ->
1122+
Array.of_list (ur :: repl)
11181123
in
11191124
let fold_cstrs (u0, cst, v) accu =
11201125
(* We know that v cannot be template because of the unbounded-from-below property *)
1121-
let () = assert (not (Univ.Level.Map.mem v umap)) in
1122-
match Univ.Level.Map.find_opt u0 umap with
1126+
let () = assert (not (Option.has_some @@ Univ.Level.var_index v)) in
1127+
match Univ.Level.var_index u0 with
11231128
| None ->
11241129
(* not template, this is technically allowed but dubious *)
11251130
Univ.Constraints.add (u0, cst, v) accu
1126-
| Some (ur, repl) ->
1127-
let accu = Univ.Constraints.add (ur, cst, v) accu in
1128-
let fold accu u = Univ.Constraints.add (u, cst, v) accu in
1129-
List.fold_left fold accu repl
1130-
in
1131-
let fold_templ u0 (ur, repl) accu =
1132-
(* This is needed to typecheck inner template applications of the inductive.
1133-
FIXME: when new-template-poly lands this can probably be removed. *)
1134-
let accu = Univ.Constraints.add (ur, Le, u0) accu in
1135-
let fold accu u = Univ.Constraints.add (u, Le, u0) accu in
1136-
List.fold_left fold accu repl
1131+
| Some u0 -> match Int.Map.find_opt u0 umap with
1132+
| None -> assert false (* unbound template level *)
1133+
| Some (ur, repl) ->
1134+
let accu = Univ.Constraints.add (ur, cst, v) accu in
1135+
let fold accu u = Univ.Constraints.add (u, cst, v) accu in
1136+
List.fold_left fold accu repl
11371137
in
1138-
let (univs, cstrs) = temp.template_context in
1139-
let univs = Univ.Level.Set.fold fold_univs univs Univ.Level.Set.empty in
1138+
let uctx = UVars.AbstractContext.repr temp.template_context in
1139+
let univs = UVars.UContext.instance uctx in
1140+
let qvars, univs = UVars.Instance.to_array univs in
1141+
let cstrs = UVars.UContext.constraints uctx in
1142+
let univs = Array.concat @@ Array.map_to_list map_univs univs in
11401143
let cstrs = Univ.Constraints.fold fold_cstrs cstrs Univ.Constraints.empty in
1141-
let cstrs = Univ.Level.Map.fold fold_templ umap cstrs in
1142-
umap, (univs, cstrs), decls
1144+
let unames = Array.make (Array.length qvars) Anonymous, Array.make (Array.length univs) Anonymous in
1145+
let uctx = UVars.UContext.make unames (UVars.Instance.of_array (qvars,univs), cstrs) in
1146+
let default_univs =
1147+
let qs, us = UVars.Instance.to_array temp.template_defaults in
1148+
let us = Array.concat @@ Array.map_to_list (fun u -> Array.make (order+1) u) us in
1149+
UVars.Instance.of_array (qs,us)
1150+
in
1151+
umap, temp.template_concl, (uctx, default_univs), decls
11431152

11441153
let rec translate_mind_body name order evdr env kn b inst =
11451154
(* XXX: What is going on here? This doesn't make sense after cumulativity *)
@@ -1174,8 +1183,8 @@ let rec translate_mind_body name order evdr env kn b inst =
11741183
let template_univs, mind_entry_params_R = match b.mind_template with
11751184
| None -> None, mind_entry_params_R
11761185
| Some temp ->
1177-
let umap, univs, params = fix_template_params order evdr env temp b mind_entry_params_R in
1178-
Some (umap, univs, temp.template_pseudo_sort_poly), params
1186+
let umap, concl, univs, params = fix_template_params order evdr env temp b mind_entry_params_R in
1187+
Some (umap, concl, univs), params
11791188
in
11801189
debug_string [`Inductive] "translatation of inductive ...";
11811190
let mind_entry_inds_R =
@@ -1188,31 +1197,37 @@ let rec translate_mind_body name order evdr env kn b inst =
11881197
| Monomorphic ->
11891198
begin match template_univs with
11901199
| None -> Monomorphic_ind_entry
1191-
| Some (_, ctx, pseudo_sort_poly) -> Template_ind_entry { univs = ctx; pseudo_sort_poly }
1200+
| Some (_, _, (uctx, default_univs)) -> Template_ind_entry { uctx; default_univs }
11921201
end
11931202
| Polymorphic _ ->
11941203
let uctx, _ = (Evd.univ_entry ~poly:true !evdr) in
11951204
match uctx with Polymorphic_entry uctx -> Polymorphic_ind_entry uctx | _ -> assert false
11961205
in
11971206
let mind_entry_inds_R = match template_univs with
11981207
| None -> mind_entry_inds_R
1199-
| Some (umap, _, _) ->
1208+
| Some (umap, concl, _) ->
12001209
let entry = match mind_entry_inds_R with
12011210
| [entry] -> entry
12021211
| _ -> assert false
12031212
in
1204-
let decls, sort = Term.decompose_prod_decls entry.mind_entry_arity in
1205-
let sort = match Constr.kind sort with
1206-
| Sort (Type u) ->
1213+
let decls, _ = Term.decompose_prod_decls entry.mind_entry_arity in
1214+
let map_univ u =
12071215
let u = Univ.Universe.repr u in
1208-
let map (u0, n) = match Univ.Level.Map.find_opt u0 umap with
1216+
let map (u0, n) =
1217+
match Option.bind (Univ.Level.var_index u0) (fun u0 ->
1218+
Some (Int.Map.get u0 umap))
1219+
with
12091220
| None -> [u0, n]
12101221
| Some (ur, repl) -> (ur, n) :: List.map (fun u -> u, n) repl
12111222
in
1212-
Constr.mkType (Univ.Universe.unrepr (List.map_append map u))
1213-
| _ -> sort
1223+
Univ.Universe.unrepr (List.map_append map u)
1224+
in
1225+
let sort = match concl with
1226+
| (Type u) -> Sorts.sort_of_univ (map_univ u)
1227+
| QSort (q,u) -> Sorts.qsort q (map_univ u)
1228+
| SProp | Prop | Set -> concl
12141229
in
1215-
let arity = Term.it_mkProd_or_LetIn sort decls in
1230+
let arity = Term.it_mkProd_or_LetIn (Constr.mkSort sort) decls in
12161231
let entry = { entry with mind_entry_arity = arity } in
12171232
[entry]
12181233
in

0 commit comments

Comments
 (0)