1 (* Title: HOL/Tools/Metis/metis_tactic.ML
2 Author: Kong W. Susanto, Cambridge University Computer Laboratory
3 Author: Lawrence C. Paulson, Cambridge University Computer Laboratory
4 Author: Jasmin Blanchette, TU Muenchen
5 Copyright Cambridge University 2007
7 HOL setup for the Metis prover.
10 signature METIS_TACTIC =
12 val trace : bool Config.T
13 val verbose : bool Config.T
14 val new_skolemizer : bool Config.T
15 val type_has_top_sort : typ -> bool
17 string list -> string -> Proof.context -> thm list -> int -> tactic
18 val metis_lam_transs : string list
19 val parse_metis_options : (string list option * string option) parser
20 val setup : theory -> theory
23 structure Metis_Tactic : METIS_TACTIC =
29 open Metis_Reconstruct
32 Attrib.setup_config_bool @{binding metis_new_skolemizer} (K false)
34 (* Designed to work also with monomorphic instances of polymorphic theorems. *)
35 fun have_common_thm ths1 ths2 =
36 exists (member (Term.aconv_untyped o pairself prop_of) ths1)
37 (map Meson.make_meta_clause ths2)
39 (*Determining which axiom clauses are actually used*)
40 fun used_axioms axioms (th, Metis_Proof.Axiom _) = SOME (lookth axioms th)
41 | used_axioms _ _ = NONE
43 (* Lightweight predicate type information comes in two flavors, "t = t'" and
44 "t => t'", where "t" and "t'" are the same term modulo type tags.
45 In Isabelle, type tags are stripped away, so we are left with "t = t" or
46 "t => t". Type tag idempotence is also handled this way. *)
47 fun reflexive_or_trivial_from_metis ctxt type_enc sym_tab concealed mth =
48 let val thy = Proof_Context.theory_of ctxt in
49 case hol_clause_from_metis ctxt type_enc sym_tab concealed mth of
50 Const (@{const_name HOL.eq}, _) $ _ $ t =>
52 val ct = cterm_of thy t
53 val cT = ctyp_of_term ct
54 in refl |> Drule.instantiate' [SOME cT] [SOME ct] end
55 | Const (@{const_name disj}, _) $ t1 $ t2 =>
56 (if can HOLogic.dest_not t1 then t2 else t1)
57 |> HOLogic.mk_Trueprop |> cterm_of thy |> Thm.trivial
58 | _ => raise Fail "expected reflexive or trivial clause"
60 |> Meson.make_meta_clause
62 fun lambda_lifted_from_metis ctxt type_enc sym_tab concealed mth =
64 val thy = Proof_Context.theory_of ctxt
65 val tac = rewrite_goals_tac @{thms lambda_def_raw} THEN rtac refl 1
66 val t = hol_clause_from_metis ctxt type_enc sym_tab concealed mth
67 val ct = cterm_of thy (HOLogic.mk_Trueprop t)
68 in Goal.prove_internal [] ct (K tac) |> Meson.make_meta_clause end
70 fun introduce_lambda_wrappers_in_theorem ctxt th =
71 if Meson_Clausify.is_quasi_lambda_free (prop_of th) then
75 val th = th |> Drule.eta_contraction_rule
76 fun conv wrap ctxt ct =
77 if Meson_Clausify.is_quasi_lambda_free (term_of ct) then
79 else case term_of ct of
81 Conv.abs_conv (conv false o snd) ctxt ct
82 |> wrap ? (fn th => th RS @{thm Metis.eq_lambdaI})
83 | _ => Conv.comb_conv (conv true ctxt) ct
84 val eqth = conv true ctxt (cprop_of th)
85 in Thm.equal_elim eqth th end
88 {ordering = Metis_KnuthBendixOrder.default,
89 orderLiterals = Metis_Clause.UnsignedLiteralOrder,
92 {clause = clause_params,
93 prefactor = #prefactor Metis_Active.default,
94 postfactor = #postfactor Metis_Active.default}
97 variablesWeight = 0.0,
100 val resolution_params = {active = active_params, waiting = waiting_params}
102 (* Main function to start Metis proof and reconstruction *)
103 fun FOL_SOLVE (type_enc :: fallback_type_encs) lam_trans ctxt cls ths0 =
104 let val thy = Proof_Context.theory_of ctxt
106 Config.get ctxt new_skolemizer orelse null (Meson.choice_theorems thy)
108 map2 (fn j => fn th =>
109 (Thm.get_name_hint th,
110 Meson_Clausify.cnf_axiom ctxt new_skolemizer
111 (lam_trans = combinatorsN) j th))
112 (0 upto length ths0 - 1) ths0
113 val ths = maps (snd o snd) th_cls_pairs
114 val dischargers = map (fst o snd) th_cls_pairs
115 val _ = trace_msg ctxt (fn () => "FOL_SOLVE: CONJECTURE CLAUSES")
116 val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) cls
117 val _ = trace_msg ctxt (fn () => "type_enc = " ^ type_enc)
118 val type_enc = type_enc_from_string Sound type_enc
119 val (sym_tab, axioms, concealed) =
120 prepare_metis_problem ctxt type_enc lam_trans cls ths
121 fun get_isa_thm mth Isa_Reflexive_or_Trivial =
122 reflexive_or_trivial_from_metis ctxt type_enc sym_tab concealed mth
123 | get_isa_thm mth Isa_Lambda_Lifted =
124 lambda_lifted_from_metis ctxt type_enc sym_tab concealed mth
125 | get_isa_thm _ (Isa_Raw ith) =
126 ith |> lam_trans = lam_liftingN
127 ? introduce_lambda_wrappers_in_theorem ctxt
128 val axioms = axioms |> map (fn (mth, ith) => (mth, get_isa_thm mth ith))
129 val _ = trace_msg ctxt (fn () => "THEOREM CLAUSES")
130 val _ = app (fn (_, th) => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) axioms
131 val _ = trace_msg ctxt (fn () => "CLAUSES GIVEN TO METIS")
132 val thms = axioms |> map fst
133 val _ = app (fn th => trace_msg ctxt (fn () => Metis_Thm.toString th)) thms
134 val _ = trace_msg ctxt (fn () => "START METIS PROVE PROCESS")
136 case filter (fn t => prop_of t aconv @{prop False}) cls of
137 false_th :: _ => [false_th RS @{thm FalseE}]
139 case Metis_Resolution.new resolution_params
140 {axioms = thms, conjecture = []}
141 |> Metis_Resolution.loop of
142 Metis_Resolution.Contradiction mth =>
143 let val _ = trace_msg ctxt (fn () => "METIS RECONSTRUCTION START: " ^
144 Metis_Thm.toString mth)
145 val ctxt' = fold Variable.declare_constraints (map prop_of cls) ctxt
146 (*add constraints arising from converting goal to clause form*)
147 val proof = Metis_Proof.proof mth
150 |> fold (replay_one_inference ctxt' type_enc concealed sym_tab) proof
151 val used = map_filter (used_axioms axioms) proof
152 val _ = trace_msg ctxt (fn () => "METIS COMPLETED...clauses actually used:")
153 val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) used
154 val names = th_cls_pairs |> map fst
157 |> map_filter (fn (name, (_, cls)) =>
158 if have_common_thm used cls then SOME name
160 val unused_names = names |> subtract (op =) used_names
162 if not (null cls) andalso not (have_common_thm used cls) then
163 verbose_warning ctxt "The assumptions are inconsistent"
166 if not (null unused_names) then
167 "Unused theorems: " ^ commas_quote unused_names
168 |> verbose_warning ctxt
173 (trace_msg ctxt (fn () => "Success: " ^ Display.string_of_thm ctxt ith);
174 [discharge_skolem_premises ctxt dischargers ith])
175 | _ => (trace_msg ctxt (fn () => "Metis: No result"); [])
177 | Metis_Resolution.Satisfiable _ =>
178 (trace_msg ctxt (fn () => "Metis: No first-order proof with the lemmas supplied");
179 if null fallback_type_encs then
182 raise METIS ("FOL_SOLVE",
183 "No first-order proof with the lemmas supplied");
186 handle METIS (loc, msg) =>
187 case fallback_type_encs of
188 [] => error ("Failed to replay Metis proof in Isabelle." ^
189 (if Config.get ctxt verbose then "\n" ^ loc ^ ": " ^ msg
191 | first_fallback :: _ =>
192 (verbose_warning ctxt
193 ("Falling back on " ^
194 quote (metis_call first_fallback lam_trans) ^ "...");
195 FOL_SOLVE fallback_type_encs lam_trans ctxt cls ths0)
197 fun neg_clausify ctxt combinators =
199 #> Meson.make_clauses_unsorted ctxt
200 #> combinators ? map Meson_Clausify.introduce_combinators_in_theorem
203 fun preskolem_tac ctxt st0 =
204 (if exists (Meson.has_too_many_clauses ctxt)
205 (Logic.prems_of_goal (prop_of st0) 1) then
206 Simplifier.full_simp_tac (Meson_Clausify.ss_only @{thms not_all not_ex}) 1
207 THEN cnf.cnfx_rewrite_tac ctxt 1
211 val type_has_top_sort =
212 exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false)
214 fun generic_metis_tac type_encs lam_trans ctxt ths i st0 =
216 val _ = trace_msg ctxt (fn () =>
217 "Metis called with theorems\n" ^
218 cat_lines (map (Display.string_of_thm ctxt) ths))
219 val type_encs = type_encs |> maps unalias_type_enc
221 resolve_tac (FOL_SOLVE type_encs lam_trans ctxt clause ths) 1
223 if exists_type type_has_top_sort (prop_of st0) then
224 verbose_warning ctxt "Proof state contains the universal sort {}"
227 Meson.MESON (preskolem_tac ctxt)
228 (maps (neg_clausify ctxt (lam_trans = combinatorsN))) tac ctxt i st0
231 fun metis_tac [] = generic_metis_tac partial_type_encs
232 | metis_tac type_encs = generic_metis_tac type_encs
234 (* Whenever "X" has schematic type variables, we treat "using X by metis" as
235 "by (metis X)" to prevent "Subgoal.FOCUS" from freezing the type variables.
236 We don't do it for nonschematic facts "X" because this breaks a few proofs
237 (in the rare and subtle case where a proof relied on extensionality not being
238 applied) and brings few benefits. *)
240 exists_type (exists_subtype (fn TVar _ => true | _ => false)) o prop_of
242 fun method default_type_encs ((override_type_encs, lam_trans), ths) ctxt facts =
245 if default_type_encs = full_type_encs then
246 legacy_feature "Old \"metisFT\" method -- use \"metis (full_types)\" instead"
249 val (schem_facts, nonschem_facts) = List.partition has_tvar facts
250 val type_encs = override_type_encs |> the_default default_type_encs
251 val lam_trans = lam_trans |> the_default metis_default_lam_trans
253 HEADGOAL (Method.insert_tac nonschem_facts THEN'
254 CHANGED_PROP o generic_metis_tac type_encs lam_trans ctxt
258 val metis_lam_transs = [hide_lamsN, lam_liftingN, combinatorsN]
261 if member (op =) metis_lam_transs s then apsnd (K (SOME s))
262 else apfst (K (SOME [s]))
264 val parse_metis_options =
266 (Args.parens (Parse.short_ident
267 -- Scan.option (Parse.$$$ "," |-- Parse.short_ident))
269 (NONE, NONE) |> consider_opt s
270 |> (case s' of SOME s' => consider_opt s' | _ => I)))
273 fun setup_method (binding, type_encs) =
274 Scan.lift parse_metis_options -- Attrib.thms >> (METHOD oo method type_encs)
275 |> Method.setup binding
278 [((@{binding metis}, partial_type_encs),
279 "Metis for FOL and HOL problems"),
280 ((@{binding metisFT}, full_type_encs),
281 "Metis for FOL/HOL problems with fully-typed translation")]
282 |> fold (uncurry setup_method)