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