src/Pure/Isar/find_theorems.ML
author kleing
Tue Feb 20 00:14:33 2007 +0100 (2007-02-20)
changeset 22340 275802767bf3
parent 19502 369cde91963d
child 22343 8e0f61d05f48
permissions -rw-r--r--
Remove duplicates from printed theorems in find_theorems
(still pretty slow, needs optimising).
Added syntax "find_theorems (..) with_dups .." to switch off removal.
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@16036
    12
  datatype 'term criterion =
kleing@16074
    13
    Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
kleing@22340
    14
  val print_theorems: Proof.context -> term option -> int option -> bool ->
wenzelm@16036
    15
    (bool * string criterion) list -> unit
wenzelm@16033
    16
end;
wenzelm@16033
    17
wenzelm@16033
    18
structure FindTheorems: FIND_THEOREMS =
wenzelm@16033
    19
struct
wenzelm@16033
    20
wenzelm@16033
    21
(** search criteria **)
wenzelm@16033
    22
wenzelm@16036
    23
datatype 'term criterion =
kleing@16074
    24
  Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
wenzelm@16036
    25
wenzelm@16036
    26
fun read_criterion _ (Name name) = Name name
wenzelm@16036
    27
  | read_criterion _ Intro = Intro
wenzelm@16036
    28
  | read_criterion _ Elim = Elim
wenzelm@16036
    29
  | read_criterion _ Dest = Dest
wenzelm@16486
    30
  | read_criterion ctxt (Simp str) =
kleing@16088
    31
      Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]))
wenzelm@16036
    32
  | read_criterion ctxt (Pattern str) =
wenzelm@16036
    33
      Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
wenzelm@16033
    34
wenzelm@16036
    35
fun pretty_criterion ctxt (b, c) =
wenzelm@16036
    36
  let
wenzelm@16036
    37
    fun prfx s = if b then s else "-" ^ s;
wenzelm@16036
    38
  in
wenzelm@16036
    39
    (case c of
wenzelm@16036
    40
      Name name => Pretty.str (prfx "name: " ^ quote name)
wenzelm@16036
    41
    | Intro => Pretty.str (prfx "intro")
wenzelm@16036
    42
    | Elim => Pretty.str (prfx "elim")
wenzelm@16036
    43
    | Dest => Pretty.str (prfx "dest")
kleing@16088
    44
    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
kleing@16088
    45
        Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))]
