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