src/Pure/Isar/find_theorems.ML
author kleing
Wed May 25 10:43:15 2005 +0200 (2005-05-25)
changeset 16074 9e569163ba8c
parent 16071 e0136cdef722
child 16077 c04f972bfabe
permissions -rw-r--r--
renamed search criterion 'rewrite' to 'simp'
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
(* matching theorems *)
wenzelm@16033
    32
wenzelm@16033
    33
local
wenzelm@16033
    34
wenzelm@16033
    35
(*returns all those facts whose subterm extracted by extract can be
wenzelm@16033
    36
  instantiated to obj; the list is sorted according to the number of premises
wenzelm@16033
    37
  and the size of the required substitution*)
wenzelm@16033
    38
fun select_match (extract_thms, extract_term) ctxt obj facts =
wenzelm@16033
    39
  let
wenzelm@16033
    40
    val match = #2 o Pattern.match (Sign.tsig_of (ProofContext.sign_of ctxt));
wenzelm@16033
    41
wenzelm@16033
    42
    fun match_size pat =
wenzelm@16033
    43
      let val subst = match (pat, obj)  (*exception Pattern.MATCH*)
wenzelm@16033
    44
      in SOME (Vartab.foldl (op + o apsnd (Term.size_of_term o #2 o #2)) (0, subst)) end
wenzelm@16033
    45
      handle Pattern.MATCH => NONE;
wenzelm@16033
    46
wenzelm@16033
    47
    fun select (fact as (_, thm)) =
wenzelm@16033
    48
      let
wenzelm@16033
    49
        fun first_match (th :: ths) res =
wenzelm@16033
    50
              (case match_size (extract_term (Thm.prop_of th)) of
wenzelm@16033
    51
                SOME s => ((Thm.nprems_of th, s), fact) :: res
wenzelm@16033
    52
              | NONE => first_match ths res)
wenzelm@16033
    53
          | first_match [] res = res;
wenzelm@16033
    54
      in first_match (extract_thms thm) end;
wenzelm@16033
    55
wenzelm@16033
    56
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
wenzelm@16033
    57
      prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0, s0), (p1, s1));
wenzelm@16033
    58
  in
wenzelm@16033
    59
    fold select facts []
wenzelm@16033
    60
    |> Library.sort thm_ord |> map #2
wenzelm@16033
    61
  end;
wenzelm@16033
    62
wenzelm@16033
    63
(*speed up retrieval by checking head symbol*)
wenzelm@16033
    64
fun index_head ctxt prop =
wenzelm@16033
    65
  (case Term.head_of (ObjectLogic.drop_judgment (ProofContext.sign_of ctxt) prop) of
wenzelm@16033
    66
    Const (c, _) => ([c], [])
wenzelm@16033
    67
  | Free (x, _) => if ProofContext.is_known ctxt x then ([], [x]) else ([], [])
wenzelm@16033
    68
  | _ => ([], []));
wenzelm@16033
    69
wenzelm@16033
    70
in
wenzelm@16033
    71
wenzelm@16033
    72
fun find_matching_thms extract ctxt prop =
wenzelm@16033
    73
  find_thms ctxt (index_head ctxt prop)
wenzelm@16033
    74
  |> select_match extract ctxt prop;
wenzelm@16033
    75
wenzelm@16033
    76
fun is_matching_thm extract ctxt prop fact =
wenzelm@16033
    77
  not (null (select_match extract ctxt prop [fact]));
wenzelm@16033
    78
wenzelm@16033
    79
end;
wenzelm@16033
    80
wenzelm@16033
    81
wenzelm@16033
    82
wenzelm@16033
    83
(** search criteria **)
wenzelm@16033
    84
wenzelm@16036
    85
datatype 'term criterion =
kleing@16074
    86
  Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
wenzelm@16036
    87
wenzelm@16036
    88
fun read_criterion _ (Name name) = Name name
wenzelm@16036
    89
  | read_criterion _ Intro = Intro
wenzelm@16036
    90
  | read_criterion _ Elim = Elim
wenzelm@16036
    91
  | read_criterion _ Dest = Dest
kleing@16074
    92
  | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term ctxt str)