wenzelm@16036
    46
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
wenzelm@16036
    47
        [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
wenzelm@16036
    48
  end;
wenzelm@16033
    49
wenzelm@16033
    50
wenzelm@16033
    51
wenzelm@16033
    52
(** search criterion filters **)
wenzelm@16033
    53
kleing@16895
    54
(*generated filters are to be of the form
wenzelm@17205
    55
  input: (thmref * thm)
wenzelm@17106
    56
  output: (p:int, s:int) option, where
kleing@16895
    57
    NONE indicates no match
wenzelm@17106
    58
    p is the primary sorting criterion
kleing@16895
    59
      (eg. number of assumptions in the theorem)
kleing@16895
    60
    s is the secondary sorting criterion
kleing@16895
    61
      (eg. size of the substitution for intro, elim and dest)
kleing@16895
    62
  when applying a set of filters to a thm, fold results in:
kleing@16895
    63
    (biggest p, sum of all s)
wenzelm@17106
    64
  currently p and s only matter for intro, elim, dest and simp filters,
wenzelm@17106
    65
  otherwise the default ordering is used.
kleing@16895
    66
*)
kleing@16895
    67
kleing@16088
    68
kleing@16088
    69
(* matching theorems *)
wenzelm@17106
    70
wenzelm@17205
    71
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
kleing@16088
    72
kleing@16964
    73
(*extract terms from term_src, refine them to the parts that concern us,
kleing@16964
    74
  if po try match them against obj else vice versa.
kleing@16964
    75
  trivial matches are ignored.
kleing@16964
    76
  returns: smallest substitution size*)
kleing@16964
    77
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
kleing@16088
    78
  let
wenzelm@17106
    79
    val thy = ProofContext.theory_of ctxt;
kleing@16088
    80
wenzelm@16486
    81
    fun matches pat =
wenzelm@17106
    82
      is_nontrivial thy pat andalso
wenzelm@17205
    83
      Pattern.matches thy (if po then (pat, obj) else (obj, pat));
kleing@16895
    84
kleing@16895
    85
    fun substsize pat =
wenzelm@18184
    86
      let val (_, subst) =
wenzelm@18184
    87
        Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
wenzelm@17205
    88
      in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
kleing@16088
    89
kleing@16895
    90
    fun bestmatch [] = NONE
wenzelm@17205
    91
     |  bestmatch xs = SOME (foldr1 Int.min xs);
kleing@16895
    92
kleing@16964
    93
    val match_thm = matches o refine_term;
wenzelm@16486
    94
  in
wenzelm@17106
    95
    map (substsize o refine_term)
wenzelm@19482
    96
        (filter match_thm (extract_terms term_src)) |> bestmatch
kleing@16088
    97
  end;
kleing@16088
    98
kleing@16088
    99
wenzelm@16033
   100
(* filter_name *)
wenzelm@16033
   101
wenzelm@17755
   102
fun match_string pat str =
wenzelm@17755
   103
  let
wenzelm@17755
   104
    fun match [] _ = true
wenzelm@17755
   105
      | match (p :: ps) s =
wenzelm@17755
   106
          size p <= size s andalso
wenzelm@17755
   107
            (case try (unprefix p) s of
wenzelm@17755
   108
              SOME s' => match ps s'
wenzelm@17755
   109
            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
wenzelm@17755
   110
  in match (space_explode "*" pat) str end;
wenzelm@16033
   111
wenzelm@17106
   112
fun filter_name str_pat (thmref, _) =
wenzelm@17755
   113
  if match_string str_pat (PureThy.name_of_thmref thmref)
wenzelm@17205
   114
  then SOME (0, 0) else NONE;
wenzelm@16033
   115
wenzelm@16033
   116
wenzelm@16036
   117
(* filter intro/elim/dest rules *)
wenzelm@16033
   118
wenzelm@17205
   119
fun filter_dest ctxt goal (_, thm) =
wenzelm@16033
   120
  let
kleing@16964
   121
    val extract_dest =
wenzelm@17205
   122
     (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
wenzelm@16033
   123
      hd o Logic.strip_imp_prems);
wenzelm@16033
   124
    val prems = Logic.prems_of_goal goal 1;
kleing@16895
   125
kleing@16964
   126
    fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
wenzelm@19482
   127
    val successful = prems |> map_filter try_subst;
wenzelm@16033
   128
  in
kleing@16895
   129
    (*if possible, keep best substitution (one with smallest size)*)
wenzelm@17106
   130
    (*dest rules always have assumptions, so a dest with one
kleing@16895
   131
      assumption is as good as an intro rule with none*)
wenzelm@17205
   132
    if not (null successful)
wenzelm@17205
   133
    then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
wenzelm@16033
   134
  end;
wenzelm@16033
   135
wenzelm@17205
   136
fun filter_intro ctxt goal (_, thm) =
wenzelm@16033
   137
  let
wenzelm@17205
   138
    val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
wenzelm@16036
   139
    val concl = Logic.concl_of_goal goal 1;
kleing@16964
   140
    val ss = is_matching_thm extract_intro ctxt true concl thm;
wenzelm@16033
   141
  in
wenzelm@18939
   142
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
wenzelm@16033
   143
  end;
wenzelm@16033
   144
wenzelm@17205
   145
fun filter_elim ctxt goal (_, thm) =
kleing@16964
   146
  if not (Thm.no_prems thm) then
kleing@16964
   147
    let
wenzelm@17205
   148
      val rule = Thm.full_prop_of thm;
kleing@16964
   149
      val prems = Logic.prems_of_goal goal 1;
kleing@16964
   150
      val goal_concl = Logic.concl_of_goal goal 1;
kleing@16964
   151
      val rule_mp = (hd o Logic.strip_imp_prems) rule;
kleing@16964
   152
      val rule_concl = Logic.strip_imp_concl rule;
kleing@16964
   153
      fun combine t1 t2 = Const ("combine", dummyT --> dummyT) $ (t1 $ t2);
kleing@16964
   154
      val rule_tree = combine rule_mp rule_concl;
kleing@16964
   155
      fun goal_tree prem = (combine prem goal_concl);
wenzelm@17106
   156
      fun try_subst prem =
kleing@16964
   157
        is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
wenzelm@19482
   158
      val successful = prems |> map_filter try_subst;
kleing@16964
   159
    in
wenzelm@17106
   160
    (*elim rules always have assumptions, so an elim with one
kleing@16964
   161
      assumption is as good as an intro rule with none*)
wenzelm@17106
   162
      if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
wenzelm@17205
   163
        andalso not (null successful)
wenzelm@17205
   164
      then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
kleing@16964
   165
    end
kleing@16964
   166
  else NONE
wenzelm@16036
   167
wenzelm@16033
   168
kleing@16074
   169
(* filter_simp *)
wenzelm@16033
   170
wenzelm@17205
   171
fun filter_simp ctxt t (_, thm) =
wenzelm@16033
   172
  let
wenzelm@16033
   173
    val (_, {mk_rews = {mk, ...}, ...}) =
wenzelm@16033
   174
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
wenzelm@17106
   175
    val extract_simp =
wenzelm@17205
   176
      (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
kleing@16964
   177
    val ss = is_matching_thm extract_simp ctxt false t thm
wenzelm@17106
   178
  in
wenzelm@18939
   179
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
kleing@16964
   180
  end;
wenzelm@16033
   181
wenzelm@16033
   182
wenzelm@16033
   183
(* filter_pattern *)
wenzelm@16033
   184
kleing@16088
   185
fun filter_pattern ctxt pat (_, thm) =
wenzelm@17205
   186
  if Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
wenzelm@17205
   187
  then SOME (0, 0) else NONE;
wenzelm@17205
   188
wenzelm@16033
   189
wenzelm@16033
   190
(* interpret criteria as filters *)
wenzelm@16033
   191
wenzelm@16036
   192
local
wenzelm@16036
   193
wenzelm@16036
   194
fun err_no_goal c =
wenzelm@16036
   195
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   196
wenzelm@16033
   197
fun filter_crit _ _ (Name name) = filter_name name
wenzelm@16036
   198
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   199
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   200
  | filter_crit _ NONE Dest = err_no_goal "dest"
wenzelm@16036
   201
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
wenzelm@16036
   202
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
wenzelm@16036
   203
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
kleing@16088
   204
  | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
kleing@16088
   205
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
wenzelm@16036
   206
wenzelm@19502
   207
fun opt_not x = if is_some x then NONE else SOME (0, 0);
kleing@16895
   208
wenzelm@17756
   209
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
wenzelm@17205
   210
 |  opt_add _ _ = NONE;
kleing@16895
   211
wenzelm@16036
   212
in
wenzelm@16033
   213
wenzelm@16033
   214
fun filter_criterion ctxt opt_goal (b, c) =
kleing@16895
   215
  (if b then I else opt_not) o filter_crit ctxt opt_goal c;
kleing@16895
   216
kleing@16895
   217
fun all_filters filters thms =
kleing@16895
   218
  let
kleing@16895
   219
    fun eval_filters filters thm =
wenzelm@17205
   220
      fold opt_add (map (fn f => f thm) filters) (SOME (0, 0));
wenzelm@16033
   221
kleing@16895
   222
    (*filters return: (number of assumptions, substitution size) option, so
kleing@16964
   223
      sort (desc. in both cases) according to number of assumptions first,
kleing@16895
   224
      then by the substitution size*)
wenzelm@17205
   225
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
wenzelm@17205
   226
      prod_ord int_ord int_ord ((p1, s1), (p0, s0));
kleing@16895
   227
  in
wenzelm@17205
   228
    map (`(eval_filters filters)) thms
wenzelm@19482
   229
    |> map_filter (fn (SOME x, y) => SOME (x, y) | (NONE, _) => NONE)
kleing@22340
   230
    |> sort thm_ord |> map #2 
kleing@16895
   231
  end;
wenzelm@16033
   232
wenzelm@16036
   233
end;
wenzelm@16036
   234
wenzelm@16033
   235
kleing@22340
   236
(* removing duplicates, preferring nicer names *)
kleing@22340
   237
kleing@22340
   238
fun rem_thm_dups xs =
kleing@22340
   239
    let 
kleing@22340
   240
      fun nicer (Fact x) (Fact y) = size x <= size y
kleing@22340
   241
        | nicer (Fact _) _        = true
kleing@22340
   242
        | nicer (PureThy.Name x) (PureThy.Name y) = size x <= size y
kleing@22340
   243
        | nicer (PureThy.Name _) (Fact _) = false
kleing@22340
   244
        | nicer (PureThy.Name _) _ = true
kleing@22340
   245
        | nicer (NameSelection (x,_)) (NameSelection (y,_)) = size x <= size y
kleing@22340
   246
        | nicer (NameSelection _) _ = false;
kleing@22340
   247
kleing@22340
   248
      fun is_in [] _ = NONE
kleing@22340
   249
        | is_in ((n,t) :: xs) t' = if eq_thm (t, t') 
kleing@22340
   250
                                   then SOME (n,t) 
kleing@22340
   251
                                   else is_in xs t';
kleing@22340
   252
kleing@22340
   253
      fun eq ((_,t1),(_,t2)) = eq_thm (t1,t2)
kleing@22340
   254
kleing@22340
   255
      fun rem_d (rev_seen, []) = rev rev_seen
kleing@22340
   256
        | rem_d (rev_seen, (x as (n',t')) :: xs) = 
kleing@22340
   257
          case is_in rev_seen t' of 
kleing@22340
   258
            NONE => rem_d (x::rev_seen, xs)
kleing@22340
   259
          | SOME (n,t) => if nicer n n' 
kleing@22340
   260
                          then rem_d (rev_seen, xs) 
kleing@22340
   261
                          else rem_d (x::remove eq (n,t) rev_seen,xs)
kleing@22340
   262
kleing@22340
   263
    in rem_d ([], xs) end;
kleing@22340
   264
kleing@22340
   265
kleing@22340
   266
wenzelm@16033
   267
(* print_theorems *)
wenzelm@16033
   268
wenzelm@17972
   269
fun find_thms ctxt spec =
wenzelm@18325
   270
  (PureThy.thms_containing (ProofContext.theory_of ctxt) spec
wenzelm@19482
   271
    |> maps PureThy.selections) @
wenzelm@18325
   272
  (ProofContext.lthms_containing ctxt spec
wenzelm@19482
   273
    |> maps PureThy.selections
wenzelm@19046
   274
    |> distinct (fn ((r1, th1), (r2, th2)) =>
wenzelm@18325
   275
        r1 = r2 andalso Drule.eq_thm_prop (th1, th2)));
wenzelm@17972
   276
kleing@22340
   277
fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
wenzelm@16033
   278
  let
wenzelm@16036
   279
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
wenzelm@16036
   280
    val filters = map (filter_criterion ctxt opt_goal) criteria;
wenzelm@16033
   281
kleing@22340
   282
    val raw_matches = all_filters filters (find_thms ctxt ([], []));
kleing@22340
   283
    val matches = if rem_dups then rem_thm_dups raw_matches else raw_matches;
kleing@22340
   284
wenzelm@16033
   285
    val len = length matches;
wenzelm@19476
   286
    val limit = the_default (! thms_containing_limit) opt_limit;
kleing@22340
   287
    val thms = Library.drop (len - limit, matches);
wenzelm@16033
   288
wenzelm@16036
   289
    fun prt_fact (thmref, thm) =
wenzelm@16036
   290
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
wenzelm@16033
   291
  in
wenzelm@16036
   292
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
kleing@22340
   293
     (if null thms then [Pretty.str "nothing found"]
wenzelm@16033
   294
      else
wenzelm@16036
   295
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
wenzelm@16036
   296
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
wenzelm@16036
   297
         Pretty.str ""] @
kleing@22340
   298
        map prt_fact thms)
wenzelm@16033
   299
    |> Pretty.chunks |> Pretty.writeln
wenzelm@16033
   300
  end;
wenzelm@16033
   301
wenzelm@16033
   302
end;