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