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