src/Pure/Isar/find_theorems.ML
author kleing
Thu May 26 10:05:28 2005 +0200 (2005-05-26)
changeset 16088 f084ba24de29
parent 16077 c04f972bfabe
child 16486 1a12cdb6ee6b
permissions -rw-r--r--
cleaned up select_match
do not return trivial matches
made simp: t work
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
kleing@16088
    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@16088
    65
kleing@16088
    66
(* matching theorems *)
kleing@16088
    67
kleing@16088
    68
fun is_matching_thm (extract_thms, extract_term) ctxt po obj thm =
kleing@16088
    69
  let
kleing@16088
    70
    val sg = ProofContext.sign_of ctxt;
kleing@16088
    71
    val tsig = Sign.tsig_of sg;
kleing@16088
    72
kleing@16088
    73
    val is_nontrivial = is_Const o head_of o ObjectLogic.drop_judgment sg;    
kleing@16088
    74
kleing@16088
    75
    fun matches pat = 
kleing@16088
    76
      is_nontrivial pat andalso 
kleing@16088
    77
      Pattern.matches tsig (if po then (pat,obj) else (obj,pat))
kleing@16088
    78
      handle Pattern.MATCH => false;
kleing@16088
    79
kleing@16088
    80
    val match_thm = matches o extract_term o Thm.prop_of
kleing@16088
    81
  in       
kleing@16088
    82
    List.exists match_thm (extract_thms thm)
kleing@16088
    83
  end;
kleing@16088
    84
kleing@16088
    85
wenzelm@16033
    86
(* filter_name *)
wenzelm@16033
    87
wenzelm@16033
    88
fun is_substring pat str =
wenzelm@16033
    89
  if String.size pat = 0 then true
wenzelm@16033
    90
  else if String.size pat > String.size str then false
wenzelm@16033
    91
  else if String.substring (str, 0, String.size pat) = pat then true
wenzelm@16033
    92
  else is_substring pat (String.extract (str, 1, NONE));
wenzelm@16033
    93
wenzelm@16033
    94
(*filter that just looks for a string in the name,
wenzelm@16033
    95
  substring match only (no regexps are performed)*)
wenzelm@16033
    96
fun filter_name str_pat ((name, _), _) = is_substring str_pat name;
wenzelm@16033
    97
wenzelm@16033
    98
wenzelm@16036
    99
(* filter intro/elim/dest rules *)
wenzelm@16033
   100
wenzelm@16036
   101
local
wenzelm@16036
   102
wenzelm@16036
   103
(*elimination rule: conclusion is a Var which does not appear in the major premise*)
wenzelm@16036
   104
fun is_elim ctxt thm =
wenzelm@16033
   105
  let
wenzelm@16036
   106
    val sg = ProofContext.sign_of ctxt;
wenzelm@16036
   107
    val prop = Thm.prop_of thm;
wenzelm@16036
   108
    val concl = ObjectLogic.drop_judgment sg (Logic.strip_imp_concl prop);
wenzelm@16036
   109
    val major_prem = Library.take (1, Logic.strip_imp_prems prop);
wenzelm@16036
   110
    val prem_vars = Drule.vars_of_terms major_prem;
wenzelm@16036
   111
  in
wenzelm@16036
   112
    not (null major_prem) andalso
wenzelm@16036
   113
    Term.is_Var concl andalso
wenzelm@16036
   114
    not (Term.dest_Var concl mem prem_vars)
wenzelm@16036
   115
  end;
wenzelm@16033
   116
kleing@16088
   117
fun filter_elim_dest check_thm ctxt goal (_,thm) =
wenzelm@16033
   118
  let
wenzelm@16033
   119
    val extract_elim =
wenzelm@16033
   120
     (fn thm => if Thm.no_prems thm then [] else [thm],
wenzelm@16033
   121
      hd o Logic.strip_imp_prems);
wenzelm@16033
   122
    val prems = Logic.prems_of_goal goal 1;
wenzelm@16033
   123
  in
kleing@16088
   124
    prems |> 
kleing@16088
   125
    List.exists (fn prem => 
kleing@16088
   126
      is_matching_thm extract_elim ctxt true prem thm
kleing@16088
   127
      andalso (check_thm ctxt) thm)
