@@ -166,45 +166,6 @@ let isuniv, univout, (univ : Univ.Universe.t API.Conversion.t) =
166166 | _ -> univ_to_be_patched.API.Conversion. readback ~depth state t
167167 end
168168}
169-
170- let sort =
171- let open API.AlgebraicData in declare {
172- ty = API.Conversion. TyName " sort" ;
173- doc = " Sorts (kinds of types)" ;
174- pp = (fun fmt -> function
175- | Sorts. Type _ -> Format. fprintf fmt " Type"
176- | Sorts. Set -> Format. fprintf fmt " Set"
177- | Sorts. Prop -> Format. fprintf fmt " Prop"
178- | Sorts. SProp -> Format. fprintf fmt " SProp"
179- | Sorts. QSort _ -> Format. fprintf fmt " Type" );
180- constructors = [
181- K (" prop" ," impredicative sort of propositions" ,N ,
182- B Sorts. prop,
183- M (fun ~ok ~ko -> function Sorts. Prop -> ok | _ -> ko () ));
184- K (" sprop" ," impredicative sort of propositions with definitional proof irrelevance" ,N ,
185- B Sorts. sprop,
186- M (fun ~ok ~ko -> function Sorts. SProp -> ok | _ -> ko () ));
187- K (" typ" ," predicative sort of data (carries a universe level)" ,A (univ,N ),
188- B (fun x -> Sorts. sort_of_univ x),
189- M (fun ~ok ~ko -> function
190- | Sorts. Type x -> ok x
191- | Sorts. Set -> ok Univ.Universe. type0
192- | _ -> ko () ));
193- K (" uvar" ," " ,A (F. uvar,N ),
194- BS (fun (k ,_ ) state ->
195- let m = S. get um state in
196- try
197- let u = UM. host k m in
198- state, Sorts. sort_of_univ u
199- with Not_found ->
200- let state, (_,u) = new_univ_level_variable state in
201- let state = S. update um state (UM. add k u) in
202- state, Sorts. sort_of_univ u),
203- M (fun ~ok ~ko _ -> ko () ));
204- ]
205- } |> API.ContextualConversion. (! < )
206-
207-
208169let universe_level_variable =
209170 let { CD. cin = levelin }, universe_level_variable_to_patch = CD. declare {
210171 CD. name = " univ.variable" ;
@@ -400,6 +361,59 @@ let pr_coq_ctx { env; db2name; db2rel } sigma =
400361 v 0 (Printer. pr_rel_context_of env sigma) ++ cut ()
401362 )
402363
364+ let propc = E.Constants. declare_global_symbol " prop"
365+ let spropc = E.Constants. declare_global_symbol " sprop"
366+ let typc = E.Constants. declare_global_symbol " typ"
367+
368+
369+ let sort : (Sorts.t, _ coq_context, API.Data.constraints) API.ContextualConversion.t =
370+ let open API.ContextualConversion in
371+ {
372+ ty = API.Conversion. TyName " sort" ;
373+ pp_doc = (fun fmt () ->
374+ Format. fprintf fmt " %% Sorts (kinds of types)\n " ;
375+ Format. fprintf fmt " kind sort type.\n " ;
376+ Format. fprintf fmt " type prop sort. %% impredicative sort of propositions\n " ;
377+ Format. fprintf fmt " type sprop sort. %% impredicative sort of propositions with definitional proof irrelevance\n " ;
378+ Format. fprintf fmt " type typ univ -> sort. %% predicative sort of data (carries a universe level)\n " ;
379+ );
380+ pp = (fun fmt -> function
381+ | Sorts. Type _ -> Format. fprintf fmt " Type"
382+ | Sorts. Set -> Format. fprintf fmt " Set"
383+ | Sorts. Prop -> Format. fprintf fmt " Prop"
384+ | Sorts. SProp -> Format. fprintf fmt " SProp"
385+ | Sorts. QSort _ -> Format. fprintf fmt " QSort" );
386+ embed = (fun ~depth { options } _ state s ->
387+ match s with
388+ | Sorts. Prop -> state, E. mkConst propc, []
389+ | Sorts. SProp -> state, E. mkConst spropc, []
390+ | Sorts. Set ->
391+ let state, u, gls = univ.embed ~depth state Univ.Universe. type0 in
392+ state, E. mkConst propc, gls
393+ | Sorts. Type u ->
394+ let state, u, gls = univ.embed ~depth state u in
395+ state, E. mkConst propc, gls
396+ | Sorts. QSort _ -> nYI " sort polymorphism" );
397+ readback = (fun ~depth { options } _ state t ->
398+ match E. look ~depth t with
399+ | E. Const c when c == propc -> state, Sorts. prop, []
400+ | E. Const c when c == spropc -> state, Sorts. sprop, []
401+ | E. App (c ,u ,[] ) when c == typc ->
402+ let state, u, gls = univ.readback ~depth state u in
403+ state, Sorts. sort_of_univ u ,gls
404+ | E. UnifVar (k ,_ ) -> begin
405+ let m = S. get um state in
406+ try
407+ let u = UM. host k m in
408+ state, Sorts. sort_of_univ u, []
409+ with Not_found ->
410+ let state, (_,u) = new_univ_level_variable state in
411+ let state = S. update um state (UM. add k u) in
412+ state, Sorts. sort_of_univ u, []
413+ end
414+ | _ -> raise API.Conversion. (TypeErr (TyName " sort" ,depth,t)));
415+ }
416+
403417let in_coq_fresh ~id_only =
404418 let mk_fresh dbl =
405419 Id. of_string_soft
@@ -949,18 +963,20 @@ let purge_algebraic_univs_sort state s =
949963 let state, _, _, s = force_level_of_universe state u in
950964 state, s
951965 | x -> state, x
952-
966+
967+ let sort = { sort with API.ContextualConversion. embed = (fun ~depth ctx csts state s ->
968+ let state, s =
969+ if ctx.options.algunivs = None || ctx.options.algunivs = Some false then
970+ purge_algebraic_univs_sort state (EConstr.ESorts. make s)
971+ else
972+ state, s in
973+ sort.API.ContextualConversion. embed ~depth ctx csts state s) }
974+
953975let in_elpi_flex_sort t = E. mkApp sortc (E. mkApp typc t [] ) []
954-
955- (* WIP: I do not know how to make this optional *)
956- (* let sort = { sort with API.Conversion.embed = (fun ~depth state s ->
957- let state, s = purge_algebraic_univs_sort state (EConstr.ESorts.make s) in
958- sort.API.Conversion.embed ~depth state s) } *)
959-
960- let in_elpi_sort ~depth state s =
961- let state, s, gl = sort.API.Conversion. embed ~depth state s in
962- assert (gl= [] );
963- state, E. mkApp sortc s []
976+
977+ let in_elpi_sort ~depth ctx csts state s =
978+ let state, s, gl = sort.API.ContextualConversion. embed ~depth ctx csts state s in
979+ state, E. mkApp sortc s [] , gl
964980
965981
966982(* ********************************* }}} ********************************** *)
@@ -1177,7 +1193,8 @@ let get_options ~depth hyps state =
11771193 no_tc = get_bool_option " coq:no_tc" ;
11781194 keepunivs = get_bool_option " coq:keepunivs" ;
11791195 redflags = get_redflags_option () ;
1180-
1196+ algunivs = keeping_algebraic_universes @@ get_string_option " coq:algunivs" ;
1197+ }
11811198let mk_coq_context ~options state =
11821199 let env = get_global_env state in
11831200 let section = section_ids env in
@@ -1319,7 +1336,10 @@ let rec constr2lp coq_ctx ~calldepth ~depth state t =
13191336 let args = CList. firstn argno args in
13201337 let state, args = CList. fold_left_map (aux ~depth env) state args in
13211338 state, E. mkUnifVar elpi_uvk ~args: (List. rev args) state
1322- | C. Sort s -> in_elpi_sort ~depth state (EC.ESorts. kind sigma s)
1339+ | C. Sort s ->
1340+ let state, s, gl = in_elpi_sort ~depth coq_ctx API.RawData. no_constraints state (EC.ESorts. kind sigma s) in
1341+ gls := gl @ ! gls;
1342+ state, s
13231343 | C. Cast (t ,_ ,ty0 ) ->
13241344 let state, t = aux ~depth env state t in
13251345 let state, ty = aux ~depth env state ty0 in
@@ -1830,7 +1850,7 @@ and lp2constr ~calldepth syntactic_constraints coq_ctx ~depth state ?(on_ty=fals
18301850 debug Pp. (fun () -> str" lp2term@" ++ int depth ++ str" :" ++ str(pp2string (P. term depth) t));
18311851 match E. look ~depth t with
18321852 | E. App (s ,p ,[] ) when sortc == s ->
1833- let state, u, gsl = sort.API.Conversion . readback ~depth state p in
1853+ let state, u, gsl = sort.API.ContextualConversion . readback ~depth coq_ctx syntactic_constraints state p in
18341854 state, EC. mkSort (EC.ESorts. make u), gsl
18351855 (* constants *)
18361856 | E. App (c ,d ,[] ) when globalc == c ->
0 commit comments