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