src/HOL/Tools/Meson/meson_clausify.ML
changeset 39940 1f01c9b2b76b
parent 39932 acde1b606b0e
child 39941 02fcd9cd1eac
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/Meson/meson_clausify.ML	Mon Oct 04 21:37:42 2010 +0200
     1.3 @@ -0,0 +1,376 @@
     1.4 +(*  Title:      HOL/Tools/Sledgehammer/meson_clausify.ML
     1.5 +    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
     1.6 +    Author:     Jasmin Blanchette, TU Muenchen
     1.7 +
     1.8 +Transformation of axiom rules (elim/intro/etc) into CNF forms.
     1.9 +*)
    1.10 +
    1.11 +signature MESON_CLAUSIFY =
    1.12 +sig
    1.13 +  val new_skolem_var_prefix : string
    1.14 +  val extensionalize_theorem : thm -> thm
    1.15 +  val introduce_combinators_in_cterm : cterm -> thm
    1.16 +  val introduce_combinators_in_theorem : thm -> thm
    1.17 +  val to_definitional_cnf_with_quantifiers : theory -> thm -> thm
    1.18 +  val cluster_of_zapped_var_name : string -> (int * (int * int)) * bool
    1.19 +  val cnf_axiom :
    1.20 +    Proof.context -> bool -> int -> thm -> (thm * term) option * thm list
    1.21 +  val meson_general_tac : Proof.context -> thm list -> int -> tactic
    1.22 +  val setup: theory -> theory
    1.23 +end;
    1.24 +
    1.25 +structure Meson_Clausify : MESON_CLAUSIFY =
    1.26 +struct
    1.27 +
    1.28 +(* the extra "?" helps prevent clashes *)
    1.29 +val new_skolem_var_prefix = "?SK"
    1.30 +val new_nonskolem_var_prefix = "?V"
    1.31 +
    1.32 +(**** Transformation of Elimination Rules into First-Order Formulas****)
    1.33 +
    1.34 +val cfalse = cterm_of @{theory HOL} HOLogic.false_const;
    1.35 +val ctp_false = cterm_of @{theory HOL} (HOLogic.mk_Trueprop HOLogic.false_const);
    1.36 +
    1.37 +(* Converts an elim-rule into an equivalent theorem that does not have the
    1.38 +   predicate variable. Leaves other theorems unchanged. We simply instantiate
    1.39 +   the conclusion variable to False. (Cf. "transform_elim_term" in
    1.40 +   "Sledgehammer_Util".) *)
    1.41 +fun transform_elim_theorem th =
    1.42 +  case concl_of th of    (*conclusion variable*)
    1.43 +       @{const Trueprop} $ (v as Var (_, @{typ bool})) =>
    1.44 +           Thm.instantiate ([], [(cterm_of @{theory HOL} v, cfalse)]) th
    1.45 +    | v as Var(_, @{typ prop}) =>
    1.46 +           Thm.instantiate ([], [(cterm_of @{theory HOL} v, ctp_false)]) th
    1.47 +    | _ => th
    1.48 +
    1.49 +
    1.50 +(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
    1.51 +
    1.52 +fun mk_old_skolem_term_wrapper t =
    1.53 +  let val T = fastype_of t in
    1.54 +    Const (@{const_name skolem}, T --> T) $ t
    1.55 +  end
    1.56 +
    1.57 +fun beta_eta_in_abs_body (Abs (s, T, t')) = Abs (s, T, beta_eta_in_abs_body t')
    1.58 +  | beta_eta_in_abs_body t = Envir.beta_eta_contract t
    1.59 +
    1.60 +(*Traverse a theorem, accumulating Skolem function definitions.*)
    1.61 +fun old_skolem_defs th =
    1.62 +  let
    1.63 +    fun dec_sko (Const (@{const_name Ex}, _) $ (body as Abs (_, T, p))) rhss =
    1.64 +        (*Existential: declare a Skolem function, then insert into body and continue*)
    1.65 +        let
    1.66 +          val args = OldTerm.term_frees body
    1.67 +          (* Forms a lambda-abstraction over the formal parameters *)
    1.68 +          val rhs =
    1.69 +            list_abs_free (map dest_Free args,
    1.70 +                           HOLogic.choice_const T $ beta_eta_in_abs_body body)
    1.71 +            |> mk_old_skolem_term_wrapper
    1.72 +          val comb = list_comb (rhs, args)
    1.73 +        in dec_sko (subst_bound (comb, p)) (rhs :: rhss) end
    1.74 +      | dec_sko (Const (@{const_name All},_) $ Abs (a, T, p)) rhss =
    1.75 +        (*Universal quant: insert a free variable into body and continue*)
    1.76 +        let val fname = Name.variant (OldTerm.add_term_names (p,[])) a
    1.77 +        in dec_sko (subst_bound (Free(fname,T), p)) rhss end
    1.78 +      | dec_sko (@{const conj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
    1.79 +      | dec_sko (@{const disj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
    1.80 +      | dec_sko (@{const Trueprop} $ p) rhss = dec_sko p rhss
    1.81 +      | dec_sko _ rhss = rhss
    1.82 +  in  dec_sko (prop_of th) []  end;
    1.83 +
    1.84 +
    1.85 +(**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
    1.86 +
    1.87 +val fun_cong_all = @{thm fun_eq_iff [THEN iffD1]}
    1.88 +
    1.89 +(* Removes the lambdas from an equation of the form "t = (%x. u)".
    1.90 +   (Cf. "extensionalize_term" in "Sledgehammer_Translate".) *)
    1.91 +fun extensionalize_theorem th =
    1.92 +  case prop_of th of
    1.93 +    _ $ (Const (@{const_name HOL.eq}, Type (_, [Type (@{type_name fun}, _), _]))
    1.94 +         $ _ $ Abs _) => extensionalize_theorem (th RS fun_cong_all)
    1.95 +  | _ => th
    1.96 +
    1.97 +fun is_quasi_lambda_free (Const (@{const_name skolem}, _) $ _) = true
    1.98 +  | is_quasi_lambda_free (t1 $ t2) =
    1.99 +    is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2
   1.100 +  | is_quasi_lambda_free (Abs _) = false
   1.101 +  | is_quasi_lambda_free _ = true
   1.102 +
   1.103 +val [f_B,g_B] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_B}));
   1.104 +val [g_C,f_C] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_C}));
   1.105 +val [f_S,g_S] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_S}));
   1.106 +
   1.107 +(* FIXME: Requires more use of cterm constructors. *)
   1.108 +fun abstract ct =
   1.109 +  let
   1.110 +      val thy = theory_of_cterm ct
   1.111 +      val Abs(x,_,body) = term_of ct
   1.112 +      val Type(@{type_name fun}, [xT,bodyT]) = typ_of (ctyp_of_term ct)
   1.113 +      val cxT = ctyp_of thy xT
   1.114 +      val cbodyT = ctyp_of thy bodyT
   1.115 +      fun makeK () =
   1.116 +        instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)]
   1.117 +                     @{thm abs_K}
   1.118 +  in
   1.119 +      case body of
   1.120 +          Const _ => makeK()
   1.121 +        | Free _ => makeK()
   1.122 +        | Var _ => makeK()  (*though Var isn't expected*)
   1.123 +        | Bound 0 => instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
   1.124 +        | rator$rand =>
   1.125 +            if loose_bvar1 (rator,0) then (*C or S*)
   1.126 +               if loose_bvar1 (rand,0) then (*S*)
   1.127 +                 let val crator = cterm_of thy (Abs(x,xT,rator))
   1.128 +                     val crand = cterm_of thy (Abs(x,xT,rand))
   1.129 +                     val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] @{thm abs_S}
   1.130 +                     val (_,rhs) = Thm.dest_equals (cprop_of abs_S')
   1.131 +                 in
   1.132 +                   Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
   1.133 +                 end
   1.134 +               else (*C*)
   1.135 +                 let val crator = cterm_of thy (Abs(x,xT,rator))
   1.136 +                     val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] @{thm abs_C}
   1.137 +                     val (_,rhs) = Thm.dest_equals (cprop_of abs_C')
   1.138 +                 in
   1.139 +                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
   1.140 +                 end
   1.141 +            else if loose_bvar1 (rand,0) then (*B or eta*)
   1.142 +               if rand = Bound 0 then Thm.eta_conversion ct
   1.143 +               else (*B*)
   1.144 +                 let val crand = cterm_of thy (Abs(x,xT,rand))
   1.145 +                     val crator = cterm_of thy rator
   1.146 +                     val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
   1.147 +                     val (_,rhs) = Thm.dest_equals (cprop_of abs_B')
   1.148 +                 in Thm.transitive abs_B' (Conv.arg_conv abstract rhs) end
   1.149 +            else makeK()
   1.150 +        | _ => raise Fail "abstract: Bad term"
   1.151 +  end;
   1.152 +
   1.153 +(* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
   1.154 +fun introduce_combinators_in_cterm ct =
   1.155 +  if is_quasi_lambda_free (term_of ct) then
   1.156 +    Thm.reflexive ct
   1.157 +  else case term_of ct of
   1.158 +    Abs _ =>
   1.159 +    let
   1.160 +      val (cv, cta) = Thm.dest_abs NONE ct
   1.161 +      val (v, _) = dest_Free (term_of cv)
   1.162 +      val u_th = introduce_combinators_in_cterm cta
   1.163 +      val cu = Thm.rhs_of u_th
   1.164 +      val comb_eq = abstract (Thm.cabs cv cu)
   1.165 +    in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
   1.166 +  | _ $ _ =>
   1.167 +    let val (ct1, ct2) = Thm.dest_comb ct in
   1.168 +        Thm.combination (introduce_combinators_in_cterm ct1)
   1.169 +                        (introduce_combinators_in_cterm ct2)
   1.170 +    end
   1.171 +
   1.172 +fun introduce_combinators_in_theorem th =
   1.173 +  if is_quasi_lambda_free (prop_of th) then
   1.174 +    th
   1.175 +  else
   1.176 +    let
   1.177 +      val th = Drule.eta_contraction_rule th
   1.178 +      val eqth = introduce_combinators_in_cterm (cprop_of th)
   1.179 +    in Thm.equal_elim eqth th end
   1.180 +    handle THM (msg, _, _) =>
   1.181 +           (warning ("Error in the combinator translation of " ^
   1.182 +                     Display.string_of_thm_without_context th ^
   1.183 +                     "\nException message: " ^ msg ^ ".");
   1.184 +            (* A type variable of sort "{}" will make abstraction fail. *)
   1.185 +            TrueI)
   1.186 +
   1.187 +(*cterms are used throughout for efficiency*)
   1.188 +val cTrueprop = cterm_of @{theory HOL} HOLogic.Trueprop;
   1.189 +
   1.190 +(*Given an abstraction over n variables, replace the bound variables by free
   1.191 +  ones. Return the body, along with the list of free variables.*)
   1.192 +fun c_variant_abs_multi (ct0, vars) =
   1.193 +      let val (cv,ct) = Thm.dest_abs NONE ct0
   1.194 +      in  c_variant_abs_multi (ct, cv::vars)  end
   1.195 +      handle CTERM _ => (ct0, rev vars);
   1.196 +
   1.197 +val skolem_def_raw = @{thms skolem_def_raw}
   1.198 +
   1.199 +(* Given the definition of a Skolem function, return a theorem to replace
   1.200 +   an existential formula by a use of that function.
   1.201 +   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
   1.202 +fun old_skolem_theorem_from_def thy rhs0 =
   1.203 +  let
   1.204 +    val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> cterm_of thy
   1.205 +    val rhs' = rhs |> Thm.dest_comb |> snd
   1.206 +    val (ch, frees) = c_variant_abs_multi (rhs', [])
   1.207 +    val (hilbert, cabs) = ch |> Thm.dest_comb |>> term_of
   1.208 +    val T =
   1.209 +      case hilbert of
   1.210 +        Const (@{const_name Eps}, Type (@{type_name fun}, [_, T])) => T
   1.211 +      | _ => raise TERM ("old_skolem_theorem_from_def: expected \"Eps\"",
   1.212 +                         [hilbert])
   1.213 +    val cex = cterm_of thy (HOLogic.exists_const T)
   1.214 +    val ex_tm = Thm.capply cTrueprop (Thm.capply cex cabs)
   1.215 +    val conc =
   1.216 +      Drule.list_comb (rhs, frees)
   1.217 +      |> Drule.beta_conv cabs |> Thm.capply cTrueprop
   1.218 +    fun tacf [prem] =
   1.219 +      rewrite_goals_tac skolem_def_raw
   1.220 +      THEN rtac ((prem |> rewrite_rule skolem_def_raw) RS @{thm someI_ex}) 1
   1.221 +  in
   1.222 +    Goal.prove_internal [ex_tm] conc tacf
   1.223 +    |> forall_intr_list frees
   1.224 +    |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   1.225 +    |> Thm.varifyT_global
   1.226 +  end
   1.227 +
   1.228 +fun to_definitional_cnf_with_quantifiers thy th =
   1.229 +  let
   1.230 +    val eqth = cnf.make_cnfx_thm thy (HOLogic.dest_Trueprop (prop_of th))
   1.231 +    val eqth = eqth RS @{thm eq_reflection}
   1.232 +    val eqth = eqth RS @{thm TruepropI}
   1.233 +  in Thm.equal_elim eqth th end
   1.234 +
   1.235 +fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
   1.236 +  (if skolem then new_skolem_var_prefix else new_nonskolem_var_prefix) ^
   1.237 +  "_" ^ string_of_int ax_no ^ "_" ^ string_of_int cluster_no ^ "_" ^
   1.238 +  string_of_int index_no ^ "_" ^ s
   1.239 +
   1.240 +fun cluster_of_zapped_var_name s =
   1.241 +  let val get_int = the o Int.fromString o nth (space_explode "_" s) in
   1.242 +    ((get_int 1, (get_int 2, get_int 3)),
   1.243 +     String.isPrefix new_skolem_var_prefix s)
   1.244 +  end
   1.245 +
   1.246 +fun zap (cluster as (cluster_no, cluster_skolem)) index_no pos ct =
   1.247 +  ct
   1.248 +  |> (case term_of ct of
   1.249 +        Const (s, _) $ Abs (s', _, _) =>
   1.250 +        if s = @{const_name all} orelse s = @{const_name All} orelse
   1.251 +           s = @{const_name Ex} then
   1.252 +          let
   1.253 +            val skolem = (pos = (s = @{const_name Ex}))
   1.254 +            val (cluster, index_no) =
   1.255 +              if skolem = cluster_skolem then (cluster, index_no)
   1.256 +              else ((cluster_no ||> cluster_skolem ? Integer.add 1, skolem), 0)
   1.257 +          in
   1.258 +            Thm.dest_comb #> snd
   1.259 +            #> Thm.dest_abs (SOME (zapped_var_name cluster index_no s'))
   1.260 +            #> snd #> zap cluster (index_no + 1) pos
   1.261 +          end
   1.262 +        else
   1.263 +          Conv.all_conv
   1.264 +      | Const (s, _) $ _ $ _ =>
   1.265 +        if s = @{const_name "==>"} orelse s = @{const_name implies} then
   1.266 +          Conv.combination_conv (Conv.arg_conv (zap cluster index_no (not pos)))
   1.267 +                                (zap cluster index_no pos)
   1.268 +        else if s = @{const_name conj} orelse s = @{const_name disj} then
   1.269 +          Conv.combination_conv (Conv.arg_conv (zap cluster index_no pos))
   1.270 +                                (zap cluster index_no pos)
   1.271 +        else
   1.272 +          Conv.all_conv
   1.273 +      | Const (s, _) $ _ =>
   1.274 +        if s = @{const_name Trueprop} then
   1.275 +          Conv.arg_conv (zap cluster index_no pos)
   1.276 +        else if s = @{const_name Not} then
   1.277 +          Conv.arg_conv (zap cluster index_no (not pos))
   1.278 +        else
   1.279 +          Conv.all_conv
   1.280 +      | _ => Conv.all_conv)
   1.281 +
   1.282 +fun ss_only ths = MetaSimplifier.clear_ss HOL_basic_ss addsimps ths
   1.283 +
   1.284 +val no_choice =
   1.285 +  @{prop "ALL x. EX y. Q x y ==> EX f. ALL x. Q x (f x)"}
   1.286 +  |> Logic.varify_global
   1.287 +  |> Skip_Proof.make_thm @{theory}
   1.288 +
   1.289 +(* Converts an Isabelle theorem into NNF. *)
   1.290 +fun nnf_axiom choice_ths new_skolemizer ax_no th ctxt =
   1.291 +  let
   1.292 +    val thy = ProofContext.theory_of ctxt
   1.293 +    val th =
   1.294 +      th |> transform_elim_theorem
   1.295 +         |> zero_var_indexes
   1.296 +         |> new_skolemizer ? forall_intr_vars
   1.297 +    val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single
   1.298 +    val th = th |> Conv.fconv_rule Object_Logic.atomize
   1.299 +                |> extensionalize_theorem
   1.300 +                |> Meson.make_nnf ctxt
   1.301 +  in
   1.302 +    if new_skolemizer then
   1.303 +      let
   1.304 +        fun skolemize choice_ths =
   1.305 +          Meson.skolemize_with_choice_thms ctxt choice_ths
   1.306 +          #> simplify (ss_only @{thms all_simps[symmetric]})
   1.307 +        val pull_out =
   1.308 +          simplify (ss_only @{thms all_simps[symmetric] ex_simps[symmetric]})
   1.309 +        val (discharger_th, fully_skolemized_th) =
   1.310 +          if null choice_ths then
   1.311 +            th |> `I |>> pull_out ||> skolemize [no_choice]
   1.312 +          else
   1.313 +            th |> skolemize choice_ths |> `I
   1.314 +        val t =
   1.315 +          fully_skolemized_th |> cprop_of
   1.316 +          |> zap ((ax_no, 0), true) 0 true |> Drule.export_without_context
   1.317 +          |> cprop_of |> Thm.dest_equals |> snd |> term_of
   1.318 +      in
   1.319 +        if exists_subterm (fn Var ((s, _), _) =>
   1.320 +                              String.isPrefix new_skolem_var_prefix s
   1.321 +                            | _ => false) t then
   1.322 +          let
   1.323 +            val (ct, ctxt) =
   1.324 +              Variable.import_terms true [t] ctxt
   1.325 +              |>> the_single |>> cterm_of thy
   1.326 +          in (SOME (discharger_th, ct), Thm.assume ct, ctxt) end
   1.327 +       else
   1.328 +         (NONE, th, ctxt)
   1.329 +      end
   1.330 +    else
   1.331 +      (NONE, th, ctxt)
   1.332 +  end
   1.333 +
   1.334 +(* Convert a theorem to CNF, with additional premises due to skolemization. *)
   1.335 +fun cnf_axiom ctxt0 new_skolemizer ax_no th =
   1.336 +  let
   1.337 +    val thy = ProofContext.theory_of ctxt0
   1.338 +    val choice_ths = Meson_Choices.get ctxt0
   1.339 +    val (opt, nnf_th, ctxt) = nnf_axiom choice_ths new_skolemizer ax_no th ctxt0
   1.340 +    fun clausify th =
   1.341 +      Meson.make_cnf (if new_skolemizer then
   1.342 +                        []
   1.343 +                      else
   1.344 +                        map (old_skolem_theorem_from_def thy)
   1.345 +                            (old_skolem_defs th)) th ctxt
   1.346 +    val (cnf_ths, ctxt) =
   1.347 +      clausify nnf_th
   1.348 +      |> (fn ([], _) =>
   1.349 +             clausify (to_definitional_cnf_with_quantifiers thy nnf_th)
   1.350 +           | p => p)
   1.351 +    fun intr_imp ct th =
   1.352 +      Thm.instantiate ([], map (pairself (cterm_of @{theory}))
   1.353 +                               [(Var (("i", 1), @{typ nat}),
   1.354 +                                 HOLogic.mk_nat ax_no)])
   1.355 +                      @{thm skolem_COMBK_D}
   1.356 +      RS Thm.implies_intr ct th
   1.357 +  in
   1.358 +    (opt |> Option.map (I #>> singleton (Variable.export ctxt ctxt0)
   1.359 +                        ##> (term_of #> HOLogic.dest_Trueprop
   1.360 +                             #> singleton (Variable.export_terms ctxt ctxt0))),
   1.361 +     cnf_ths |> map (introduce_combinators_in_theorem
   1.362 +                     #> (case opt of SOME (_, ct) => intr_imp ct | NONE => I))
   1.363 +             |> Variable.export ctxt ctxt0
   1.364 +             |> Meson.finish_cnf
   1.365 +             |> map Thm.close_derivation)
   1.366 +  end
   1.367 +  handle THM _ => (NONE, [])
   1.368 +
   1.369 +fun meson_general_tac ctxt ths =
   1.370 +  let val ctxt = Classical.put_claset HOL_cs ctxt in
   1.371 +    Meson.meson_tac ctxt (maps (snd o cnf_axiom ctxt false 0) ths)
   1.372 +  end
   1.373 +
   1.374 +val setup =
   1.375 +  Method.setup @{binding meson} (Attrib.thms >> (fn ths => fn ctxt =>
   1.376 +     SIMPLE_METHOD' (CHANGED_PROP o meson_general_tac ctxt ths)))
   1.377 +     "MESON resolution proof procedure"
   1.378 +
   1.379 +end;