src/Pure/Isar/proof_context.ML
author wenzelm
Wed Mar 15 18:26:53 2000 +0100 (2000-03-15 ago)
changeset 8462 7f4e4e875c13
parent 8426 f6e022588624
child 8616 90d2fed59be1
permissions -rw-r--r--
pretty chunks;
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@7557
    14
  val prems_of: context -> thm list
wenzelm@6985
    15
  val show_hyps: bool ref
wenzelm@6985
    16
  val pretty_thm: thm -> Pretty.T
wenzelm@6528
    17
  val verbose: bool ref
wenzelm@5819
    18
  val print_binds: context -> unit
wenzelm@5819
    19
  val print_thms: context -> unit
wenzelm@8373
    20
  val print_cases: context -> unit
wenzelm@8462
    21
  val pretty_prems: context -> Pretty.T list
wenzelm@8462
    22
  val pretty_context: context -> Pretty.T list
wenzelm@5819
    23
  val print_proof_data: theory -> unit
wenzelm@5874
    24
  val init: theory -> context
wenzelm@5819
    25
  val read_typ: context -> string -> typ
wenzelm@5819
    26
  val cert_typ: context -> typ -> typ
wenzelm@7679
    27
  val cert_skolem: context -> string -> string
wenzelm@5874
    28
  val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@5819
    29
  val read_term: context -> string -> term
wenzelm@5819
    30
  val read_prop: context -> string -> term
wenzelm@8096
    31
  val read_termT_pats: context -> (string * typ) list -> term list
wenzelm@8096
    32
  val read_term_pats: typ -> context -> string list -> term list
wenzelm@8096
    33
  val read_prop_pats: context -> string list -> term list
wenzelm@5819
    34
  val cert_term: context -> term -> term
wenzelm@5819
    35
  val cert_prop: context -> term -> term
wenzelm@8096
    36
  val cert_term_pats: typ -> context -> term list -> term list
wenzelm@8096
    37
  val cert_prop_pats: context -> term list -> term list
wenzelm@5819
    38
  val declare_term: term -> context -> context
wenzelm@5819
    39
  val declare_terms: term list -> context -> context
wenzelm@7925
    40
  val warn_extra_tfrees: context -> context -> context
wenzelm@7606
    41
  val add_binds: (indexname * string option) list -> context -> context
wenzelm@7606
    42
  val add_binds_i: (indexname * term option) list -> context -> context
wenzelm@7925
    43
  val auto_bind_goal: term -> context -> context
wenzelm@7925
    44
  val auto_bind_facts: string -> term list -> context -> context
wenzelm@8096
    45
  val match_bind: (string list * string) list -> context -> context
wenzelm@8096
    46
  val match_bind_i: (term list * term) list -> context -> context
wenzelm@8096
    47
  val read_propp: context * (string * (string list * string list))
wenzelm@8096
    48
    -> context * (term * (term list * term list))
wenzelm@8096
    49
  val cert_propp: context * (term * (term list * term list))
wenzelm@8096
    50
    -> context * (term * (term list * term list))
wenzelm@8096
    51
  val bind_propp: context * (string * (string list * string list)) -> context * term
wenzelm@8096
    52
  val bind_propp_i: context * (term * (term list * term list)) -> context * term
wenzelm@6091
    53
  val get_thm: context -> string -> thm
wenzelm@6091
    54
  val get_thms: context -> string -> thm list
wenzelm@6091
    55
  val get_thmss: context -> string list -> thm list
wenzelm@6091
    56
  val put_thm: string * thm -> context -> context
wenzelm@6091
    57
  val put_thms: string * thm list -> context -> context
wenzelm@6091
    58
  val put_thmss: (string * thm list) list -> context -> context
wenzelm@7606
    59
  val reset_thms: string -> context -> context
wenzelm@6875
    60
  val have_thmss: thm list -> string -> context attribute list
wenzelm@6091
    61
    -> (thm list * context attribute list) list -> context -> context * (string * thm list)
wenzelm@6931
    62
  val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
wenzelm@5819
    63
  val fixed_names: context -> string list
wenzelm@8186
    64
  val most_general_varify_tfrees: thm -> thm
wenzelm@7270
    65
  val assume: ((int -> tactic) * (int -> tactic))
wenzelm@7270
    66
    -> (string * context attribute list * (string * (string list * string list)) list) list
wenzelm@7270
    67
    -> context -> context * ((string * thm list) list * thm list)
wenzelm@7270
    68
  val assume_i: ((int -> tactic) * (int -> tactic))
wenzelm@7270
    69
    -> (string * context attribute list * (term * (term list * term list)) list) list
wenzelm@7270
    70
    -> context -> context * ((string * thm list) list * thm list)
wenzelm@8096
    71
  val read_vars: context * (string list * string option) -> context * (string list * typ option)
wenzelm@8096
    72
  val cert_vars: context * (string list * typ option) -> context * (string list * typ option)
wenzelm@7411
    73
  val fix: (string list * string option) list -> context -> context
wenzelm@7663
    74
  val fix_i: (string list * typ option) list -> context -> context
wenzelm@8373
    75
  val get_case: context -> string -> RuleCases.T
wenzelm@8373
    76
  val add_cases: (string * RuleCases.T) list -> context -> context
wenzelm@5819
    77
  val setup: (theory -> theory) list
wenzelm@5819
    78
end;
wenzelm@5819
    79
wenzelm@8151
    80
signature PRIVATE_PROOF_CONTEXT =
wenzelm@5819
    81
sig
wenzelm@5819
    82
  include PROOF_CONTEXT
wenzelm@5819
    83
  val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit)
wenzelm@5819
    84
    -> theory -> theory
wenzelm@5819
    85
  val print_data: Object.kind -> context -> unit
wenzelm@5819
    86
  val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a
wenzelm@5819
    87
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context
wenzelm@5819
    88
end;
wenzelm@5819
    89
wenzelm@8151
    90
structure ProofContext: PRIVATE_PROOF_CONTEXT =
wenzelm@5819
    91
struct
wenzelm@5819
    92
wenzelm@5819
    93
wenzelm@5819
    94
(** datatype context **)
wenzelm@5819
    95
wenzelm@5819
    96
datatype context =
wenzelm@5819
    97
  Context of
