src/Pure/Isar/find_theorems.ML
author kleing
Wed, 11 Feb 2009 14:48:14 +1100
changeset 29857 2cc976ed8a3c
parent 29848 a7c164e228e1
child 29858 c8cee17d7e50
permissions -rw-r--r--
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/find_theorems.ML
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
     2
    Author:     Rafal Kolanski and Gerwin Klein, NICTA
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     3
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     4
Retrieve theorems from proof context.
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     5
*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     6
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     7
signature FIND_THEOREMS =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     8
sig
25992
928594f50893 renamed thms_containing_limit to FindTheorems.limit;
wenzelm
parents: 25226
diff changeset
     9
  val limit: int ref
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    10
  val tac_limit: int ref
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    11
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    12
  datatype 'term criterion =
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    13
    Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    14
    Pattern of 'term
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    15
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    16
  val find_theorems: Proof.context -> thm option -> bool ->
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    17
    (bool * string criterion) list -> (Facts.ref * thm) list
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    18
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    19
  val print_theorems: Proof.context -> thm option -> int option -> bool ->
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    20
    (bool * string criterion) list -> unit
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    21
end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    22
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    23
structure FindTheorems: FIND_THEOREMS =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    24
struct
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    25
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    26
(** search criteria **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    27
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    28
datatype 'term criterion =
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    29
  Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    30
  Pattern of 'term;
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    31
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    32
fun read_criterion _ (Name name) = Name name
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    33
  | read_criterion _ Intro = Intro
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    34
  | read_criterion _ Elim = Elim
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    35
  | read_criterion _ Dest = Dest
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    36
  | read_criterion _ Solves = Solves
24683
c62320337a4e ProofContext.read_term_pattern;
wenzelm
parents: 22709
diff changeset
    37
  | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str)
