@@ -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 () -> () );
@@ -4369,5 +4391,104 @@ Supported attributes:
43694391 DocAbove)
43704392
43714393 ]
4394+ @ Syntactic. ml_data @
4395+ [MLDataC(type_constraint );
4396+ MLCode(Pred("syntax.default-elab" ,
4397+ In(Syntactic. arg_type , "SyntaxArg" ,
4398+ Out(Rocq_elpi_arg_HOAS. arg_type , "Arg" ,
4399+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4400+ Full(global , "Elaborates the syntactic argument with the settings of #[arguments(elaborated)]" )))),
4401+ fun sarg _ diag ~depth coq_ctx _csts state ->
4402+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4403+ let base = Option. get (State. get base state ) in
4404+ try
4405+ let state , res , extra_goals =
4406+ Syntactic. top_of_res sarg |>
4407+ Rocq_elpi_arg_HOAS. in_elpi_cmd ~loc ~depth ~base ~kind :Elaborated coq_ctx state
4408+ in
4409+ state , (!: res +! B.mkOK ), extra_goals
4410+ with e ->
4411+ diag_error_lazy diag @@ fun () ->
4412+ let error =
4413+ string_of_ppcmds coq_ctx .options @@
4414+ try CErrors. print_no_report e with | _ -> raise No_clause
4415+ in
4416+ state , ?: None +! B. mkERROR error , []
4417+ ),
4418+ DocAbove);
4419+ MLCode(Pred("syntax.default-unelab" ,
4420+ In(Syntactic. arg_type , "SyntaxArg" ,
4421+ Out(Rocq_elpi_arg_HOAS. arg_type , "Arg" ,
4422+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4423+ Full(global , "Elaborates the syntactic argument with the settings of #[arguments(unelaborated)]" )))),
4424+ fun sarg _ diag ~depth coq_ctx _csts state ->
4425+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4426+ let base = Option. get (State. get base state ) in
4427+ try
4428+ let state , res , extra_goals =
4429+ Syntactic. top_of_res sarg |>
4430+ Rocq_elpi_arg_HOAS. in_elpi_cmd ~loc ~depth ~base ~kind :Unelaborated coq_ctx state
4431+ in
4432+ state , (!: res +! B.mkOK ), extra_goals
4433+ with e ->
4434+ diag_error_lazy diag @@ fun () ->
4435+ let error =
4436+ string_of_ppcmds coq_ctx .options @@
4437+ try CErrors. print_no_report e with | _ -> raise No_clause
4438+ in
4439+ state , ?: None +! B. mkERROR error , []
4440+ ),
4441+ DocAbove);
4442+ MLCode(Pred("syntax.push-scope" ,
4443+ In(Syntactic. trm_type , "SyntaxTerm" ,
4444+ In(Syntactic. delimiter_depth , "DelimiterDepth" ,
4445+ In(B. string , "ScopeName" ,
4446+ Out(Syntactic. trm_type , "ScopedSyntaxTerm" ,
4447+ Full(global , "Pushes the scope ScopeName on top of SyntaxTerm." ))))),
4448+ fun t delim_depth scope _ ~depth coq_context _csts state ->
4449+ let open Syntactic in
4450+ let loc = to_coq_loc @@ State. get Rocq_elpi_builtins_synterp. invocation_site_loc state in
4451+ let Tag. {vl; _} = t in
4452+ let vl = CAst. make ~loc (Constrexpr. CDelimiters (delim_depth , scope , vl )) in
4453+ let ot = Tag. {t with vl} in
4454+ state , (!: ot ), []
4455+ ),
4456+ DocAbove);
4457+ MLCode(Pred("syntax.elaborate" ,
4458+ In(Syntactic. trm_type , "SyntaxTerm" ,
4459+ CIn(type_constraint , "TypeConstraint" ,
4460+ COut(term , "Term" ,
4461+ InOut(B. ioarg B. diagnostic , "Diagnostic" ,
4462+ Full(proof_context , "Elaborates SyntaxTerm using TypeConstraint. Respects @no-tc! and @no-coercion!" ))))),
4463+ fun t expected_type _ diag ~depth coq_ctx csts state ->
4464+ let open Syntactic in
4465+ let Tag. {is;gs;vl} = t in
4466+ let vl = Ltac_plugin.Tacintern. intern_constr gs vl in
4467+ let sigma = get_sigma state in
4468+ let flags =
4469+ let open Pretyping in
4470+ let flags = all_no_fail_flags in
4471+ let options = coq_ctx .options in
4472+ let use_typeclasses = if Option. default false options .no_tc then NoUseTC else UseTC in
4473+ let use_coercions = not @@ Option. default false options .no_coercion in
4474+ { flags with use_typeclasses; use_coercions }
4475+ in
4476+ try
4477+ let sigma , vl =
4478+ Ltac_plugin.Tacinterp. interp_open_constr ~flags ~expected_type is coq_ctx .env sigma vl
4479+ in
4480+ let state , extra_goals = set_current_sigma ~depth state sigma in
4481+ state , (!: vl +! B.mkOK ), extra_goals
4482+ with e ->
4483+ diag_error_lazy diag @@ fun () ->
4484+ let error =
4485+ string_of_ppcmds coq_ctx .options @@
4486+ try CErrors. print_no_report e with | _ -> raise No_clause
4487+ in
4488+ state , ?: None +! B. mkERROR error , []
4489+ ),
4490+ DocAbove);
4491+ ]
4492+
43724493
43734494;;
0 commit comments