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