src/Pure/Isar/find_theorems.ML
author wenzelm
Sun May 22 16:54:09 2005 +0200 (2005-05-22)
changeset 16033 f93ca3d4ffa7
child 16036 1da07ac33711
permissions -rw-r--r--
Retrieve theorems from proof context -- improved version of
thms_containing 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@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@16033
    16
  datatype search_criterion =
wenzelm@16033
    17
    Name of string | Intro | Elim | Dest | Rewrite of string | Pattern of string
wenzelm@16033
    18
  val print_theorems: Proof.context -> term option -> int option ->
wenzelm@16033
    19
    (bool * search_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@16033
   104
datatype search_criterion =
wenzelm@16033
   105
  Name of string | Intro | Elim | Dest | Rewrite of string | Pattern of string;
wenzelm@16033
   106
wenzelm@16033
   107
fun string_of_crit (Name name) = "name: " ^ quote name
wenzelm@16033
   108
  | string_of_crit Intro = "intro"
wenzelm@16033
   109
  | string_of_crit Elim = "elim"
wenzelm@16033
   110
  | string_of_crit Dest = "dest"
wenzelm@16033
   111
  | string_of_crit (Rewrite s) = "rewrite: " ^ quote s
wenzelm@16033
   112
  | string_of_crit (Pattern s) = quote s;
wenzelm@16033
   113
wenzelm@16033
   114
fun string_of_criterion (b, c) = (if b then "" else "-") ^ string_of_crit c;
wenzelm@16033
   115
wenzelm@16033
   116
wenzelm@16033
   117
wenzelm@16033
   118
(** search criterion filters **)
wenzelm@16033
   119
wenzelm@16033
   120
(* filter_name *)
wenzelm@16033
   121
wenzelm@16033
   122
fun is_substring pat str =
wenzelm@16033
   123
  if String.size pat = 0 then true
wenzelm@16033
   124
  else if String.size pat > String.size str then false
wenzelm@16033
   125
  else if String.substring (str, 0, String.size pat) = pat then true
wenzelm@16033
   126
  else is_substring pat (String.extract (str, 1, NONE));
wenzelm@16033
   127
wenzelm@16033
   128
(*filter that just looks for a string in the name,
wenzelm@16033
   129
  substring match only (no regexps are performed)*)
wenzelm@16033
   130
fun filter_name str_pat ((name, _), _) = is_substring str_pat name;
wenzelm@16033
   131
wenzelm@16033
   132
wenzelm@16033
   133
(* filter_intro *)
wenzelm@16033
   134
wenzelm@16033
   135
(*checking conclusion of theorem against conclusion of goal*)
wenzelm@16033
   136
fun filter_intro ctxt goal =
wenzelm@16033
   137
  let
wenzelm@16033
   138
    val extract_intro = (single, Logic.strip_imp_concl);
wenzelm@16033
   139
    val concl = Logic.concl_of_goal goal 1;
wenzelm@16033
   140
  in is_matching_thm extract_intro ctxt concl end;
wenzelm@16033
   141
wenzelm@16033
   142
wenzelm@16033
   143
(* filter_elim_dest *)
wenzelm@16033
   144
wenzelm@16033
   145
(*for elimination and destruction rules, we must check if the major
wenzelm@16033
   146
  premise matches with one of the assumptions in the top subgoal, but
wenzelm@16033
   147
  we must additionally make sure that we tell elim/dest apart, using
wenzelm@16033
   148
  check_thm (cf. is_elim_rule below)*)
wenzelm@16033
   149
fun filter_elim_dest check_thm ctxt goal =
wenzelm@16033
   150
  let
wenzelm@16033
   151
    val extract_elim =
wenzelm@16033
   152
     (fn thm => if Thm.no_prems thm then [] else [thm],
wenzelm@16033
   153
      hd o Logic.strip_imp_prems);
wenzelm@16033
   154
wenzelm@16033
   155
    val prems = Logic.prems_of_goal goal 1;
wenzelm@16033
   156
  in
wenzelm@16033
   157
    fn fact => List.exists (fn prem =>
wenzelm@16033
   158
      is_matching_thm extract_elim ctxt prem fact andalso check_thm (#2 fact)) prems
wenzelm@16033
   159
  end;
wenzelm@16033
   160
wenzelm@16033
   161
(*elimination rule: conclusion is a Var which does not appears in the
wenzelm@16033
   162
  major premise*)
wenzelm@16033
   163
fun is_elim_rule thm =
wenzelm@16033
   164
  let
wenzelm@16033
   165
    val prop = Thm.prop_of thm;
wenzelm@16033
   166
    val concl = ObjectLogic.drop_judgment (Thm.sign_of_thm thm) (Logic.strip_imp_concl prop);
wenzelm@16033
   167
    val major_prem = hd (Logic.strip_imp_prems prop);
wenzelm@16033
   168
    val prem_vars = Drule.vars_of_terms [major_prem];
wenzelm@16033
   169
  in
wenzelm@16033
   170
    Term.is_Var concl andalso not (Term.dest_Var concl mem prem_vars)
wenzelm@16033
   171
  end;
wenzelm@16033
   172
wenzelm@16033
   173
wenzelm@16033
   174
(* filter_rewrite *)
wenzelm@16033
   175
wenzelm@16033
   176
fun filter_rewrite ctxt str =
wenzelm@16033
   177
  let
wenzelm@16033
   178
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   179
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
wenzelm@16033
   180
    val extract_rewrite = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
wenzelm@16033
   181
  in is_matching_thm extract_rewrite ctxt (ProofContext.read_term ctxt str) end;
wenzelm@16033
   182
wenzelm@16033
   183
wenzelm@16033
   184
(* filter_pattern *)
wenzelm@16033
   185
wenzelm@16033
   186
(*subterm pattern supplied as raw string*)
wenzelm@16033
   187
fun filter_pattern ctxt str =
wenzelm@16033
   188
  let
wenzelm@16033
   189
    val tsig = Sign.tsig_of (ProofContext.sign_of ctxt);
wenzelm@16033
   190
    val [pat] = ProofContext.read_term_pats TypeInfer.logicT ctxt [str];
wenzelm@16033
   191
  in fn (_, thm) => Pattern.matches_subterm tsig (pat, Thm.prop_of thm) end;
wenzelm@16033
   192
wenzelm@16033
   193
wenzelm@16033
   194
(* interpret criteria as filters *)
wenzelm@16033
   195
wenzelm@16033
   196
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16033
   197
  | filter_crit ctxt _ (Rewrite str) = filter_rewrite ctxt str
wenzelm@16033
   198
  | filter_crit ctxt _ (Pattern str) = filter_pattern ctxt str
wenzelm@16033
   199
  (*beyond this point, only criteria requiring a goal!*)
wenzelm@16033
   200
  | filter_crit _ NONE c =
wenzelm@16033
   201
      error ("Need to have a current goal to use " ^ string_of_crit c)
wenzelm@16033
   202
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16033
   203
  | filter_crit ctxt (SOME goal) Elim = filter_elim_dest is_elim_rule ctxt goal
wenzelm@16033
   204
  | filter_crit ctxt (SOME goal) Dest =
wenzelm@16033
   205
      filter_elim_dest (not o is_elim_rule) ctxt goal;
wenzelm@16033
   206
      (*in this case all that is not elim is dest*)
wenzelm@16033
   207
wenzelm@16033
   208
fun filter_criterion ctxt opt_goal (b, c) =
wenzelm@16033
   209
  (if b then I else not) o filter_crit ctxt opt_goal c;
wenzelm@16033
   210
wenzelm@16033
   211
fun all_filters filters = List.filter (fn x => List.all (fn f => f x) filters);
wenzelm@16033
   212
wenzelm@16033
   213
wenzelm@16033
   214
(* print_theorems *)
wenzelm@16033
   215
wenzelm@16033
   216
fun print_theorems ctxt opt_goal opt_limit criteria =
wenzelm@16033
   217
  let
wenzelm@16033
   218
    fun prt_fact (thmref, thm) =
wenzelm@16033
   219
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   220
    val prt_crit = Pretty.str o string_of_criterion;
wenzelm@16033
   221
wenzelm@16033
   222
    val all_facts = find_thms ctxt ([], []);
wenzelm@16033
   223
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   224
    val matches = all_filters filters all_facts;
wenzelm@16033
   225
wenzelm@16033
   226
    val len = length matches;
wenzelm@16033
   227
    val limit = if_none opt_limit (! thms_containing_limit);
wenzelm@16033
   228
    val count = Int.min (limit, len);
wenzelm@16033
   229
wenzelm@16033
   230
    val pruned = len <= limit;
wenzelm@16033
   231
    val found = "found " ^ string_of_int len ^ " theorems" ^
wenzelm@16033
   232
      (if pruned then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":";
wenzelm@16033
   233
  in
wenzelm@16033
   234
    Pretty.big_list "searched for:" (map prt_crit criteria) ::
wenzelm@16033
   235
     (if null matches then [Pretty.str "nothing found"]
wenzelm@16033
   236
      else
wenzelm@16033
   237
       [Pretty.str "", Pretty.str found] @
wenzelm@16033
   238
       (if pruned then [] else [Pretty.str "..."]) @
wenzelm@16033
   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;