Skip to content

Commit ae5b0a6

Browse files
committed
patch cs hook
1 parent 40e7379 commit ae5b0a6

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

apps/cs/src/coq_elpi_cs_hook.mlg

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ module Evarconv = Evarconv
1010
module Evarconv_hacked = Evarconv_hacked
1111

1212

13-
let elpi_cs_hook program env sigma lhs rhs =
14-
let (lhead, largs) = EConstr.decompose_app sigma lhs in
15-
let (rhead, rargs) = EConstr.decompose_app sigma rhs in
16-
if (EConstr.isConst sigma lhead && Structures.Structure.is_projection (fst (EConstr.destConst sigma lhead))) ||
17-
(EConstr.isConst sigma rhead && Structures.Structure.is_projection (fst (EConstr.destConst sigma rhead)))
18-
then begin
13+
let elpi_cs_hook program env sigma (t1, sk1) (t2, sk2) =
1914
let loc = API.Ast.Loc.initial "(unknown)" in
2015
let atts = [] in
16+
let () = Feedback.msg_info (Pp.str "cs hook start") in
2117
let (proji, u), arg =
2218
match Termops.global_app_of_constr sigma t1 with
2319
| (Names.GlobRef.ConstRef proji, u), arg -> (proji, u), arg
24-
| (proji, _), _ -> raise Not_found in
20+
| (proji, _), _ -> let () = Feedback.msg_info Pp.(str "proj is not const" ++ Names.GlobRef.print proji) in raise Not_found in
21+
let () = Feedback.msg_info (Pp.str "cs hook got proj") in
2522
let structure = Structures.Structure.find_from_projection proji in
23+
let () = Feedback.msg_info (Pp.str "cs hook got structure") in
2624
let params1, c1, extra_args1 =
2725
match arg with
2826
| Some c -> (* A primitive projection applied to c *)
@@ -35,6 +33,7 @@ let elpi_cs_hook program env sigma lhs rhs =
3533
match Reductionops.Stack.strip_n_app structure.nparams sk1 with
3634
| Some (params1, c1, extra_args1) -> (Option.get @@ Reductionops.Stack.list_of_app_stack params1), c1, extra_args1
3735
| _ -> raise Not_found in
36+
let () = Feedback.msg_info Pp.(str "cs hook got params & arg " ++ int (List.length sk2) ++ str " " ++ int (List.length extra_args1)) in
3837
let sk2, extra_args2 =
3938
if Reductionops.Stack.args_size sk2 = Reductionops.Stack.args_size extra_args1 then [], sk2
4039
else match Reductionops.Stack.strip_n_app (Reductionops.Stack.args_size sk2 - Reductionops.Stack.args_size extra_args1 - 1) sk2 with
@@ -45,36 +44,38 @@ let elpi_cs_hook program env sigma lhs rhs =
4544
let goal_evar, _ = EConstr.destEvar sigma goal in
4645
let query ~depth state =
4746
let state, (loc, q), gls =
48-
Coq_elpi_HOAS.goals2query sigma [goal_evar] loc ~main:(Coq_elpi_HOAS.Solve [])
47+
Coq_elpi_HOAS.goals2query sigma [goal_evar] loc ~main:(Coq_elpi_HOAS.Solve [EConstr.mkApp (EConstr.mkConstU (proji, EConstr.EInstance.empty), Array.of_list params1); rhs])
4948
~in_elpi_tac_arg:Coq_elpi_arg_HOAS.in_elpi_tac_econstr ~depth state in
5049
let state, qatts = atts2impl loc Summary.Stage.Interp ~depth state atts q in
5150
let state = API.State.set Coq_elpi_builtins.tactic_mode state true in
5251
state, (loc, qatts), gls
53-
in
52+
in let () = Feedback.msg_info Pp.(str "compile solver") in
5453
match Interp.get_and_compile program with
5554
| None -> None
5655
| Some (cprogram, _) ->
57-
begin match Interp.run ~static_check:false cprogram (`Fun query) with
58-
| API.Execute.Success solution ->
56+
let () = Feedback.msg_info Pp.(str "run solver\n") in
57+
begin try match Interp.run ~static_check:false cprogram (`Fun query) with
58+
| API.Execute.Success solution -> let () = Feedback.msg_info Pp.(str "found solution\n") in
5959
let gls = Evar.Set.singleton goal_evar in
6060
let sigma, _, _ = Coq_elpi_HOAS.solution2evd sigma solution gls in
6161
if Evd.is_defined sigma goal_evar then
6262
let lhs = Reductionops.Stack.zip sigma (EConstr.mkConstU (proji, EConstr.EInstance.empty), Reductionops.Stack.append_app_list (params1 @ [goal]) Reductionops.Stack.empty) in
6363
let lhs = Reductionops.whd_const proji env sigma lhs in
6464
let lhs = Reductionops.whd_betaiotazeta env sigma lhs in
6565
let hh, sk1 = EConstr.decompose_app sigma lhs in
66+
let () = Feedback.msg_info Pp.(str "aha" ++ Printer.pr_econstr_env env sigma lhs) in
6667
let h2, sk2 = EConstr.decompose_app sigma rhs in
6768
let _, params = EConstr.decompose_app sigma (Retyping.get_type_of env sigma goal) in
6869
Some (sigma, (hh, h2), goal, [], (Array.to_list params, params1), (Array.to_list sk1, Array.to_list sk2), (extra_args1, extra_args2), c1, (None, rhs))
6970
else None
7071
| API.Execute.NoMoreSteps
71-
| API.Execute.Failure -> None
72-
end
72+
| API.Execute.Failure -> let () = Feedback.msg_info Pp.(str "solver failed\n") in None
73+
with e -> let () = Feedback.msg_info Pp.(str "solver crashed\n") in raise e end
74+
| exception e -> let () = Feedback.msg_info Pp.(str "compiler crashed\n") in raise e
7375

