src/Pure/Isar/find_theorems.ML
author kleing
Mon Aug 01 03:27:31 2005 +0200 (2005-08-01)
changeset 16964 6a25e42eaff5
parent 16895 df67fc190e06
child 17106 2bd6c20cdda1
permissions -rw-r--r--
Ordering is now: first by number of assumptions, second by the substitution size.

Treat elim/dest rules like erule/drule would:

* "elim" is now a subset of "dest" and matches on conclusion of goal and
major premise against any premise of goal. Computed substitution size
takes both into account.

* "dest" no longer has a restriction that limits its conclusion to a var.

(contributed by Raf)
wenzelm@16033
     1
(*  Title:      Pure/Isar/find_theorems.ML
wenzelm@16033
     2
    ID:         $Id$
wenzelm@16033
     3
    Author:     Rafal Kolanski, NICTA and Tobias Nipkow, TU Muenchen
wenzelm@16033
     4
wenzelm@16033
     5
Retrieve theorems from proof context.
wenzelm@16033
     6
*)
wenzelm@16033
     7
wenzelm@16033
     8
val thms_containing_limit = ref 40;
wenzelm@16033
     9
wenzelm@16033
    10
signature FIND_THEOREMS =
wenzelm@16033
    11
sig
wenzelm@16033
    12
  val find_thms: Proof.context -> FactIndex.spec -> (thmref * thm) list
wenzelm@16036
    13
  datatype 'term criterion =
kleing@16074
    14
    Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
wenzelm@16033
    15
  val print_theorems: Proof.context -> term option -> int option ->
wenzelm@16036
    16
    (bool * string criterion) list -> unit
wenzelm@16033
    17
end;
wenzelm@16033
    18
wenzelm@16033
    19
structure FindTheorems: FIND_THEOREMS =
wenzelm@16033
    20
struct
wenzelm@16033
    21
wenzelm@16033
    22
wenzelm@16033
    23
(* find_thms *)
wenzelm@16033
    24
wenzelm@16033
    25
fun find_thms ctxt spec =
wenzelm@16033
    26
  (PureThy.thms_containing (ProofContext.theory_of ctxt) spec @
wenzelm@16033
    27
    ProofContext.lthms_containing ctxt spec)
wenzelm@16033
    28
  |> map PureThy.selections |> List.concat;
wenzelm@16033
    29
wenzelm@16033
    30
wenzelm@16033
    31
wenzelm@16033
    32
(** search criteria **)
wenzelm@16033
    33
wenzelm@16036
    34
datatype 'term criterion =
kleing@16074
    35
  Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
wenzelm@16036
    36
wenzelm@16036
    37
fun read_criterion _ (Name name) = Name name
wenzelm@16036
    38
  | read_criterion _ Intro = Intro
wenzelm@16036
    39
  | read_criterion _ Elim = Elim
wenzelm@16036
    40
  | read_criterion _ Dest = Dest
wenzelm@16486
    41
  | read_criterion ctxt (Simp str) =
kleing@16088
    42
      Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]))
wenzelm@16036
    43
  | read_criterion ctxt (Pattern str) =
wenzelm@16036
    44
      Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
wenzelm@16033
    45
wenzelm@16036
    46
fun pretty_criterion ctxt (b, c) =
wenzelm@16036
    47
  let
wenzelm@16036
    48
    fun prfx s = if b then s else "-" ^ s;
wenzelm@16036
    49
  in
wenzelm@16036
    50
    (case c of
wenzelm@16036
    51
      Name name => Pretty.str (prfx "name: " ^ quote name)
wenzelm@16036
    52
    | Intro => Pretty.str (prfx "intro")
wenzelm@16036
    53
    | Elim => Pretty.str (prfx "elim")
wenzelm@16036
    54
    | Dest => Pretty.str (prfx "dest")
kleing@16088
    55
    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
kleing@16088
    56
        Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))]