wenzelm@16036
    93
  | read_criterion ctxt (Pattern str) =
wenzelm@16036
    94
      Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
wenzelm@16033
    95
wenzelm@16036
    96
fun pretty_criterion ctxt (b, c) =
wenzelm@16036
    97
  let
wenzelm@16036
    98
    fun prfx s = if b then s else "-" ^ s;
wenzelm@16036
    99
  in
wenzelm@16036
   100
    (case c of
wenzelm@16036
   101
      Name name => Pretty.str (prfx "name: " ^ quote name)
wenzelm@16036
   102
    | Intro => Pretty.str (prfx "intro")
wenzelm@16036
   103
    | Elim => Pretty.str (prfx "elim")
wenzelm@16036
   104
    | Dest => Pretty.str (prfx "dest")
kleing@16074
   105
    | Simp t => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
wenzelm@16036
   106
        Pretty.quote (ProofContext.pretty_term ctxt t)]
wenzelm@16036
   107
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
wenzelm@16036
   108
        [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
wenzelm@16036
   109
  end;
wenzelm@16033
   110
wenzelm@16033
   111
wenzelm@16033
   112
wenzelm@16033
   113
(** search criterion filters **)
wenzelm@16033
   114
wenzelm@16033
   115
(* filter_name *)
wenzelm@16033
   116
wenzelm@16033
   117
fun is_substring pat str =
wenzelm@16033
   118
  if String.size pat = 0 then true
wenzelm@16033
   119
  else if String.size pat > String.size str then false
wenzelm@16033
   120
  else if String.substring (str, 0, String.size pat) = pat then true
wenzelm@16033
   121
  else is_substring pat (String.extract (str, 1, NONE));
wenzelm@16033
   122
wenzelm@16033
   123
(*filter that just looks for a string in the name,
wenzelm@16033
   124
  substring match only (no regexps are performed)*)
wenzelm@16033
   125
fun filter_name str_pat ((name, _), _) = is_substring str_pat name;
wenzelm@16033
   126
wenzelm@16033
   127
wenzelm@16036
   128
(* filter intro/elim/dest rules *)
wenzelm@16033
   129
wenzelm@16036
   130
local
wenzelm@16036
   131
wenzelm@16036
   132
(*elimination rule: conclusion is a Var which does not appear in the major premise*)
wenzelm@16036
   133
fun is_elim ctxt thm =
wenzelm@16033
   134
  let
wenzelm@16036
   135
    val sg = ProofContext.sign_of ctxt;
wenzelm@16036
   136
    val prop = Thm.prop_of thm;
wenzelm@16036
   137
    val concl = ObjectLogic.drop_judgment sg (Logic.strip_imp_concl prop);
wenzelm@16036
   138
    val major_prem = Library.take (1, Logic.strip_imp_prems prop);
wenzelm@16036
   139
    val prem_vars = Drule.vars_of_terms major_prem;
wenzelm@16036
   140
  in
wenzelm@16036
   141
    not (null major_prem) andalso
wenzelm@16036
   142
    Term.is_Var concl andalso
wenzelm@16036
   143
    not (Term.dest_Var concl mem prem_vars)
wenzelm@16036
   144
  end;
wenzelm@16033
   145
wenzelm@16033
   146
fun filter_elim_dest check_thm ctxt goal =
wenzelm@16033
   147
  let
wenzelm@16033
   148
    val extract_elim =
wenzelm@16033
   149
     (fn thm => if Thm.no_prems thm then [] else [thm],
wenzelm@16033
   150
      hd o Logic.strip_imp_prems);
wenzelm@16033
   151
    val prems = Logic.prems_of_goal goal 1;
wenzelm@16033
   152
  in
wenzelm@16036
   153
    fn fact => prems |> List.exists (fn prem =>
wenzelm@16036
   154
      is_matching_thm extract_elim ctxt prem fact
wenzelm@16036
   155
      andalso (check_thm ctxt) (#2 fact))
wenzelm@16033
   156
  end;
wenzelm@16033
   157
wenzelm@16036
   158
in
wenzelm@16036
   159
wenzelm@16036
   160
fun filter_intro ctxt goal =
wenzelm@16033
   161
  let
wenzelm@16036
   162
    val extract_intro = (single, Logic.strip_imp_concl);
wenzelm@16036
   163
    val concl = Logic.concl_of_goal goal 1;
wenzelm@16033
   164
  in
wenzelm@16036
   165
    fn fact => is_matching_thm extract_intro ctxt concl fact
wenzelm@16036
   166
      andalso not (is_elim ctxt (#2 fact))
wenzelm@16033
   167
  end;
wenzelm@16033
   168
wenzelm@16036
   169
fun filter_elim ctxt = filter_elim_dest is_elim ctxt;
wenzelm@16036
   170
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt;
wenzelm@16036
   171
wenzelm@16036
   172
end;
wenzelm@16036
   173
wenzelm@16033
   174
kleing@16074
   175
(* filter_simp *)
wenzelm@16033
   176
kleing@16074
   177
fun filter_simp ctxt t =
wenzelm@16033
   178
  let
wenzelm@16033
   179
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   180
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
kleing@16074
   181
    val extract_simp = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
kleing@16074
   182
  in is_matching_thm extract_simp ctxt t end;
wenzelm@16033
   183
wenzelm@16033
   184
wenzelm@16033
   185
(* filter_pattern *)
wenzelm@16033
   186
wenzelm@16036
   187
fun filter_pattern ctxt pat =
wenzelm@16036
   188
  let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt)
wenzelm@16033
   189
  in fn (_, thm) => Pattern.matches_subterm tsig (pat, Thm.prop_of thm) end;
wenzelm@16033
   190
wenzelm@16033
   191
wenzelm@16033
   192
(* interpret criteria as filters *)
wenzelm@16033
   193
wenzelm@16036
   194
local
wenzelm@16036
   195
wenzelm@16036
   196
fun err_no_goal c =
wenzelm@16036
   197
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   198
wenzelm@16033
   199
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16036
   200
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   201
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   202
  | filter_crit _ NONE Dest = err_no_goal "dest"
wenzelm@16036
   203
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16036
   204
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
wenzelm@16036
   205
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
kleing@16074
   206
  | filter_crit ctxt _ (Simp str) = filter_simp ctxt str
wenzelm@16036
   207
  | filter_crit ctxt _ (Pattern str) = filter_pattern ctxt str;
wenzelm@16036
   208
wenzelm@16036
   209
in
wenzelm@16033
   210
wenzelm@16033
   211
fun filter_criterion ctxt opt_goal (b, c) =
wenzelm@16033
   212
  (if b then I else not) o filter_crit ctxt opt_goal c;
wenzelm@16033
   213
wenzelm@16033
   214
fun all_filters filters = List.filter (fn x => List.all (fn f => f x) filters);
wenzelm@16033
   215
wenzelm@16036
   216
end;
wenzelm@16036
   217
wenzelm@16033
   218
wenzelm@16033
   219
(* print_theorems *)
wenzelm@16033
   220
wenzelm@16036
   221
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
wenzelm@16033
   222
  let
wenzelm@16036
   223
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
wenzelm@16036
   224
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   225
wenzelm@16036
   226
    val matches = all_filters filters (find_thms ctxt ([], []));
wenzelm@16033
   227
    val len = length matches;
wenzelm@16033
   228
    val limit = if_none opt_limit (! thms_containing_limit);
wenzelm@16033
   229
wenzelm@16036
   230
    fun prt_fact (thmref, thm) =
wenzelm@16036
   231
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   232
  in
wenzelm@16036
   233
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
wenzelm@16033
   234
     (if null matches then [Pretty.str "nothing found"]
wenzelm@16033
   235
      else
wenzelm@16036
   236
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
wenzelm@16036
   237
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
wenzelm@16036
   238
         Pretty.str ""] @
wenzelm@16036
   239
        map prt_fact (Library.drop (len - limit, matches)))
wenzelm@16033
   240
    |> Pretty.chunks |> Pretty.writeln
wenzelm@16033
   241
  end;
wenzelm@16033
   242
wenzelm@16033
   243
end;