src/HOL/Tools/Sledgehammer/clausifier.ML
changeset 39720 0b93a954da4f
parent 39719 b876d7525e72
child 39721 76a61ca09d5d
     1.1 --- a/src/HOL/Tools/Sledgehammer/clausifier.ML	Mon Sep 27 09:17:24 2010 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,254 +0,0 @@
     1.4 -(*  Title:      HOL/Tools/Sledgehammer/clausifier.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 CLAUSIFIER =
    1.12 -sig
    1.13 -  val extensionalize_theorem : thm -> thm
    1.14 -  val introduce_combinators_in_cterm : cterm -> thm
    1.15 -  val introduce_combinators_in_theorem : thm -> thm
    1.16 -  val to_definitional_cnf_with_quantifiers : theory -> thm -> thm
    1.17 -  val cnf_axiom : theory -> thm -> thm list
    1.18 -end;
    1.19 -
    1.20 -structure Clausifier : CLAUSIFIER =
    1.21 -struct
    1.22 -
    1.23 -(**** Transformation of Elimination Rules into First-Order Formulas****)
    1.24 -
    1.25 -val cfalse = cterm_of @{theory HOL} HOLogic.false_const;
    1.26 -val ctp_false = cterm_of @{theory HOL} (HOLogic.mk_Trueprop HOLogic.false_const);
    1.27 -
    1.28 -(* Converts an elim-rule into an equivalent theorem that does not have the
    1.29 -   predicate variable. Leaves other theorems unchanged. We simply instantiate
    1.30 -   the conclusion variable to False. (Cf. "transform_elim_term" in
    1.31 -   "Sledgehammer_Util".) *)
    1.32 -fun transform_elim_theorem th =
    1.33 -  case concl_of th of    (*conclusion variable*)
    1.34 -       @{const Trueprop} $ (v as Var (_, @{typ bool})) =>
    1.35 -           Thm.instantiate ([], [(cterm_of @{theory HOL} v, cfalse)]) th
    1.36 -    | v as Var(_, @{typ prop}) =>
    1.37 -           Thm.instantiate ([], [(cterm_of @{theory HOL} v, ctp_false)]) th
    1.38 -    | _ => th
    1.39 -
    1.40 -
    1.41 -(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
    1.42 -
    1.43 -fun mk_skolem t =
    1.44 -  let val T = fastype_of t in
    1.45 -    Const (@{const_name skolem}, T --> T) $ t
    1.46 -  end
    1.47 -
    1.48 -fun beta_eta_under_lambdas (Abs (s, T, t')) =
    1.49 -    Abs (s, T, beta_eta_under_lambdas t')
    1.50 -  | beta_eta_under_lambdas t = Envir.beta_eta_contract t
    1.51 -
    1.52 -(*Traverse a theorem, accumulating Skolem function definitions.*)
    1.53 -fun assume_skolem_funs th =
    1.54 -  let
    1.55 -    fun dec_sko (Const (@{const_name Ex}, _) $ (body as Abs (_, T, p))) rhss =
    1.56 -        (*Existential: declare a Skolem function, then insert into body and continue*)
    1.57 -        let
    1.58 -          val args = OldTerm.term_frees body
    1.59 -          (* Forms a lambda-abstraction over the formal parameters *)
    1.60 -          val rhs =
    1.61 -            list_abs_free (map dest_Free args,
    1.62 -                           HOLogic.choice_const T $ beta_eta_under_lambdas body)
    1.63 -            |> mk_skolem
    1.64 -          val comb = list_comb (rhs, args)
    1.65 -        in dec_sko (subst_bound (comb, p)) (rhs :: rhss) end
    1.66 -      | dec_sko (Const (@{const_name All},_) $ Abs (a, T, p)) rhss =
    1.67 -        (*Universal quant: insert a free variable into body and continue*)
    1.68 -        let val fname = Name.variant (OldTerm.add_term_names (p,[])) a
    1.69 -        in dec_sko (subst_bound (Free(fname,T), p)) rhss end
    1.70 -      | dec_sko (@{const HOL.conj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
    1.71 -      | dec_sko (@{const HOL.disj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
    1.72 -      | dec_sko (@{const Trueprop} $ p) rhss = dec_sko p rhss
    1.73 -      | dec_sko _ rhss = rhss
    1.74 -  in  dec_sko (prop_of th) []  end;
    1.75 -
    1.76 -
    1.77 -(**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
    1.78 -
    1.79 -val fun_cong_all = @{thm fun_eq_iff [THEN iffD1]}
    1.80 -
    1.81 -(* Removes the lambdas from an equation of the form "t = (%x. u)".
    1.82 -   (Cf. "extensionalize_term" in "Sledgehammer_Translate".) *)
    1.83 -fun extensionalize_theorem th =
    1.84 -  case prop_of th of
    1.85 -    _ $ (Const (@{const_name HOL.eq}, Type (_, [Type (@{type_name fun}, _), _]))
    1.86 -         $ _ $ Abs _) => extensionalize_theorem (th RS fun_cong_all)
    1.87 -  | _ => th
    1.88 -
    1.89 -fun is_quasi_lambda_free (Const (@{const_name skolem}, _) $ _) = true
    1.90 -  | is_quasi_lambda_free (t1 $ t2) =
    1.91 -    is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2
    1.92 -  | is_quasi_lambda_free (Abs _) = false
    1.93 -  | is_quasi_lambda_free _ = true
    1.94 -
    1.95 -val [f_B,g_B] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_B}));
    1.96 -val [g_C,f_C] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_C}));
    1.97 -val [f_S,g_S] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_S}));
    1.98 -
    1.99 -(* FIXME: Requires more use of cterm constructors. *)
   1.100 -fun abstract ct =
   1.101 -  let
   1.102 -      val thy = theory_of_cterm ct
   1.103 -      val Abs(x,_,body) = term_of ct
   1.104 -      val Type(@{type_name fun}, [xT,bodyT]) = typ_of (ctyp_of_term ct)
   1.105 -      val cxT = ctyp_of thy xT
   1.106 -      val cbodyT = ctyp_of thy bodyT
   1.107 -      fun makeK () =
   1.108 -        instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)]
   1.109 -                     @{thm abs_K}
   1.110 -  in
   1.111 -      case body of
   1.112 -          Const _ => makeK()
   1.113 -        | Free _ => makeK()
   1.114 -        | Var _ => makeK()  (*though Var isn't expected*)
   1.115 -        | Bound 0 => instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
   1.116 -        | rator$rand =>
   1.117 -            if loose_bvar1 (rator,0) then (*C or S*)
   1.118 -               if loose_bvar1 (rand,0) then (*S*)
   1.119 -                 let val crator = cterm_of thy (Abs(x,xT,rator))
   1.120 -                     val crand = cterm_of thy (Abs(x,xT,rand))
   1.121 -                     val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] @{thm abs_S}
   1.122 -                     val (_,rhs) = Thm.dest_equals (cprop_of abs_S')
   1.123 -                 in
   1.124 -                   Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
   1.125 -                 end
   1.126 -               else (*C*)
   1.127 -                 let val crator = cterm_of thy (Abs(x,xT,rator))
   1.128 -                     val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] @{thm abs_C}
   1.129 -                     val (_,rhs) = Thm.dest_equals (cprop_of abs_C')
   1.130 -                 in
   1.131 -                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
   1.132 -                 end
   1.133 -            else if loose_bvar1 (rand,0) then (*B or eta*)
   1.134 -               if rand = Bound 0 then Thm.eta_conversion ct
   1.135 -               else (*B*)
   1.136 -                 let val crand = cterm_of thy (Abs(x,xT,rand))
   1.137 -                     val crator = cterm_of thy rator
   1.138 -                     val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
   1.139 -                     val (_,rhs) = Thm.dest_equals (cprop_of abs_B')
   1.140 -                 in Thm.transitive abs_B' (Conv.arg_conv abstract rhs) end
   1.141 -            else makeK()
   1.142 -        | _ => raise Fail "abstract: Bad term"
   1.143 -  end;
   1.144 -
   1.145 -(* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
   1.146 -fun introduce_combinators_in_cterm ct =
   1.147 -  if is_quasi_lambda_free (term_of ct) then
   1.148 -    Thm.reflexive ct
   1.149 -  else case term_of ct of
   1.150 -    Abs _ =>
   1.151 -    let
   1.152 -      val (cv, cta) = Thm.dest_abs NONE ct
   1.153 -      val (v, _) = dest_Free (term_of cv)
   1.154 -      val u_th = introduce_combinators_in_cterm cta
   1.155 -      val cu = Thm.rhs_of u_th
   1.156 -      val comb_eq = abstract (Thm.cabs cv cu)
   1.157 -    in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
   1.158 -  | _ $ _ =>
   1.159 -    let val (ct1, ct2) = Thm.dest_comb ct in
   1.160 -        Thm.combination (introduce_combinators_in_cterm ct1)
   1.161 -                        (introduce_combinators_in_cterm ct2)
   1.162 -    end
   1.163 -
   1.164 -fun introduce_combinators_in_theorem th =
   1.165 -  if is_quasi_lambda_free (prop_of th) then
   1.166 -    th
   1.167 -  else
   1.168 -    let
   1.169 -      val th = Drule.eta_contraction_rule th
   1.170 -      val eqth = introduce_combinators_in_cterm (cprop_of th)
   1.171 -    in Thm.equal_elim eqth th end
   1.172 -    handle THM (msg, _, _) =>
   1.173 -           (warning ("Error in the combinator translation of " ^
   1.174 -                     Display.string_of_thm_without_context th ^
   1.175 -                     "\nException message: " ^ msg ^ ".");
   1.176 -            (* A type variable of sort "{}" will make abstraction fail. *)
   1.177 -            TrueI)
   1.178 -
   1.179 -(*cterms are used throughout for efficiency*)
   1.180 -val cTrueprop = cterm_of @{theory HOL} HOLogic.Trueprop;
   1.181 -
   1.182 -(*Given an abstraction over n variables, replace the bound variables by free
   1.183 -  ones. Return the body, along with the list of free variables.*)
   1.184 -fun c_variant_abs_multi (ct0, vars) =
   1.185 -      let val (cv,ct) = Thm.dest_abs NONE ct0
   1.186 -      in  c_variant_abs_multi (ct, cv::vars)  end
   1.187 -      handle CTERM _ => (ct0, rev vars);
   1.188 -
   1.189 -val skolem_def_raw = @{thms skolem_def_raw}
   1.190 -
   1.191 -(* Given the definition of a Skolem function, return a theorem to replace
   1.192 -   an existential formula by a use of that function.
   1.193 -   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
   1.194 -fun skolem_theorem_of_def thy rhs0 =
   1.195 -  let
   1.196 -    val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> cterm_of thy
   1.197 -    val rhs' = rhs |> Thm.dest_comb |> snd
   1.198 -    val (ch, frees) = c_variant_abs_multi (rhs', [])
   1.199 -    val (hilbert, cabs) = ch |> Thm.dest_comb |>> term_of
   1.200 -    val T =
   1.201 -      case hilbert of
   1.202 -        Const (@{const_name Eps}, Type (@{type_name fun}, [_, T])) => T
   1.203 -      | _ => raise TERM ("skolem_theorem_of_def: expected \"Eps\"", [hilbert])
   1.204 -    val cex = cterm_of thy (HOLogic.exists_const T)
   1.205 -    val ex_tm = Thm.capply cTrueprop (Thm.capply cex cabs)
   1.206 -    val conc =
   1.207 -      Drule.list_comb (rhs, frees)
   1.208 -      |> Drule.beta_conv cabs |> Thm.capply cTrueprop
   1.209 -    fun tacf [prem] =
   1.210 -      rewrite_goals_tac skolem_def_raw
   1.211 -      THEN rtac ((prem |> rewrite_rule skolem_def_raw) RS @{thm someI_ex}) 1
   1.212 -  in
   1.213 -    Goal.prove_internal [ex_tm] conc tacf
   1.214 -    |> forall_intr_list frees
   1.215 -    |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   1.216 -    |> Thm.varifyT_global
   1.217 -  end
   1.218 -
   1.219 -(* Converts an Isabelle theorem (intro, elim or simp format, even higher-order)
   1.220 -   into NNF. *)
   1.221 -fun to_nnf th ctxt0 =
   1.222 -  let
   1.223 -    val th1 = th |> transform_elim_theorem |> zero_var_indexes
   1.224 -    val ((_, [th2]), ctxt) = Variable.import true [th1] ctxt0
   1.225 -    val th3 = th2 |> Conv.fconv_rule Object_Logic.atomize
   1.226 -                  |> extensionalize_theorem
   1.227 -                  |> Meson.make_nnf ctxt
   1.228 -  in (th3, ctxt) end
   1.229 -
   1.230 -fun to_definitional_cnf_with_quantifiers thy th =
   1.231 -  let
   1.232 -    val eqth = cnf.make_cnfx_thm thy (HOLogic.dest_Trueprop (prop_of th))
   1.233 -    val eqth = eqth RS @{thm eq_reflection}
   1.234 -    val eqth = eqth RS @{thm TruepropI}
   1.235 -  in Thm.equal_elim eqth th end
   1.236 -
   1.237 -(* Convert a theorem to CNF, with Skolem functions as additional premises. *)
   1.238 -fun cnf_axiom thy th =
   1.239 -  let
   1.240 -    val ctxt0 = Variable.global_thm_context th
   1.241 -    val (nnf_th, ctxt) = to_nnf th ctxt0
   1.242 -    fun aux th =
   1.243 -      Meson.make_cnf (map (skolem_theorem_of_def thy) (assume_skolem_funs th))
   1.244 -                     th ctxt
   1.245 -    val (cnf_ths, ctxt) =
   1.246 -      aux nnf_th
   1.247 -      |> (fn ([], _) => aux (to_definitional_cnf_with_quantifiers thy nnf_th)
   1.248 -           | p => p)
   1.249 -  in
   1.250 -    cnf_ths |> map introduce_combinators_in_theorem
   1.251 -            |> Variable.export ctxt ctxt0
   1.252 -            |> Meson.finish_cnf
   1.253 -            |> map Thm.close_derivation
   1.254 -  end
   1.255 -  handle THM _ => []
   1.256 -
   1.257 -end;