src/Pure/Isar/find_theorems.ML
changeset 29857 2cc976ed8a3c
parent 29848 a7c164e228e1
child 29858 c8cee17d7e50
     1.1 --- a/src/Pure/Isar/find_theorems.ML	Tue Feb 10 17:53:51 2009 -0800
     1.2 +++ b/src/Pure/Isar/find_theorems.ML	Wed Feb 11 14:48:14 2009 +1100
     1.3 @@ -7,9 +7,16 @@
     1.4  signature FIND_THEOREMS =
     1.5  sig
     1.6    val limit: int ref
     1.7 +  val tac_limit: int ref
     1.8 +
     1.9    datatype 'term criterion =
    1.10 -    Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
    1.11 -  val print_theorems: Proof.context -> term option -> int option -> bool ->
    1.12 +    Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    1.13 +    Pattern of 'term
    1.14 +
    1.15 +  val find_theorems: Proof.context -> thm option -> bool ->
    1.16 +    (bool * string criterion) list -> (Facts.ref * thm) list
    1.17 +
    1.18 +  val print_theorems: Proof.context -> thm option -> int option -> bool ->
    1.19      (bool * string criterion) list -> unit
    1.20  end;
    1.21  
    1.22 @@ -19,12 +26,14 @@
    1.23  (** search criteria **)
    1.24  
    1.25  datatype 'term criterion =
    1.26 -  Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
    1.27 +  Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    1.28 +  Pattern of 'term;
    1.29  
    1.30  fun read_criterion _ (Name name) = Name name
    1.31    | read_criterion _ Intro = Intro
    1.32    | read_criterion _ Elim = Elim
    1.33    | read_criterion _ Dest = Dest
    1.34 +  | read_criterion _ Solves = Solves
    1.35    | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str)
    1.36    | read_criterion ctxt (Pattern str) = Pattern (ProofContext.read_term_pattern ctxt str);
    1.37  
    1.38 @@ -37,6 +46,7 @@
    1.39      | Intro => Pretty.str (prfx "intro")
    1.40      | Elim => Pretty.str (prfx "elim")
    1.41      | Dest => Pretty.str (prfx "dest")
    1.42 +    | Solves => Pretty.str (prfx "solves")
    1.43      | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
    1.44          Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))]
    1.45      | Pattern pat => Pretty.enclose (prfx " \"") "\""
    1.46 @@ -108,7 +118,7 @@
    1.47    then SOME (0, 0) else NONE;
    1.48  
    1.49  
    1.50 -(* filter intro/elim/dest rules *)
    1.51 +(* filter intro/elim/dest/solves rules *)
    1.52  
    1.53  fun filter_dest ctxt goal (_, thm) =
    1.54    let
    1.55 @@ -159,6 +169,20 @@
    1.56      end
    1.57    else NONE
    1.58  
    1.59 +val tac_limit = ref 5;
    1.60 +
    1.61 +fun filter_solves ctxt goal = let
    1.62 +    val baregoal = Logic.get_goal (prop_of goal) 1;
    1.63 +
    1.64 +    fun etacn thm i = Seq.take (!tac_limit) o etac thm i;
    1.65 +    fun try_thm thm = if Thm.no_prems thm then rtac thm 1 goal
    1.66 +                      else (etacn thm THEN_ALL_NEW
    1.67 +                             (Goal.norm_hhf_tac THEN'
    1.68 +                               Method.assumption_tac ctxt)) 1 goal;
    1.69 +  in
    1.70 +    fn (_, thm) => if (is_some o Seq.pull o try_thm) thm
    1.71 +                   then SOME (Thm.nprems_of thm, 0) else NONE
    1.72 +  end;
    1.73  
    1.74  (* filter_simp *)
    1.75  
    1.76 @@ -176,26 +200,23 @@
    1.77  
    1.78  (* filter_pattern *)
    1.79  
    1.80 -fun get_names (_, thm) =
    1.81 -  fold_aterms (fn Const (c, _) => insert (op =) c | Free (x, _) => insert (op =) x | _ => I)
    1.82 -    (Thm.full_prop_of thm) [];
    1.83 -
    1.84 -fun add_pat_names (t, cs) =
    1.85 -      case strip_comb t of
    1.86 -          (Const (c, _), args) => foldl add_pat_names (insert (op =) c cs) args
    1.87 -        | (Free (c, _), args) => foldl add_pat_names (insert (op =) c cs) args
    1.88 -        | (Abs (_, _, t), _) => add_pat_names (t, cs)
    1.89 -        | _ => cs;
    1.90 -    (* Only include constants and frees that cannot be thrown away.
    1.91 -       for example, from "(% x y z. y + 1) 7 8 9" give [1].
    1.92 -       The result [1, 8] would be more accurate, but only a
    1.93 -       sound approximation is required and variables must
    1.94 -       be ignored: e.g. "_ 7 8 9". *)
    1.95 +fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []);
    1.96 +fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
    1.97 +  (* Including all constants and frees is only sound because
    1.98 +     matching uses higher-order patterns. If full matching
    1.99 +     were used, then constants that may be subject to
   1.100 +     beta-reduction after substitution of frees should
   1.101 +     not be included for LHS set because they could be
   1.102 +     thrown away by the substituted function.
   1.103 +     e.g. for (?F 1 2) do not include 1 or 2, if it were
   1.104 +          possible for ?F to be (% x y. 3)
   1.105 +     The largest possible set should always be included on
   1.106 +     the RHS. *)
   1.107  
   1.108  fun filter_pattern ctxt pat = let
   1.109 -    val pat_consts = add_pat_names (pat, []);
   1.110 +    val pat_consts = get_names pat;
   1.111  
   1.112 -    fun check (t, NONE) = check (t, SOME (get_names t))
   1.113 +    fun check (t, NONE) = check (t, SOME (get_thm_names t))
   1.114        | check ((_, thm), c as SOME thm_consts) =
   1.115            (if pat_consts subset_string thm_consts
   1.116                andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt)
   1.117 @@ -210,13 +231,21 @@
   1.118  fun err_no_goal c =
   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    | filter_crit _ NONE Elim = err_no_goal "elim"
   1.127    | filter_crit _ NONE Dest = err_no_goal "dest"
   1.128 -  | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt goal)
   1.129 -  | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt goal)
   1.130 -  | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt goal)
   1.131 +  | filter_crit _ NONE Solves = err_no_goal "solves"
   1.132 +  | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt
   1.133 +                                                  (fix_goal goal))
   1.134 +  | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt 
   1.135 +                                                  (fix_goal goal))
   1.136 +  | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt
   1.137 +                                                  (fix_goal goal))
   1.138 +  | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
   1.139    | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
   1.140    | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
   1.141  
   1.142 @@ -314,11 +343,14 @@
   1.143  
   1.144  val limit = ref 40;
   1.145  
   1.146 -fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
   1.147 +fun find_theorems ctxt opt_goal rem_dups raw_criteria =
   1.148    let
   1.149 -    val start = start_timing ();
   1.150 +    val add_prems = Seq.hd o (TRY (Method.insert_tac
   1.151 +                                     (Assumption.prems_of ctxt) 1));
   1.152 +    val opt_goal' = Option.map add_prems opt_goal;
   1.153 +
   1.154      val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   1.155 -    val filters = map (filter_criterion ctxt opt_goal) criteria;
   1.156 +    val filters = map (filter_criterion ctxt opt_goal') criteria;
   1.157  
   1.158      val raw_matches = all_filters filters (all_facts_of ctxt);
   1.159  
   1.160 @@ -326,21 +358,28 @@
   1.161        if rem_dups
   1.162        then rem_thm_dups (nicer_shortest ctxt) raw_matches
   1.163        else raw_matches;
   1.164 +  in matches end;
   1.165 +
   1.166 +fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria = let
   1.167 +    val start = start_timing ();
   1.168 +
   1.169 +    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   1.170 +    val matches = find_theorems ctxt opt_goal rem_dups raw_criteria;
   1.171  
   1.172      val len = length matches;
   1.173      val lim = the_default (! limit) opt_limit;
   1.174      val thms = Library.drop (len - lim, matches);
   1.175  
   1.176 +    fun prt_fact (thmref, thm) = Pretty.block
   1.177 +      [Pretty.str (Facts.string_of_ref thmref), Pretty.str ":", Pretty.brk 1,
   1.178 +        ProofContext.pretty_thm ctxt thm];
   1.179 +
   1.180      val end_msg = " in " ^
   1.181                    (List.nth (String.tokens Char.isSpace (end_timing start), 3))
   1.182                    ^ " secs"
   1.183 -
   1.184 -    fun prt_fact (thmref, thm) = Pretty.block
   1.185 -      [Pretty.str (Facts.string_of_ref thmref), Pretty.str ":", Pretty.brk 1,
   1.186 -        ProofContext.pretty_thm ctxt thm];
   1.187    in
   1.188      Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria)
   1.189 -      :: Pretty.str "" ::
   1.190 +        :: Pretty.str "" ::
   1.191       (if null thms then [Pretty.str ("nothing found" ^ end_msg)]
   1.192        else
   1.193          [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
   1.194 @@ -349,6 +388,6 @@
   1.195             ^ end_msg ^ ":"), Pretty.str ""] @
   1.196          map prt_fact thms)
   1.197      |> Pretty.chunks |> Pretty.writeln
   1.198 -  end;
   1.199 +  end
   1.200  
   1.201  end;