wenzelm@6847
    98
   {thy: theory,                                                        (*current theory*)
wenzelm@6847
    99
    data: Object.T Symtab.table,                                        (*user data*)
wenzelm@5819
   100
    asms:
wenzelm@7663
   101
      ((cterm * ((int -> tactic) * (int -> tactic))) list *             (*assumes: A ==> _*)
wenzelm@6931
   102
        (string * thm list) list) *
wenzelm@6847
   103
      ((string * string) list * string list),                           (*fixes: !!x. _*)
wenzelm@7606
   104
    binds: (term * typ) option Vartab.table,                            (*term bindings*)
wenzelm@7606
   105
    thms: thm list option Symtab.table,                                 (*local thms*)
wenzelm@8426
   106
    cases: (string * RuleCases.T) list,                                 (*local contexts*)
wenzelm@5819
   107
    defs:
wenzelm@6847
   108
      typ Vartab.table *                                                (*type constraints*)
wenzelm@6847
   109
      sort Vartab.table *                                               (*default sorts*)
wenzelm@6847
   110
      string list};                                                     (*used type var names*)
wenzelm@5819
   111
wenzelm@5819
   112
exception CONTEXT of string * context;
wenzelm@5819
   113
wenzelm@5819
   114
wenzelm@8373
   115
fun make_context (thy, data, asms, binds, thms, cases, defs) =
wenzelm@8373
   116
  Context {thy = thy, data = data, asms = asms, binds = binds, thms = thms,
wenzelm@8373
   117
    cases = cases, defs = defs};
wenzelm@5819
   118
wenzelm@8373
   119
fun map_context f (Context {thy, data, asms, binds, thms, cases, defs}) =
wenzelm@8373
   120
  make_context (f (thy, data, asms, binds, thms, cases, defs));
wenzelm@5819
   121
wenzelm@5819
   122
fun theory_of (Context {thy, ...}) = thy;
wenzelm@5819
   123
val sign_of = Theory.sign_of o theory_of;
wenzelm@5819
   124
wenzelm@7270
   125
fun prems_of (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms);
wenzelm@7270
   126
wenzelm@5819
   127
wenzelm@5819
   128
wenzelm@5819
   129
(** print context information **)
wenzelm@5819
   130
wenzelm@6985
   131
val show_hyps = ref false;
wenzelm@7575
   132
wenzelm@7575
   133
fun pretty_thm thm =
wenzelm@7575
   134
  if ! show_hyps then setmp Display.show_hyps true Display.pretty_thm_no_quote thm
wenzelm@7575
   135
  else Display.pretty_cterm (#prop (Thm.crep_thm thm));
wenzelm@6985
   136
wenzelm@6528
   137
val verbose = ref false;
wenzelm@8373
   138
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@8462
   139
fun verb_single x = verb Library.single x;
wenzelm@5819
   140
wenzelm@8462
   141
fun pretty_items prt name items =
wenzelm@5819
   142
  let
wenzelm@8462
   143
    fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x]
wenzelm@8462
   144
      | prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs);
wenzelm@6721
   145
  in
wenzelm@6847
   146
    if null items andalso not (! verbose) then []
wenzelm@8462
   147
    else [Pretty.big_list name (map prt_itms items)]
wenzelm@6721
   148
  end;
wenzelm@5819
   149
wenzelm@5819
   150
wenzelm@5819
   151
(* term bindings *)
wenzelm@5819
   152
wenzelm@7606
   153
val smash_option = fn (_, None) => None | (xi, Some b) => Some (xi, b);
wenzelm@7606
   154
wenzelm@8462
   155
fun pretty_binds (ctxt as Context {binds, ...}) =
wenzelm@5819
   156
  let
wenzelm@7575
   157
    val prt_term = Sign.pretty_term (sign_of ctxt);
wenzelm@8462
   158
    fun prt_bind (xi, (t, T)) = prt_term (Logic.mk_equals (Var (xi, T), t));
wenzelm@7606
   159
    val bs = mapfilter smash_option (Vartab.dest binds);
wenzelm@6721
   160
  in
wenzelm@7606
   161
    if null bs andalso not (! verbose) then []
wenzelm@8462
   162
    else [Pretty.big_list "term bindings:" (map prt_bind bs)]
wenzelm@6721
   163
  end;
wenzelm@5819
   164
wenzelm@8462
   165
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@6847
   166
wenzelm@5819
   167
wenzelm@5819
   168
(* local theorems *)
wenzelm@5819
   169
wenzelm@8462
   170
fun pretty_thms (Context {thms, ...}) =
wenzelm@8462
   171
  pretty_items pretty_thm "local theorems:" (mapfilter smash_option (Symtab.dest thms));
wenzelm@6847
   172
wenzelm@8462
   173
val print_thms = Pretty.writeln o Pretty.chunks o pretty_thms;
wenzelm@5819
   174
wenzelm@5819
   175
wenzelm@8373
   176
(* local contexts *)
wenzelm@8373
   177
wenzelm@8462
   178
fun pretty_cases (ctxt as Context {cases, ...}) =
wenzelm@8373
   179
  let
wenzelm@8373
   180
    val prt_term = Sign.pretty_term (sign_of ctxt);
wenzelm@8373
   181
wenzelm@8373
   182
    fun prt_sect _ _ [] = []
wenzelm@8373
   183
      | prt_sect s prt xs = [Pretty.block (Pretty.breaks (Pretty.str s :: map prt xs))];
wenzelm@8373
   184
wenzelm@8373
   185
    fun prt_case (name, (xs, ts)) = Pretty.block (Pretty.breaks
wenzelm@8373
   186
      (Pretty.str (name ^ ":") ::
wenzelm@8373
   187
        prt_sect "fix" (prt_term o Free) xs @
wenzelm@8373
   188
        prt_sect "assume" (Pretty.quote o prt_term) ts));
wenzelm@8426
   189
wenzelm@8426
   190
    val cases' = rev (Library.gen_distinct Library.eq_fst cases);
wenzelm@8373
   191
  in
wenzelm@8426
   192
    if null cases andalso not (! verbose) then []