wenzelm@16036
    57
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
wenzelm@16036
    58
        [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
wenzelm@16036
    59
  end;
wenzelm@16033
    60
wenzelm@16033
    61
wenzelm@16033
    62
wenzelm@16033
    63
(** search criterion filters **)
wenzelm@16033
    64
kleing@16895
    65
(*generated filters are to be of the form
kleing@16895
    66
  input: (PureThy.thmref * Thm.thm)
kleing@16895
    67
  output: (p::int, s::int) option, where
kleing@16895
    68
    NONE indicates no match
kleing@16895
    69
    p is the primary sorting criterion 
kleing@16895
    70
      (eg. number of assumptions in the theorem)
kleing@16895
    71
    s is the secondary sorting criterion
kleing@16895
    72
      (eg. size of the substitution for intro, elim and dest)
kleing@16895
    73
  when applying a set of filters to a thm, fold results in:
kleing@16895
    74
    (biggest p, sum of all s)
kleing@16895
    75
  currently p and s only matter for intro, elim, dest and simp filters, 
kleing@16895
    76
  otherwise the default ordering ("by package") is used.
kleing@16895
    77
*)
kleing@16895
    78
kleing@16088
    79
kleing@16088
    80
(* matching theorems *)
kleing@16964
    81
    
kleing@16964
    82
fun is_nontrivial sg = is_Const o head_of o ObjectLogic.drop_judgment sg;
kleing@16088
    83
kleing@16964
    84
(*extract terms from term_src, refine them to the parts that concern us,
kleing@16964
    85
  if po try match them against obj else vice versa.
kleing@16964
    86
  trivial matches are ignored.
kleing@16964
    87
  returns: smallest substitution size*)
kleing@16964
    88
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
kleing@16088
    89
  let
kleing@16088
    90
    val sg = ProofContext.sign_of ctxt;
kleing@16088
    91
    val tsig = Sign.tsig_of sg;
kleing@16088
    92
wenzelm@16486
    93
    fun matches pat =
kleing@16964
    94
      is_nontrivial sg pat andalso
kleing@16895
    95
      Pattern.matches tsig (if po then (pat,obj) else (obj,pat));
kleing@16895
    96
kleing@16895
    97
    fun substsize pat =
kleing@16895
    98
      let 
kleing@16895
    99
        val (_,subst) = Pattern.match tsig (if po then (pat,obj) else (obj,pat))
kleing@16895
   100
      in Vartab.foldl (op + o apsnd (size_of_term o snd o snd)) (0, subst)
kleing@16895
   101
      end;
kleing@16088
   102
kleing@16895
   103
    fun bestmatch [] = NONE
kleing@16964
   104
     |  bestmatch (x :: xs) = SOME (foldl Int.min x xs);
kleing@16895
   105
kleing@16964
   106
    val match_thm = matches o refine_term;
wenzelm@16486
   107
  in
kleing@16964
   108
    map (substsize o refine_term) 
kleing@16964
   109
        (List.filter match_thm (extract_terms term_src)) |> bestmatch
kleing@16088
   110
  end;
kleing@16088
   111
kleing@16088
   112
wenzelm@16033
   113
(* filter_name *)
wenzelm@16033
   114
wenzelm@16033
   115
fun is_substring pat str =
wenzelm@16033
   116
  if String.size pat = 0 then true
wenzelm@16033
   117
  else if String.size pat > String.size str then false
wenzelm@16033
   118
  else if String.substring (str, 0, String.size pat) = pat then true
wenzelm@16033
   119
  else is_substring pat (String.extract (str, 1, NONE));
wenzelm@16033
   120
wenzelm@16033
   121
(*filter that just looks for a string in the name,
wenzelm@16033
   122
  substring match only (no regexps are performed)*)
kleing@16895
   123
fun filter_name str_pat (thmref, _) = 
kleing@16895
   124
  if is_substring str_pat (PureThy.name_of_thmref thmref) 
kleing@16895
   125
  then SOME (0,0) else NONE;
wenzelm@16033
   126
wenzelm@16033
   127
wenzelm@16036
   128
(* filter intro/elim/dest rules *)
wenzelm@16033
   129
kleing@16964
   130
fun filter_dest ctxt goal (_,thm) =
wenzelm@16033
   131
  let
kleing@16964
   132
    val extract_dest =
kleing@16964
   133
     (fn thm => if Thm.no_prems thm then [] else [prop_of thm],
wenzelm@16033
   134
      hd o Logic.strip_imp_prems);
wenzelm@16033
   135
    val prems = Logic.prems_of_goal goal 1;
kleing@16895
   136
kleing@16964
   137
    fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
kleing@16895
   138
    
kleing@16895
   139
    (*keep successful substitutions*)
kleing@16895
   140
    val ss = prems |> List.map try_subst 
kleing@16895
   141
             |> List.filter isSome 
kleing@16964
   142
             |> List.map valOf;
wenzelm@16033
   143
  in
kleing@16895
   144
    (*if possible, keep best substitution (one with smallest size)*)
kleing@16964
   145
    (*dest rules always have assumptions, so a dest with one 
kleing@16895
   146
      assumption is as good as an intro rule with none*)
kleing@16964
   147
    if not (null ss) 
kleing@16895
   148
    then SOME (nprems_of thm - 1, foldl Int.min (hd ss) (tl ss)) else NONE
wenzelm@16033
   149
  end;
wenzelm@16033
   150
kleing@16088
   151
fun filter_intro ctxt goal (_,thm) =
wenzelm@16033
   152
  let
kleing@16964
   153
    val extract_intro = (single o prop_of, Logic.strip_imp_concl);
wenzelm@16036
   154
    val concl = Logic.concl_of_goal goal 1;
kleing@16964
   155
    val ss = is_matching_thm extract_intro ctxt true concl thm;
wenzelm@16033
   156
  in
kleing@16964
   157
    if is_some ss then SOME (nprems_of thm, valOf ss) else NONE
wenzelm@16033
   158
  end;
wenzelm@16033
   159
kleing@16964
   160
fun filter_elim ctxt goal (_,thm) =
kleing@16964
   161
  if not (Thm.no_prems thm) then
kleing@16964
   162
    let
kleing@16964
   163
      val rule = prop_of thm;
kleing@16964
   164
      val prems = Logic.prems_of_goal goal 1;
kleing@16964
   165
      val goal_concl = Logic.concl_of_goal goal 1;
kleing@16964
   166
      val rule_mp = (hd o Logic.strip_imp_prems) rule;
kleing@16964
   167
      val rule_concl = Logic.strip_imp_concl rule;
kleing@16964
   168
      fun combine t1 t2 = Const ("combine", dummyT --> dummyT) $ (t1 $ t2);
kleing@16964
   169
      val rule_tree = combine rule_mp rule_concl;
kleing@16964
   170
      fun goal_tree prem = (combine prem goal_concl);
kleing@16964
   171
      fun try_subst prem = 
kleing@16964
   172
        is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
kleing@16964
   173
      (*keep successful substitutions*)
kleing@16964
   174
      val ss = prems |> List.map try_subst 
kleing@16964
   175
               |> List.filter isSome 
kleing@16964
   176
               |> List.map valOf; 
kleing@16964
   177
    in
kleing@16964
   178
    (*elim rules always have assumptions, so an elim with one 
kleing@16964
   179
      assumption is as good as an intro rule with none*)
kleing@16964
   180
      if is_nontrivial (ProofContext.sign_of ctxt) (Thm.major_prem_of thm) 
kleing@16964
   181
        andalso not (null ss)
kleing@16964
   182
      then SOME (nprems_of thm - 1, foldl Int.min (hd ss) (tl ss)) else NONE
kleing@16964
   183
    end
kleing@16964
   184
  else NONE
wenzelm@16036
   185
wenzelm@16033
   186
kleing@16074
   187
(* filter_simp *)
wenzelm@16033
   188
kleing@16088
   189
fun filter_simp ctxt t (_,thm) =
wenzelm@16033
   190
  let
wenzelm@16033
   191
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   192
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
kleing@16964
   193
    val extract_simp = 
kleing@16964
   194
      ((List.map prop_of) o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
kleing@16964
   195
    val ss = is_matching_thm extract_simp ctxt false t thm
kleing@16964
   196
  in 
kleing@16964
   197
    if is_some ss then SOME (nprems_of thm, valOf ss) else NONE 
kleing@16964
   198
  end;
wenzelm@16033
   199
wenzelm@16033
   200
wenzelm@16033
   201
(* filter_pattern *)
wenzelm@16033
   202
kleing@16088
   203
fun filter_pattern ctxt pat (_, thm) =
wenzelm@16036
   204
  let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt)
kleing@16895
   205
  in if Pattern.matches_subterm tsig (pat, Thm.prop_of thm) then SOME (0,0) 
kleing@16895
   206
     else NONE end;
wenzelm@16033
   207
wenzelm@16033
   208
(* interpret criteria as filters *)
wenzelm@16033
   209
wenzelm@16036
   210
local
wenzelm@16036
   211
wenzelm@16036
   212
fun err_no_goal c =
wenzelm@16036
   213
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   214
wenzelm@16033
   215
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16036
   216
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   217
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   218
  | filter_crit _ NONE Dest = err_no_goal "dest"
wenzelm@16036
   219
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16036
   220
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
wenzelm@16036
   221
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
kleing@16088
   222
  | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
kleing@16088
   223
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
wenzelm@16036
   224
kleing@16895
   225
fun opt_not x = if isSome x then NONE else SOME (0,0);
kleing@16895
   226
kleing@16895
   227
fun opt_add (SOME (a,x), SOME (b,y)) = SOME ((Int.max (a,b)), (x + y))
kleing@16895
   228
 |  opt_add _ = NONE;
kleing@16895
   229
wenzelm@16036
   230
in
wenzelm@16033
   231
wenzelm@16033
   232
fun filter_criterion ctxt opt_goal (b, c) =
kleing@16895
   233
  (if b then I else opt_not) o filter_crit ctxt opt_goal c;
kleing@16895
   234
kleing@16895
   235
fun all_filters filters thms =
kleing@16895
   236
  let
kleing@16895
   237
    fun eval_filters filters thm =
kleing@16895
   238
      map (fn f => f thm) filters |> List.foldl opt_add (SOME (0,0));
wenzelm@16033
   239
kleing@16895
   240
    (*filters return: (number of assumptions, substitution size) option, so
kleing@16964
   241
      sort (desc. in both cases) according to number of assumptions first,
kleing@16895
   242
      then by the substitution size*)
kleing@16895
   243
    fun thm_ord (((p0,s0),_),((p1,s1),_)) =
kleing@16964
   244
      prod_ord int_ord int_ord ((p1,s1),(p0,s0));
kleing@16895
   245
kleing@16895
   246
    val processed = List.map (fn t => (eval_filters filters t, t)) thms;
kleing@16895
   247
    val filtered = List.filter (isSome o #1) processed;
kleing@16895
   248
  in
kleing@16895
   249
    filtered |> List.map (apfst valOf) |> sort thm_ord |> map #2
kleing@16895
   250
  end;
wenzelm@16033
   251
wenzelm@16036
   252
end;
wenzelm@16036
   253
wenzelm@16033
   254
wenzelm@16033
   255
(* print_theorems *)
wenzelm@16033
   256
wenzelm@16036
   257
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
wenzelm@16033
   258
  let
wenzelm@16036
   259
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
wenzelm@16036
   260
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   261
wenzelm@16036
   262
    val matches = all_filters filters (find_thms ctxt ([], []));
wenzelm@16033
   263
    val len = length matches;
wenzelm@16033
   264
    val limit = if_none opt_limit (! thms_containing_limit);
wenzelm@16033
   265
wenzelm@16036
   266
    fun prt_fact (thmref, thm) =
wenzelm@16036
   267
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   268
  in
wenzelm@16036
   269
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
wenzelm@16033
   270
     (if null matches then [Pretty.str "nothing found"]
wenzelm@16033
   271
      else
wenzelm@16036
   272
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
wenzelm@16036
   273
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
wenzelm@16036
   274
         Pretty.str ""] @
wenzelm@16036
   275
        map prt_fact (Library.drop (len - limit, matches)))
wenzelm@16033
   276
    |> Pretty.chunks |> Pretty.writeln
wenzelm@16033
   277
  end;
wenzelm@16033
   278
wenzelm@16033
   279
end;