src/Pure/Isar/proof_context.ML
author wenzelm
Tue, 21 Sep 1999 17:24:50 +0200
changeset 7557 1b977741f530
parent 7505 a9690e9cc58a
child 7575 e1e2d07287d8
permissions -rw-r--r--
export prems_of;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/proof_context.ML
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     4
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     5
Proof context information.
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     6
*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     7
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     8
signature PROOF_CONTEXT =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
     9
sig
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    10
  type context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    11
  exception CONTEXT of string * context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    12
  val theory_of: context -> theory
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    13
  val sign_of: context -> Sign.sg
7557
1b977741f530 export prems_of;
wenzelm
parents: 7505
diff changeset
    14
  val prems_of: context -> thm list
6985
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
    15
  val show_hyps: bool ref
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
    16
  val pretty_thm: thm -> Pretty.T
6528
ed8c5f738ab3 verbose flag;
wenzelm
parents: 6091
diff changeset
    17
  val verbose: bool ref
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    18
  val print_binds: context -> unit
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    19
  val print_thms: context -> unit
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    20
  val strings_of_context: context -> string list
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    21
  val print_proof_data: theory -> unit
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
    22
  val init: theory -> context
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    23
  val read_typ: context -> string -> typ
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    24
  val cert_typ: context -> typ -> typ
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
    25
  val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    26
  val read_term: context -> string -> term
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    27
  val read_prop: context -> string -> term
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
    28
  val read_term_pat: context -> string -> term
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
    29
  val read_prop_pat: context -> string -> term
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    30
  val cert_term: context -> term -> term
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    31
  val cert_prop: context -> term -> term
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    32
  val declare_term: term -> context -> context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    33
  val declare_terms: term list -> context -> context
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
    34
  val declare_thm: thm -> context -> context
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    35
  val add_binds: (indexname * string) list -> context -> context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    36
  val add_binds_i: (indexname * term) list -> context -> context
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
    37
  val match_binds: (string list * string) list -> context -> context
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
    38
  val match_binds_i: (term list * term) list -> context -> context
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
    39
  val bind_propp: context * (string * (string list * string list)) -> context * term
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
    40
  val bind_propp_i: context * (term * (term list * term list)) -> context * term
6789
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
    41
  val auto_bind_goal: term -> context -> context
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
    42
  val auto_bind_facts: string -> term list -> context -> context
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    43
  val get_thm: context -> string -> thm
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    44
  val get_thms: context -> string -> thm list
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    45
  val get_thmss: context -> string list -> thm list
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    46
  val put_thm: string * thm -> context -> context
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    47
  val put_thms: string * thm list -> context -> context
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    48
  val put_thmss: (string * thm list) list -> context -> context
6875
df31250ccb3a have_thmss: more_ths;
wenzelm
parents: 6870
diff changeset
    49
  val have_thmss: thm list -> string -> context attribute list
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
    50
    -> (thm list * context attribute list) list -> context -> context * (string * thm list)
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
    51
  val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    52
  val fixed_names: context -> string list
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    53
  val assume: ((int -> tactic) * (int -> tactic))
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    54
    -> (string * context attribute list * (string * (string list * string list)) list) list
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    55
    -> context -> context * ((string * thm list) list * thm list)
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    56
  val assume_i: ((int -> tactic) * (int -> tactic))
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    57
    -> (string * context attribute list * (term * (term list * term list)) list) list
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
    58
    -> context -> context * ((string * thm list) list * thm list)
7411
4dbff71ac480 fix: common constraints;
wenzelm
parents: 7270
diff changeset
    59
  val fix: (string list * string option) list -> context -> context