wenzelm@8462
   193
    else [Pretty.big_list "cases:" (map prt_case cases')]
wenzelm@8373
   194
  end;
wenzelm@8373
   195
wenzelm@8462
   196
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@8373
   197
wenzelm@8373
   198
wenzelm@5819
   199
(* main context *)
wenzelm@5819
   200
wenzelm@8462
   201
fun pretty_prems ctxt =
wenzelm@7575
   202
  (case prems_of ctxt of
wenzelm@7575
   203
    [] => []
wenzelm@8462
   204
  | prems => [Pretty.big_list "prems:" (map pretty_thm prems)]);
wenzelm@7575
   205
wenzelm@8462
   206
fun pretty_context (ctxt as Context {asms = (_, (fixes, _)), cases,
wenzelm@7670
   207
    defs = (types, sorts, used), ...}) =
wenzelm@5819
   208
  let
wenzelm@7575
   209
    val sign = sign_of ctxt;
wenzelm@5819
   210
wenzelm@5819
   211
    val prt_term = Sign.pretty_term sign;
wenzelm@5819
   212
    val prt_typ = Sign.pretty_typ sign;
wenzelm@5819
   213
    val prt_sort = Sign.pretty_sort sign;
wenzelm@5819
   214
wenzelm@5819
   215
    (*theory*)
wenzelm@5819
   216
    val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign];
wenzelm@5819
   217
wenzelm@5819
   218
    (*fixes*)
wenzelm@7606
   219
    fun prt_fix (x, x') = Pretty.block
wenzelm@7606
   220
      [prt_term (Syntax.free x), Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@7606
   221
wenzelm@7575
   222
    fun prt_fixes xs = Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@7606
   223
      Pretty.commas (map prt_fix xs));
wenzelm@7200
   224
wenzelm@8373
   225
    (*defaults*)
wenzelm@5819
   226
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@5819
   227
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@5819
   228
wenzelm@5819
   229
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@5819
   230
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@5819
   231
wenzelm@5819
   232
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@5819
   233
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@5819
   234
wenzelm@5819
   235
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@5819
   236
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@5819
   237
  in
wenzelm@8462
   238
    verb_single (K pretty_thy) @
wenzelm@8462
   239
    (if null fixes then [] else [prt_fixes (rev fixes)]) @
wenzelm@8462
   240
    pretty_prems ctxt @
wenzelm@8462
   241
    verb pretty_binds (K ctxt) @
wenzelm@8462
   242
    verb pretty_thms (K ctxt) @
wenzelm@8462
   243
    verb pretty_cases (K ctxt) @
wenzelm@8462
   244
    verb_single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@8462
   245
    verb_single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
wenzelm@8462
   246
    verb_single (fn () => Pretty.strs ("used type variable names:" :: used))
wenzelm@5819
   247
  end;
wenzelm@5819
   248
wenzelm@5819
   249
wenzelm@5819
   250
wenzelm@5819
   251
(** user data **)
wenzelm@5819
   252
wenzelm@5819
   253
(* errors *)
wenzelm@5819
   254
wenzelm@5819
   255
fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy);
wenzelm@5819
   256
wenzelm@5819
   257
fun err_inconsistent kinds =
wenzelm@5819
   258
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data");
wenzelm@5819
   259
wenzelm@5819
   260
fun err_dup_init thy kind =
wenzelm@5819
   261
  error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy);
wenzelm@5819
   262
wenzelm@5819
   263
fun err_undef ctxt kind =
wenzelm@5819
   264
  raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt);
wenzelm@5819
   265
wenzelm@5819
   266
fun err_uninit ctxt kind =
wenzelm@5819
   267
  raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^
wenzelm@5819
   268
    of_theory (theory_of ctxt), ctxt);
wenzelm@5819
   269
wenzelm@5819
   270
fun err_access ctxt kind =
wenzelm@5819
   271
  raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^
wenzelm@5819
   272
    of_theory (theory_of ctxt), ctxt);
wenzelm@5819
   273
wenzelm@5819
   274
wenzelm@5819
   275
(* data kind 'Isar/proof_data' *)
wenzelm@5819
   276
wenzelm@5819
   277
structure ProofDataDataArgs =
wenzelm@5819
   278
struct
wenzelm@5819
   279
  val name = "Isar/proof_data";
wenzelm@5819
   280
  type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table;
wenzelm@5819
   281
wenzelm@5819
   282
  val empty = Symtab.empty;
wenzelm@6550
   283
  val copy = I;
wenzelm@5819
   284
  val prep_ext = I;
wenzelm@5819
   285
  fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs
wenzelm@5819
   286
    handle Symtab.DUPS kinds => err_inconsistent kinds;
wenzelm@5819
   287
  fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab)));
wenzelm@5819
   288
end;
wenzelm@5819
   289
wenzelm@5819
   290
structure ProofDataData = TheoryDataFun(ProofDataDataArgs);
wenzelm@5819
   291
val print_proof_data = ProofDataData.print;
wenzelm@5819
   292
wenzelm@5819
   293
wenzelm@5819
   294
(* init proof data *)
wenzelm@5819
   295
wenzelm@5819
   296
fun init_data kind meths thy =
wenzelm@5819
   297
  let
wenzelm@5819
   298
    val name = Object.name_of_kind kind;
wenzelm@5819
   299
    val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy)
wenzelm@5819
   300
      handle Symtab.DUP _ => err_dup_init thy name;
wenzelm@5819
   301
  in thy |> ProofDataData.put tab end;
wenzelm@5819
   302
wenzelm@5819
   303
wenzelm@5819
   304
(* access data *)
wenzelm@5819
   305
wenzelm@5819
   306
fun lookup_data (ctxt as Context {data, ...}) kind =
wenzelm@5819
   307
  let
wenzelm@5819
   308
    val thy = theory_of ctxt;
wenzelm@5819
   309
    val name = Object.name_of_kind kind;
wenzelm@5819
   310
  in
wenzelm@5819
   311
    (case Symtab.lookup (ProofDataData.get thy, name) of
wenzelm@5819
   312
      Some (k, meths) =>
wenzelm@5819
   313
        if Object.eq_kind (kind, k) then
wenzelm@5819
   314
          (case Symtab.lookup (data, name) of
wenzelm@5819
   315
            Some x => (x, meths)
wenzelm@5819
   316
          | None => err_undef ctxt name)
wenzelm@5819
   317
        else err_access ctxt name
wenzelm@5819
   318
    | None => err_uninit ctxt name)
wenzelm@5819
   319
  end;
wenzelm@5819
   320
wenzelm@5819
   321
fun get_data kind f ctxt =
wenzelm@5819
   322
  let val (x, _) = lookup_data ctxt kind
wenzelm@5819
   323
  in f x handle Match => Object.kind_error kind end;
wenzelm@5819
   324
wenzelm@5819
   325
fun print_data kind ctxt =
wenzelm@5819
   326
  let val (x, (_, prt)) = lookup_data ctxt kind
wenzelm@5819
   327
  in prt ctxt x end;
wenzelm@5819
   328
wenzelm@5819
   329
