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