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