fun put_data kind f x ctxt =
wenzelm@8373
   330
 (lookup_data ctxt kind;
wenzelm@8373
   331
  ctxt |> map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   332
    (thy, Symtab.update ((Object.name_of_kind kind, f x), data), asms, binds, thms, cases, defs)));
wenzelm@5819
   333
wenzelm@5819
   334
wenzelm@5819
   335
(* init context *)
wenzelm@5819
   336
wenzelm@5874
   337
fun init thy =
wenzelm@5874
   338
  let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in
wenzelm@8426
   339
    make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty, [],
wenzelm@7670
   340
      (Vartab.empty, Vartab.empty, []))
wenzelm@5819
   341
  end;
wenzelm@5819
   342
wenzelm@5819
   343
wenzelm@8096
   344
wenzelm@7663
   345
(** default sorts and types **)
wenzelm@7663
   346
wenzelm@7670
   347
fun def_sort (Context {defs = (_, sorts, _), ...}) xi = Vartab.lookup (sorts, xi);
wenzelm@7663
   348
wenzelm@7670
   349
fun def_type (Context {binds, defs = (types, _, _), ...}) is_pat xi =
wenzelm@7663
   350
  (case Vartab.lookup (types, xi) of
wenzelm@7663
   351
    None =>
wenzelm@7663
   352
      if is_pat then None
wenzelm@7663
   353
      else (case Vartab.lookup (binds, xi) of Some (Some (_, T)) => Some T | _ => None)
wenzelm@7663
   354
  | some => some);
wenzelm@7663
   355
wenzelm@7663
   356
wenzelm@5819
   357
wenzelm@5819
   358
(** prepare types **)
wenzelm@5819
   359
wenzelm@7663
   360
fun read_typ ctxt s =
wenzelm@7663
   361
  transform_error (Sign.read_typ (sign_of ctxt, def_sort ctxt)) s
wenzelm@7663
   362
    handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt);
wenzelm@5819
   363
wenzelm@5819
   364
fun cert_typ ctxt raw_T =
wenzelm@5819
   365
  Sign.certify_typ (sign_of ctxt) raw_T
wenzelm@5819
   366
    handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
wenzelm@5819
   367
wenzelm@5819
   368
wenzelm@7679
   369
(* internalize Skolem constants *)
wenzelm@7679
   370
wenzelm@7679
   371
fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x);
wenzelm@7679
   372
wenzelm@7679
   373
fun check_skolem ctxt check x =
wenzelm@7679
   374
  if check andalso can Syntax.dest_skolem x then
wenzelm@7679
   375
    raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
wenzelm@7679
   376
  else x;
wenzelm@7679
   377
wenzelm@7679
   378
fun intern_skolem ctxt check =
wenzelm@7679
   379
  let
wenzelm@7679
   380
    fun intern (t as Free (x, T)) =
wenzelm@7679
   381
          (case get_skolem ctxt (check_skolem ctxt check x) of
wenzelm@7679
   382
            Some x' => Free (x', T)
wenzelm@7679
   383
          | None => t)
wenzelm@7679
   384
      | intern (t $ u) = intern t $ intern u
wenzelm@7679
   385
      | intern (Abs (x, T, t)) = Abs (x, T, intern t)
wenzelm@7679
   386
      | intern a = a;
wenzelm@7679
   387
  in intern end;
wenzelm@7679
   388
wenzelm@7679
   389
fun cert_skolem ctxt x =
wenzelm@7679
   390
  (case get_skolem ctxt x of
wenzelm@7679
   391
    None => raise CONTEXT ("Undeclared variable: " ^ quote x, ctxt)
wenzelm@7679
   392
  | Some x' => x');
wenzelm@7679
   393
wenzelm@7679
   394
wenzelm@8096
   395
wenzelm@5819
   396
(** prepare terms and propositions **)
wenzelm@5819
   397
wenzelm@5819
   398
(*
wenzelm@5819
   399
  (1) read / certify wrt. signature of context
wenzelm@5819
   400
  (2) intern Skolem constants
wenzelm@5819
   401
  (3) expand term bindings
wenzelm@5819
   402
*)
wenzelm@5819
   403
wenzelm@5819
   404
wenzelm@5819
   405
(* read / certify wrt. signature *)     (*exception ERROR*) (*exception TERM*)
wenzelm@5819
   406
wenzelm@5874
   407
fun read_def_termTs freeze sg (types, sorts, used) sTs =
wenzelm@5874
   408
  let val (cts, env) = Thm.read_def_cterms (sg, types, sorts) used freeze sTs
wenzelm@5874
   409
  in (map Thm.term_of cts, env) end;
wenzelm@5874
   410
wenzelm@5874
   411
fun read_def_termT freeze sg defs sT = apfst hd (read_def_termTs freeze sg defs [sT]);
wenzelm@5874
   412
wenzelm@8096
   413
fun read_term_sg freeze sg defs s = #1 (read_def_termT freeze sg defs (s, TypeInfer.logicT));
wenzelm@8096
   414
fun read_prop_sg freeze sg defs s = #1 (read_def_termT freeze sg defs (s, propT));
wenzelm@5819
   415
wenzelm@5819
   416
wenzelm@5819
   417
fun cert_term_sg sg t = Thm.term_of (Thm.cterm_of sg t);
wenzelm@5819
   418
wenzelm@5819
   419
fun cert_prop_sg sg tm =
wenzelm@5819
   420
  let
wenzelm@5819
   421
    val ctm = Thm.cterm_of sg tm;
wenzelm@5819
   422
    val {t, T, ...} = Thm.rep_cterm ctm;
wenzelm@5819
   423
  in
wenzelm@5819
   424
    if T = propT then t
wenzelm@5819
   425
    else raise TERM ("Term not of type prop", [t])
wenzelm@5819
   426
  end;
wenzelm@5819
   427
wenzelm@5819
   428
wenzelm@5819
   429
(* norm_term *)
wenzelm@5819
   430
wenzelm@5819
   431
(*beta normal form for terms (not eta normal form), chase variables in
wenzelm@5819
   432
  bindings environment (code taken from Pure/envir.ML)*)
wenzelm@5819
   433
wenzelm@5819
   434
fun norm_term (ctxt as Context {binds, ...}) =
wenzelm@5819
   435
  let
wenzelm@5819
   436
    (*raised when norm has no effect on a term, to do sharing instead of copying*)
wenzelm@5819
   437
    exception SAME;
wenzelm@5819
   438
wenzelm@5819
   439
    fun norm (t as Var (xi, T)) =