7476
let add_cs_hook =
7577
let cs_hook_program = Summary.ref ~name:"elpi-cs" None in
7678
let cs_hook env sigma proj pat =
77-
Feedback.msg_info (Pp.str "run");
7879
match !cs_hook_program with
7980
| None -> None
8081
| Some h -> elpi_cs_hook h env sigma proj pat in
@@ -83,8 +84,6 @@ let add_cs_hook =
8384
let inCs =
8485
let cache program =
8586
cs_hook_program := Some program;
86-
Feedback.msg_info (Pp.str "activate");
87-
8887
Evarconv_hacked.activate_hook ~name in
8988
let open Libobject in
9089
declare_object

apps/cs/tests/test_cs.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ From Coq Require Import Bool.
33

44
Elpi Override CS All.
55

6-
Structure S : Type :=
7-
{ sort :> Type }.
6+
Structure S (T : Type) : Type :=
7+
{ sort :> T -> T }.
88

99
Elpi Accumulate canonical_solution lp:{{
1010

apps/cs/theories/cs.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@ From elpi Require Import elpi.
33

44
Elpi Db cs.db lp:{{
55

6-
% predicate [canonical-solution Ctx Lhs Rhs] used to unify Lhs with Rhs, with
6+
% predicate [cs Ctx Proj Rhs Sol] used to find Sol such that Proj Sol = Rhs, where
77
% - [Ctx] is the context
8-
% - [Lhs] and [Rhs] are the terms to unify
8+
% - [Proj] is the projector of some structure, applied to the structure's parameters if any
9+
% - [Rhs] the term to find a structure on.
910
:index (0 6 6)
10-
pred cs i:goal-ctx, o:term, o:term.
11+
pred cs i:goal-ctx, i:term, i:term, o:term.
1112

1213
}}.
1314

1415

1516

1617
Elpi Tactic canonical_solution.
17-
Elpi Accumulate lp:{{
18+
Elpi Accumulate Db cs.db.
19+
Elpi Accumulate canonical_solution lp:{{
1820

1921
solve (goal Ctx _ _Ty Sol [trm Proj, trm Rhs]) _ :-
2022
cs Ctx Proj Rhs Sol,
2123
% std.assert! (P = {{ eq_refl lp:Lhs }}) "cs: wrong solution".
2224
true.
2325

2426
}}.
25-
Elpi Accumulate Db cs.db.
26-
Elpi Typecheck.
27+
Elpi Typecheck canonical_solution.
2728
Elpi CSFallbackTactic canonical_solution.

0 commit comments

Comments
 (0)