src/HOL/Tools/res_axioms.ML
changeset 26928 ca87aff1ad2d
parent 26653 60e0cf6bef89
child 26939 1035c89b4c02
equal deleted inserted replaced
26927:8684b5240f11 26928:ca87aff1ad2d
   174 val [g_C,f_C] = map (cterm_of @{theory}) (term_vars (prop_of abs_C));
   174 val [g_C,f_C] = map (cterm_of @{theory}) (term_vars (prop_of abs_C));
   175 val [f_S,g_S] = map (cterm_of @{theory}) (term_vars (prop_of abs_S));
   175 val [f_S,g_S] = map (cterm_of @{theory}) (term_vars (prop_of abs_S));
   176 
   176 
   177 (*FIXME: requires more use of cterm constructors*)
   177 (*FIXME: requires more use of cterm constructors*)
   178 fun abstract ct =
   178 fun abstract ct =
   179   let val _ = Output.debug (fn()=>"  abstraction: " ^ string_of_cterm ct)
   179   let val _ = Output.debug (fn()=>"  abstraction: " ^ Display.string_of_cterm ct)
   180       val Abs(x,_,body) = term_of ct
   180       val Abs(x,_,body) = term_of ct
   181       val thy = theory_of_cterm ct
   181       val thy = theory_of_cterm ct
   182       val Type("fun",[xT,bodyT]) = typ_of (ctyp_of_term ct)
   182       val Type("fun",[xT,bodyT]) = typ_of (ctyp_of_term ct)
   183       val cxT = ctyp_of thy xT and cbodyT = ctyp_of thy bodyT
   183       val cxT = ctyp_of thy xT and cbodyT = ctyp_of thy bodyT
   184       fun makeK() = instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)] abs_K
   184       fun makeK() = instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)] abs_K
   226   else
   226   else
   227   case term_of ct of
   227   case term_of ct of
   228       Abs _ =>
   228       Abs _ =>
   229 	let val (cv,cta) = Thm.dest_abs NONE ct
   229 	let val (cv,cta) = Thm.dest_abs NONE ct
   230 	    val (v,Tv) = (dest_Free o term_of) cv
   230 	    val (v,Tv) = (dest_Free o term_of) cv
   231 	    val _ = Output.debug (fn()=>"  recursion: " ^ string_of_cterm cta);
   231 	    val _ = Output.debug (fn()=>"  recursion: " ^ Display.string_of_cterm cta);
   232 	    val u_th = combinators_aux cta
   232 	    val u_th = combinators_aux cta
   233 	    val _ = Output.debug (fn()=>"  returned " ^ string_of_thm u_th);
   233 	    val _ = Output.debug (fn()=>"  returned " ^ Display.string_of_thm u_th);
   234 	    val cu = Thm.rhs_of u_th
   234 	    val cu = Thm.rhs_of u_th
   235 	    val comb_eq = abstract (Thm.cabs cv cu)
   235 	    val comb_eq = abstract (Thm.cabs cv cu)
   236 	in Output.debug (fn()=>"  abstraction result: " ^ string_of_thm comb_eq);
   236 	in Output.debug (fn()=>"  abstraction result: " ^ Display.string_of_thm comb_eq);
   237 	   (transitive (abstract_rule v cv u_th) comb_eq) end
   237 	   (transitive (abstract_rule v cv u_th) comb_eq) end
   238     | t1 $ t2 =>
   238     | t1 $ t2 =>
   239 	let val (ct1,ct2) = Thm.dest_comb ct
   239 	let val (ct1,ct2) = Thm.dest_comb ct
   240 	in  combination (combinators_aux ct1) (combinators_aux ct2)  end;
   240 	in  combination (combinators_aux ct1) (combinators_aux ct2)  end;
   241             
   241             
   242 fun combinators th =
   242 fun combinators th =
   243   if lambda_free (prop_of th) then th 
   243   if lambda_free (prop_of th) then th 
   244   else
   244   else
   245     let val _ = Output.debug (fn()=>"Conversion to combinators: " ^ string_of_thm th);
   245     let val _ = Output.debug (fn()=>"Conversion to combinators: " ^ Display.string_of_thm th);
   246 	val th = Drule.eta_contraction_rule th
   246 	val th = Drule.eta_contraction_rule th
   247 	val eqth = combinators_aux (cprop_of th)
   247 	val eqth = combinators_aux (cprop_of th)
   248 	val _ = Output.debug (fn()=>"Conversion result: " ^ string_of_thm eqth);
   248 	val _ = Output.debug (fn()=>"Conversion result: " ^ Display.string_of_thm eqth);
   249     in  equal_elim eqth th   end
   249     in  equal_elim eqth th   end
   250     handle THM (msg,_,_) => 
   250     handle THM (msg,_,_) => 
   251       (warning ("Error in the combinator translation of " ^ string_of_thm th);
   251       (warning ("Error in the combinator translation of " ^ Display.string_of_thm th);
   252        warning ("  Exception message: " ^ msg);
   252        warning ("  Exception message: " ^ msg);
   253        TrueI);  (*A type variable of sort {} will cause make abstraction fail.*)
   253        TrueI);  (*A type variable of sort {} will cause make abstraction fail.*)
   254 
   254 
   255 (*cterms are used throughout for efficiency*)
   255 (*cterms are used throughout for efficiency*)
   256 val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
   256 val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
   309   map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns s th);
   309   map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns s th);
   310 
   310 
   311 fun assert_lambda_free ths msg =
   311 fun assert_lambda_free ths msg =
   312   case filter (not o lambda_free o prop_of) ths of
   312   case filter (not o lambda_free o prop_of) ths of
   313       [] => ()
   313       [] => ()
   314     | ths' => error (msg ^ "\n" ^ cat_lines (map string_of_thm ths'));
   314     | ths' => error (msg ^ "\n" ^ cat_lines (map Display.string_of_thm ths'));
   315 
   315 
   316 
   316 
   317 (*** Blacklisting (duplicated in ResAtp? ***)
   317 (*** Blacklisting (duplicated in ResAtp? ***)
   318 
   318 
   319 val max_lambda_nesting = 3;
   319 val max_lambda_nesting = 3;
   365   if PureThy.has_name_hint th then flatten_name (PureThy.get_name_hint th)
   365   if PureThy.has_name_hint th then flatten_name (PureThy.get_name_hint th)
   366   else gensym "unknown_thm_";
   366   else gensym "unknown_thm_";
   367 
   367 
   368 fun name_or_string th =
   368 fun name_or_string th =
   369   if PureThy.has_name_hint th then PureThy.get_name_hint th
   369   if PureThy.has_name_hint th then PureThy.get_name_hint th
   370   else string_of_thm th;
   370   else Display.string_of_thm th;
   371 
   371 
   372 (*Declare Skolem functions for a theorem, supplied in nnf and with its name.
   372 (*Declare Skolem functions for a theorem, supplied in nnf and with its name.
   373   It returns a modified theory, unless skolemization fails.*)
   373   It returns a modified theory, unless skolemization fails.*)
   374 fun skolem thy th =
   374 fun skolem thy th =
   375   let val ctxt0 = Variable.thm_context th
   375   let val ctxt0 = Variable.thm_context th
   376       val _ = Output.debug (fn () => "skolemizing " ^ name_or_string th)
   376       val _ = Output.debug (fn () => "skolemizing " ^ name_or_string th)
   377   in
   377   in
   378      Option.map
   378      Option.map
   379         (fn (nnfth,ctxt1) =>
   379         (fn (nnfth,ctxt1) =>
   380           let 
   380           let 
   381               val _ = Output.debug (fn () => "  initial nnf: " ^ string_of_thm nnfth)
   381               val _ = Output.debug (fn () => "  initial nnf: " ^ Display.string_of_thm nnfth)
   382               val s = fake_name th
   382               val s = fake_name th
   383               val (thy',defs) = declare_skofuns s nnfth thy
   383               val (thy',defs) = declare_skofuns s nnfth thy
   384               val (cnfs,ctxt2) = Meson.make_cnf (map skolem_of_def defs) nnfth ctxt1
   384               val (cnfs,ctxt2) = Meson.make_cnf (map skolem_of_def defs) nnfth ctxt1
   385               val _ = Output.debug (fn () => Int.toString (length cnfs) ^ " clauses yielded")
   385               val _ = Output.debug (fn () => Int.toString (length cnfs) ^ " clauses yielded")
   386               val cnfs' = cnfs |> map combinators |> Variable.export ctxt2 ctxt0 
   386               val cnfs' = cnfs |> map combinators |> Variable.export ctxt2 ctxt0 
   529       val defs = filter (is_absko o Thm.term_of) newhyps
   529       val defs = filter (is_absko o Thm.term_of) newhyps
   530       val remaining_hyps = filter_out (member (op aconv) (map Thm.term_of defs))
   530       val remaining_hyps = filter_out (member (op aconv) (map Thm.term_of defs))
   531                                       (map Thm.term_of hyps)
   531                                       (map Thm.term_of hyps)
   532       val fixed = term_frees (concl_of st) @
   532       val fixed = term_frees (concl_of st) @
   533                   foldl (gen_union (op aconv)) [] (map term_frees remaining_hyps)
   533                   foldl (gen_union (op aconv)) [] (map term_frees remaining_hyps)
   534   in  Output.debug (fn _ => "expand_defs_tac: " ^ string_of_thm st);
   534   in  Output.debug (fn _ => "expand_defs_tac: " ^ Display.string_of_thm st);
   535       Output.debug (fn _ => "  st0: " ^ string_of_thm st0);
   535       Output.debug (fn _ => "  st0: " ^ Display.string_of_thm st0);
   536       Output.debug (fn _ => "  defs: " ^ commas (map string_of_cterm defs));
   536       Output.debug (fn _ => "  defs: " ^ commas (map Display.string_of_cterm defs));
   537       Seq.of_list [LocalDefs.expand (filter (is_okdef fixed o Thm.term_of) defs) st]
   537       Seq.of_list [LocalDefs.expand (filter (is_okdef fixed o Thm.term_of) defs) st]
   538   end;
   538   end;
   539 
   539 
   540 
   540 
   541 fun meson_general_tac ths i st0 =
   541 fun meson_general_tac ths i st0 =
   542  let val _ = Output.debug (fn () => "Meson called: " ^ cat_lines (map string_of_thm ths))
   542  let val _ = Output.debug (fn () => "Meson called: " ^ cat_lines (map Display.string_of_thm ths))
   543  in  (Meson.meson_claset_tac (cnf_rules_of_ths ths) HOL_cs i THEN expand_defs_tac st0) st0 end;
   543  in  (Meson.meson_claset_tac (cnf_rules_of_ths ths) HOL_cs i THEN expand_defs_tac st0) st0 end;
   544 
   544 
   545 val meson_method_setup = Method.add_methods
   545 val meson_method_setup = Method.add_methods
   546   [("meson", Method.thms_args (fn ths =>
   546   [("meson", Method.thms_args (fn ths =>
   547       Method.SIMPLE_METHOD' (CHANGED_PROP o meson_general_tac ths)),
   547       Method.SIMPLE_METHOD' (CHANGED_PROP o meson_general_tac ths)),