| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Ltac_plugin |
| 12 |
open CErrors |
| 13 |
open Util |
| 14 |
open Term |
| 15 |
open Constr |
| 16 |
open Context |
| 17 |
open Proof_search |
| 18 |
open Context.Named.Declaration |
| 19 |
open UnivGen |
| 20 |
|
| 21 |
module Search = struct |
| 22 |
|
| 23 |
open Pp |
| 24 |
|
| 25 |
type position = int list |
| 26 |
|
| 27 |
let msg_with_position (p : position) s = match p with |
| 28 |
| [] -> () |
| 29 |
| _ :: _ -> |
| 30 |
let pp = Proof_search.pp s in |
| 31 |
let rec pp_rec = function |
| 32 |
| [] -> mt () |
| 33 |
| [i] -> int i |
| 34 |
| i :: l -> pp_rec l ++ str "." ++ int i |
| 35 |
in |
| 36 |
Feedback.msg_debug (h (pp_rec p) ++ pp) |
| 37 |
|
| 38 |
let push i p = match p with [] -> [] | _ :: _ -> i :: p |
| 39 |
|
| 40 |
let depth_first ?(debug=false) s = |
| 41 |
let rec explore p s = |
| 42 |
let () = msg_with_position p s in |
| 43 |
if Proof_search.success s then s else explore_many 1 p (Proof_search.branching s) |
| 44 |
and explore_many i p = function |
| 45 |
| [] -> raise Not_found |
| 46 |
| [s] -> explore (push i p) s |
| 47 |
| s :: l -> |
| 48 |
try explore (push i p) s with Not_found -> explore_many (succ i) p l |
| 49 |
in |
| 50 |
let pos = if debug then [1] else [] in |
| 51 |
explore pos s |
| 52 |
|
| 53 |
end |
| 54 |
|
| 55 |
let force count lazc = incr count;Lazy.force lazc |
| 56 |
|
| 57 |
let step_count = ref 0 |
| 58 |
|
| 59 |
let node_count = ref 0 |
| 60 |
|
| 61 |
let li_False = lazy (destInd (constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref "core.False.type")) |
| 62 |
let li_and = lazy (destInd (constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref "core.and.type")) |
| 63 |
let li_or = lazy (destInd (constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref "core.or.type")) |
| 64 |
|
| 65 |
let gen_constant n = lazy (constr_of_monomorphic_global (Global.env ()) (Rocqlib.lib_ref n)) |
| 66 |
|
| 67 |
let l_xI = gen_constant "num.pos.xI" |
| 68 |
let l_xO = gen_constant "num.pos.xO" |
| 69 |
let l_xH = gen_constant "num.pos.xH" |
| 70 |
|
| 71 |
let l_empty = gen_constant "plugins.rtauto.empty" |
| 72 |
let l_push = gen_constant "plugins.rtauto.push" |
| 73 |
|
| 74 |
let l_Reflect = gen_constant "plugins.rtauto.Reflect" |
| 75 |
|
| 76 |
let l_Atom = gen_constant "plugins.rtauto.Atom" |
| 77 |
let l_Arrow = gen_constant "plugins.rtauto.Arrow" |
| 78 |
let l_Bot = gen_constant "plugins.rtauto.Bot" |
| 79 |
let l_Conjunct = gen_constant "plugins.rtauto.Conjunct" |
| 80 |
let l_Disjunct = gen_constant "plugins.rtauto.Disjunct" |
| 81 |
|
| 82 |
let l_Ax = gen_constant "plugins.rtauto.Ax" |
| 83 |
let l_I_Arrow = gen_constant "plugins.rtauto.I_Arrow" |
| 84 |
let l_E_Arrow = gen_constant "plugins.rtauto.E_Arrow" |
| 85 |
let l_D_Arrow = gen_constant "plugins.rtauto.D_Arrow" |
| 86 |
let l_E_False = gen_constant "plugins.rtauto.E_False" |
| 87 |
let l_I_And = gen_constant "plugins.rtauto.I_And" |
| 88 |
let l_E_And = gen_constant "plugins.rtauto.E_And" |
| 89 |
let l_D_And = gen_constant "plugins.rtauto.D_And" |
| 90 |
let l_I_Or_l = gen_constant "plugins.rtauto.I_Or_l" |
| 91 |
let l_I_Or_r = gen_constant "plugins.rtauto.I_Or_r" |
| 92 |
let l_E_Or = gen_constant "plugins.rtauto.E_Or" |
| 93 |
let l_D_Or = gen_constant "plugins.rtauto.D_Or" |
| 94 |
|
| 95 |
let special_whd env sigma c = |
| 96 |
Reductionops.clos_whd_flags RedFlags.all env sigma c |
| 97 |
|
| 98 |
let special_nf env sigma c = |
| 99 |
Reductionops.clos_norm_flags RedFlags.betaiotazeta env sigma c |
| 100 |
|
| 101 |
type atom_env= |
| 102 |
{mutable next:int; |
| 103 |
mutable env:(constr*int) list} |
| 104 |
|
| 105 |
let make_atom atom_env term= |
| 106 |
let term = EConstr.Unsafe.to_constr term in |
| 107 |
try |
| 108 |
let (_,i)= |
| 109 |
List.find (fun (t,_)-> Constr.equal term t) atom_env.env |
| 110 |
in Atom i |
| 111 |
with Not_found -> |
| 112 |
let i=atom_env.next in |
| 113 |
atom_env.env <- (term,i)::atom_env.env; |
| 114 |
atom_env.next<- i + 1; |
| 115 |
Atom i |
| 116 |
|
| 117 |
let rec make_form env sigma atom_env term = |
| 118 |
let open EConstr in |
| 119 |
let open Vars in |
| 120 |
let normalize = special_nf env sigma in |
| 121 |
let cciterm = special_whd env sigma term in |
| 122 |
match EConstr.kind sigma cciterm with |
| 123 |
Prod(_,a,b) -> |
| 124 |
if noccurn sigma 1 b && |
| 125 |
QualityOrSet.is_prop (Retyping.get_sort_quality_of env sigma a) |
| 126 |
then |
| 127 |
let fa = make_form env sigma atom_env a in |
| 128 |
let fb = make_form env sigma atom_env b in |
| 129 |
Arrow (fa,fb) |
| 130 |
else |
| 131 |
make_atom atom_env (normalize term) |
| 132 |
| Cast(a,_,_) -> |
| 133 |
make_form env sigma atom_env a |
| 134 |
| Ind (ind, _) -> |
| 135 |
if Environ.QInd.equal env ind (fst (Lazy.force li_False)) |
| 136 |
then Bot |
| 137 |
else make_atom atom_env (normalize term) |
| 138 |
| App(hd,argv) when Int.equal (Array.length argv) 2 -> |
| 139 |
begin |
| 140 |
try |
| 141 |
let ind, _ = destInd sigma hd in |
| 142 |
if Environ.QInd.equal env ind (fst (Lazy.force li_and)) then |
| 143 |
let fa = make_form env sigma atom_env argv.(0) in |
| 144 |
let fb = make_form env sigma atom_env argv.(1) in |
| 145 |
Conjunct (fa,fb) |
| 146 |
else if Environ.QInd.equal env ind (fst (Lazy.force li_or)) then |
| 147 |
let fa = make_form env sigma atom_env argv.(0) in |
| 148 |
let fb = make_form env sigma atom_env argv.(1) in |
| 149 |
Disjunct (fa,fb) |
| 150 |
else make_atom atom_env (normalize term) |
| 151 |
with DestKO -> make_atom atom_env (normalize term) |
| 152 |
end |
| 153 |
| _ -> make_atom atom_env (normalize term) |
| 154 |
|
| 155 |
let rec make_hyps env sigma atom_env lenv = function |
| 156 |
[] -> [] |
| 157 |
| LocalDef (_,body,typ)::rest -> |
| 158 |
make_hyps env sigma atom_env (typ::body::lenv) rest |
| 159 |
| LocalAssum (id,typ)::rest -> |
| 160 |
let hrec= |
| 161 |
make_hyps env sigma atom_env (typ::lenv) rest in |
| 162 |
if List.exists (fun c -> Termops.local_occur_var sigma id.binder_name c) lenv || |
| 163 |
(not (QualityOrSet.is_prop (Retyping.get_sort_quality_of env sigma typ))) |
| 164 |
then |
| 165 |
hrec |
| 166 |
else |
| 167 |
(id,make_form env sigma atom_env typ)::hrec |
| 168 |
|
| 169 |
let rec build_pos n = |
| 170 |
if n<=1 then force node_count l_xH |
| 171 |
else if Int.equal (n land 1) 0 then |
| 172 |
mkApp (force node_count l_xO,[|build_pos (n asr 1)|]) |
| 173 |
else |
| 174 |
mkApp (force node_count l_xI,[|build_pos (n asr 1)|]) |
| 175 |
|
| 176 |
let rec build_form = function |
| 177 |
Atom n -> mkApp (force node_count l_Atom,[|build_pos n|]) |
| 178 |
| Arrow (f1,f2) -> |
| 179 |
mkApp (force node_count l_Arrow,[|build_form f1;build_form f2|]) |
| 180 |
| Bot -> force node_count l_Bot |
| 181 |
| Conjunct (f1,f2) -> |
| 182 |
mkApp (force node_count l_Conjunct,[|build_form f1;build_form f2|]) |
| 183 |
| Disjunct (f1,f2) -> |
| 184 |
mkApp (force node_count l_Disjunct,[|build_form f1;build_form f2|]) |
| 185 |
|
| 186 |
let rec decal k = function |
| 187 |
[] -> k |
| 188 |
| (start,delta)::rest -> |
| 189 |
if k>start then |
| 190 |
k - delta |
| 191 |
else |
| 192 |
decal k rest |
| 193 |
|
| 194 |
let add_pop size d pops= |
| 195 |
match pops with |
| 196 |
[] -> [size+d,d] |
| 197 |
| (_,sum)::_ -> (size+sum,sum+d)::pops |
| 198 |
|
| 199 |
let rec build_proof pops size = |
| 200 |
function |
| 201 |
Ax i -> |
| 202 |
mkApp (force step_count l_Ax, |
| 203 |
[|build_pos (decal i pops)|]) |
| 204 |
| I_Arrow p -> |
| 205 |
mkApp (force step_count l_I_Arrow, |
| 206 |
[|build_proof pops (size + 1) p|]) |
| 207 |
| E_Arrow(i,j,p) -> |
| 208 |
mkApp (force step_count l_E_Arrow, |
| 209 |
[|build_pos (decal i pops); |
| 210 |
build_pos (decal j pops); |
| 211 |
build_proof pops (size + 1) p|]) |
| 212 |
| D_Arrow(i,p1,p2) -> |
| 213 |
mkApp (force step_count l_D_Arrow, |
| 214 |
[|build_pos (decal i pops); |
| 215 |
build_proof pops (size + 2) p1; |
| 216 |
build_proof pops (size + 1) p2|]) |
| 217 |
| E_False i -> |
| 218 |
mkApp (force step_count l_E_False, |
| 219 |
[|build_pos (decal i pops)|]) |
| 220 |
| I_And(p1,p2) -> |
| 221 |
mkApp (force step_count l_I_And, |
| 222 |
[|build_proof pops size p1; |
| 223 |
build_proof pops size p2|]) |
| 224 |
| E_And(i,p) -> |
| 225 |
mkApp (force step_count l_E_And, |
| 226 |
[|build_pos (decal i pops); |
| 227 |
build_proof pops (size + 2) p|]) |
| 228 |
| D_And(i,p) -> |
| 229 |
mkApp (force step_count l_D_And, |
| 230 |
[|build_pos (decal i pops); |
| 231 |
build_proof pops (size + 1) p|]) |
| 232 |
| I_Or_l(p) -> |
| 233 |
mkApp (force step_count l_I_Or_l, |
| 234 |
[|build_proof pops size p|]) |
| 235 |
| I_Or_r(p) -> |
| 236 |
mkApp (force step_count l_I_Or_r, |
| 237 |
[|build_proof pops size p|]) |
| 238 |
| E_Or(i,p1,p2) -> |
| 239 |
mkApp (force step_count l_E_Or, |
| 240 |
[|build_pos (decal i pops); |
| 241 |
build_proof pops (size + 1) p1; |
| 242 |
build_proof pops (size + 1) p2|]) |
| 243 |
| D_Or(i,p) -> |
| 244 |
mkApp (force step_count l_D_Or, |
| 245 |
[|build_pos (decal i pops); |
| 246 |
build_proof pops (size + 2) p|]) |
| 247 |
| Pop(d,p) -> |
| 248 |
build_proof (add_pop size d pops) size p |
| 249 |
|
| 250 |
let build_env gamma= |
| 251 |
List.fold_right (fun (p,_) e -> |
| 252 |
mkApp(force node_count l_push,[|mkProp;p;e|])) |
| 253 |
gamma.env (mkApp (force node_count l_empty,[|mkProp|])) |
| 254 |
|
| 255 |
let { Goptions.get = verbose } = |
| 256 |
Goptions.declare_bool_option_and_ref |
| 257 |
~key:["Rtauto";"Verbose"] |
| 258 |
~value:false |
| 259 |
() |
| 260 |
|
| 261 |
let { Goptions.get = check } = |
| 262 |
Goptions.declare_bool_option_and_ref |
| 263 |
~key:["Rtauto";"Check"] |
| 264 |
~value:false |
| 265 |
() |
| 266 |
|
| 267 |
open Pp |
| 268 |
|
| 269 |
let rtauto_tac = |
| 270 |
Proofview.Goal.enter begin fun gl -> |
| 271 |
let hyps = Proofview.Goal.hyps gl in |
| 272 |
let concl = Proofview.Goal.concl gl in |
| 273 |
let env = Proofview.Goal.env gl in |
| 274 |
let sigma = Proofview.Goal.sigma gl in |
| 275 |
Rocqlib.check_required_library ["Stdlib";"rtauto";"Rtauto"]; |
| 276 |
let gamma={next=1;env=[]} in |
| 277 |
let () = |
| 278 |
if not (QualityOrSet.is_prop (Retyping.get_sort_quality_of env sigma concl)) |
| 279 |
then user_err (Pp.str "Goal should be in Prop.") in |
| 280 |
let glf = make_form env sigma gamma concl in |
| 281 |
let hyps = make_hyps env sigma gamma [concl] hyps in |
| 282 |
let formula= |
| 283 |
List.fold_left (fun gl (_,f)-> Arrow (f,gl)) glf hyps in |
| 284 |
let debug = match Tacinterp.get_debug () with |
| 285 |
| Tactic_debug.DebugOn 0 -> true |
| 286 |
| _ -> false |
| 287 |
in |
| 288 |
let search_fun s = Search.depth_first ~debug s in |
| 289 |
let () = |
| 290 |
begin |
| 291 |
reset_info (); |
| 292 |
if verbose () then |
| 293 |
Feedback.msg_info (str "Starting proof-search ..."); |
| 294 |
end in |
| 295 |
let search_start_time = System.get_time () in |
| 296 |
let prf = |
| 297 |
try project (search_fun (init_state [] formula)) |
| 298 |
with Not_found -> |
| 299 |
user_err (Pp.str "rtauto couldn't find a proof.") in |
| 300 |
let search_end_time = System.get_time () in |
| 301 |
let () = if verbose () then |
| 302 |
begin |
| 303 |
Feedback.msg_info (str "Proof tree found in " ++ |
| 304 |
System.fmt_time_difference search_start_time search_end_time); |
| 305 |
pp_info (); |
| 306 |
Feedback.msg_info (str "Building proof term ... ") |
| 307 |
end in |
| 308 |
let build_start_time=System.get_time () in |
| 309 |
let () = step_count := 0; node_count := 0 in |
| 310 |
let main = mkApp (force node_count l_Reflect, |
| 311 |
[|build_env gamma; |
| 312 |
build_form formula; |
| 313 |
build_proof [] 0 prf|]) in |
| 314 |
let term= |
| 315 |
applistc main (List.rev_map (fun (id,_) -> mkVar id.binder_name) hyps) in |
| 316 |
let build_end_time=System.get_time () in |
| 317 |
let () = if verbose () then |
| 318 |
begin |
| 319 |
Feedback.msg_info (str "Proof term built in " ++ |
| 320 |
System.fmt_time_difference build_start_time build_end_time ++ |
| 321 |
fnl () ++ |
| 322 |
str "Proof size : " ++ int !step_count ++ |
| 323 |
str " steps" ++ fnl () ++ |
| 324 |
str "Proof term size : " ++ int (!step_count+ !node_count) ++ |
| 325 |
str " nodes (constants)" ++ fnl () ++ |
| 326 |
str "Giving proof term to Rocq ... ") |
| 327 |
end in |
| 328 |
let tac_start_time = System.get_time () in |
| 329 |
let term = EConstr.of_constr term in |
| 330 |
let result= |
| 331 |
if check () then |
| 332 |
Tactics.exact_check term |
| 333 |
else |
| 334 |
Tactics.exact_no_check term in |
| 335 |
let tac_end_time = System.get_time () in |
| 336 |
let () = |
| 337 |
if check () then Feedback.msg_info (str "Proof term type-checking is on"); |
| 338 |
if verbose () then |
| 339 |
Feedback.msg_info (str "Internal tactic executed in " ++ |
| 340 |
System.fmt_time_difference tac_start_time tac_end_time) in |
| 341 |
result |
| 342 |
end |