src/Pure/Isar/find_theorems.ML
author kleing
Wed Jul 20 07:40:23 2005 +0200 (2005-07-20)
changeset 16895 df67fc190e06
parent 16486 1a12cdb6ee6b
child 16964 6a25e42eaff5
permissions -rw-r--r--
Sort search results in order of relevance, where relevance =
a) better if 0 premises for intro or 1 premise for elim/dest rules
b) better if substitution size wrt to current goal is smaller

Only applies to intro, dest, elim, and simp

(contributed by Rafal Kolanski, NICTA)
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@16088
    81
kleing@16088
    82
fun is_matching_thm (extract_thms, extract_term) ctxt po obj thm =
kleing@16088
    83
  let
kleing@16088
    84
    val sg = ProofContext.sign_of ctxt;
kleing@16088
    85
    val tsig = Sign.tsig_of sg;
kleing@16088
    86
wenzelm@16486
    87
    val is_nontrivial = is_Const o head_of o ObjectLogic.drop_judgment sg;
kleing@16088
    88
wenzelm@16486
    89
    fun matches pat =
wenzelm@16486
    90
      is_nontrivial pat andalso
kleing@16895
    91
      Pattern.matches tsig (if po then (pat,obj) else (obj,pat));
kleing@16895
    92
kleing@16895
    93
    fun substsize pat =
kleing@16895
    94
      let 
kleing@16895
    95
        val (_,subst) = Pattern.match tsig (if po then (pat,obj) else (obj,pat))
kleing@16895
    96
      in Vartab.foldl (op + o apsnd (size_of_term o snd o snd)) (0, subst)
kleing@16895
    97
      end;
kleing@16088
    98
kleing@16895
    99
    fun bestmatch [] = NONE
kleing@16895
   100
     |  bestmatch (x :: xs) = SOME (nprems_of thm, foldl Int.min x xs);
kleing@16895
   101
kleing@16895
   102
    val match_thm = matches o extract_term o Thm.prop_of;
wenzelm@16486
   103
  in
kleing@16895
   104
    map (substsize o extract_term o Thm.prop_of) 
kleing@16895
   105
        (List.filter match_thm (extract_thms thm)) |> bestmatch
kleing@16088
   106
  end;
kleing@16088
   107
kleing@16088
   108
wenzelm@16033
   109
(* filter_name *)
wenzelm@16033
   110
wenzelm@16033
   111
fun is_substring pat str =
wenzelm@16033
   112
  if String.size pat = 0 then true
wenzelm@16033
   113
  else if String.size pat > String.size str then false
wenzelm@16033
   114
  else if String.substring (str, 0, String.size pat) = pat then true
wenzelm@16033
   115
  else is_substring pat (String.extract (str, 1, NONE));
wenzelm@16033
   116
wenzelm@16033
   117
(*filter that just looks for a string in the name,
wenzelm@16033
   118
  substring match only (no regexps are performed)*)
kleing@16895
   119
fun filter_name str_pat (thmref, _) = 
kleing@16895
   120
  if is_substring str_pat (PureThy.name_of_thmref thmref) 
kleing@16895
   121
  then SOME (0,0) else NONE;
wenzelm@16033
   122
wenzelm@16033
   123
wenzelm@16036
   124
(* filter intro/elim/dest rules *)
wenzelm@16033
   125
wenzelm@16036
   126
local
wenzelm@16036
   127
wenzelm@16036
   128
(*elimination rule: conclusion is a Var which does not appear in the major premise*)
wenzelm@16036
   129
fun is_elim ctxt thm =
wenzelm@16033
   130
  let
wenzelm@16036
   131
    val sg = ProofContext.sign_of ctxt;
wenzelm@16036
   132
    val prop = Thm.prop_of thm;
wenzelm@16036
   133
    val concl = ObjectLogic.drop_judgment sg (Logic.strip_imp_concl prop);
wenzelm@16036
   134
    val major_prem = Library.take (1, Logic.strip_imp_prems prop);
wenzelm@16036
   135
    val prem_vars = Drule.vars_of_terms major_prem;
wenzelm@16036
   136
  in
wenzelm@16036
   137
    not (null major_prem) andalso
wenzelm@16036
   138
    Term.is_Var concl andalso
wenzelm@16036
   139
    not (Term.dest_Var concl mem prem_vars)
wenzelm@16036
   140
  end;
wenzelm@16033
   141
kleing@16088
   142
fun filter_elim_dest check_thm ctxt goal (_,thm) =
wenzelm@16033
   143
  let
wenzelm@16033
   144
    val extract_elim =
wenzelm@16033
   145
     (fn thm => if Thm.no_prems thm then [] else [thm],
wenzelm@16033
   146
      hd o Logic.strip_imp_prems);
wenzelm@16033
   147
    val prems = Logic.prems_of_goal goal 1;
kleing@16895
   148
kleing@16895
   149
    fun try_subst prem = is_matching_thm extract_elim ctxt true prem thm;
kleing@16895
   150
    
kleing@16895
   151
    (*keep successful substitutions*)
kleing@16895
   152
    val ss = prems |> List.map try_subst 
kleing@16895
   153
             |> List.filter isSome 
