src/Pure/Isar/find_theorems.ML
author wenzelm
Mon, 06 Feb 2006 20:59:06 +0100
changeset 18939 18e2a2676d80
parent 18325 2d504ea54e5b
child 19046 bc5c6c9b114e
permissions -rw-r--r--
tuned;
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
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     3
    Author:     Rafal Kolanski, NICTA and Tobias Nipkow, TU Muenchen
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     4
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     5
Retrieve theorems from proof context.
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
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     8
val thms_containing_limit = ref 40;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
     9
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    10
signature FIND_THEOREMS =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    11
sig
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    12
  datatype 'term criterion =
16074
9e569163ba8c renamed search criterion 'rewrite' to 'simp'
kleing
parents: 16071
diff changeset
    13
    Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    14
  val print_theorems: Proof.context -> term option -> int option ->
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    15
    (bool * string criterion) list -> unit
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    16
end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    17
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    18
structure FindTheorems: FIND_THEOREMS =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    19
struct
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    20
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    21
(** search criteria **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    22
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    23
datatype 'term criterion =
16074
9e569163ba8c renamed search criterion 'rewrite' to 'simp'
kleing
parents: 16071
diff changeset
    24
  Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    25
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    26
fun read_criterion _ (Name name) = Name name
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    27
  | read_criterion _ Intro = Intro
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    28
  | read_criterion _ Elim = Elim
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    29
  | read_criterion _ Dest = Dest
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16088
diff changeset
    30
  | read_criterion ctxt (Simp str) =
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    31
      Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]))
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    32
  | read_criterion ctxt (Pattern str) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    33
      Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    34
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    35
fun pretty_criterion ctxt (b, c) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    36
  let
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    37
    fun prfx s = if b then s else "-" ^ s;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    38
  in
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    39
    (case c of
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    40
      Name name => Pretty.str (prfx "name: " ^ quote name)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    41
    | Intro => Pretty.str (prfx "intro")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    42
    | Elim => Pretty.str (prfx "elim")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    43
    | Dest => Pretty.str (prfx "dest")
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    44
    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    45
        Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))]
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    46
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    47
        [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    48
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    49
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    50
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    51
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    52
(** search criterion filters **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    53
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    54
(*generated filters are to be of the form
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    55
  input: (thmref * thm)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    56
  output: (p:int, s:int) option, where
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    57
    NONE indicates no match
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    58
    p is the primary sorting criterion
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    59
      (eg. number of assumptions in the theorem)
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    60
    s is the secondary sorting criterion
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    61
      (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
    62
  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
    63
    (biggest p, sum of all s)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    64
  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
    65
  otherwise the default ordering is used.
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    66
*)
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    67
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    68
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    69
(* matching theorems *)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    70
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    71
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
    72
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    73
(*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
    74
  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
    75
  trivial matches are ignored.
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    76
  returns: smallest substitution size*)
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    77
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    78
  let
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    79
    val thy = ProofContext.theory_of ctxt;
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    80
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16088
diff changeset
    81
    fun matches pat =
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    82
      is_nontrivial thy pat andalso
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    83
      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
    84
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    85
    fun substsize pat =
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17972
diff changeset
    86
      let val (_, subst) =
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17972
diff changeset
    87
        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
    88
      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
    89
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    90
    fun bestmatch [] = NONE
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
    91
     |  bestmatch xs = SOME (foldr1 Int.min xs);
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
    92
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    93
    val match_thm = matches o refine_term;
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16088
diff changeset
    94
  in
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
    95
    map (substsize o refine_term)
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
    96
        (List.filter match_thm (extract_terms term_src)) |> bestmatch
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    97
  end;
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    98
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
    99
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   100
(* filter_name *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   101
17755
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   102
fun match_string pat str =
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   103
  let
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   104
    fun match [] _ = true
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   105
      | match (p :: ps) s =
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   106
          size p <= size s andalso
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   107
            (case try (unprefix p) s of
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   108
              SOME s' => match ps s'
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   109
            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   110
  in match (space_explode "*" pat) str end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   111
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   112
fun filter_name str_pat (thmref, _) =
17755
b0cd55afead1 find_theorems: support * wildcard in name: criterion;
wenzelm
parents: 17205
diff changeset
   113
  if match_string str_pat (PureThy.name_of_thmref thmref)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   114
  then SOME (0, 0) else NONE;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   115
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   116
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   117
(* filter intro/elim/dest rules *)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   118
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   119
fun filter_dest ctxt goal (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   120
  let
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   121
    val extract_dest =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   122
     (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
   123
      hd o Logic.strip_imp_prems);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   124
    val prems = Logic.prems_of_goal goal 1;
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   125
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   126
    fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   127
    val successful = prems |> List.mapPartial try_subst;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   128
  in
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   129
    (*if possible, keep best substitution (one with smallest size)*)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   130
    (*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
   131
      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
   132
    if not (null successful)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   133
    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
   134
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   135
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   136
fun filter_intro ctxt goal (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   137
  let
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   138
    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
   139
    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
   140
    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
   141
  in
18939
wenzelm
parents: 18325
diff changeset
   142
    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
   143
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   144
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   145
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
   146
  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
   147
    let
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   148
      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
   149
      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
   150
      val goal_concl = Logic.concl_of_goal goal 1;
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   151
      val rule_mp = (hd o Logic.strip_imp_prems) rule;
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   152
      val rule_concl = Logic.strip_imp_concl rule;
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   153
      fun combine t1 t2 = Const ("combine", dummyT --> dummyT) $ (t1 $ t2);
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   154
      val rule_tree = combine rule_mp rule_concl;
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   155
      fun goal_tree prem = (combine prem goal_concl);
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   156
      fun try_subst prem =
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   157
        is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   158
      val successful = prems |> List.mapPartial try_subst;
16964
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   159
    in
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   160
    (*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
   161
      assumption is as good as an intro rule with none*)
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   162
      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
   163
        andalso not (null successful)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   164
      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
   165
    end
6a25e42eaff5 Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents: 16895
diff changeset
   166
  else NONE
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   167
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   168
16074
9e569163ba8c renamed search criterion 'rewrite' to 'simp'
kleing
parents: 16071
diff changeset
   169
(* filter_simp *)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   170
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   171
fun filter_simp ctxt t (_, thm) =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   172
  let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   173
    val (_, {mk_rews = {mk, ...}, ...}) =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   174
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
17106
2bd6c20cdda1 use theory instead of obsolete Sign.sg;
wenzelm
parents: 16964
diff changeset
   175
    val extract_simp =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   176
      (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
   177
    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
   178
  in
18939
wenzelm
parents: 18325
diff changeset
   179
    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
   180
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   181
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   182
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   183
(* filter_pattern *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   184
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   185
fun filter_pattern ctxt pat (_, thm) =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   186
  if Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   187
  then SOME (0, 0) else NONE;
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   188
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   189
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   190
(* interpret criteria as filters *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   191
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   192
local
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   193
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   194
fun err_no_goal c =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   195
  error ("Current goal required for " ^ c ^ " search criterion");
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   196
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   197
fun filter_crit _ _ (Name name) = filter_name name
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   198
  | filter_crit _ NONE Intro = err_no_goal "intro"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   199
  | filter_crit _ NONE Elim = err_no_goal "elim"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   200
  | filter_crit _ NONE Dest = err_no_goal "dest"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   201
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   202
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   203
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
16088
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   204
  | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
f084ba24de29 cleaned up select_match
kleing
parents: 16077
diff changeset
   205
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   206
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   207
fun opt_not x = if isSome x then NONE else SOME (0, 0);
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   208
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17755
diff changeset
   209
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   210
 |  opt_add _ _ = NONE;
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   211
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   212
in
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   213
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   214
fun filter_criterion ctxt opt_goal (b, c) =
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   215
  (if b then I else opt_not) o filter_crit ctxt opt_goal c;
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   216
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   217
fun all_filters filters thms =
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   218
  let
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   219
    fun eval_filters filters thm =
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   220
      fold opt_add (map (fn f => f thm) filters) (SOME (0, 0));
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   221
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   222
    (*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
   223
      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
   224
      then by the substitution size*)
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   225
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   226
      prod_ord int_ord int_ord ((p1, s1), (p0, s0));
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   227
  in
17205
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   228
    map (`(eval_filters filters)) thms
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   229
    |> List.mapPartial (fn (SOME x, y) => SOME (x, y) | (NONE, _) => NONE)
8994750ae33c refer to theory instead of low-level tsig;
wenzelm
parents: 17106
diff changeset
   230
    |> sort thm_ord |> map #2
16895
df67fc190e06 Sort search results in order of relevance, where relevance =
kleing
parents: 16486
diff changeset
   231
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   232
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   233
end;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   234
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   235
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   236
(* print_theorems *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   237
17972
4969d6eb4c97 do not export find_thms;
wenzelm
parents: 17789
diff changeset
   238
fun find_thms ctxt spec =
18325
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   239
  (PureThy.thms_containing (ProofContext.theory_of ctxt) spec
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   240
    |> map PureThy.selections
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   241
    |> List.concat) @
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   242
  (ProofContext.lthms_containing ctxt spec
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   243
    |> map PureThy.selections
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   244
    |> List.concat
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   245
    |> gen_distinct (fn ((r1, th1), (r2, th2)) =>
2d504ea54e5b ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents: 18184
diff changeset
   246
        r1 = r2 andalso Drule.eq_thm_prop (th1, th2)));
17972
4969d6eb4c97 do not export find_thms;
wenzelm
parents: 17789
diff changeset
   247
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   248
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   249
  let
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   250
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   251
    val filters = map (filter_criterion ctxt opt_goal) criteria;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   252
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   253
    val matches = all_filters filters (find_thms ctxt ([], []));
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   254
    val len = length matches;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   255
    val limit = if_none opt_limit (! thms_containing_limit);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   256
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   257
    fun prt_fact (thmref, thm) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   258
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   259
  in
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   260
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   261
     (if null matches then [Pretty.str "nothing found"]
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   262
      else
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   263
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   264
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   265
         Pretty.str ""] @
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   266
        map prt_fact (Library.drop (len - limit, matches)))
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   267
    |> Pretty.chunks |> Pretty.writeln
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   268
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   269
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   270
end;