@@ -174,53 +174,6 @@ let isuniv, univout, univino, (univ : Univ.Universe.t API.Conversion.t) =
174174 end
175175}
176176
177- let propc = E.Constants. declare_global_symbol " prop"
178- let spropc = E.Constants. declare_global_symbol " sprop"
179- let typc = E.Constants. declare_global_symbol " typ"
180-
181- let sort =
182- let open API.AlgebraicData in declare {
183- ty = API.Conversion. TyName " sort" ;
184- doc = " Sorts (kinds of types)" ;
185- pp = (fun fmt -> function
186- | Sorts. Type _ -> Format. fprintf fmt " Type"
187- | Sorts. Set -> Format. fprintf fmt " Set"
188- | Sorts. Prop -> Format. fprintf fmt " Prop"
189- | Sorts. SProp -> Format. fprintf fmt " SProp"
190- | Sorts. QSort _ -> Format. fprintf fmt " Type" );
191- constructors = [
192- K (" prop" ," impredicative sort of propositions" ,N ,
193- B Sorts. prop,
194- M (fun ~ok ~ko -> function Sorts. Prop -> ok | _ -> ko () ));
195- K (" sprop" ," impredicative sort of propositions with definitional proof irrelevance" ,N ,
196- B Sorts. sprop,
197- M (fun ~ok ~ko -> function Sorts. SProp -> ok | _ -> ko () ));
198- K (" typ" ," predicative sort of data (carries a universe level)" ,A (univ,N ),
199- B (fun x -> Sorts. sort_of_univ x),
200- M (fun ~ok ~ko -> function
201- | Sorts. Type x -> ok x
202- | Sorts. Set -> ok Univ.Universe. type0
203- | _ -> ko () ));
204- K (" uvar" ," " ,A (F. uvar,N ),
205- BS (fun (k ,_ ) state ->
206- let m = S. get um state in
207- try
208- let u = UM. host k m in
209- state, Sorts. sort_of_univ u
210- with Not_found ->
211- let state, (_,u) = new_univ_level_variable state in
212- let state = S. update um state (UM. add k u) in
213- state, Sorts. sort_of_univ u),
214- M (fun ~ok ~ko _ -> ko () ));
215- ]
216- } |> API.ContextualConversion. (! < )
217-
218- let ast_sort ~loc = function
219- | Sorts. Prop -> A. mkGlobal ~loc propc
220- | Sorts. SProp -> A. mkGlobal ~loc spropc
221- | Sorts. Set -> A. mkAppGlobal ~loc typc (A. mkOpaque ~loc @@ univino Univ.Universe. type0) []
222- | Sorts. Type u -> A. mkAppGlobal ~loc typc (A. mkOpaque ~loc @@ univino u) []
223- | _ -> assert false
224177
225178let universe_level_variable =
226179 let { CD. cin = levelin }, universe_level_variable_to_patch = CD. declare {
@@ -364,6 +317,7 @@ type options = {
364317 keepunivs : bool option ;
365318 redflags : RedFlags .reds option ;
366319 no_tc : bool option ;
320+ algunivs : bool option ;
367321}
368322let default_options () = {
369323 hoas_holes = Some Verbatim ;
@@ -382,14 +336,15 @@ let default_options () = {
382336 keepunivs = None ;
383337 redflags = None ;
384338 no_tc = None ;
339+ algunivs = None ;
385340}
386341let make_options ~hoas_holes ~local ~warn ~depr ~primitive ~failsafe ~ppwidth
387342 ~pp ~pplevel ~using ~inline ~uinstance ~universe_decl ~reversible ~keepunivs
388- ~redflags ~no_tc =
343+ ~redflags ~no_tc ~ algunivs =
389344 let user_warns = Some UserWarn. { depr; warn } in
390345 { hoas_holes; local; user_warns; primitive; failsafe; ppwidth; pp;
391346 pplevel; using; inline; uinstance; universe_decl; reversible; keepunivs;
392- redflags; no_tc; }
347+ redflags; no_tc; algunivs; }
393348let make_warn = UserWarn. make_warn
394349
395350type 'a coq_context = {
@@ -422,6 +377,67 @@ let pr_coq_ctx { env; db2name; db2rel } sigma =
422377 v 0 (Printer. pr_rel_context_of env sigma) ++ cut ()
423378 )
424379
380+ let propc = E.Constants. declare_global_symbol " prop"
381+ let spropc = E.Constants. declare_global_symbol " sprop"
382+ let typc = E.Constants. declare_global_symbol " typ"
383+
384+
385+ let sort : (Sorts.t, _ coq_context, API.Data.constraints) API.ContextualConversion.t =
386+ let open API.ContextualConversion in
387+ {
388+ ty = API.Conversion. TyName " sort" ;
389+ pp_doc = (fun fmt () ->
390+ Format. fprintf fmt " %% Sorts (kinds of types)\n " ;
391+ Format. fprintf fmt " kind sort type.\n " ;
392+ Format. fprintf fmt " type prop sort. %% impredicative sort of propositions\n " ;
393+ Format. fprintf fmt " type sprop sort. %% impredicative sort of propositions with definitional proof irrelevance\n " ;
394+ Format. fprintf fmt " type typ univ -> sort. %% predicative sort of data (carries a universe level)\n " ;
395+ );
396+ pp = (fun fmt -> function
397+ | Sorts. Type _ -> Format. fprintf fmt " Type"
398+ | Sorts. Set -> Format. fprintf fmt " Set"
399+ | Sorts. Prop -> Format. fprintf fmt " Prop"
400+ | Sorts. SProp -> Format. fprintf fmt " SProp"
401+ | Sorts. QSort _ -> Format. fprintf fmt " QSort" );
402+ embed = (fun ~depth { options } _ state s ->
403+ match s with
404+ | Sorts. Prop -> state, E. mkConst propc, []
405+ | Sorts. SProp -> state, E. mkConst spropc, []
406+ | Sorts. Set ->
407+ let state, u, gls = univ.embed ~depth state Univ.Universe. type0 in
408+ state, E. mkApp typc u [] , gls
409+ | Sorts. Type u ->
410+ let state, u, gls = univ.embed ~depth state u in
411+ state, E. mkApp typc u [] , gls
412+ | Sorts. QSort _ -> nYI " sort polymorphism" );
413+ readback = (fun ~depth { options } _ state t ->
414+ match E. look ~depth t with
415+ | E. Const c when c == propc -> state, Sorts. prop, []
416+ | E. Const c when c == spropc -> state, Sorts. sprop, []
417+ | E. App (c ,u ,[] ) when c == typc ->
418+ let state, u, gls = univ.readback ~depth state u in
419+ state, Sorts. sort_of_univ u ,gls
420+ | E. UnifVar (k ,_ ) -> begin
421+ let m = S. get um state in
422+ try
423+ let u = UM. host k m in
424+ state, Sorts. sort_of_univ u, []
425+ with Not_found ->
426+ let state, (_,u) = new_univ_level_variable state in
427+ let state = S. update um state (UM. add k u) in
428+ state, Sorts. sort_of_univ u, []
429+ end
430+ | _ -> raise API.Conversion. (TypeErr (TyName " sort" ,depth,t)));
431+ }
432+
433+ let ast_sort ~loc = function
434+ | Sorts. Prop -> A. mkGlobal ~loc propc
435+ | Sorts. SProp -> A. mkGlobal ~loc spropc
436+ | Sorts. Set -> A. mkAppGlobal ~loc typc (A. mkOpaque ~loc @@ univino Univ.Universe. type0) []
437+ | Sorts. Type u -> A. mkAppGlobal ~loc typc (A. mkOpaque ~loc @@ univino u) []
438+ | _ -> assert false
439+
440+
425441let in_coq_fresh ~id_only =
426442 let mk_fresh dbl =
427443 Id. of_string_soft
@@ -1052,7 +1068,6 @@ let section_ids env =
10521068 ~init: [] named_ctx
10531069
10541070let sortc = E.Constants. declare_global_symbol " sort"
1055- let typc = E.Constants. declare_global_symbol " typ"
10561071
10571072let force_level_of_universe state u =
10581073 match Univ.Universe. level u with
@@ -1074,14 +1089,18 @@ let in_elpi_flex_sort t = E.mkApp sortc (E.mkApp typc t []) []
10741089let in_elpiast_flex_sort ~loc t =
10751090 A. mkAppGlobal ~loc sortc (A. mkAppGlobal ~loc typc t [] ) []
10761091
1077- let sort = { sort with API.Conversion. embed = (fun ~depth state s ->
1078- let state, s = purge_algebraic_univs_sort state (EConstr.ESorts. make s) in
1079- sort.API.Conversion. embed ~depth state s) }
1092+ let sort = { sort with API.ContextualConversion. embed = (fun ~depth ctx csts state s ->
1093+ let state, s =
1094+ if ctx.options.algunivs = None || ctx.options.algunivs = Some false then
1095+ purge_algebraic_univs_sort state (EConstr.ESorts. make s)
1096+ else
1097+ state, s in
1098+ sort.API.ContextualConversion. embed ~depth ctx csts state s) }
10801099
1081- let in_elpi_sort ~depth state s =
1082- let state, s, gl = sort.API.Conversion . embed ~depth state s in
1100+ let in_elpi_sort ~depth ctx csts state s =
1101+ let state, s, gl = sort.API.ContextualConversion . embed ~depth ctx csts state s in
10831102 assert (gl= [] );
1084- state, E. mkApp sortc s []
1103+ state, E. mkApp sortc s [] , gl
10851104
10861105let in_elpiast_sort ~loc state s =
10871106 A. mkAppGlobal ~loc sortc (ast_sort ~loc s) []
@@ -1301,10 +1320,10 @@ let get_options ~depth hyps state =
13011320 let no_tc = get_bool_option " coq:no_tc" in
13021321 let keepunivs = get_bool_option " coq:keepunivs" in
13031322 let redflags = get_redflags_option () in
1323+ let algunivs = get_bool_option " coq:keepalgunivs" in
13041324 make_options ~hoas_holes ~local ~warn ~depr ~primitive ~failsafe ~ppwidth
13051325 ~pp ~pplevel ~using ~inline ~uinstance ~universe_decl ~reversible ~keepunivs
1306- ~redflags ~no_tc
1307-
1326+ ~redflags ~no_tc ~algunivs
13081327let mk_coq_context ~options state =
13091328 let env = get_global_env state in
13101329 let section = section_ids env in
@@ -1452,7 +1471,10 @@ let rec constr2lp coq_ctx ~calldepth ~depth state t =
14521471 let args = CList. firstn argno args in
14531472 let state, args = CList. fold_left_map (aux ~depth env) state args in
14541473 state, E. mkUnifVar elpi_uvk ~args: (List. rev args) state
1455- | C. Sort s -> in_elpi_sort ~depth state (EC.ESorts. kind sigma s)
1474+ | C. Sort s ->
1475+ let state, s, gl = in_elpi_sort ~depth coq_ctx API.RawData. no_constraints state (EC.ESorts. kind sigma s) in
1476+ gls := gl @ ! gls;
1477+ state, s
14561478 | C. Cast (t ,_ ,ty0 ) ->
14571479 let state, t = aux ~depth env state t in
14581480 let state, ty = aux ~depth env state ty0 in
@@ -1962,7 +1984,7 @@ and lp2constr ~calldepth syntactic_constraints coq_ctx ~depth state ?(on_ty=fals
19621984 debug Pp. (fun () -> str" lp2term@" ++ int depth ++ str" :" ++ str(pp2string (P. term depth) t));
19631985 match E. look ~depth t with
19641986 | E. App (s ,p ,[] ) when sortc == s ->
1965- let state, u, gsl = sort.API.Conversion . readback ~depth state p in
1987+ let state, u, gsl = sort.API.ContextualConversion . readback ~depth coq_ctx syntactic_constraints state p in
19661988 state, EC. mkSort (EC.ESorts. make u), gsl
19671989 (* constants *)
19681990 | E. App (c ,d ,[] ) when globalc == c ->
0 commit comments