kleing@16895
   154
             |> List.map (#2 o valOf);
wenzelm@16033
   155
  in
kleing@16895
   156
    (*if possible, keep best substitution (one with smallest size)*)
kleing@16895
   157
    (*elim and dest rules always have assumptions, so an elim with one 
kleing@16895
   158
      assumption is as good as an intro rule with none*)
kleing@16895
   159
    if check_thm ctxt thm andalso not (null ss) 
kleing@16895
   160
    then SOME (nprems_of thm - 1, foldl Int.min (hd ss) (tl ss)) else NONE
wenzelm@16033
   161
  end;
wenzelm@16033
   162
wenzelm@16036
   163
in
wenzelm@16036
   164
kleing@16088
   165
fun filter_intro ctxt goal (_,thm) =
wenzelm@16033
   166
  let
wenzelm@16036
   167
    val extract_intro = (single, Logic.strip_imp_concl);
wenzelm@16036
   168
    val concl = Logic.concl_of_goal goal 1;
wenzelm@16033
   169
  in
kleing@16088
   170
    is_matching_thm extract_intro ctxt true concl thm
wenzelm@16033
   171
  end;
wenzelm@16033
   172
wenzelm@16036
   173
fun filter_elim ctxt = filter_elim_dest is_elim ctxt;
wenzelm@16036
   174
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt;
wenzelm@16036
   175
wenzelm@16036
   176
end;
wenzelm@16036
   177
wenzelm@16033
   178
kleing@16074
   179
(* filter_simp *)
wenzelm@16033
   180
kleing@16088
   181
fun filter_simp ctxt t (_,thm) =
wenzelm@16033
   182
  let
wenzelm@16033
   183
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   184
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
kleing@16074
   185
    val extract_simp = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
kleing@16088
   186
  in is_matching_thm extract_simp ctxt false t thm end;
wenzelm@16033
   187
wenzelm@16033
   188
wenzelm@16033
   189
(* filter_pattern *)
wenzelm@16033
   190
kleing@16088
   191
fun filter_pattern ctxt pat (_, thm) =
wenzelm@16036
   192
  let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt)
kleing@16895
   193
  in if Pattern.matches_subterm tsig (pat, Thm.prop_of thm) then SOME (0,0) 
kleing@16895
   194
     else NONE end;
wenzelm@16033
   195
wenzelm@16033
   196
(* interpret criteria as filters *)
wenzelm@16033
   197
wenzelm@16036
   198
local
wenzelm@16036
   199
wenzelm@16036
   200
fun err_no_goal c =
wenzelm@16036
   201
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   202
wenzelm@16033
   203
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16036
   204
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   205
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   206
  | filter_crit _ NONE Dest = err_no_goal "dest"
wenzelm@16036
   207
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16036
   208
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
wenzelm@16036
   209
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
kleing@16088
   210
  | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
kleing@16088
   211
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
wenzelm@16036
   212
kleing@16895
   213
fun opt_not x = if isSome x then NONE else SOME (0,0);
kleing@16895
   214
kleing@16895
   215
fun opt_add (SOME (a,x), SOME (b,y)) = SOME ((Int.max (a,b)), (x + y))
kleing@16895
   216
 |  opt_add _ = NONE;
kleing@16895
   217
wenzelm@16036
   218
in
wenzelm@16033
   219
wenzelm@16033
   220
fun filter_criterion ctxt opt_goal (b, c) =
kleing@16895
   221
  (if b then I else opt_not) o filter_crit ctxt opt_goal c;
kleing@16895
   222
kleing@16895
   223
fun all_filters filters thms =
kleing@16895
   224
  let
kleing@16895
   225
    fun eval_filters filters thm =
kleing@16895
   226
      map (fn f => f thm) filters |> List.foldl opt_add (SOME (0,0));
wenzelm@16033
   227
kleing@16895
   228
    (*filters return: (number of assumptions, substitution size) option, so
kleing@16895
   229
      sort (desc. in both cases) according to whether a theorem has assumptions,
kleing@16895
   230
      then by the substitution size*)
kleing@16895
   231
    fun thm_ord (((p0,s0),_),((p1,s1),_)) =
kleing@16895
   232
      prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) 
kleing@16895
   233
               int_ord ((p1,s1),(p0,s0));
kleing@16895
   234
kleing@16895
   235
    val processed = List.map (fn t => (eval_filters filters t, t)) thms;
kleing@16895
   236
    val filtered = List.filter (isSome o #1) processed;
kleing@16895
   237
  in
kleing@16895
   238
    filtered |> List.map (apfst valOf) |> sort thm_ord |> map #2
kleing@16895
   239
  end;
wenzelm@16033
   240
wenzelm@16036
   241
end;
wenzelm@16036
   242
wenzelm@16033
   243
wenzelm@16033
   244
(* print_theorems *)
wenzelm@16033
   245
wenzelm@16036
   246
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
wenzelm@16033
   247
  let
wenzelm@16036
   248
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
wenzelm@16036
   249
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   250
wenzelm@16036
   251
    val matches = all_filters filters (find_thms ctxt ([], []));
wenzelm@16033
   252
    val len = length matches;
wenzelm@16033
   253
    val limit = if_none opt_limit (! thms_containing_limit);
wenzelm@16033
   254
wenzelm@16036
   255
    fun prt_fact (thmref, thm) =
wenzelm@16036
   256
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   257
  in
wenzelm@16036
   258
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
wenzelm@16033
   259
     (if null matches then [Pretty.str "nothing found"]
wenzelm@16033
   260
      else
wenzelm@16036
   261
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
wenzelm@16036
   262
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
wenzelm@16036
   263
         Pretty.str ""] @
wenzelm@16036
   264
        map prt_fact (Library.drop (len - limit, matches)))
wenzelm@16033
   265
    |> Pretty.chunks |> Pretty.writeln
wenzelm@16033
   266
  end;
wenzelm@16033
   267
wenzelm@16033
   268
end;