wenzelm@5819
   440
          (case Vartab.lookup (binds, xi) of
wenzelm@7606
   441
            Some (Some (u, U)) =>
wenzelm@5819
   442
              if T = U then (norm u handle SAME => u)
wenzelm@5819
   443
              else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u])
wenzelm@7606
   444
          | _ => raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
wenzelm@5819
   445
      | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
wenzelm@5819
   446
      | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
wenzelm@5819
   447
      | norm (f $ t) =
wenzelm@5819
   448
          ((case norm f of
wenzelm@5819
   449
            Abs (_, _, body) => normh (subst_bound (t, body))
wenzelm@5819
   450
          | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t)
wenzelm@5819
   451
      | norm _ =  raise SAME
wenzelm@5819
   452
    and normh t = norm t handle SAME => t
wenzelm@5819
   453
  in normh end;
wenzelm@5819
   454
wenzelm@5819
   455
wenzelm@6550
   456
(* dummy patterns *)
wenzelm@6550
   457
wenzelm@6762
   458
local
wenzelm@6762
   459
wenzelm@6560
   460
fun is_dummy (Const (c, _)) = c = PureThy.dummy_patternN
wenzelm@6550
   461
  | is_dummy _ = false;
wenzelm@6550
   462
wenzelm@6762
   463
fun prep_dummy (i, t) =
wenzelm@6762
   464
  if is_dummy t then (i + 1, Var (("_dummy_", i), Term.fastype_of t)) else (i, t);
wenzelm@6762
   465
wenzelm@6762
   466
in
wenzelm@6762
   467
wenzelm@6762
   468
fun prepare_dummies tm = #2 (Term.foldl_map_aterms prep_dummy (1, tm));
wenzelm@6762
   469
wenzelm@6550
   470
fun reject_dummies ctxt tm =
wenzelm@6550
   471
  if foldl_aterms (fn (ok, t) => ok andalso not (is_dummy t)) (true, tm) then tm
wenzelm@6550
   472
  else raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt);
wenzelm@6550
   473
wenzelm@6762
   474
end;
wenzelm@6550
   475
wenzelm@6550
   476
wenzelm@5819
   477
(* read terms *)
wenzelm@5819
   478
wenzelm@7670
   479
fun gen_read read app is_pat (ctxt as Context {defs = (_, _, used), ...}) s =
wenzelm@7663
   480
  (transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
wenzelm@7663
   481
    handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
wenzelm@7663
   482
    | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
wenzelm@7663
   483
  |> app (intern_skolem ctxt true)
wenzelm@7663
   484
  |> app (if is_pat then I else norm_term ctxt)
wenzelm@7663
   485
  |> app (if is_pat then prepare_dummies else (reject_dummies ctxt));
wenzelm@5819
   486
wenzelm@5874
   487
val read_termTs = gen_read (read_def_termTs false) (apfst o map) false;
wenzelm@8096
   488
val read_termT_pats = #1 oo gen_read (read_def_termTs false) (apfst o map) true;
wenzelm@8096
   489
wenzelm@8096
   490
fun read_term_pats T ctxt pats = read_termT_pats ctxt (map (rpair T) pats);
wenzelm@8096
   491
val read_prop_pats = read_term_pats propT;
wenzelm@8096
   492
wenzelm@6762
   493
val read_term = gen_read (read_term_sg true) I false;
wenzelm@6762
   494
val read_prop = gen_read (read_prop_sg true) I false;
wenzelm@5819
   495
wenzelm@5819
   496
wenzelm@5819
   497
(* certify terms *)
wenzelm@5819
   498
wenzelm@5819
   499
fun gen_cert cert is_pat ctxt t =
wenzelm@5819
   500
  (cert (sign_of ctxt) t handle TERM (msg, _) => raise CONTEXT (msg, ctxt))
wenzelm@5819
   501
  |> intern_skolem ctxt false
wenzelm@5819
   502
  |> (if is_pat then I else norm_term ctxt);
wenzelm@5819
   503
wenzelm@5819
   504
val cert_term = gen_cert cert_term_sg false;
wenzelm@5819
   505
val cert_prop = gen_cert cert_prop_sg false;
wenzelm@8096
   506
wenzelm@8096
   507
fun cert_term_pats _ = map o gen_cert cert_term_sg true;
wenzelm@8096
   508
val cert_prop_pats = map o gen_cert cert_prop_sg true;
wenzelm@5819
   509
wenzelm@5819
   510
wenzelm@5819
   511
(* declare terms *)
wenzelm@5819
   512
wenzelm@5819
   513
val ins_types = foldl_aterms
wenzelm@5819
   514
  (fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types)
wenzelm@5819
   515
    | (types, Var v) => Vartab.update (v, types)
wenzelm@5819
   516
    | (types, _) => types);
wenzelm@5819
   517
wenzelm@5819
   518
val ins_sorts = foldl_types (foldl_atyps
wenzelm@5819
   519
  (fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts)
wenzelm@5819
   520
    | (sorts, TVar v) => Vartab.update (v, sorts)
wenzelm@5819
   521
    | (sorts, _) => sorts));
wenzelm@5819
   522
wenzelm@5819
   523
val ins_used = foldl_types (foldl_atyps
wenzelm@5819
   524
  (fn (used, TFree (x, _)) => x ins used
wenzelm@5819
   525
    | (used, TVar ((x, _), _)) => x ins used
wenzelm@5819
   526
    | (used, _) => used));
wenzelm@5819
   527
wenzelm@7663
   528
fun ins_skolem def_ty = foldr
wenzelm@5994
   529
  (fn ((x, x'), types) =>
wenzelm@7663
   530
    (case def_ty x' of
wenzelm@5994
   531
      Some T => Vartab.update (((x, ~1), T), types)
wenzelm@5994
   532
    | None => types));
wenzelm@5994
   533
wenzelm@8373
   534
fun map_defaults f = map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   535
  (thy, data, asms, binds, thms, cases, f defs));
wenzelm@5819
   536
wenzelm@5994
   537
fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) =
wenzelm@5819
   538
  ctxt
wenzelm@7670
   539
  |> map_defaults (fn (types, sorts, used) => (ins_types (types, t), sorts, used))
wenzelm@7670
   540
  |> map_defaults (fn (types, sorts, used) => (types, ins_sorts (sorts, t), used))
wenzelm@7670
   541
  |> map_defaults (fn (types, sorts, used) => (types, sorts, ins_used (used, t)))
