| 1 |
DECLARE PLUGIN "rocq-elpi.cs" |
| 2 |
|
| 3 |
{ |
| 4 |
|
| 5 |
open Elpi |
| 6 |
open Elpi_plugin |
| 7 |
open Rocq_elpi_arg_syntax |
| 8 |
open Rocq_elpi_vernacular |
| 9 |
|
| 10 |
[%%if coq = "9.0" || coq = "9.1"] |
| 11 |
let structure_projection_nparams env ind = Structures.Structure.projection_nparams ind |
| 12 |
[%%else] |
| 13 |
let structure_projection_nparams env ind = Structures.Structure.projection_nparams env ind |
| 14 |
[%%endif] |
| 15 |
|
| 16 |
let elpi_cs_hook program env sigma ((proji, u), params1, c1) (t2, args2) = |
| 17 |
let atts = [] in |
| 18 |
let rhs = Reductionops.Stack.zip sigma (t2, Reductionops.Stack.append_app_list args2 Reductionops.Stack.empty) in |
| 19 |
let sigma, (goal_ty, _) = Evarutil.new_type_evar env sigma Evd.UnivRigid in |
| 20 |
let sigma, goal = Evarutil.new_evar ~typeclass_candidate:false env sigma goal_ty in |
| 21 |
let goal_evar, _ = EConstr.destEvar sigma goal in |
| 22 |
let nparams = structure_projection_nparams env proji in |
| 23 |
let query state = |
| 24 |
let loc = Elpi.API.State.get Rocq_elpi_builtins_synterp.invocation_site_loc state in |
| 25 |
let state, q, gls = |
| 26 |
let lhs = match params1 with |
| 27 |
| Some params1 -> EConstr.mkApp (EConstr.mkConstU (proji, u), Array.of_list params1) |
| 28 |
| None -> EConstr.mkConstU (proji, u) in |
| 29 |
Rocq_elpi_HOAS.solvegoals2query sigma [goal_evar] loc ~main:[lhs; rhs] |
| 30 |
~in_elpi_tac_arg:Rocq_elpi_arg_HOAS.in_elpi_tac_econstr ~depth:0 ~base:() state in |
| 31 |
let state, qatts = atts2impl loc Summary.Stage.Interp ~depth:0 state atts q in |
| 32 |
let state = API.State.set Rocq_elpi_builtins.tactic_mode state true in |
| 33 |
state, qatts, gls |
| 34 |
in |
| 35 |
let loc = Loc.initial Loc.ToplevelInput in |
| 36 |
match Interp.get_and_compile ~loc program with |
| 37 |
| None -> None |
| 38 |
| Some (cprogram, _) -> |
| 39 |
begin match Interp.run ~loc cprogram (Fun query) with |
| 40 |
| API.Execute.Success solution -> |
| 41 |
let gls = Evar.Set.singleton goal_evar in |
| 42 |
let sigma, _, _ = Rocq_elpi_HOAS.solution2evd ~eta_contract_solution:false sigma solution gls in |
| 43 |
if Evd.is_defined sigma goal_evar then |
| 44 |
let constant = EConstr.to_constr sigma goal in |
| 45 |
let args_goal, t = Reduction.whd_decompose_lambda env constant in |
| 46 |
let args_type = List.rev_map Context.Rel.Declaration.get_type args_goal in |
| 47 |
let args_type = List.map EConstr.of_constr args_type in |
| 48 |
let args = snd (Constr.decompose_app_list t) in |
| 49 |
let params, projs = CList.chop nparams args in |
| 50 |
let i = Structures.Structure.projection_number env proji in |
| 51 |
let lhs = List.nth projs i in |
| 52 |
let lhs = EConstr.of_constr lhs in |
| 53 |
let _, sk1 = EConstr.decompose_app sigma lhs in |
| 54 |
let open Structures.CanonicalSolution in |
| 55 |
Some (sigma, { |
| 56 |
constant = EConstr.of_constr constant; |
| 57 |
abstractions_ty = args_type; |
| 58 |
body = lhs; |
| 59 |
nparams; |
| 60 |
params = List.map EConstr.of_constr params; |
| 61 |
cvalue_abstraction = None; |
| 62 |
cvalue_arguments = Array.to_list sk1}) |
| 63 |
else None |
| 64 |
| API.Execute.NoMoreSteps |
| 65 |
| API.Execute.Failure -> None |
| 66 |
end |
| 67 |
|
| 68 |
let add_cs_hook = |
| 69 |
let cs_hook_program = Summary.ref ~name:"elpi-cs" None in |
| 70 |
let cs_hook env sigma proj pat = |
| 71 |
match !cs_hook_program with |
| 72 |
| None -> None |
| 73 |
| Some h -> elpi_cs_hook h env sigma proj pat in |
| 74 |
let name = "elpi-cs" in |
| 75 |
Evarconv.register_hook ~name cs_hook; |
| 76 |
let inCs = |
| 77 |
let cache program = |
| 78 |
cs_hook_program := Some program; |
| 79 |
Evarconv.activate_hook ~name in |
| 80 |
let open Libobject in |
| 81 |
declare_object |
| 82 |
@@ superglobal_object_nodischarge "ELPI-CS" ~cache ~subst:None in |
| 83 |
fun program -> Lib.add_leaf (inCs program) |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
VERNAC COMMAND EXTEND ElpiCS CLASSIFIED AS SIDEFF |
| 88 |
| #[ atts = any_attribute ] [ "Elpi" "CS" qualified_name(p) ] -> { |
| 89 |
let () = ignore_unknown_attributes atts in |
| 90 |
add_cs_hook (snd p) } |
| 91 |
END |