wenzelm@16033
   128
  end;
wenzelm@16033
   129
wenzelm@16036
   130
in
wenzelm@16036
   131
kleing@16088
   132
fun filter_intro ctxt goal (_,thm) =
wenzelm@16033
   133
  let
wenzelm@16036
   134
    val extract_intro = (single, Logic.strip_imp_concl);
wenzelm@16036
   135
    val concl = Logic.concl_of_goal goal 1;
wenzelm@16033
   136
  in
kleing@16088
   137
    is_matching_thm extract_intro ctxt true concl thm
wenzelm@16033
   138
  end;
wenzelm@16033
   139
wenzelm@16036
   140
fun filter_elim ctxt = filter_elim_dest is_elim ctxt;
wenzelm@16036
   141
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt;
wenzelm@16036
   142
wenzelm@16036
   143
end;
wenzelm@16036
   144
wenzelm@16033
   145
kleing@16074
   146
(* filter_simp *)
wenzelm@16033
   147
kleing@16088
   148
fun filter_simp ctxt t (_,thm) =
wenzelm@16033
   149
  let
wenzelm@16033
   150
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   151
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
kleing@16074
   152
    val extract_simp = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
kleing@16088
   153
  in is_matching_thm extract_simp ctxt false t thm end;
wenzelm@16033
   154
wenzelm@16033
   155
wenzelm@16033
   156
(* filter_pattern *)
wenzelm@16033
   157
kleing@16088
   158
fun filter_pattern ctxt pat (_, thm) =
wenzelm@16036
   159
  let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt)
kleing@16088
   160
  in Pattern.matches_subterm tsig (pat, Thm.prop_of thm) end;
wenzelm@16033
   161
wenzelm@16033
   162
wenzelm@16033
   163
(* interpret criteria as filters *)
wenzelm@16033
   164
wenzelm@16036
   165
local
wenzelm@16036
   166
wenzelm@16036
   167
fun err_no_goal c =
wenzelm@16036
   168
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   169
wenzelm@16033
   170
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16036
   171
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   172
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   173
  | filter_crit _ NONE Dest = err_no_goal "dest"
wenzelm@16036
   174
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16036
   175
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
wenzelm@16036
   176
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
kleing@16088
   177
  | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
kleing@16088
   178
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
wenzelm@16036
   179
wenzelm@16036
   180
in
wenzelm@16033
   181
wenzelm@16033
   182
fun filter_criterion ctxt opt_goal (b, c) =
wenzelm@16033
   183
  (if b then I else not) o filter_crit ctxt opt_goal c;
wenzelm@16033
   184
wenzelm@16033
   185
fun all_filters filters = List.filter (fn x => List.all (fn f => f x) filters);
wenzelm@16033
   186
wenzelm@16036
   187
end;
wenzelm@16036
   188
wenzelm@16033
   189
wenzelm@16033
   190
(* print_theorems *)
wenzelm@16033
   191
wenzelm@16036
   192
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
wenzelm@16033
   193
  let
wenzelm@16036
   194
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
wenzelm@16036
   195
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   196
wenzelm@16036
   197
    val matches = all_filters filters (find_thms ctxt ([], []));
wenzelm@16033
   198
    val len = length matches;
wenzelm@16033
   199
    val limit = if_none opt_limit (! thms_containing_limit);
wenzelm@16033
   200
wenzelm@16036
   201
    fun prt_fact (thmref, thm) =
wenzelm@16036
   202
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   203
  in
wenzelm@16036
   204
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
wenzelm@16033
   205
     (if null matches then [Pretty.str "nothing found"]
wenzelm@16033
   206
      else
wenzelm@16036
   207
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
wenzelm@16036
   208
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
wenzelm@16036
   209
         Pretty.str ""] @
wenzelm@16036
   210
        map prt_fact (Library.drop (len - limit, matches)))
wenzelm@16033
   211
    |> Pretty.chunks |> Pretty.writeln
wenzelm@16033
   212
  end;
wenzelm@16033
   213
wenzelm@16033
   214
end;