src/Pure/Isar/find_theorems.ML
author Timothy Bourke
Mon Feb 09 17:25:07 2009 +1100 (2009-02-09)
changeset 29848 a7c164e228e1
parent 29302 eb782d1dc07c
child 29857 2cc976ed8a3c
permissions -rw-r--r--
Nicer names in FindTheorems.
* Patch NameSpace.get_accesses, contributed by Timothy Bourke:

NameSpace.get_accesses has been patched to fix the following
bug:
theory OverHOL imports Main begin
lemma conjI: "a & b --> b"
by blast

ML {* val ns = PureThy.facts_of @{theory} |> Facts.space_of;
val x1 = NameSpace.get_accesses ns "HOL.conjI";
val x2 = NameSpace.get_accesses ns "OverHOL.conjI"; *}
end

where x1 = ["conjI", "HOL.conjI"] and x2 = ["conjI", "OverHOL.conjI"],
but x1 should be just ["HOL.conjI"].

NameSpace.get_accesses is only used within the NameSpace
structure itself. The two uses have been modified to retain
their original behaviour.

Note that NameSpace.valid_accesses gives different results:
get_accesses ns "HOL.eq_class.eq"
gives ["eq", "eq_class.eq", "HOL.eq_class.eq"]
but,
valid_accesses ns "HOL.eq_class.eq"
gives ["HOL.eq_class.eq", "eq_class.eq", "HOL.eq", "eq"]

* Patch FindTheorems:
Prefer names that are shorter to type in the current context.

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