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