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