src/Pure/Tools/find_theorems.ML
changeset 32798 4b85b59a9f66
parent 32738 15bb09ca0378
child 32859 204f749f35a9
     1.1 --- a/src/Pure/Tools/find_theorems.ML	Wed Sep 30 23:49:53 2009 +0200
     1.2 +++ b/src/Pure/Tools/find_theorems.ML	Thu Oct 01 00:32:00 2009 +0200
     1.3 @@ -76,18 +76,9 @@
     1.4  
     1.5  fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
     1.6  
     1.7 -(* Note: ("op =" : "bool --> bool --> bool") does not exist in Pure. *)
     1.8 -fun is_Iff c =
     1.9 -  (case dest_Const c of
    1.10 -     ("op =", ty) =>
    1.11 -       (ty
    1.12 -        |> strip_type
    1.13 -        |> swap
    1.14 -        |> (op ::)
    1.15 -        |> map (fst o dest_Type)
    1.16 -        |> forall (curry (op =) "bool")
    1.17 -        handle TYPE _ => false)
    1.18 -   | _ => false);
    1.19 +(*educated guesses on HOL*)  (* FIXME broken *)
    1.20 +val boolT = Type ("bool", []);
    1.21 +val iff_const = Const ("op =", boolT --> boolT --> boolT);
    1.22  
    1.23  (*extract terms from term_src, refine them to the parts that concern us,
    1.24    if po try match them against obj else vice versa.
    1.25 @@ -97,19 +88,20 @@
    1.26    let
    1.27      val thy = ProofContext.theory_of ctxt;
    1.28  
    1.29 -    val chkmatch = obj |> (if po then rpair else pair) #> Pattern.matches thy;
    1.30 +    fun check_match pat = Pattern.matches thy (if po then (pat, obj) else (obj, pat));
    1.31      fun matches pat =
    1.32        let
    1.33          val jpat = ObjectLogic.drop_judgment thy pat;
    1.34          val c = Term.head_of jpat;
    1.35          val pats =
    1.36            if Term.is_Const c
    1.37 -          then if doiff andalso is_Iff c
    1.38 -               then pat :: map (ObjectLogic.ensure_propT thy) ((snd o strip_comb) jpat)
    1.39 -                    |> filter (is_nontrivial thy)
    1.40 -               else [pat]
    1.41 +          then
    1.42 +            if doiff andalso c = iff_const then
    1.43 +              (pat :: map (ObjectLogic.ensure_propT thy) (snd (strip_comb jpat)))
    1.44 +                |> filter (is_nontrivial thy)
    1.45 +            else [pat]
    1.46            else [];
    1.47 -      in filter chkmatch pats end;
    1.48 +      in filter check_match pats end;
    1.49  
    1.50      fun substsize pat =
    1.51        let val (_, subst) =
    1.52 @@ -117,12 +109,11 @@
    1.53        in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
    1.54  
    1.55      fun bestmatch [] = NONE
    1.56 -     |  bestmatch xs = SOME (foldr1 Int.min xs);
    1.57 +      | bestmatch xs = SOME (foldr1 Int.min xs);
    1.58  
    1.59      val match_thm = matches o refine_term;
    1.60    in
    1.61 -    map match_thm (extract_terms term_src)
    1.62 -    |> flat
    1.63 +    maps match_thm (extract_terms term_src)
    1.64      |> map substsize
    1.65      |> bestmatch
    1.66    end;
    1.67 @@ -178,8 +169,8 @@
    1.68          is_matching_thm false (single, I) ctxt true (goal_tree prem) rule_tree;
    1.69        val successful = prems |> map_filter try_subst;
    1.70      in
    1.71 -    (*elim rules always have assumptions, so an elim with one
    1.72 -      assumption is as good as an intro rule with none*)
    1.73 +      (*elim rules always have assumptions, so an elim with one
    1.74 +        assumption is as good as an intro rule with none*)
    1.75        if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
    1.76          andalso not (null successful)
    1.77        then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
    1.78 @@ -190,15 +181,13 @@
    1.79  
    1.80  fun filter_solves ctxt goal =
    1.81    let
    1.82 -    val baregoal = Logic.get_goal (Thm.prop_of goal) 1;
    1.83 -
    1.84      fun etacn thm i = Seq.take (! tac_limit) o etac thm i;
    1.85      fun try_thm thm =
    1.86        if Thm.no_prems thm then rtac thm 1 goal
    1.87        else (etacn thm THEN_ALL_NEW (Goal.norm_hhf_tac THEN' Method.assm_tac ctxt)) 1 goal;
    1.88    in
    1.89      fn (_, thm) =>
    1.90 -      if (is_some o Seq.pull o try_thm) thm
    1.91 +      if is_some (Seq.pull (try_thm thm))
    1.92        then SOME (Thm.nprems_of thm, 0) else NONE
    1.93    end;
    1.94  
    1.95 @@ -218,7 +207,7 @@
    1.96  
    1.97  (* filter_pattern *)
    1.98  
    1.99 -fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []);
   1.100 +fun get_names t = Term.add_const_names t (Term.add_free_names t []);
   1.101  fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
   1.102  
   1.103  (*Including all constants and frees is only sound because
   1.104 @@ -238,10 +227,9 @@
   1.105  
   1.106      fun check (t, NONE) = check (t, SOME (get_thm_names t))
   1.107        | check ((_, thm), c as SOME thm_consts) =
   1.108 -          (if pat_consts subset_string thm_consts
   1.109 -              andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt)
   1.110 -                                               (pat, Thm.full_prop_of thm))
   1.111 -           then SOME (0, 0) else NONE, c);
   1.112 +         (if pat_consts subset_string thm_consts andalso
   1.113 +            Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
   1.114 +          then SOME (0, 0) else NONE, c);
   1.115    in check end;
   1.116  
   1.117  
   1.118 @@ -253,7 +241,6 @@
   1.119    error ("Current goal required for " ^ c ^ " search criterion");
   1.120  
   1.121  val fix_goal = Thm.prop_of;
   1.122 -val fix_goalo = Option.map fix_goal;
   1.123  
   1.124  fun filter_crit _ _ (Name name) = apfst (filter_name name)
   1.125    | filter_crit _ NONE Intro = err_no_goal "intro"
   1.126 @@ -276,7 +263,7 @@
   1.127  fun app_filters thm =
   1.128    let
   1.129      fun app (NONE, _, _) = NONE
   1.130 -      | app (SOME v, consts, []) = SOME (v, thm)
   1.131 +      | app (SOME v, _, []) = SOME (v, thm)
   1.132        | app (r, consts, f :: fs) =
   1.133            let val (r', consts') = f (thm, consts)
   1.134            in app (opt_add r r', consts', fs) end;
   1.135 @@ -439,6 +426,7 @@
   1.136    end;
   1.137  
   1.138  
   1.139 +
   1.140  (** command syntax **)
   1.141  
   1.142  fun find_theorems_cmd ((opt_lim, rem_dups), spec) =