wenzelm@7670
   542
  |> map_defaults (fn (types, sorts, used) =>
wenzelm@7670
   543
      (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, used));
wenzelm@5819
   544
wenzelm@5819
   545
wenzelm@5819
   546
fun declare_term t ctxt = declare (ctxt, t);
wenzelm@5819
   547
fun declare_terms ts ctxt = foldl declare (ctxt, ts);
wenzelm@5819
   548
wenzelm@5819
   549
wenzelm@7925
   550
(* warn_extra_tfrees *)
wenzelm@7925
   551
wenzelm@7925
   552
fun warn_extra used1 used2 =
wenzelm@7925
   553
  if used1 = used2 then ()
wenzelm@7925
   554
  else
wenzelm@7925
   555
    (case Library.gen_rems (op =) (used2, used1) of
wenzelm@7925
   556
      [] => ()
wenzelm@8096
   557
    | extras => warning ("DANGER!! Just introduced free type variable(s): " ^ commas (rev extras)));
wenzelm@7925
   558
wenzelm@7925
   559
fun warn_extra_tfrees (ctxt1 as Context {defs = (_, _, used1), ...})
wenzelm@7925
   560
    (ctxt2 as Context {defs = (_, _, used2), ...}) = (warn_extra used1 used2; ctxt2);
wenzelm@7925
   561
wenzelm@7925
   562
wenzelm@5819
   563
wenzelm@5819
   564
(** bindings **)
wenzelm@5819
   565
wenzelm@5819
   566
(* update_binds *)
wenzelm@5819
   567
wenzelm@7606
   568
fun del_bind (ctxt, xi) =
wenzelm@7606
   569
  ctxt
wenzelm@8373
   570
  |> map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   571
      (thy, data, asms, Vartab.update ((xi, None), binds), thms, cases, defs));
wenzelm@7606
   572
wenzelm@5819
   573
fun upd_bind (ctxt, (xi, t)) =
wenzelm@5819
   574
  let val T = fastype_of t in
wenzelm@5819
   575
    ctxt
wenzelm@5819
   576
    |> declare_term t
wenzelm@8373
   577
    |> map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   578
        (thy, data, asms, Vartab.update ((xi, Some (t, T)), binds), thms, cases, defs))
wenzelm@5819
   579
  end;
wenzelm@5819
   580
wenzelm@7606
   581
fun del_upd_bind (ctxt, (xi, None)) = del_bind (ctxt, xi)
wenzelm@7606
   582
  | del_upd_bind (ctxt, (xi, Some t)) = upd_bind (ctxt, (xi, t));
wenzelm@7606
   583
wenzelm@5819
   584
fun update_binds bs ctxt = foldl upd_bind (ctxt, bs);
wenzelm@7663
   585
fun update_binds_env env =      (*Note: Envir.norm_term ensures proper type instantiation*)
wenzelm@7479
   586
  update_binds (map (apsnd (Envir.norm_term env)) (Envir.alist_of env));
wenzelm@5819
   587
wenzelm@7606
   588
fun delete_update_binds bs ctxt = foldl del_upd_bind (ctxt, bs);
wenzelm@7606
   589
wenzelm@5819
   590
wenzelm@8096
   591
(* simult_matches *)
wenzelm@8096
   592
wenzelm@8096
   593
fun simult_matches [] ctxt = ctxt
wenzelm@8096
   594
  | simult_matches pairs ctxt =
wenzelm@8096
   595
      let
wenzelm@8096
   596
        val maxidx = foldl (fn (i, (t1, t2)) =>
wenzelm@8096
   597
          Int.max (i, Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2))) (~1, pairs);
wenzelm@8096
   598
        val envs = Unify.smash_unifiers (sign_of ctxt, Envir.empty maxidx, pairs);
wenzelm@8096
   599
        val env =
wenzelm@8096
   600
          (case Seq.pull envs of
wenzelm@8096
   601
            None => raise CONTEXT ("Pattern match failed!", ctxt)      (* FIXME improve msg (!?) *)
wenzelm@8096
   602
          | Some (env, _) => env);
wenzelm@8096
   603
      in ctxt |> update_binds_env env end;
wenzelm@8096
   604
wenzelm@8096
   605
wenzelm@8096
   606
(* add_binds(_i) *)
wenzelm@5819
   607
wenzelm@7925
   608
local
wenzelm@7925
   609
wenzelm@5819
   610
fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) =
wenzelm@7606
   611
  ctxt |> delete_update_binds [(xi, apsome (prep ctxt) raw_t)];
wenzelm@5819
   612
wenzelm@7925
   613
fun gen_binds prep binds ctxt =
wenzelm@7925
   614
  warn_extra_tfrees ctxt (foldl (gen_bind prep) (ctxt, binds));
wenzelm@7925
   615
wenzelm@7925
   616
in
wenzelm@5819
   617
wenzelm@5819
   618
val add_binds = gen_binds read_term;
wenzelm@5819
   619
val add_binds_i = gen_binds cert_term;
wenzelm@7925
   620
val auto_bind_goal = add_binds_i o AutoBind.goal;
wenzelm@7925
   621
val auto_bind_facts = add_binds_i oo AutoBind.facts;
wenzelm@7925
   622
wenzelm@7925
   623
end;
wenzelm@5819
   624
wenzelm@5819
   625
wenzelm@8096
   626
(* match_bind(_i) *)
wenzelm@5819
   627
wenzelm@8096
   628
local
wenzelm@8096
   629
wenzelm@8096
   630
fun gen_bind (prep, prep_pats) (ctxt, (raw_pats, raw_t)) =
wenzelm@5819
   631
  let
wenzelm@5935
   632
    val t = prep ctxt raw_t;
wenzelm@8096
   633
    val ctxt' = declare_term t ctxt;
wenzelm@8096
   634
    val pats = prep_pats (fastype_of t) ctxt' raw_pats;
wenzelm@8096
   635
    val ctxt'' = ctxt' |> simult_matches (map (rpair t) pats);
wenzelm@8096
   636
  in ctxt'' end;
wenzelm@7670
   637
wenzelm@8096
   638
fun gen_binds prepp binds ctxt =
wenzelm@8096
   639
  warn_extra_tfrees ctxt (foldl (gen_bind prepp) (ctxt, binds));
wenzelm@8096
   640
wenzelm@8096
   641
in
wenzelm@5935
   642
wenzelm@8096
   643
val match_bind = gen_binds (read_term, read_term_pats);
wenzelm@8096
   644