4dbff71ac480 fix: common constraints;
wenzelm
parents: 7270
diff changeset
    60
  val fix_i: (string list * typ) list -> context -> context
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    61
  val setup: (theory -> theory) list
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    62
end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    63
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    64
signature PROOF_CONTEXT_PRIVATE =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    65
sig
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    66
  include PROOF_CONTEXT
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    67
  val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    68
    -> theory -> theory
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    69
  val print_data: Object.kind -> context -> unit
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    70
  val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    71
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    72
end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    73
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    74
structure ProofContext: PROOF_CONTEXT_PRIVATE =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    75
struct
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    76
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    77
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    78
(** datatype context **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    79
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    80
datatype context =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    81
  Context of
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    82
   {thy: theory,                                                        (*current theory*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    83
    data: Object.T Symtab.table,                                        (*user data*)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    84
    asms:
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
    85
      ((cterm * ((int -> tactic) * (int -> tactic))) list *		(*assumes: A ==> _*)
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
    86
        (string * thm list) list) *
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    87
      ((string * string) list * string list),                           (*fixes: !!x. _*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    88
    binds: (term * typ) Vartab.table,                                   (*term bindings*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    89
    thms: thm list Symtab.table,                                        (*local thms*)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    90
    defs:
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    91
      typ Vartab.table *                                                (*type constraints*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    92
      sort Vartab.table *                                               (*default sorts*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    93
      int *                                                             (*maxidx*)
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
    94
      string list};                                                     (*used type var names*)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    95
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    96
exception CONTEXT of string * context;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    97
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    98
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
    99
fun make_context (thy, data, asms, binds, thms, defs) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   100
  Context {thy = thy, data = data, asms = asms, binds = binds, thms = thms, defs = defs};
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   101
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   102
fun map_context f (Context {thy, data, asms, binds, thms, defs}) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   103
  make_context (f (thy, data, asms, binds, thms, defs));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   104
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   105
fun theory_of (Context {thy, ...}) = thy;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   106
val sign_of = Theory.sign_of o theory_of;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   107
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   108
fun prems_of (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms);
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   109
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   110
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   111
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   112
(** print context information **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   113
6985
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
   114
val show_hyps = ref false;
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
   115
fun pretty_thm th = setmp Display.show_hyps (! show_hyps) Display.pretty_thm th;
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
   116
6528
ed8c5f738ab3 verbose flag;
wenzelm
parents: 6091
diff changeset
   117
val verbose = ref false;
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   118
fun verb f x = if ! verbose then f x else [];
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   119
val verb_string = verb (Library.single o Pretty.string_of);
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   120
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   121
fun strings_of_items prt name items =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   122
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   123
    fun pretty_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x]
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   124
      | pretty_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs);
6721
dcee829f8e21 tuned print_context;
wenzelm
parents: 6560
diff changeset
   125
  in
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   126
    if null items andalso not (! verbose) then []
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   127
    else [Pretty.string_of (Pretty.big_list name (map pretty_itms items))]
6721
dcee829f8e21 tuned print_context;
wenzelm
parents: 6560
diff changeset
   128
  end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   129
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   130
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   131
(* term bindings *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   132
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   133
fun strings_of_binds (Context {thy, binds, ...}) =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   134
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   135
    val prt_term = Sign.pretty_term (Theory.sign_of thy);
7479
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   136
    fun pretty_bind (xi, (t, T)) = prt_term (Logic.mk_equals (Var (xi, T), t));
6721
dcee829f8e21 tuned print_context;
wenzelm
parents: 6560
diff changeset
   137
  in
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   138
    if Vartab.is_empty binds andalso not (! verbose) then []
7486
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   139
    else [Pretty.string_of (Pretty.big_list "Term bindings:"
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   140
      (map pretty_bind (Vartab.dest binds)))]
6721
dcee829f8e21 tuned print_context;
wenzelm
parents: 6560
diff changeset
   141
  end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   142
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   143
val print_binds = seq writeln o strings_of_binds;
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   144
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   145
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   146
(* local theorems *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   147
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   148
fun strings_of_thms (Context {thms, ...}) =
6985
2af6405a6ef3 added show_hyps flag;
wenzelm
parents: 6931
diff changeset
   149
  strings_of_items pretty_thm "Local theorems:" (Symtab.dest thms);
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   150
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   151
val print_thms = seq writeln o strings_of_thms;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   152
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   153
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   154
(* main context *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   155
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   156
fun strings_of_context (ctxt as Context {thy, data = _, asms = (_, (fixes, _)), binds = _,
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   157
    thms = _, defs = (types, sorts, maxidx, used)}) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   158
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   159
    val sign = Theory.sign_of thy;
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   160
    val prems = prems_of ctxt;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   161
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   162
    val prt_term = Sign.pretty_term sign;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   163
    val prt_typ = Sign.pretty_typ sign;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   164
    val prt_sort = Sign.pretty_sort sign;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   165
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   166
    (*theory*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   167
    val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign];
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   168
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   169
    (*fixes*)
7200
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   170
    fun prt_fixes xs = Pretty.block (Pretty.str "Fixed variables:" :: Pretty.brk 1 ::
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   171
      Pretty.commas (map (fn (x, x') => Pretty.str (x ^ " = " ^ x')) xs));
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   172
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   173
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   174
    (* defaults *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   175
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   176
    fun prt_atom prt prtT (x, X) = Pretty.block
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   177
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   178
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   179
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   180
      | prt_var xi = prt_term (Syntax.var xi);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   181
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   182
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   183
      | prt_varT xi = prt_typ (TVar (xi, []));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   184
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   185
    val prt_defT = prt_atom prt_var prt_typ;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   186
    val prt_defS = prt_atom prt_varT prt_sort;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   187
  in
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   188
    verb_string pretty_thy @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   189
    (if null fixes andalso not (! verbose) then []
7200
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   190
      else [Pretty.string_of (prt_fixes (rev fixes))]) @
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   191
    (if null prems andalso not (! verbose) then []
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   192
      else [Pretty.string_of (Pretty.big_list "Assumptions:"
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   193
        (map (prt_term o #prop o Thm.rep_thm) prems))]) @
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   194
    verb strings_of_binds ctxt @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   195
    verb strings_of_thms ctxt @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   196
    verb_string (Pretty.big_list "Type constraints:" (map prt_defT (Vartab.dest types))) @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   197
    verb_string (Pretty.big_list "Default sorts:" (map prt_defS (Vartab.dest sorts))) @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   198
    verb_string (Pretty.str ("Maxidx: " ^ string_of_int maxidx)) @
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   199
    verb_string (Pretty.strs ("Used type variable names:" :: used))
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   200
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   201
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   202
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   203
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   204
(** user data **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   205
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   206
(* errors *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   207
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   208
fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   209
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   210
fun err_inconsistent kinds =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   211
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data");
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   212
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   213
fun err_dup_init thy kind =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   214
  error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   215
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   216
fun err_undef ctxt kind =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   217
  raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   218
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   219
fun err_uninit ctxt kind =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   220
  raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   221
    of_theory (theory_of ctxt), ctxt);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   222
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   223
fun err_access ctxt kind =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   224
  raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   225
    of_theory (theory_of ctxt), ctxt);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   226
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   227
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   228
(* data kind 'Isar/proof_data' *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   229
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   230
structure ProofDataDataArgs =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   231
struct
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   232
  val name = "Isar/proof_data";
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   233
  type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   234
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   235
  val empty = Symtab.empty;
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   236
  val copy = I;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   237
  val prep_ext = I;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   238
  fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   239
    handle Symtab.DUPS kinds => err_inconsistent kinds;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   240
  fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab)));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   241
end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   242
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   243
structure ProofDataData = TheoryDataFun(ProofDataDataArgs);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   244
val print_proof_data = ProofDataData.print;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   245
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   246
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   247
(* init proof data *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   248
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   249
fun init_data kind meths thy =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   250
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   251
    val name = Object.name_of_kind kind;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   252
    val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   253
      handle Symtab.DUP _ => err_dup_init thy name;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   254
  in thy |> ProofDataData.put tab end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   255
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   256
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   257
(* access data *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   258
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   259
fun lookup_data (ctxt as Context {data, ...}) kind =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   260
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   261
    val thy = theory_of ctxt;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   262
    val name = Object.name_of_kind kind;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   263
  in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   264
    (case Symtab.lookup (ProofDataData.get thy, name) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   265
      Some (k, meths) =>
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   266
        if Object.eq_kind (kind, k) then
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   267
          (case Symtab.lookup (data, name) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   268
            Some x => (x, meths)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   269
          | None => err_undef ctxt name)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   270
        else err_access ctxt name
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   271
    | None => err_uninit ctxt name)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   272
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   273
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   274
fun get_data kind f ctxt =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   275
  let val (x, _) = lookup_data ctxt kind
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   276
  in f x handle Match => Object.kind_error kind end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   277
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   278
fun print_data kind ctxt =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   279
  let val (x, (_, prt)) = lookup_data ctxt kind
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   280
  in prt ctxt x end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   281
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   282
fun put_data kind f x ctxt =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   283
  (lookup_data ctxt kind;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   284
    ctxt |> map_context (fn (thy, data, asms, binds, thms, defs) =>
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   285
      (thy, Symtab.update ((Object.name_of_kind kind, f x), data), asms, binds, thms, defs)));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   286
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   287
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   288
(* init context *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   289
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   290
fun init thy =
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   291
  let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   292
    make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty,
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   293
      (Vartab.empty, Vartab.empty, ~1, []))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   294
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   295
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   296
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   297
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   298
(** prepare types **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   299
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   300
fun read_typ (ctxt as Context {defs = (_, sorts, _, _), ...}) s =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   301
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   302
    val sign = sign_of ctxt;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   303
    fun def_sort xi = Vartab.lookup (sorts, xi);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   304
  in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   305
    transform_error (Sign.read_typ (sign, def_sort)) s
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   306
      handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   307
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   308
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   309
fun cert_typ ctxt raw_T =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   310
  Sign.certify_typ (sign_of ctxt) raw_T
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   311
    handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   312
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   313
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   314
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   315
(** prepare terms and propositions **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   316
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   317
(*
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   318
  (1) read / certify wrt. signature of context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   319
  (2) intern Skolem constants
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   320
  (3) expand term bindings
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   321
*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   322
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   323
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   324
(* read / certify wrt. signature *)     (*exception ERROR*) (*exception TERM*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   325
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   326
fun read_def_termTs freeze sg (types, sorts, used) sTs =
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   327
  let val (cts, env) = Thm.read_def_cterms (sg, types, sorts) used freeze sTs
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   328
  in (map Thm.term_of cts, env) end;
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   329
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   330
fun read_def_termT freeze sg defs sT = apfst hd (read_def_termTs freeze sg defs [sT]);
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   331
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   332
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   333
fun read_term_sg freeze sg (defs as (_, _, used)) s =
7505
a9690e9cc58a read_def_termT: dummyT;
wenzelm
parents: 7486
diff changeset
   334
  #1 (read_def_termT freeze sg defs (s, dummyT));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   335
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   336
fun read_prop_sg freeze sg defs s =
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   337
  #1 (read_def_termT freeze sg defs (s, propT));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   338
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   339
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   340
fun cert_term_sg sg t = Thm.term_of (Thm.cterm_of sg t);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   341
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   342
fun cert_prop_sg sg tm =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   343
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   344
    val ctm = Thm.cterm_of sg tm;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   345
    val {t, T, ...} = Thm.rep_cterm ctm;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   346
  in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   347
    if T = propT then t
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   348
    else raise TERM ("Term not of type prop", [t])
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   349
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   350
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   351
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   352
(* intern_skolem *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   353
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   354
fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   355
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   356
fun check_skolem ctxt check x =
7200
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   357
  if not check then x
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   358
  else if not (Syntax.is_identifier x) then
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   359
    raise CONTEXT ("Bad variable name: " ^ quote x, ctxt)
c9b03d9d6647 tuned strings_of_context;
wenzelm
parents: 6996
diff changeset
   360
  else if can Syntax.dest_skolem x then
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   361
    raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   362
  else x;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   363
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   364
fun intern_skolem ctxt check =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   365
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   366
    fun intern (t as Free (x, T)) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   367
          (case get_skolem ctxt (check_skolem ctxt check x) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   368
            Some x' => Free (x', T)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   369
          | None => t)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   370
      | intern (t $ u) = intern t $ intern u
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   371
      | intern (Abs (x, T, t)) = Abs (x, T, intern t)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   372
      | intern a = a;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   373
  in intern end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   374
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   375
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   376
(* norm_term *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   377
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   378
(*beta normal form for terms (not eta normal form), chase variables in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   379
  bindings environment (code taken from Pure/envir.ML)*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   380
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   381
fun norm_term (ctxt as Context {binds, ...}) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   382
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   383
    (*raised when norm has no effect on a term, to do sharing instead of copying*)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   384
    exception SAME;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   385
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   386
    fun norm (t as Var (xi, T)) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   387
          (case Vartab.lookup (binds, xi) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   388
            Some (u, U) =>
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   389
              if T = U then (norm u handle SAME => u)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   390
              else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u])
7479
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   391
          | None => raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   392
      | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   393
      | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   394
      | norm (f $ t) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   395
          ((case norm f of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   396
            Abs (_, _, body) => normh (subst_bound (t, body))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   397
          | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   398
      | norm _ =  raise SAME
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   399
    and normh t = norm t handle SAME => t
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   400
  in normh end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   401
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   402
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   403
(* dummy patterns *)
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   404
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   405
local
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   406
6560
1436349f8b28 renamed 'dummy' to 'dummy_pattern' (less dangerous);
wenzelm
parents: 6550
diff changeset
   407
fun is_dummy (Const (c, _)) = c = PureThy.dummy_patternN
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   408
  | is_dummy _ = false;
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   409
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   410
fun prep_dummy (i, t) =
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   411
  if is_dummy t then (i + 1, Var (("_dummy_", i), Term.fastype_of t)) else (i, t);
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   412
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   413
in
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   414
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   415
fun prepare_dummies tm = #2 (Term.foldl_map_aterms prep_dummy (1, tm));
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   416
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   417
fun reject_dummies ctxt tm =
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   418
  if foldl_aterms (fn (ok, t) => ok andalso not (is_dummy t)) (true, tm) then tm
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   419
  else raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt);
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   420
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   421
end;
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   422
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   423
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   424
(* read terms *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   425
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   426
fun gen_read read app is_pat (ctxt as Context {binds, defs = (types, sorts, _, used), ...}) s =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   427
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   428
    val sign = sign_of ctxt;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   429
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   430
    fun def_type xi =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   431
      (case Vartab.lookup (types, xi) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   432
        None => if is_pat then None else apsome #2 (Vartab.lookup (binds, xi))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   433
      | some => some);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   434
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   435
    fun def_sort xi = Vartab.lookup (sorts, xi);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   436
  in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   437
    (transform_error (read sign (def_type, def_sort, used)) s
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   438
      handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   439
      | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   440
    |> app (intern_skolem ctxt true)
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   441
    |> app (if is_pat then I else norm_term ctxt)
6550
68f950b1a664 dummy patterns;
wenzelm
parents: 6528
diff changeset
   442
    |> app (if is_pat then prepare_dummies else (reject_dummies ctxt))
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   443
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   444
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   445
val read_termTs = gen_read (read_def_termTs false) (apfst o map) false;
6762
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   446
val read_term = gen_read (read_term_sg true) I false;
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   447
val read_prop = gen_read (read_prop_sg true) I false;
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   448
val read_term_pat = gen_read (read_term_sg false) I true;
a9a515a43ae0 read_term/prop_pat: do not freeze;
wenzelm
parents: 6721
diff changeset
   449
val read_prop_pat = gen_read (read_prop_sg false) I true;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   450
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   451
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   452
(* certify terms *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   453
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   454
fun gen_cert cert is_pat ctxt t =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   455
  (cert (sign_of ctxt) t handle TERM (msg, _) => raise CONTEXT (msg, ctxt))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   456
  |> intern_skolem ctxt false
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   457
  |> (if is_pat then I else norm_term ctxt);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   458
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   459
val cert_term = gen_cert cert_term_sg false;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   460
val cert_prop = gen_cert cert_prop_sg false;
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   461
val cert_term_pat = gen_cert cert_term_sg true;
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   462
val cert_prop_pat = gen_cert cert_prop_sg true;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   463
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   464
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   465
(* declare terms *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   466
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   467
val ins_types = foldl_aterms
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   468
  (fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   469
    | (types, Var v) => Vartab.update (v, types)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   470
    | (types, _) => types);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   471
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   472
val ins_sorts = foldl_types (foldl_atyps
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   473
  (fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   474
    | (sorts, TVar v) => Vartab.update (v, sorts)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   475
    | (sorts, _) => sorts));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   476
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   477
val ins_used = foldl_types (foldl_atyps
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   478
  (fn (used, TFree (x, _)) => x ins used
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   479
    | (used, TVar ((x, _), _)) => x ins used
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   480
    | (used, _) => used));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   481
5994
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   482
fun ins_skolem def_type = foldr
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   483
  (fn ((x, x'), types) =>
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   484
    (case def_type x' of
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   485
      Some T => Vartab.update (((x, ~1), T), types)
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   486
    | None => types));
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   487
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   488
fun map_defaults f = map_context
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   489
  (fn (thy, data, asms, binds, thms, defs) => (thy, data, asms, binds, thms, f defs));
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   490
5994
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   491
fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   492
  ctxt
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   493
  |> map_defaults (fn (types, sorts, maxidx, used) => (ins_types (types, t), sorts, maxidx, used))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   494
  |> map_defaults (fn (types, sorts, maxidx, used) => (types, ins_sorts (sorts, t), maxidx, used))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   495
  |> map_defaults (fn (types, sorts, maxidx, used) => (types, sorts, maxidx, ins_used (used, t)))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   496
  |> map_defaults (fn (types, sorts, maxidx, used) =>
5994
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   497
      (types, sorts, Int.max (maxidx, Term.maxidx_of_term t), used))
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   498
  |> map_defaults (fn (types, sorts, maxidx, used) =>
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   499
      (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, maxidx, used));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   500
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   501
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   502
fun declare_term t ctxt = declare (ctxt, t);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   503
fun declare_terms ts ctxt = foldl declare (ctxt, ts);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   504
5874
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   505
fun declare_thm thm ctxt =
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   506
  let val {prop, hyps, ...} = Thm.rep_thm thm
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   507
  in ctxt |> declare_terms (prop :: hyps) end;
a58d4528121d renamed init_context to init;
wenzelm
parents: 5819
diff changeset
   508
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   509
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   510
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   511
(** bindings **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   512
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   513
(* update_binds *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   514
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   515
fun upd_bind (ctxt, (xi, t)) =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   516
  let val T = fastype_of t in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   517
    ctxt
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   518
    |> declare_term t
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   519
    |> map_context (fn (thy, data, asms, binds, thms, defs) =>
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   520
        (thy, data, asms, Vartab.update ((xi, (t, T)), binds), thms, defs))
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   521
  end;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   522
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   523
fun update_binds bs ctxt = foldl upd_bind (ctxt, bs);
7479
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   524
fun update_binds_env env = (*Envir.norm_term ensures proper type instantiation*)
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   525
  update_binds (map (apsnd (Envir.norm_term env)) (Envir.alist_of env));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   526
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   527
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   528
(* add_binds(_i) -- sequential *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   529
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   530
fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) =
7479
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   531
  ctxt |> update_binds [(xi, prep ctxt raw_t)];
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   532
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   533
fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds);
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   534
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   535
val add_binds = gen_binds read_term;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   536
val add_binds_i = gen_binds cert_term;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   537
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   538
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   539
(* match_binds(_i) -- parallel *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   540
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   541
fun prep_declare_match (prep_pat, prep) (ctxt, (raw_pats, raw_t)) =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   542
  let
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   543
    val t = prep ctxt raw_t;
5994
7b84677315ed fixed declatation of patterns and skolem;
wenzelm
parents: 5935
diff changeset
   544
    val ctxt' = ctxt |> declare_term t;
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   545
    val matches = map (fn (f, raw_pat) => (prep_pat ctxt' raw_pat, f t)) raw_pats;
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   546
           (* FIXME seq / par / simult (??) *)
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   547
  in (ctxt', (matches, t)) end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   548
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   549
fun gen_match_binds _ [] ctxt = ctxt
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   550
  | gen_match_binds prepp args ctxt =
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   551
      let
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   552
        val raw_pairs = map (apfst (map (pair I))) args;
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   553
        val (ctxt', matches) = foldl_map (prep_declare_match prepp) (ctxt, raw_pairs);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   554
        val pairs = flat (map #1 matches);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   555
        val Context {defs = (_, _, maxidx, _), ...} = ctxt';
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   556
        val envs = Unify.smash_unifiers (sign_of ctxt', Envir.empty maxidx, pairs);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   557
        val env =
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   558
          (case Seq.pull envs of
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   559
            None => raise CONTEXT ("Pattern match failed!", ctxt')
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   560
          | Some (env, _) => env);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   561
      in ctxt' |> update_binds_env env end;
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   562
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   563
val match_binds = gen_match_binds (read_term_pat, read_term);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   564
val match_binds_i = gen_match_binds (cert_term_pat, cert_term);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   565
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   566
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   567
(* bind proposition patterns *)
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   568
6931
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   569
fun gen_bind_propp prepp (ctxt, (raw_prop, (raw_pats1, raw_pats2))) =
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   570
  let
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   571
    val raw_pats = map (pair I) raw_pats1 @ map (pair Logic.strip_imp_concl) raw_pats2;
bd8aa6ae6bcd propp: 'concl' patterns;
wenzelm
parents: 6895
diff changeset
   572
    val (ctxt', (pairs, prop)) = prep_declare_match prepp (ctxt, (raw_pats, raw_prop));
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   573
  in (ctxt' |> match_binds_i (map (apfst single) pairs), prop) end;
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   574
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   575
val bind_propp = gen_bind_propp (read_prop_pat, read_prop);
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   576
val bind_propp_i = gen_bind_propp (cert_prop_pat, cert_prop);
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   577
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   578
6789
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
   579
(* auto binds *)
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
   580
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
   581
val auto_bind_goal = add_binds_i o AutoBind.goal;
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   582
val auto_bind_facts = add_binds_i oo AutoBind.facts;
6789
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
   583
0e5a965de17a auto_bind_goal, auto_bind_facts;
wenzelm
parents: 6762
diff changeset
   584
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   585
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   586
(** theorems **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   587
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   588
(* get_thm(s) *)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   589
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   590
fun get_thm (ctxt as Context {thy, thms, ...}) name =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   591
  (case Symtab.lookup (thms, name) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   592
    Some [th] => th
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   593
  | Some _ => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt)
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   594
  | None => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   595
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   596
fun get_thms (ctxt as Context {thy, thms, ...}) name =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   597
  (case Symtab.lookup (thms, name) of
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   598
    Some ths => ths
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   599
  | None => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   600
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   601
fun get_thmss ctxt names = flat (map (get_thms ctxt) names);
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   602
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   603
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   604
(* put_thm(s) *)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   605
7479
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   606
fun put_thms ("", _) = I
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   607
  | put_thms (name, ths) = map_context
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   608
      (fn (thy, data, asms, binds, thms, defs) =>
b482d827899c eliminated Syntax.binding;
wenzelm
parents: 7411
diff changeset
   609
        (thy, data, asms, binds, Symtab.update ((name, ths), thms), defs));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   610
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   611
fun put_thm (name, th) = put_thms (name, [th]);
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   612
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   613
fun put_thmss [] ctxt = ctxt
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   614
  | put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   615
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   616
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   617
(* have_thmss *)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   618
6875
df31250ccb3a have_thmss: more_ths;
wenzelm
parents: 6870
diff changeset
   619
fun have_thmss more_ths name more_attrs ths_attrs ctxt =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   620
  let
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   621
    fun app ((ct, ths), (th, attrs)) =
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   622
      let val (ct', th') = Thm.applys_attributes ((ct, th), attrs @ more_attrs)
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   623
      in (ct', th' :: ths) end
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   624
    val (ctxt', rev_thms) = foldl app ((ctxt, []), ths_attrs);
6875
df31250ccb3a have_thmss: more_ths;
wenzelm
parents: 6870
diff changeset
   625
    val thms = flat (rev rev_thms) @ more_ths;
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   626
  in (ctxt' |> put_thms (name, thms), (name, thms)) end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   627
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   628
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   629
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   630
(** assumptions **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   631
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   632
(* get assumptions *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   633
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   634
fun assumptions (Context {asms = ((asms, _), _), ...}) = asms;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   635
fun fixed_names (Context {asms = (_, (fixes, _)), ...}) = map #2 fixes;
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   636
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   637
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   638
(* assume *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   639
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   640
local
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   641
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   642
fun gen_assm prepp tac (ctxt, (name, attrs, raw_prop_pats)) =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   643
  let
5935
6a82c8a1808f term_pat vs. prop_pat;
wenzelm
parents: 5919
diff changeset
   644
    val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats);
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   645
    val sign = sign_of ctxt';
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   646
    val Context {defs = (_, _, maxidx, _), ...} = ctxt';
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   647
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   648
    val cprops = map (Thm.cterm_of sign) props;
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   649
    val cprops_tac = map (rpair tac) cprops;
6996
1a28d968c5aa handle cgoal;
wenzelm
parents: 6985
diff changeset
   650
    val asms = map (Drule.forall_elim_vars (maxidx + 1) o Drule.assume_goal) cprops;
5919
a5b2d4b9ed6f have_tthms;
wenzelm
parents: 5874
diff changeset
   651
a5b2d4b9ed6f have_tthms;
wenzelm
parents: 5874
diff changeset
   652
    val ths = map (fn th => ([th], [])) asms;
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5994
diff changeset
   653
    val (ctxt'', (_, thms)) =
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   654
      ctxt'
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   655
      |> auto_bind_facts name props
6875
df31250ccb3a have_thmss: more_ths;
wenzelm
parents: 6870
diff changeset
   656
      |> have_thmss [] name (attrs @ [Drule.tag_assumption]) ths;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   657
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   658
    val ctxt''' =
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   659
      ctxt''
6797
f86b96a0f6fb improved handling of assumptions;
wenzelm
parents: 6789
diff changeset
   660
      |> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, defs) =>
6847
f175f56c57a6 tuned output: print_context replaced by strings_of_context;
wenzelm
parents: 6797
diff changeset
   661
        (thy, data, ((asms_ct @ cprops_tac, asms_th @ [(name, asms)]), fixes), binds, thms, defs));
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   662
  in (ctxt''', (name, thms)) end;
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   663
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   664
fun gen_assms prepp tac args ctxt =
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   665
  let val (ctxt', thmss) = foldl_map (gen_assm prepp tac) (ctxt, args)
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   666
  in (ctxt', (thmss, prems_of ctxt')) end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   667
7270
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   668
in
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   669
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   670
val assume = gen_assms bind_propp;
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   671
val assume_i = gen_assms bind_propp_i;
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   672
2b64729d9acb warn_vars;
wenzelm
parents: 7200
diff changeset
   673
end;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   674
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   675
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   676
(* fix *)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   677
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   678
fun gen_fix prep check (ctxt, (x, raw_T)) =
7486
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   679
  (check_skolem ctxt check x;
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   680
    ctxt
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   681
    |> (case prep ctxt raw_T of None => I | Some T => declare_term (Free (x, T)))
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   682
    |> map_context (fn (thy, data, (assumes, (fixes, names)), binds, thms, defs) =>
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   683
      let val x' = variant names x in
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   684
        (thy, data, (assumes, ((x, Syntax.skolem x') :: fixes, x' :: names)), binds, thms, defs)
7486
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   685
      end));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   686
7411
4dbff71ac480 fix: common constraints;
wenzelm
parents: 7270
diff changeset
   687
fun gen_fixs prep check vars ctxt =
4dbff71ac480 fix: common constraints;
wenzelm
parents: 7270
diff changeset
   688
  foldl (gen_fix prep check) (ctxt, flat (map (fn (xs, T) => map (rpair T) xs) vars));
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   689
7486
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   690
val fix = gen_fixs (apsome o read_typ) true;
1f9eceb434db removed thms_closure (unused);
wenzelm
parents: 7479
diff changeset
   691
val fix_i = gen_fixs (Some oo cert_typ) false;
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   692
6895
450b1f67f099 added transfer_used_names;
wenzelm
parents: 6875
diff changeset
   693
5819
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   694
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   695
(** theory setup **)
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   696
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   697
val setup = [ProofDataData.init];
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   698
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   699
5fff21d4ca3a Proof context information.
wenzelm
parents:
diff changeset
   700
end;