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