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