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