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