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