src/Pure/Isar/find_theorems.ML
author wenzelm
Sun, 22 May 2005 19:26:16 +0200
changeset 16036 1da07ac33711
parent 16033 f93ca3d4ffa7
child 16071 e0136cdef722
permissions -rw-r--r--
added read_criterion/pretty_criterion; improved rule filters -- intro excludes elim rules; tuned output; 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
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    12
  val find_thms: Proof.context -> FactIndex.spec -> (thmref * thm) list
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    13
  val find_intros: Proof.context -> term -> (thmref * thm) list
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    14
  val find_intros_goal: Proof.context -> thm -> int -> (thmref * thm) list
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    15
  val find_elims: Proof.context -> term -> (thmref * thm) list
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    16
  datatype 'term criterion =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    17
    Name of string | Intro | Elim | Dest | Rewrite of 'term | Pattern of 'term
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    18
  val print_theorems: Proof.context -> term option -> int option ->
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
    19
    (bool * string criterion) list -> unit
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    20
end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    21
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    22
structure FindTheorems: FIND_THEOREMS =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    23
struct
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    24
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
(* find_thms *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    27
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    28
fun find_thms ctxt spec =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    29
  (PureThy.thms_containing (ProofContext.theory_of ctxt) spec @
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    30
    ProofContext.lthms_containing ctxt spec)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    31
  |> map PureThy.selections |> List.concat;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    32
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    33
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    34
(* matching theorems *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    35
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    36
local
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    37
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    38
(*returns all those facts whose subterm extracted by extract can be
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    39
  instantiated to obj; the list is sorted according to the number of premises
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    40
  and the size of the required substitution*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    41
fun select_match (extract_thms, extract_term) ctxt obj facts =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    42
  let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    43
    val match = #2 o Pattern.match (Sign.tsig_of (ProofContext.sign_of ctxt));
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    44
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    45
    fun match_size pat =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    46
      let val subst = match (pat, obj)  (*exception Pattern.MATCH*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    47
      in SOME (Vartab.foldl (op + o apsnd (Term.size_of_term o #2 o #2)) (0, subst)) end
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    48
      handle Pattern.MATCH => NONE;
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
    fun select (fact as (_, thm)) =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    51
      let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    52
        fun first_match (th :: ths) res =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    53
              (case match_size (extract_term (Thm.prop_of th)) of
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    54
                SOME s => ((Thm.nprems_of th, s), fact) :: res
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    55
              | NONE => first_match ths res)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    56
          | first_match [] res = res;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    57
      in first_match (extract_thms thm) end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    58
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    59
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    60
      prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0, s0), (p1, s1));
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    61
  in
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    62
    fold select facts []
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    63
    |> Library.sort thm_ord |> map #2
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    64
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    65
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    66
(*speed up retrieval by checking head symbol*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    67
fun index_head ctxt prop =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    68
  (case Term.head_of (ObjectLogic.drop_judgment (ProofContext.sign_of ctxt) prop) of
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    69
    Const (c, _) => ([c], [])
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    70
  | Free (x, _) => if ProofContext.is_known ctxt x then ([], [x]) else ([], [])
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    71
  | _ => ([], []));
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    72
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    73
in
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    74
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    75
fun find_matching_thms extract ctxt prop =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    76
  find_thms ctxt (index_head ctxt prop)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    77
  |> select_match extract ctxt prop;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    78
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    79
fun is_matching_thm extract ctxt prop fact =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    80
  not (null (select_match extract ctxt prop [fact]));
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    81
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    82
end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    83
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    84
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    85
(* intro/elim rules *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    86
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    87
(*match statement against conclusion of some rule*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    88
val find_intros =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    89
  find_matching_thms (single, Logic.strip_imp_concl);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    90
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    91
(*match conclusion of subgoal i against conclusion of some rule*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    92
fun find_intros_goal ctxt st i =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    93
  find_intros ctxt (Logic.concl_of_goal (Thm.prop_of st) i);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    94
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    95
(*match statement against major premise of some rule*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    96
val find_elims = find_matching_thms
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    97
  (fn thm => if Thm.no_prems thm then [] else [thm],
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    98
   hd o Logic.strip_imp_prems);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
    99
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   100
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   101
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   102
(** search criteria **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   103
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   104
datatype 'term criterion =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   105
  Name of string | Intro | Elim | Dest | Rewrite of 'term | Pattern of 'term;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   106
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   107
fun read_criterion _ (Name name) = Name name
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   108
  | read_criterion _ Intro = Intro
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   109
  | read_criterion _ Elim = Elim
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   110
  | read_criterion _ Dest = Dest
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   111
  | read_criterion ctxt (Rewrite str) = Rewrite (ProofContext.read_term ctxt str)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   112
  | read_criterion ctxt (Pattern str) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   113
      Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   114
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   115
fun pretty_criterion ctxt (b, c) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   116
  let
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   117
    fun prfx s = if b then s else "-" ^ s;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   118
  in
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   119
    (case c of
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   120
      Name name => Pretty.str (prfx "name: " ^ quote name)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   121
    | Intro => Pretty.str (prfx "intro")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   122
    | Elim => Pretty.str (prfx "elim")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   123
    | Dest => Pretty.str (prfx "dest")
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   124
    | Rewrite t => Pretty.block [Pretty.str (prfx "rewrite:"), Pretty.brk 1,
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   125
        Pretty.quote (ProofContext.pretty_term ctxt t)]
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   126
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   127
        [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   128
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   129
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   130
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   131
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   132
(** search criterion filters **)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   133
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   134
(* filter_name *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   135
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   136
fun is_substring pat str =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   137
  if String.size pat = 0 then true
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   138
  else if String.size pat > String.size str then false
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   139
  else if String.substring (str, 0, String.size pat) = pat then true
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   140
  else is_substring pat (String.extract (str, 1, NONE));
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   141
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   142
(*filter that just looks for a string in the name,
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   143
  substring match only (no regexps are performed)*)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   144
fun filter_name str_pat ((name, _), _) = is_substring str_pat name;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   145
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   146
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   147
(* filter intro/elim/dest rules *)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   148
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   149
local
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   150
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   151
(*elimination rule: conclusion is a Var which does not appear in the major premise*)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   152
fun is_elim ctxt thm =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   153
  let
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   154
    val sg = ProofContext.sign_of ctxt;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   155
    val prop = Thm.prop_of thm;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   156
    val concl = ObjectLogic.drop_judgment sg (Logic.strip_imp_concl prop);
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   157
    val major_prem = Library.take (1, Logic.strip_imp_prems prop);
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   158
    val prem_vars = Drule.vars_of_terms major_prem;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   159
  in
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   160
    not (null major_prem) andalso
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   161
    Term.is_Var concl andalso
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   162
    not (Term.dest_Var concl mem prem_vars)
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   163
  end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   164
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   165
fun filter_elim_dest check_thm ctxt goal =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   166
  let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   167
    val extract_elim =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   168
     (fn thm => if Thm.no_prems thm then [] else [thm],
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   169
      hd o Logic.strip_imp_prems);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   170
    val prems = Logic.prems_of_goal goal 1;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   171
  in
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   172
    fn fact => prems |> List.exists (fn prem =>
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   173
      is_matching_thm extract_elim ctxt prem fact
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   174
      andalso (check_thm ctxt) (#2 fact))
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   175
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   176
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   177
in
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   178
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   179
fun filter_intro ctxt goal =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   180
  let
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   181
    val extract_intro = (single, Logic.strip_imp_concl);
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   182
    val concl = Logic.concl_of_goal goal 1;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   183
  in
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   184
    fn fact => is_matching_thm extract_intro ctxt concl fact
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   185
      andalso not (is_elim ctxt (#2 fact))
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   186
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   187
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   188
fun filter_elim ctxt = filter_elim_dest is_elim ctxt;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   189
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   190
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   191
end;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   192
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   193
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   194
(* filter_rewrite *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   195
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   196
fun filter_rewrite ctxt t =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   197
  let
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   198
    val (_, {mk_rews = {mk, ...}, ...}) =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   199
      MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   200
    val extract_rewrite = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   201
  in is_matching_thm extract_rewrite ctxt t end;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   202
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   203
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   204
(* filter_pattern *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   205
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   206
fun filter_pattern ctxt pat =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   207
  let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt)
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   208
  in fn (_, thm) => Pattern.matches_subterm tsig (pat, Thm.prop_of thm) end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   209
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   210
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   211
(* interpret criteria as filters *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   212
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   213
local
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   214
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   215
fun err_no_goal c =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   216
  error ("Current goal required for " ^ c ^ " search criterion");
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   217
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   218
fun filter_crit _ _ (Name name) = filter_name name
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   219
  | filter_crit _ NONE Intro = err_no_goal "intro"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   220
  | filter_crit _ NONE Elim = err_no_goal "elim"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   221
  | filter_crit _ NONE Dest = err_no_goal "dest"
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   222
  | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   223
  | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   224
  | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   225
  | filter_crit ctxt _ (Rewrite str) = filter_rewrite ctxt str
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   226
  | filter_crit ctxt _ (Pattern str) = filter_pattern ctxt str;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   227
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   228
in
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   229
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   230
fun filter_criterion ctxt opt_goal (b, c) =
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   231
  (if b then I else not) o filter_crit ctxt opt_goal c;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   232
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   233
fun all_filters filters = List.filter (fn x => List.all (fn f => f x) filters);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   234
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   235
end;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   236
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   237
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   238
(* print_theorems *)
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   239
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   240
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   241
  let
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   242
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   243
    val filters = map (filter_criterion ctxt opt_goal) criteria;
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   244
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   245
    val matches = all_filters filters (find_thms ctxt ([], []));
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   246
    val len = length matches;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   247
    val limit = if_none opt_limit (! thms_containing_limit);
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   248
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   249
    fun prt_fact (thmref, thm) =
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   250
      ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   251
  in
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   252
    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
   253
     (if null matches then [Pretty.str "nothing found"]
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   254
      else
16036
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   255
        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   256
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   257
         Pretty.str ""] @
1da07ac33711 added read_criterion/pretty_criterion;
wenzelm
parents: 16033
diff changeset
   258
        map prt_fact (Library.drop (len - limit, matches)))
16033
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   259
    |> Pretty.chunks |> Pretty.writeln
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   260
  end;
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   261
f93ca3d4ffa7 Retrieve theorems from proof context -- improved version of
wenzelm
parents:
diff changeset
   262
end;