c62320337a4e ProofContext.read_term_pattern;
wenzelm
parents: 22709
diff changeset
    38
  | read_criterion ctxt (Pattern str) = Pattern (ProofContext.read_term_pattern ctxt str);
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    39
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    40
fun pretty_criterion ctxt (b, c) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    41
  let
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    42
    fun prfx s = if b then s else "-" ^ s;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    43
  in
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    44
    (case c of
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    45
      Name name => Pretty.str (prfx "name: " ^ quote name)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    46
    | Intro => Pretty.str (prfx "intro")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    47
    | Elim => Pretty.str (prfx "elim")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    48
    | Dest => Pretty.str (prfx "dest")
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
    49
    | Solves => Pretty.str (prfx "solves")
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    50
    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24683
diff changeset
    51
        Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))]
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    52
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24683
diff changeset
    53
        [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)])
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    54
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    55
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    56
(** search criterion filters **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    57
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    58
(*generated filters are to be of the form
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
    59
  input: (Facts.ref * thm)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    60
  output: (p:int, s:int) option, where
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    61
    NONE indicates no match
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    62
    p is the primary sorting criterion
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    63
      (eg. number of assumptions in the theorem)
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    64
    s is the secondary sorting criterion
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    65
      (eg. size of the substitution for intro, elim and dest)
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    66
  when applying a set of filters to a thm, fold results in:
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    67
    (biggest p, sum of all s)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    68
  currently p and s only matter for intro, elim, dest and simp filters,
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    69
  otherwise the default ordering is used.
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    70
*)
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    71
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    72
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    73
(* matching theorems *)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    74
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    75
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    76
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    77
(*extract terms from term_src, refine them to the parts that concern us,
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    78
  if po try match them against obj else vice versa.
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    79
  trivial matches are ignored.
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    80
  returns: smallest substitution size*)
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    81
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    82
  let
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    83
    val thy = ProofContext.theory_of ctxt;
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    84
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16088
diff changeset
    85
    fun matches pat =
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    86
      is_nontrivial thy pat andalso
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    87
      Pattern.matches thy (if po then (pat, obj) else (obj, pat));
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    88
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    89
    fun substsize pat =
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17972
diff changeset
    90
      let val (_, subst) =
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17972
diff changeset
    91
        Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    92
      in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    93
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    94
    fun bestmatch [] = NONE
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    95
     |  bestmatch xs = SOME (foldr1 Int.min xs);
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    96
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    97
    val match_thm = matches o refine_term;
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16088
diff changeset
    98
  in
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
    99
    map (substsize o refine_term) (filter match_thm (extract_terms term_src))
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   100
    |> bestmatch
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   101
  end;
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   102
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   103
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   104
(* filter_name *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   105
17755
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   106
fun match_string pat str =
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   107
  let
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   108
    fun match [] _ = true
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   109
      | match (p :: ps) s =
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   110
          size p <= size s andalso
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   111
            (case try (unprefix p) s of
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   112
              SOME s' => match ps s'
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   113
            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   114
  in match (space_explode "*" pat) str end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   115
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   116
fun filter_name str_pat (thmref, _) =
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   117
  if match_string str_pat (Facts.name_of_ref thmref)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   118
  then SOME (0, 0) else NONE;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   119
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   120
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   121
(* filter intro/elim/dest/solves rules *)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   122
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   123
fun filter_dest ctxt goal (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   124
  let
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   125
    val extract_dest =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   126
     (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   127
      hd o Logic.strip_imp_prems);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   128
    val prems = Logic.prems_of_goal goal 1;
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   129
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   130
    fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19476
diff changeset
   131
    val successful = prems |> map_filter try_subst;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   132
  in
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   133
    (*if possible, keep best substitution (one with smallest size)*)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   134
    (*dest rules always have assumptions, so a dest with one
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   135
      assumption is as good as an intro rule with none*)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   136
    if not (null successful)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   137
    then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   138
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   139
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   140
fun filter_intro ctxt goal (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   141
  let
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   142
    val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   143
    val concl = Logic.concl_of_goal goal 1;
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   144
    val ss = is_matching_thm extract_intro ctxt true concl thm;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   145
  in
18939
wenzelm
parents: 18325
diff changeset
   146
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   147
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   148
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   149
fun filter_elim ctxt goal (_, thm) =
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   150
  if not (Thm.no_prems thm) then
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   151
    let
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   152
      val rule = Thm.full_prop_of thm;
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   153
      val prems = Logic.prems_of_goal goal 1;
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   154
      val goal_concl = Logic.concl_of_goal goal 1;
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   155
      val rule_mp = hd (Logic.strip_imp_prems rule);
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   156
      val rule_concl = Logic.strip_imp_concl rule;
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   157
      fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) $ (t1 $ t2);
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   158
      val rule_tree = combine rule_mp rule_concl;
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   159
      fun goal_tree prem = combine prem goal_concl;
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   160
      fun try_subst prem =
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   161
        is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19476
diff changeset
   162
      val successful = prems |> map_filter try_subst;
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   163
    in
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   164
    (*elim rules always have assumptions, so an elim with one
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   165
      assumption is as good as an intro rule with none*)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   166
      if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   167
        andalso not (null successful)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   168
      then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   169
    end
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   170
  else NONE
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   171
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   172
val tac_limit = ref 5;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   173
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   174
fun filter_solves ctxt goal = let
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   175
    val baregoal = Logic.get_goal (prop_of goal) 1;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   176
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   177
    fun etacn thm i = Seq.take (!tac_limit) o etac thm i;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   178
    fun try_thm thm = if Thm.no_prems thm then rtac thm 1 goal
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   179
                      else (etacn thm THEN_ALL_NEW
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   180
                             (Goal.norm_hhf_tac THEN'
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   181
                               Method.assumption_tac ctxt)) 1 goal;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   182
  in
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   183
    fn (_, thm) => if (is_some o Seq.pull o try_thm) thm
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   184
                   then SOME (Thm.nprems_of thm, 0) else NONE
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   185
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   186
16074
9e569163ba8c renamed search criterion 'rewrite' to 'simp'
kleing
parents: 16071
diff changeset
   187
(* filter_simp *)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   188
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   189
fun filter_simp ctxt t (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   190
  let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   191
    val (_, {mk_rews = {mk, ...}, ...}) =
29302
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29274
diff changeset
   192
      Simplifier.rep_ss (Simplifier.local_simpset_of ctxt);
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   193
    val extract_simp =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   194
      (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   195
    val ss = is_matching_thm extract_simp ctxt false t thm
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   196
  in
18939
wenzelm
parents: 18325
diff changeset
   197
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   198
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   199
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   200
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   201
(* filter_pattern *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   202
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   203
fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []);
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   204
fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   205
  (* Including all constants and frees is only sound because
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   206
     matching uses higher-order patterns. If full matching
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   207
     were used, then constants that may be subject to
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   208
     beta-reduction after substitution of frees should
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   209
     not be included for LHS set because they could be
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   210
     thrown away by the substituted function.
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   211
     e.g. for (?F 1 2) do not include 1 or 2, if it were
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   212
          possible for ?F to be (% x y. 3)
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   213
     The largest possible set should always be included on
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   214
     the RHS. *)
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   215
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   216
fun filter_pattern ctxt pat = let
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   217
    val pat_consts = get_names pat;
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   218
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   219
    fun check (t, NONE) = check (t, SOME (get_thm_names t))
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   220
      | check ((_, thm), c as SOME thm_consts) =
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   221
          (if pat_consts subset_string thm_consts
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   222
              andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt)
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   223
                                               (pat, Thm.full_prop_of thm))
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   224
           then SOME (0, 0) else NONE, c);
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   225
  in check end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   226
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   227
(* interpret criteria as filters *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   228
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   229
local
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   230
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   231
fun err_no_goal c =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   232
  error ("Current goal required for " ^ c ^ " search criterion");
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   233
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   234
val fix_goal = Thm.prop_of;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   235
val fix_goalo = Option.map fix_goal;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   236
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   237
fun filter_crit _ _ (Name name) = apfst (filter_name name)
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   238
  | filter_crit _ NONE Intro = err_no_goal "intro"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   239
  | filter_crit _ NONE Elim = err_no_goal "elim"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   240
  | filter_crit _ NONE Dest = err_no_goal "dest"
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   241
  | filter_crit _ NONE Solves = err_no_goal "solves"
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   242
  | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   243
                                                  (fix_goal goal))
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   244
  | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt 
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   245
                                                  (fix_goal goal))
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   246
  | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   247
                                                  (fix_goal goal))
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   248
  | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   249
  | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   250
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   251
19502
wenzelm
parents: 19482
diff changeset
   252
fun opt_not x = if is_some x then NONE else SOME (0, 0);
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   253
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17755
diff changeset
   254
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   255
  | opt_add _ _ = NONE;
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   256
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   257
fun app_filters thm = let
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   258
    fun app (NONE, _, _) = NONE
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   259
      | app (SOME v, consts, []) = SOME (v, thm)
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   260
      | app (r, consts, f::fs) = let val (r', consts') = f (thm, consts)
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   261
                                 in app (opt_add r r', consts', fs) end;
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   262
  in app end;
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   263
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   264
in
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   265
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   266
fun filter_criterion ctxt opt_goal (b, c) =
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   267
  (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   268
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   269
fun all_filters filters thms =
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   270
  let
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   271
    fun eval_filters thm = app_filters thm (SOME (0, 0), NONE, filters);
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   272
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   273
    (*filters return: (number of assumptions, substitution size) option, so
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   274
      sort (desc. in both cases) according to number of assumptions first,
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   275
      then by the substitution size*)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   276
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   277
      prod_ord int_ord int_ord ((p1, s1), (p0, s0));
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   278
  in map_filter eval_filters thms |> sort thm_ord |> map #2 end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   279
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   280
end;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   281
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   282
22414
3f189ea9bfe7 an O(n log n) version of removing duplicates
kleing
parents: 22360
diff changeset
   283
(* removing duplicates, preferring nicer names, roughly n log n *)
22340
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   284
25226
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   285
local
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   286
27486
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   287
val index_ord = option_ord (K EQUAL);
25226
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   288
val hidden_ord = bool_ord o pairself NameSpace.is_hidden;
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   289
val qual_ord = int_ord o pairself (length o NameSpace.explode);
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   290
val txt_ord = int_ord o pairself size;
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   291
27486
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   292
fun nicer_name (x, i) (y, j) =
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   293
  (case hidden_ord (x, y) of EQUAL =>
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   294
    (case index_ord (i, j) of EQUAL =>
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   295
      (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
c61507a98bff prefer theorem names without numbers
huffman
parents: 27173
diff changeset
   296
    | ord => ord)
25226
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   297
  | ord => ord) <> GREATER;
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   298
29848
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   299
fun rem_cdups nicer xs =
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   300
  let
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   301
    fun rem_c rev_seen [] = rev rev_seen
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   302
      | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   303
      | rem_c rev_seen ((x as ((n, t), _)) :: (y as ((n', t'), _)) :: xs) =
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   304
        if Thm.eq_thm_prop (t, t')
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   305
        then rem_c rev_seen ((if nicer n n' then x else y) :: xs)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   306
        else rem_c (x :: rev_seen) (y :: xs)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   307
  in rem_c [] xs end;
25226
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   308
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   309
in
25226
502d8676cdd6 improved notion of 'nicer' fact names (observe some name space properties);
wenzelm
parents: 24920
diff changeset
   310
29848
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   311
fun nicer_shortest ctxt = let
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   312
    val ns = ProofContext.theory_of ctxt
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   313
             |> PureThy.facts_of
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   314
             |> Facts.space_of;
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   315
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   316
    val len_sort = sort (int_ord o (pairself size));
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   317
    fun shorten s = (case len_sort (NameSpace.get_accesses ns s) of
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   318
                       [] => s
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   319
                     | s'::_ => s');
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   320
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   321
    fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   322
          nicer_name (shorten x, i) (shorten y, j)
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   323
      | nicer (Facts.Fact _) (Facts.Named _) = true
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   324
      | nicer (Facts.Named _) (Facts.Fact _) = false;
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   325
  in nicer end;
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   326
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   327
fun rem_thm_dups nicer xs =
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   328
  xs ~~ (1 upto length xs)
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 28900
diff changeset
   329
  |> sort (TermOrd.fast_term_ord o pairself (Thm.prop_of o #2 o #1))
29848
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   330
  |> rem_cdups nicer
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   331
  |> sort (int_ord o pairself #2)
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   332
  |> map #1;
22340
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   333
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   334
end;
22340
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   335
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   336
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   337
(* print_theorems *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   338
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   339
fun all_facts_of ctxt =
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26283
diff changeset
   340
  maps Facts.selections
27173
9ae98c3cd3d6 Facts.dest/export_static: content difference;
wenzelm
parents: 26690
diff changeset
   341
   (Facts.dest_static [] (PureThy.facts_of (ProofContext.theory_of ctxt)) @
9ae98c3cd3d6 Facts.dest/export_static: content difference;
wenzelm
parents: 26690
diff changeset
   342
    Facts.dest_static [] (ProofContext.facts_of ctxt));
17972
4969d6eb4c97 do not export find_thms;
wenzelm
parents: 17789
diff changeset
   343
25992
928594f50893 renamed thms_containing_limit to FindTheorems.limit;
wenzelm
parents: 25226
diff changeset
   344
val limit = ref 40;
928594f50893 renamed thms_containing_limit to FindTheorems.limit;
wenzelm
parents: 25226
diff changeset
   345
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   346
fun find_theorems ctxt opt_goal rem_dups raw_criteria =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   347
  let
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   348
    val add_prems = Seq.hd o (TRY (Method.insert_tac
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   349
                                     (Assumption.prems_of ctxt) 1));
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   350
    val opt_goal' = Option.map add_prems opt_goal;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   351
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   352
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   353
    val filters = map (filter_criterion ctxt opt_goal') criteria;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   354
26283
e19a5a7e83f1 more precise Author line;
wenzelm
parents: 25992
diff changeset
   355
    val raw_matches = all_filters filters (all_facts_of ctxt);
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   356
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22343
diff changeset
   357
    val matches =
22414
3f189ea9bfe7 an O(n log n) version of removing duplicates
kleing
parents: 22360
diff changeset
   358
      if rem_dups
29848
a7c164e228e1 Nicer names in FindTheorems.
Timothy Bourke
parents: 29302
diff changeset
   359
      then rem_thm_dups (nicer_shortest ctxt) raw_matches
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22343
diff changeset
   360
      else raw_matches;
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   361
  in matches end;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   362
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   363
fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria = let
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   364
    val start = start_timing ();
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   365
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   366
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   367
    val matches = find_theorems ctxt opt_goal rem_dups raw_criteria;
22340
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   368
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   369
    val len = length matches;
25992
928594f50893 renamed thms_containing_limit to FindTheorems.limit;
wenzelm
parents: 25226
diff changeset
   370
    val lim = the_default (! limit) opt_limit;
928594f50893 renamed thms_containing_limit to FindTheorems.limit;
wenzelm
parents: 25226
diff changeset
   371
    val thms = Library.drop (len - lim, matches);
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   372
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   373
    fun prt_fact (thmref, thm) = Pretty.block
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   374
      [Pretty.str (Facts.string_of_ref thmref), Pretty.str ":", Pretty.brk 1,
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   375
        ProofContext.pretty_thm ctxt thm];
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   376
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   377
    val end_msg = " in " ^
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   378
                  (List.nth (String.tokens Char.isSpace (end_timing start), 3))
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   379
                  ^ " secs"
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   380
  in
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   381
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria)
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   382
        :: Pretty.str "" ::
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   383
     (if null thms then [Pretty.str ("nothing found" ^ end_msg)]
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   384
      else
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   385
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
28900
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   386
          (if len <= lim then ""
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   387
           else " (" ^ string_of_int lim ^ " displayed)")
53fd5cc685b4 FindTheorems performance improvements (from Timothy Bourke)
kleing
parents: 28211
diff changeset
   388
           ^ end_msg ^ ":"), Pretty.str ""] @
22340
275802767bf3 Remove duplicates from printed theorems in find_theorems
kleing
parents: 19502
diff changeset
   389
        map prt_fact thms)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   390
    |> Pretty.chunks |> Pretty.writeln
29857
2cc976ed8a3c FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents: 29848
diff changeset
   391
  end
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   392
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   393
end;