src/HOL/Tools/res_axioms.ML
changeset 24827 646bdc51eb7d
parent 24821 cc55041ca8ba
child 24854 0ebcd575d3c6
     1.1 --- a/src/HOL/Tools/res_axioms.ML	Wed Oct 03 22:33:17 2007 +0200
     1.2 +++ b/src/HOL/Tools/res_axioms.ML	Thu Oct 04 12:32:58 2007 +0200
     1.3 @@ -15,7 +15,7 @@
     1.4    val cnf_rules_of_ths: thm list -> thm list
     1.5    val neg_clausify: thm list -> thm list
     1.6    val expand_defs_tac: thm -> tactic
     1.7 -  val assume_abstract_list: string -> thm list -> thm list
     1.8 +  val combinators: thm -> thm
     1.9    val neg_conjecture_clauses: thm -> int -> thm list * (string * typ) list
    1.10    val claset_rules_of: Proof.context -> (string * thm) list   (*FIXME DELETE*)
    1.11    val simpset_rules_of: Proof.context -> (string * thm) list  (*FIXME DELETE*)
    1.12 @@ -31,26 +31,6 @@
    1.13  (* FIXME legacy *)
    1.14  fun freeze_thm th = #1 (Drule.freeze_thaw th);
    1.15  
    1.16 -val lhs_of = #1 o Logic.dest_equals o Thm.prop_of;
    1.17 -val rhs_of = #2 o Logic.dest_equals o Thm.prop_of;
    1.18 -
    1.19 -
    1.20 -(*Store definitions of abstraction functions, ensuring that identical right-hand
    1.21 -  sides are denoted by the same functions and thereby reducing the need for
    1.22 -  extensionality in proofs.
    1.23 -  FIXME!  Store in theory data!!*)
    1.24 -
    1.25 -(*Populate the abstraction cache with common combinators.*)
    1.26 -fun seed th net =
    1.27 -  let val (_,ct) = Thm.dest_abs NONE (Thm.rhs_of th)
    1.28 -      val t = Logic.legacy_varify (term_of ct)
    1.29 -  in  Net.insert_term Thm.eq_thm (t, th) net end;
    1.30 -
    1.31 -val abstraction_cache = ref
    1.32 -      (seed (thm"ATP_Linkup.I_simp")
    1.33 -       (seed (thm"ATP_Linkup.B_simp")
    1.34 -        (seed (thm"ATP_Linkup.K_simp") Net.empty)));
    1.35 -
    1.36  
    1.37  (**** Transformation of Elimination Rules into First-Order Formulas****)
    1.38  
    1.39 @@ -149,7 +129,7 @@
    1.40    in  dec_sko (prop_of th) []  end;
    1.41  
    1.42  
    1.43 -(**** REPLACING ABSTRACTIONS BY FUNCTION DEFINITIONS ****)
    1.44 +(**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
    1.45  
    1.46  (*Returns the vars of a theorem*)
    1.47  fun vars_of_thm th =
    1.48 @@ -175,201 +155,98 @@
    1.49                strip_lambdas (n-1) (freeze_thm (th RS xfun_cong x))
    1.50          | _ => th;
    1.51  
    1.52 -(*Convert meta- to object-equality. Fails for theorems like split_comp_eq,
    1.53 -  where some types have the empty sort.*)
    1.54 -val meta_eq_to_obj_eq = thm "meta_eq_to_obj_eq";
    1.55 -fun mk_object_eq th = th RS meta_eq_to_obj_eq
    1.56 -    handle THM _ => error ("Theorem contains empty sort: " ^ string_of_thm th);
    1.57 -
    1.58 -(*Apply a function definition to an argument, beta-reducing the result.*)
    1.59 -fun beta_comb cf x =
    1.60 -  let val th1 = combination cf (reflexive x)
    1.61 -      val th2 = beta_conversion false (Thm.rhs_of th1)
    1.62 -  in  transitive th1 th2  end;
    1.63 -
    1.64 -(*Apply a function definition to arguments, beta-reducing along the way.*)
    1.65 -fun list_combination cf [] = cf
    1.66 -  | list_combination cf (x::xs) = list_combination (beta_comb cf x) xs;
    1.67 -
    1.68 -fun list_cabs ([] ,     t) = t
    1.69 -  | list_cabs (v::vars, t) = Thm.cabs v (list_cabs(vars,t));
    1.70 -
    1.71  fun assert_eta_free ct =
    1.72    let val t = term_of ct
    1.73    in if (t aconv Envir.eta_contract t) then ()
    1.74       else error ("Eta redex in term: " ^ string_of_cterm ct)
    1.75    end;
    1.76  
    1.77 -fun eq_absdef (th1, th2) =
    1.78 -    Context.joinable (theory_of_thm th1, theory_of_thm th2)  andalso
    1.79 -    rhs_of th1 aconv rhs_of th2;
    1.80 -
    1.81  val lambda_free = not o Term.has_abs;
    1.82  
    1.83  val monomorphic = not o Term.exists_type (Term.exists_subtype Term.is_TVar);
    1.84  
    1.85 -fun dest_abs_list ct =
    1.86 -  let val (cv,ct') = Thm.dest_abs NONE ct
    1.87 -      val (cvs,cu) = dest_abs_list ct'
    1.88 -  in (cv::cvs, cu) end
    1.89 -  handle CTERM _ => ([],ct);
    1.90 +val abs_S = @{thm"abs_S"};
    1.91 +val abs_K = @{thm"abs_K"};
    1.92 +val abs_I = @{thm"abs_I"};
    1.93 +val abs_B = @{thm"abs_B"};
    1.94 +val abs_C = @{thm"abs_C"};
    1.95  
    1.96 -fun abstract_rule_list [] [] th = th
    1.97 -  | abstract_rule_list (v::vs) (ct::cts) th = abstract_rule v ct (abstract_rule_list vs cts th)
    1.98 -  | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]);
    1.99 -
   1.100 -
   1.101 -val Envir.Envir {asol = tenv0, iTs = tyenv0, ...} = Envir.empty 0
   1.102 +val [f_B,g_B] = map (cterm_of @{theory}) (term_vars (prop_of abs_B));
   1.103 +val [g_C,f_C] = map (cterm_of @{theory}) (term_vars (prop_of abs_C));
   1.104 +val [f_S,g_S] = map (cterm_of @{theory}) (term_vars (prop_of abs_S));
   1.105  
   1.106 -(*Does an existing abstraction definition have an RHS that matches the one we need now?
   1.107 -  thy is the current theory, which must extend that of theorem th.*)
   1.108 -fun match_rhs thy t th =
   1.109 -  let val _ = Output.debug (fn()=> "match_rhs: " ^ string_of_cterm (cterm_of thy t) ^
   1.110 -                                   " against\n" ^ string_of_thm th);
   1.111 -      val (tyenv,tenv) = Pattern.first_order_match thy (rhs_of th, t) (tyenv0,tenv0)
   1.112 -      val term_insts = map Meson.term_pair_of (Vartab.dest tenv)
   1.113 -      val ct_pairs = if subthy (theory_of_thm th, thy) andalso
   1.114 -                        forall lambda_free (map #2 term_insts)
   1.115 -                     then map (pairself (cterm_of thy)) term_insts
   1.116 -                     else raise Pattern.MATCH (*Cannot allow lambdas in the instantiation*)
   1.117 -      fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
   1.118 -      val th' = cterm_instantiate ct_pairs th
   1.119 -  in  SOME (th, instantiate (map ctyp2 (Vartab.dest tyenv), []) th')  end
   1.120 -  handle _ => NONE;
   1.121 +(*FIXME: requires more use of cterm constructors*)
   1.122 +fun abstract ct =
   1.123 +  let val Abs(x,_,body) = term_of ct
   1.124 +      val thy = theory_of_cterm ct
   1.125 +      val Type("fun",[xT,bodyT]) = typ_of (ctyp_of_term ct)
   1.126 +      val cxT = ctyp_of thy xT and cbodyT = ctyp_of thy bodyT
   1.127 +      fun makeK() = instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)] abs_K
   1.128 +  in
   1.129 +      case body of
   1.130 +          Const _ => makeK()
   1.131 +        | Free _ => makeK()
   1.132 +        | Var _ => makeK()  (*though Var isn't expected*)
   1.133 +        | Bound 0 => instantiate' [SOME cxT] [] abs_I (*identity: I*)
   1.134 +        | rator$rand =>
   1.135 +	    if loose_bvar1 (rator,0) then (*C or S*) 
   1.136 +	       if loose_bvar1 (rand,0) then (*S*)
   1.137 +	         let val crator = cterm_of thy (Abs(x,xT,rator))
   1.138 +	             val crand = cterm_of thy (Abs(x,xT,rand))
   1.139 +	             val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] abs_S
   1.140 +	             val (_,rhs) = Thm.dest_equals (cprop_of abs_S') 
   1.141 +	         in
   1.142 +	           Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
   1.143 +	         end
   1.144 +	       else (*C*)
   1.145 +	         let val crator = cterm_of thy (Abs(x,xT,rator))
   1.146 +	             val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] abs_C
   1.147 +	             val (_,rhs) = Thm.dest_equals (cprop_of abs_C') 
   1.148 +	         in
   1.149 +	           Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
   1.150 +	         end
   1.151 +	    else if loose_bvar1 (rand,0) then (*B or eta*) 
   1.152 +	       if rand = Bound 0 then eta_conversion ct
   1.153 +	       else (*B*)
   1.154 +	         let val crand = cterm_of thy (Abs(x,xT,rand))
   1.155 +	             val abs_B' = cterm_instantiate [(f_B, cterm_of thy rator),(g_B,crand)] abs_B
   1.156 +	             val (_,rhs) = Thm.dest_equals (cprop_of abs_B') 
   1.157 +	         in
   1.158 +	           Thm.transitive abs_B' (Conv.arg_conv abstract rhs)
   1.159 +	         end
   1.160 +	    else makeK()
   1.161 +        | _ => error "abstract: Bad term"
   1.162 +  end;
   1.163  
   1.164  (*Traverse a theorem, declaring abstraction function definitions. String s is the suggested
   1.165    prefix for the constants. Resulting theory is returned in the first theorem. *)
   1.166 -fun declare_absfuns s th =
   1.167 -  let val nref = ref 0
   1.168 -      fun abstract thy ct =
   1.169 -        if lambda_free (term_of ct) then (transfer thy (reflexive ct), [])
   1.170 -        else
   1.171 -        case term_of ct of
   1.172 -          Abs _ =>
   1.173 -            let val cname = "llabs_" ^ s ^ "_" ^ Int.toString (inc nref)
   1.174 -                val _ = assert_eta_free ct;
   1.175 -                val (cvs,cta) = dest_abs_list ct
   1.176 -                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
   1.177 -                val _ = Output.debug (fn()=>"Nested lambda: " ^ string_of_cterm cta);
   1.178 -                val (u'_th,defs) = abstract thy cta
   1.179 -                val _ = Output.debug (fn()=>"Returned " ^ string_of_thm u'_th);
   1.180 -                val cu' = Thm.rhs_of u'_th
   1.181 -                val u' = term_of cu'
   1.182 -                val abs_v_u = fold_rev (lambda o term_of) cvs u'
   1.183 -                (*get the formal parameters: ALL variables free in the term*)
   1.184 -                val args = term_frees abs_v_u
   1.185 -                val _ = Output.debug (fn()=>Int.toString (length args) ^ " arguments");
   1.186 -                val rhs = list_abs_free (map dest_Free args, abs_v_u)
   1.187 -                      (*Forms a lambda-abstraction over the formal parameters*)
   1.188 -                val _ = Output.debug (fn()=>"Looking up " ^ string_of_cterm cu');
   1.189 -                val thy = theory_of_thm u'_th
   1.190 -                val (ax,ax',thy) =
   1.191 -                 case List.mapPartial (match_rhs thy abs_v_u)
   1.192 -                         (Net.match_term (!abstraction_cache) u') of
   1.193 -                     (ax,ax')::_ =>
   1.194 -                       (Output.debug (fn()=>"Re-using axiom " ^ string_of_thm ax);
   1.195 -                        (ax,ax',thy))
   1.196 -                   | [] =>
   1.197 -                      let val _ = Output.debug (fn()=>"Lookup was empty");
   1.198 -                          val Ts = map type_of args
   1.199 -                          val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   1.200 -                          val c = Const (Sign.full_name thy cname, cT)
   1.201 -                          val thy =
   1.202 -                            Sign.add_consts_authentic [Markup.property_internal] [(cname, cT, NoSyn)] thy
   1.203 -                                     (*Theory is augmented with the constant,
   1.204 -                                       then its definition*)
   1.205 -                          val cdef = cname ^ "_def"
   1.206 -                          val thy = Theory.add_defs_i true false
   1.207 -                                       [(cdef, equals cT $ c $ rhs)] thy
   1.208 -                                    handle ERROR _ => raise Clausify_failure thy
   1.209 -                          val ax = Thm.get_axiom_i thy (Sign.full_name thy cdef)
   1.210 -                                     |> Drule.unvarify
   1.211 -                                     |> mk_object_eq |> strip_lambdas (length args)
   1.212 -                                     |> mk_meta_eq |> Meson.generalize
   1.213 -                          val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
   1.214 -                          val _ = Output.debug (fn()=> "Declaring: " ^ string_of_thm ax ^ "\n" ^
   1.215 -                                                       "Instance: " ^ string_of_thm ax');
   1.216 -                          val _ = abstraction_cache := Net.insert_term eq_absdef
   1.217 -                                            ((Logic.varify u'), ax) (!abstraction_cache)
   1.218 -                            handle Net.INSERT =>
   1.219 -                              raise THM ("declare_absfuns: INSERT", 0, [th,u'_th,ax])
   1.220 -                       in  (ax,ax',thy)  end
   1.221 -            in Output.debug (fn()=>"Lookup result: " ^ string_of_thm ax');
   1.222 -               (transitive (abstract_rule_list vs cvs u'_th) (symmetric ax'), ax::defs) end
   1.223 -        | (t1$t2) =>
   1.224 -            let val (ct1,ct2) = Thm.dest_comb ct
   1.225 -                val (th1,defs1) = abstract thy ct1
   1.226 -                val (th2,defs2) = abstract (theory_of_thm th1) ct2
   1.227 -            in  (combination th1 th2, defs1@defs2)  end
   1.228 -      val _ = Output.debug (fn()=>"declare_absfuns, Abstracting: " ^ string_of_thm th);
   1.229 -      val (eqth,defs) = abstract (theory_of_thm th) (cprop_of th)
   1.230 -      val ths = equal_elim eqth th :: map (strip_lambdas ~1 o mk_object_eq o freeze_thm) defs
   1.231 -      val _ = Output.debug (fn()=>"declare_absfuns, Result: " ^ string_of_thm (hd ths));
   1.232 -  in  (theory_of_thm eqth, map Drule.eta_contraction_rule ths)  end;
   1.233 -
   1.234 -fun name_of def = try (#1 o dest_Free o lhs_of) def;
   1.235 -
   1.236 -(*A name is valid provided it isn't the name of a defined abstraction.*)
   1.237 -fun valid_name defs (Free(x,T)) = not (x mem_string (List.mapPartial name_of defs))
   1.238 -  | valid_name defs _ = false;
   1.239 -
   1.240 -(*s is the theorem name (hint) or the word "subgoal"*)
   1.241 -fun assume_absfuns s th =
   1.242 -  let val thy = theory_of_thm th
   1.243 -      val cterm = cterm_of thy
   1.244 -      val abs_count = ref 0
   1.245 -      fun abstract ct =
   1.246 -        if lambda_free (term_of ct) then (reflexive ct, [])
   1.247 -        else
   1.248 -        case term_of ct of
   1.249 -          Abs (_,T,u) =>
   1.250 -            let val _ = assert_eta_free ct;
   1.251 -                val (cvs,cta) = dest_abs_list ct
   1.252 -                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
   1.253 -                val (u'_th,defs) = abstract cta
   1.254 -                val cu' = Thm.rhs_of u'_th
   1.255 -                val u' = term_of cu'
   1.256 -                (*Could use Thm.cabs instead of lambda to work at level of cterms*)
   1.257 -                val abs_v_u = fold_rev (lambda o term_of) cvs (term_of cu')
   1.258 -                (*get the formal parameters: free variables not present in the defs
   1.259 -                  (to avoid taking abstraction function names as parameters) *)
   1.260 -                val args = filter (valid_name defs) (term_frees abs_v_u)
   1.261 -                val crhs = list_cabs (map cterm args, cterm abs_v_u)
   1.262 -                      (*Forms a lambda-abstraction over the formal parameters*)
   1.263 -                val rhs = term_of crhs
   1.264 -                val (ax,ax') =
   1.265 -                 case List.mapPartial (match_rhs thy abs_v_u)
   1.266 -                        (Net.match_term (!abstraction_cache) u') of
   1.267 -                     (ax,ax')::_ =>
   1.268 -                       (Output.debug (fn()=>"Re-using axiom " ^ string_of_thm ax);
   1.269 -                        (ax,ax'))
   1.270 -                   | [] =>
   1.271 -                      let val Ts = map type_of args
   1.272 -                          val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
   1.273 -                          val id = "llabs_" ^ s ^ "_" ^ Int.toString (inc abs_count)
   1.274 -                          val c = Free (id, const_ty)
   1.275 -                          val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs)
   1.276 -                                     |> mk_object_eq |> strip_lambdas (length args)
   1.277 -                                     |> mk_meta_eq |> Meson.generalize
   1.278 -                          val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
   1.279 -                          val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax)
   1.280 -                                    (!abstraction_cache)
   1.281 -                            handle Net.INSERT =>
   1.282 -                              raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax])
   1.283 -                      in (ax,ax') end
   1.284 -            in Output.debug (fn()=>"Lookup result: " ^ string_of_thm ax');
   1.285 -               (transitive (abstract_rule_list vs cvs u'_th) (symmetric ax'), ax::defs) end
   1.286 -        | (t1$t2) =>
   1.287 -            let val (ct1,ct2) = Thm.dest_comb ct
   1.288 -                val (t1',defs1) = abstract ct1
   1.289 -                val (t2',defs2) = abstract ct2
   1.290 -            in  (combination t1' t2', defs1@defs2)  end
   1.291 -      val _ = Output.debug (fn()=>"assume_absfuns, Abstracting: " ^ string_of_thm th);
   1.292 -      val (eqth,defs) = abstract (cprop_of th)
   1.293 -      val ths = equal_elim eqth th :: map (strip_lambdas ~1 o mk_object_eq o freeze_thm) defs
   1.294 -      val _ = Output.debug (fn()=>"assume_absfuns, Result: " ^ string_of_thm (hd ths));
   1.295 -  in  map Drule.eta_contraction_rule ths  end;
   1.296 -
   1.297 +fun combinators_aux ct =
   1.298 +  if lambda_free (term_of ct) then reflexive ct
   1.299 +  else
   1.300 +  case term_of ct of
   1.301 +      Abs _ =>
   1.302 +	let val _ = assert_eta_free ct;
   1.303 +	    val (cv,cta) = Thm.dest_abs NONE ct
   1.304 +	    val (v,Tv) = (dest_Free o term_of) cv
   1.305 +	    val _ = Output.debug (fn()=>"  recursion: " ^ string_of_cterm cta);
   1.306 +	    val u_th = combinators_aux cta
   1.307 +	    val _ = Output.debug (fn()=>"  returned " ^ string_of_thm u_th);
   1.308 +	    val cu = Thm.rhs_of u_th
   1.309 +	    val comb_eq = abstract (Thm.cabs cv cu)
   1.310 +	in Output.debug (fn()=>"  abstraction result: " ^ string_of_thm comb_eq);
   1.311 +	   (transitive (abstract_rule v cv u_th) comb_eq) end
   1.312 +    | t1 $ t2 =>
   1.313 +	let val (ct1,ct2) = Thm.dest_comb ct
   1.314 +	in  combination (combinators_aux ct1) (combinators_aux ct2)  end;
   1.315 +            
   1.316 +fun combinators th =
   1.317 +  if lambda_free (prop_of th) then th 
   1.318 +  else
   1.319 +    let val _ = Output.debug (fn()=>"Conversion to combinators: " ^ string_of_thm th);
   1.320 +	val th = Drule.eta_contraction_rule th
   1.321 +	val eqth = combinators_aux (cprop_of th)
   1.322 +	val _ = Output.debug (fn()=>"Conversion result: " ^ string_of_thm eqth);
   1.323 +    in  equal_elim eqth th   end;
   1.324  
   1.325  (*cterms are used throughout for efficiency*)
   1.326  val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
   1.327 @@ -430,27 +307,6 @@
   1.328        [] => ()
   1.329      | ths' => error (msg ^ "\n" ^ cat_lines (map string_of_thm ths'));
   1.330  
   1.331 -fun assume_abstract s th =
   1.332 -  if lambda_free (prop_of th) then [th]
   1.333 -  else th |> Drule.eta_contraction_rule |> assume_absfuns s
   1.334 -          |> tap (fn ths => assert_lambda_free ths "assume_abstract: lambdas")
   1.335 -
   1.336 -(*Replace lambdas by assumed function definitions in the theorems*)
   1.337 -fun assume_abstract_list s ths = List.concat (map (assume_abstract s) ths);
   1.338 -
   1.339 -(*Replace lambdas by declared function definitions in the theorems*)
   1.340 -fun declare_abstract s (thy, []) = (thy, [])
   1.341 -  | declare_abstract s (thy, th::ths) =
   1.342 -      let val (thy', th_defs) =
   1.343 -            if lambda_free (prop_of th) then (thy, [th])
   1.344 -            else
   1.345 -                th |> zero_var_indexes |> freeze_thm
   1.346 -                   |> Drule.eta_contraction_rule |> transfer thy |> declare_absfuns s
   1.347 -                handle Clausify_failure thy_e => (thy_e,[])
   1.348 -          val _ = assert_lambda_free th_defs "declare_abstract: lambdas"
   1.349 -          val (thy'', ths') = declare_abstract (s^"'") (thy', ths)
   1.350 -      in  (thy'', th_defs @ ths')  end;
   1.351 -
   1.352  (*Keep the full complexity of the original name*)
   1.353  fun flatten_name s = space_implode "_X" (NameSpace.explode s);
   1.354  
   1.355 @@ -465,7 +321,7 @@
   1.356  (*Skolemize a named theorem, with Skolem functions as additional premises.*)
   1.357  fun skolem_thm th =
   1.358    let val nnfth = to_nnf th and s = fake_name th
   1.359 -  in  Meson.make_cnf (skolem_of_nnf s nnfth) nnfth |> assume_abstract_list s |> Meson.finish_cnf
   1.360 +  in  Meson.make_cnf (skolem_of_nnf s nnfth) nnfth |>  map combinators |> Meson.finish_cnf
   1.361    end
   1.362    handle THM _ => [];
   1.363  
   1.364 @@ -480,8 +336,8 @@
   1.365                val (thy',defs) = declare_skofuns s nnfth thy
   1.366                val cnfs = Meson.make_cnf (map skolem_of_def defs) nnfth
   1.367                val _ = Output.debug (fn () => Int.toString (length cnfs) ^ " clauses yielded")
   1.368 -              val (thy'',cnfs') = declare_abstract s (thy',cnfs)
   1.369 -          in (map Goal.close_result (Meson.finish_cnf cnfs'), thy'') end
   1.370 +              val cnfs' = map combinators cnfs
   1.371 +          in (map Goal.close_result (Meson.finish_cnf cnfs'), thy') end
   1.372            handle Clausify_failure thy_e => ([],thy_e)
   1.373          )
   1.374        (try to_nnf th);
   1.375 @@ -575,8 +431,26 @@
   1.376  
   1.377  val mark_skolemized = Sign.add_consts_i [("ResAxioms_endtheory", HOLogic.boolT, NoSyn)];
   1.378  
   1.379 +val max_lambda_nesting = 3;
   1.380 +     
   1.381 +fun excessive_lambdas (f$t, k) = excessive_lambdas (f,k) orelse excessive_lambdas (t,k)
   1.382 +  | excessive_lambdas (Abs(_,_,t), k) = k=0 orelse excessive_lambdas (t,k-1)
   1.383 +  | excessive_lambdas _ = false;
   1.384 +
   1.385 +fun is_formula_type T = (T = HOLogic.boolT orelse T = propT);
   1.386 +
   1.387 +(*Don't count nested lambdas at the level of formulas, as they are quantifiers*)
   1.388 +fun excessive_lambdas_fm Ts (Abs(_,T,t)) = excessive_lambdas_fm (T::Ts) t
   1.389 +  | excessive_lambdas_fm Ts t =
   1.390 +      if is_formula_type (fastype_of1 (Ts, t))
   1.391 +      then exists (excessive_lambdas_fm Ts) (#2 (strip_comb t))
   1.392 +      else excessive_lambdas (t, max_lambda_nesting);
   1.393 +
   1.394 +fun too_complex t = 
   1.395 +  Meson.too_many_clauses t orelse excessive_lambdas_fm [] t;
   1.396 +  
   1.397  fun skolem_cache th thy =
   1.398 -  if PureThy.is_internal th then thy
   1.399 +  if PureThy.is_internal th orelse too_complex (prop_of th) then thy
   1.400    else #2 (skolem_cache_thm th thy);
   1.401  
   1.402  val skolem_cache_theorems_of = Symtab.fold (fold skolem_cache o snd) o #2 o PureThy.theorems_of;
   1.403 @@ -598,7 +472,7 @@
   1.404  fun cnf_rules_of_ths ths = List.concat (map cnf_axiom ths);
   1.405  
   1.406  (*Expand all new*definitions of abstraction or Skolem functions in a proof state.*)
   1.407 -fun is_absko (Const ("==", _) $ Free (a,_) $ u) = String.isPrefix "llabs_" a orelse String.isPrefix "sko_" a
   1.408 +fun is_absko (Const ("==", _) $ Free (a,_) $ u) = String.isPrefix "sko_" a
   1.409    | is_absko _ = false;
   1.410  
   1.411  fun is_okdef xs (Const ("==", _) $ t $ u) =   (*Definition of Free, not in certain terms*)
   1.412 @@ -607,7 +481,7 @@
   1.413  
   1.414  (*This function tries to cope with open locales, which introduce hypotheses of the form
   1.415    Free == t, conjecture clauses, which introduce various hypotheses, and also definitions
   1.416 -  of llabs_ and sko_ functions. *)
   1.417 +  of sko_ functions. *)
   1.418  fun expand_defs_tac st0 st =
   1.419    let val hyps0 = #hyps (rep_thm st0)
   1.420        val hyps = #hyps (crep_thm st)
   1.421 @@ -652,7 +526,7 @@
   1.422  val no_tvars = null o term_tvars o prop_of;
   1.423  
   1.424  val neg_clausify =
   1.425 -  filter no_tvars o Meson.finish_cnf o assume_abstract_list "subgoal" o Meson.make_clauses;
   1.426 +  filter no_tvars o Meson.finish_cnf o map combinators o Meson.make_clauses;
   1.427  
   1.428  fun neg_conjecture_clauses st0 n =
   1.429    let val st = Seq.hd (neg_skolemize_tac n st0)