@@ -27,6 +27,7 @@ open Names
2727
2828open Rocq_elpi_utils
2929open Rocq_elpi_HOAS
30+ open Rocq_elpi_arg_HOAS
3031
3132let string_of_ppcmds options pp =
3233 let b = Buffer. create 512 in
@@ -252,6 +253,27 @@ let term_skeleton = {
252253 embed = (fun ~depth _ _ _ _ -> assert false );
253254}
254255
256+ let type_constraint =
257+ API.AlgebraicData. declare {
258+ ty = TyName " type-constraint" ;
259+ doc = " The expected type for elaborating syntactic terms" ;
260+ pp = (fun fmt _ -> Format. fprintf fmt " <TODO>" );
261+ constructors = Pretyping. [
262+ K (" without-type-constraint" , " Pretype without type constraint" , N ,
263+ B (WithoutTypeConstraint ),
264+ M (fun ~ok ~ko -> function WithoutTypeConstraint -> ok | _ -> ko () )
265+ );
266+ K (" of-type" , " Pretype with a specific expected type" , CA (term, N ),
267+ B (fun t -> OfType t),
268+ M (fun ~ok ~ko -> function OfType t -> ok t | _ -> ko () )
269+ );
270+ K (" is-type" , " Pretype as a type" , N ,
271+ B (IsType ),
272+ M (fun ~ok ~ko -> function IsType -> ok | _ -> ko () )
273+ );
274+ ];
275+ }
276+
255277let sealed_goal = {
256278 Conv. ty = Conv. TyName " sealed-goal" ;
257279 pp_doc = (fun fmt () -> () );
@@ -4390,5 +4412,104 @@ Supported attributes:
43904412 DocAbove)
43914413
43924414 ]
4415+ @ Syntactic. ml_data @
4416+ [MLDataC(type_constraint );
4417+ MLCode(Pred("syntax.default-elab" ,
4418+ In(Syntactic. arg_type , "SyntaxArg" ,
4419+ Out(Rocq_elpi_arg_HOAS. arg_type , "Arg" ,
4420+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4421+ Full(global , "Elaborates the syntactic argument with the settings of #[arguments(elaborated)]" )))),
4422+ fun sarg _ diag ~depth coq_ctx _csts state ->
4423+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4424+ let base = Option. get (State. get base state ) in
4425+ try
4426+ let state , res , extra_goals =
4427+ Syntactic. top_of_res sarg |>
4428+ Rocq_elpi_arg_HOAS. in_elpi_cmd ~loc ~depth ~base ~kind :Elaborated coq_ctx state
4429+ in
4430+ state , (!: res +! B.mkOK ), extra_goals
4431+ with e ->
4432+ diag_error_lazy diag @@ fun () ->
4433+ let error =
4434+ string_of_ppcmds coq_ctx .options @@
4435+ try CErrors. print_no_report e with | _ -> raise No_clause
4436+ in
4437+ state , ?: None +! B. mkERROR error , []
4438+ ),
4439+ DocAbove);
4440+ MLCode(Pred("syntax.default-unelab" ,
4441+ In(Syntactic. arg_type , "SyntaxArg" ,
4442+ Out(Rocq_elpi_arg_HOAS. arg_type , "Arg" ,
4443+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4444+ Full(global , "Elaborates the syntactic argument with the settings of #[arguments(unelaborated)]" )))),
4445+ fun sarg _ diag ~depth coq_ctx _csts state ->
4446+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4447+ let base = Option. get (State. get base state ) in
4448+ try
4449+ let state , res , extra_goals =
4450+ Syntactic. top_of_res sarg |>
4451+ Rocq_elpi_arg_HOAS. in_elpi_cmd ~loc ~depth ~base ~kind :Unelaborated coq_ctx state
4452+ in
4453+ state , (!: res +! B.mkOK ), extra_goals
4454+ with e ->
4455+ diag_error_lazy diag @@ fun () ->
4456+ let error =
4457+ string_of_ppcmds coq_ctx .options @@
4458+ try CErrors. print_no_report e with | _ -> raise No_clause
4459+ in
4460+ state , ?: None +! B. mkERROR error , []
4461+ ),
4462+ DocAbove);
4463+ MLCode(Pred("syntax.push-scope" ,
4464+ In(Syntactic. trm_type , "SyntaxTerm" ,
4465+ In(Syntactic. delimiter_depth , "DelimiterDepth" ,
4466+ In(B. string , "ScopeName" ,
4467+ Out(Syntactic. trm_type , "ScopedSyntaxTerm" ,
4468+ Full(global , "Pushes the scope ScopeName on top of SyntaxTerm." ))))),
4469+ fun t delim_depth scope _ ~depth coq_context _csts state ->
4470+ let open Syntactic in
4471+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4472+ let Tag. {vl; _} = t in
4473+ let vl = CAst. make ~loc (Constrexpr. CDelimiters (delim_depth , scope , vl )) in
4474+ let ot = Tag. {t with vl} in
4475+ state , (!: ot ), []
4476+ ),
4477+ DocAbove);
4478+ MLCode(Pred("syntax.elaborate" ,
4479+ In(Syntactic. trm_type , "SyntaxTerm" ,
4480+ CIn(type_constraint , "TypeConstraint" ,
4481+ COut(term , "Term" ,
4482+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4483+ Full(proof_context , "Elaborates SyntaxTerm using TypeConstraint. Respects @no-tc! and @no-coercion!" ))))),
4484+ fun t expected_type _ diag ~depth coq_ctx csts state ->
4485+ let open Syntactic in
4486+ let Tag. {is;gs;vl} = t in
4487+ let vl = Ltac_plugin.Tacintern. intern_constr gs vl in
4488+ let sigma = get_sigma state in
4489+ let flags =
4490+ let open Pretyping in
4491+ let flags = all_no_fail_flags in
4492+ let options = coq_ctx .options in
4493+ let use_typeclasses = if Option. default false options .no_tc then NoUseTC else UseTC in
4494+ let use_coercions = not @@ Option. default false options .no_coercion in
4495+ { flags with use_typeclasses; use_coercions }
4496+ in
4497+ try
4498+ let sigma , vl =
4499+ Ltac_plugin.Tacinterp. interp_open_constr ~flags ~expected_type is coq_ctx .env sigma vl
4500+ in
4501+ let state , extra_goals = set_current_sigma ~depth state sigma in
4502+ state , (!: vl +! B.mkOK ), extra_goals
4503+ with e ->
4504+ diag_error_lazy diag @@ fun () ->
4505+ let error =
4506+ string_of_ppcmds coq_ctx .options @@
4507+ try CErrors. print_no_report e with | _ -> raise No_clause
4508+ in
4509+ state , ?: None +! B. mkERROR error , []
4510+ ),
4511+ DocAbove);
4512+ ]
4513+
43934514
43944515;;
0 commit comments