val match_bind_i = gen_binds (cert_term, cert_term_pats);
wenzelm@8096
   645
wenzelm@8096
   646
end;
wenzelm@5935
   647
wenzelm@5935
   648
wenzelm@8096
   649
(* proposition patterns *)
wenzelm@5935
   650
wenzelm@8096
   651
fun prep_propp prep_prop prep_pats (ctxt, (raw_prop, (raw_pats1, raw_pats2))) =
wenzelm@6931
   652
  let
wenzelm@8096
   653
    val prop = prep_prop ctxt raw_prop;
wenzelm@8096
   654
    val ctxt' = declare_term prop ctxt;
wenzelm@8096
   655
wenzelm@8096
   656
    val pats = prep_pats ctxt' (raw_pats1 @ raw_pats2);    (*simultaneous type inference!*)
wenzelm@8096
   657
    val len1 = length raw_pats1;
wenzelm@8096
   658
  in (ctxt', (prop, (take (len1, pats), drop (len1, pats)))) end;
wenzelm@8096
   659
wenzelm@8096
   660
val read_propp = prep_propp read_prop read_prop_pats;
wenzelm@8096
   661
val cert_propp = prep_propp cert_prop cert_prop_pats;
wenzelm@5935
   662
wenzelm@8096
   663
wenzelm@8096
   664
fun gen_bind_propp prepp (ctxt, propp) =
wenzelm@8096
   665
  let
wenzelm@8096
   666
    val (ctxt', (prop, (pats1, pats2))) = prepp (ctxt, propp);
wenzelm@8096
   667
    val pairs = map (rpair prop) pats1 @ map (rpair (Logic.strip_imp_concl prop)) pats2;
wenzelm@8096
   668
  in (ctxt' |> simult_matches pairs, prop) end;
wenzelm@8096
   669
wenzelm@8096
   670
val bind_propp = gen_bind_propp read_propp;
wenzelm@8096
   671
val bind_propp_i = gen_bind_propp cert_propp;
wenzelm@6789
   672
wenzelm@6789
   673
wenzelm@5819
   674
wenzelm@5819
   675
(** theorems **)
wenzelm@5819
   676
wenzelm@6091
   677
(* get_thm(s) *)
wenzelm@5819
   678
wenzelm@6091
   679
fun get_thm (ctxt as Context {thy, thms, ...}) name =
wenzelm@5819
   680
  (case Symtab.lookup (thms, name) of
wenzelm@7606
   681
    Some (Some [th]) => th
wenzelm@7606
   682
  | Some (Some _) => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt)
wenzelm@7606
   683
  | _ => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
wenzelm@5819
   684
wenzelm@6091
   685
fun get_thms (ctxt as Context {thy, thms, ...}) name =
wenzelm@5819
   686
  (case Symtab.lookup (thms, name) of
wenzelm@7606
   687
    Some (Some ths) => ths
wenzelm@7606
   688
  | _ => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
wenzelm@5819
   689
wenzelm@6091
   690
fun get_thmss ctxt names = flat (map (get_thms ctxt) names);
wenzelm@5819
   691
wenzelm@5819
   692
wenzelm@6091
   693
(* put_thm(s) *)
wenzelm@5819
   694
wenzelm@7479
   695
fun put_thms ("", _) = I
wenzelm@7479
   696
  | put_thms (name, ths) = map_context
wenzelm@8373
   697
      (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   698
        (thy, data, asms, binds, Symtab.update ((name, Some ths), thms), cases, defs));
wenzelm@5819
   699
wenzelm@6091
   700
fun put_thm (name, th) = put_thms (name, [th]);
wenzelm@5819
   701
wenzelm@6091
   702
fun put_thmss [] ctxt = ctxt
wenzelm@6091
   703
  | put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths;
wenzelm@5819
   704
wenzelm@5819
   705
wenzelm@7606
   706
(* reset_thms *)
wenzelm@7606
   707
wenzelm@8373
   708
fun reset_thms name = map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8373
   709
  (thy, data, asms, binds, Symtab.update ((name, None), thms), cases, defs));
wenzelm@7606
   710
wenzelm@7606
   711
wenzelm@6091
   712
(* have_thmss *)
wenzelm@5819
   713
wenzelm@6875
   714
fun have_thmss more_ths name more_attrs ths_attrs ctxt =
wenzelm@5819
   715
  let
wenzelm@5819
   716
    fun app ((ct, ths), (th, attrs)) =
wenzelm@6091
   717
      let val (ct', th') = Thm.applys_attributes ((ct, th), attrs @ more_attrs)
wenzelm@5819
   718
      in (ct', th' :: ths) end
wenzelm@5819
   719
    val (ctxt', rev_thms) = foldl app ((ctxt, []), ths_attrs);
wenzelm@6875
   720
    val thms = flat (rev rev_thms) @ more_ths;
wenzelm@6091
   721
  in (ctxt' |> put_thms (name, thms), (name, thms)) end;
wenzelm@5819
   722
wenzelm@5819
   723
wenzelm@5819
   724
wenzelm@5819
   725
(** assumptions **)
wenzelm@5819
   726
wenzelm@5819
   727
(* get assumptions *)
wenzelm@5819
   728
wenzelm@6847
   729
fun assumptions (Context {asms = ((asms, _), _), ...}) = asms;
wenzelm@5819
   730
fun fixed_names (Context {asms = (_, (fixes, _)), ...}) = map #2 fixes;
wenzelm@5819
   731
wenzelm@5819
   732
wenzelm@5819
   733
(* assume *)
wenzelm@5819
   734
wenzelm@8186
   735
fun most_general_varify_tfrees thm =
wenzelm@8186
   736
  let
wenzelm@8186
   737
    val {hyps, prop, ...} = Thm.rep_thm thm;
wenzelm@8186
   738
    val frees = foldr Term.add_term_frees (prop :: hyps, []);
wenzelm@8186
   739
    val leave_tfrees = foldr Term.add_term_tfree_names (frees, []);
wenzelm@8186
   740
  in thm |> Thm.varifyT' leave_tfrees end;
wenzelm@8186
   741
wenzelm@8186
   742
wenzelm@7270
   743
local
wenzelm@6797
   744
wenzelm@7270
   745
fun gen_assm prepp tac (ctxt, (name, attrs, raw_prop_pats)) =
wenzelm@5819
   746
  let
wenzelm@8096
   747
    val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats);
wenzelm@5819
   748
wenzelm@7575
   749
    val cprops = map (Thm.cterm_of (sign_of ctxt')) props;
wenzelm@6847
   750
    val cprops_tac = map (rpair tac) cprops;
wenzelm@8186
   751
    val asms =
wenzelm@8186
   752
      map (most_general_varify_tfrees o Drule.forall_elim_vars 0 o Drule.assume_goal) cprops;
wenzelm@5919
   753
wenzelm@5919
   754
    val ths = map (fn th => ([th], [])) asms;
wenzelm@6091
   755
    val (ctxt'', (_, thms)) =
wenzelm@5819
   756
      ctxt'
wenzelm@6797
   757
      |> auto_bind_facts name props
wenzelm@6875
   758
      |> have_thmss [] name (attrs @ [Drule.tag_assumption]) ths;
wenzelm@5819
   759
wenzelm@5819
   760
    val ctxt''' =
wenzelm@5819
   761
      ctxt''
wenzelm@8373
   762
      |> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) =>
wenzelm@8373
   763
        (thy, data, ((asms_ct @ cprops_tac, asms_th @ [(name, asms)]), fixes),
wenzelm@8373
   764
          binds, thms, cases, defs));
wenzelm@7270
   765
  in (ctxt''', (name, thms)) end;
wenzelm@7270
   766
wenzelm@7270
   767
fun gen_assms prepp tac args ctxt =
wenzelm@7270
   768
  let val (ctxt', thmss) = foldl_map (gen_assm prepp tac) (ctxt, args)
wenzelm@7928
   769
  in (warn_extra_tfrees ctxt ctxt', (thmss, prems_of ctxt')) end;
wenzelm@5819
   770
wenzelm@7270
   771
in
wenzelm@7270
   772
wenzelm@7928
   773
val assume = gen_assms bind_propp;
wenzelm@7928
   774
val assume_i = gen_assms bind_propp_i;
wenzelm@7270
   775
wenzelm@7270
   776
end;
wenzelm@5819
   777
wenzelm@5819
   778
wenzelm@8096
   779
(* variables *)
wenzelm@8096
   780
wenzelm@8096
   781
fun prep_vars prep_typ check (ctxt, (xs, raw_T)) =
wenzelm@8096
   782
  let
wenzelm@8096
   783
    val _ = (case filter (not o Syntax.is_identifier) (map (check_skolem ctxt check) xs) of
wenzelm@8096
   784
      [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
wenzelm@8096
   785
wenzelm@8096
   786
    val opt_T = apsome (prep_typ ctxt) raw_T;
wenzelm@8096
   787
    val T = if_none opt_T TypeInfer.logicT;
wenzelm@8096
   788
    val ctxt' = ctxt |> declare_terms (map (fn x => Free (x, T)) xs);
wenzelm@8096
   789
  in (ctxt', (xs, opt_T)) end;
wenzelm@8096
   790
wenzelm@8096
   791
val read_vars = prep_vars read_typ true;
wenzelm@8096
   792
val cert_vars = prep_vars cert_typ false;
wenzelm@8096
   793
wenzelm@8096
   794
wenzelm@5819
   795
(* fix *)
wenzelm@5819
   796
wenzelm@8096
   797
local
wenzelm@8096
   798
wenzelm@8096
   799
fun add_vars xs (fixes, names) =
wenzelm@8096
   800
  let
wenzelm@8096
   801
    val xs' = variantlist (xs, names);
wenzelm@8096
   802
    val fixes' = (xs ~~ map Syntax.skolem xs') @ fixes;
wenzelm@8096
   803
    val names' = xs' @ names;
wenzelm@8096
   804
  in (fixes', names') end;
wenzelm@8096
   805
wenzelm@8373
   806
fun map_vars f = map_context (fn (thy, data, (assumes, vars), binds, thms, cases, defs) =>
wenzelm@8373
   807
  (thy, data, (assumes, f vars), binds, thms, cases, defs));
wenzelm@5819
   808
wenzelm@8096
   809
fun gen_fix prep raw_vars ctxt =
wenzelm@8096
   810
  let
wenzelm@8096
   811
    val (ctxt', varss) = foldl_map prep (ctxt, raw_vars);
wenzelm@8096
   812
    val xs = flat (map fst varss);
wenzelm@8096
   813
  in
wenzelm@8096
   814
    (case Library.duplicates xs of
wenzelm@8096
   815
      [] => ()
wenzelm@8096
   816
    | dups => raise CONTEXT ("Duplicate variable name(s): " ^ commas_quote dups, ctxt));
wenzelm@8096
   817
    ctxt' |> map_vars (add_vars xs)
wenzelm@8096
   818
  end;
wenzelm@5819
   819
wenzelm@8096
   820
in
wenzelm@7679
   821
wenzelm@8096
   822
val fix = gen_fix read_vars;
wenzelm@8096
   823
val fix_i = gen_fix cert_vars;
wenzelm@8096
   824
wenzelm@8096
   825
end;
wenzelm@5819
   826
wenzelm@6895
   827
wenzelm@5819
   828
wenzelm@8373
   829
(** cases **)
wenzelm@8373
   830
wenzelm@8403
   831
fun check_case ctxt name (xs, ts) =
wenzelm@8403
   832
  if null (foldr Term.add_typ_tvars (map snd xs, [])) andalso
wenzelm@8403
   833
    null (foldr Term.add_term_vars (ts, [])) then ()
wenzelm@8403
   834
  else raise CONTEXT ("Illegal schematic variable(s) in case " ^ quote name, ctxt);
wenzelm@8403
   835
wenzelm@8373
   836
fun get_case (ctxt as Context {cases, ...}) name =
wenzelm@8426
   837
  (case assoc (cases, name) of
wenzelm@8373
   838
    None => raise CONTEXT ("Unknown case: " ^ quote name, ctxt)
wenzelm@8403
   839
  | Some c => (check_case ctxt name c; c));
wenzelm@8373
   840
wenzelm@8384
   841
wenzelm@8373
   842
fun add_cases xs = map_context (fn (thy, data, asms, binds, thms, cases, defs) =>
wenzelm@8426
   843
  (thy, data, asms, binds, thms, rev (filter_out (equal "" o #1) xs) @ cases, defs));
wenzelm@8373
   844
wenzelm@8373
   845
wenzelm@8373
   846
wenzelm@5819
   847
(** theory setup **)
wenzelm@5819
   848
wenzelm@5819
   849
val setup = [ProofDataData.init];
wenzelm@5819
   850
wenzelm@5819
   851
wenzelm@5819
   852
end;