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