| 1 |
DECLARE PLUGIN "rocq-elpi.coercion" |
| 2 |
|
| 3 |
{ |
| 4 |
|
| 5 |
open Elpi |
| 6 |
open Elpi_plugin |
| 7 |
open Rocq_elpi_arg_syntax |
| 8 |
open Rocq_elpi_vernacular |
| 9 |
|
| 10 |
let relevant = EConstr.ERelevance.relevant |
| 11 |
let anonR = Context.make_annot Names.Name.Anonymous EConstr.ERelevance.irrelevant |
| 12 |
let nameR x = Context.make_annot (Names.Name.Name x) EConstr.ERelevance.irrelevant |
| 13 |
let annotR x = Context.make_annot x EConstr.ERelevance.irrelevant |
| 14 |
let build_expected_type env sigma expected = |
| 15 |
match expected with |
| 16 |
| Coercion.Type t -> sigma, t, false |
| 17 |
| Coercion.Product -> |
| 18 |
let (sigma, (source, _)) = Evarutil.new_type_evar env sigma Evd.univ_flexible in |
| 19 |
let (sigma, (target, _)) = |
| 20 |
let env = EConstr.push_rel (Context.Rel.Declaration.LocalAssum (EConstr.annotR (Names.Name (Namegen.default_dependent_ident)) , source)) env in |
| 21 |
Evarutil.new_type_evar env sigma Evd.univ_flexible in |
| 22 |
sigma, EConstr.mkProd (annotR (Names.Name (Namegen.default_type_ident)), source, target), true |
| 23 |
| Coercion.Sort -> let (sigma, t) = Evarutil.new_Type sigma in sigma, t, true |
| 24 |
let return s g t = Some (s,g,t) |
| 25 |
|
| 26 |
let elpi_coercion_hook program env sigma ~flags v ~inferred ~expected = |
| 27 |
let atts = [] in |
| 28 |
let sigma, expected, retype = build_expected_type env sigma expected in |
| 29 |
let sigma, goal = Evarutil.new_evar ~typeclass_candidate:false env sigma expected in |
| 30 |
let goal_evar, _ = EConstr.destEvar sigma goal in |
| 31 |
let query state = |
| 32 |
let loc = Elpi.API.State.get Rocq_elpi_builtins_synterp.invocation_site_loc state in |
| 33 |
let depth = 0 in |
| 34 |
let state, q, gls = |
| 35 |
Rocq_elpi_HOAS.solvegoals2query sigma [goal_evar] loc ~main:[v; inferred] |
| 36 |
~in_elpi_tac_arg:Rocq_elpi_arg_HOAS.in_elpi_tac_econstr ~depth ~base:() state in |
| 37 |
let state, qatts = atts2impl loc Summary.Stage.Interp ~depth state atts q in |
| 38 |
let state = API.State.set Rocq_elpi_builtins.tactic_mode state true in |
| 39 |
state, qatts, gls |
| 40 |
in |
| 41 |
let loc = Loc.initial Loc.ToplevelInput in |
| 42 |
match Interp.get_and_compile ~loc program with |
| 43 |
| None -> None |
| 44 |
| Some (cprogram, _) -> |
| 45 |
match Interp.run ~loc cprogram (Fun (query)) with |
| 46 |
| API.Execute.Success solution -> |
| 47 |
let gls = Evar.Set.singleton goal_evar in |
| 48 |
let sigma, _, _ = Rocq_elpi_HOAS.solution2evd ~eta_contract_solution:false sigma solution gls in |
| 49 |
if Evd.is_defined sigma goal_evar then |
| 50 |
let t = if retype then Retyping.get_type_of env sigma goal else expected in |
| 51 |
return sigma goal t |
| 52 |
else |
| 53 |
None |
| 54 |
| API.Execute.NoMoreSteps |
| 55 |
| API.Execute.Failure -> None |
| 56 |
| exception (Rocq_elpi_utils.LtacFail (level, msg)) -> None |
| 57 |
|
| 58 |
let add_coercion_hook = |
| 59 |
let coercion_hook_program = Summary.ref ~name:"elpi-coercion" None in |
| 60 |
let coercion_hook env sigma ~flags v ~inferred ~expected = |
| 61 |
match !coercion_hook_program with |
| 62 |
| None -> None |
| 63 |
| Some h -> elpi_coercion_hook h env sigma ~flags v ~inferred ~expected in |
| 64 |
let name = "elpi-coercion" in |
| 65 |
Coercion.register_hook ~name coercion_hook; |
| 66 |
let inCoercion = |
| 67 |
let cache program = |
| 68 |
coercion_hook_program := Some program; |
| 69 |
Coercion.activate_hook ~name in |
| 70 |
let open Libobject in |
| 71 |
declare_object |
| 72 |
@@ superglobal_object_nodischarge "ELPI-COERCION" ~cache ~subst:None in |
| 73 |
fun program -> Lib.add_leaf (inCoercion program) |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
VERNAC COMMAND EXTEND ElpiCoercion CLASSIFIED AS SIDEFF |
| 78 |
| #[ atts = any_attribute ] [ "Elpi" "CoercionFallbackTactic" qualified_name(p) ] -> { |
| 79 |
let () = ignore_unknown_attributes atts in |
| 80 |
add_coercion_hook (snd p) } |
| 81 |
|
| 82 |
END |