src/Pure/Isar/proof_context.ML
author wenzelm
Fri Mar 28 20:02:04 2008 +0100 (2008-03-28 ago)
changeset 26463 9283b4185fdf
parent 26435 bdce320cd426
child 26673 cda3df424bad
permissions -rw-r--r--
Context.>> : operate on Context.generic;
     1 (*  Title:      Pure/Isar/proof_context.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The key concept of Isar proof contexts: elevates primitive local
     6 reasoning Gamma |- phi to a structured concept, with generic context
     7 elements.  See also structure Variable and Assumption.
     8 *)
     9 
    10 signature PROOF_CONTEXT =
    11 sig
    12   val theory_of: Proof.context -> theory
    13   val init: theory -> Proof.context
    14   type mode
    15   val mode_default: mode
    16   val mode_stmt: mode
    17   val mode_pattern: mode
    18   val mode_schematic: mode
    19   val mode_abbrev: mode
    20   val set_mode: mode -> Proof.context -> Proof.context
    21   val get_mode: Proof.context -> mode
    22   val restore_mode: Proof.context -> Proof.context -> Proof.context
    23   val set_stmt: bool -> Proof.context -> Proof.context
    24   val naming_of: Proof.context -> NameSpace.naming
    25   val full_name: Proof.context -> bstring -> string
    26   val consts_of: Proof.context -> Consts.T
    27   val const_syntax_name: Proof.context -> string -> string
    28   val the_const_constraint: Proof.context -> string -> typ
    29   val mk_const: Proof.context -> string * typ list -> term
    30   val set_syntax_mode: Syntax.mode -> Proof.context -> Proof.context
    31   val restore_syntax_mode: Proof.context -> Proof.context -> Proof.context
    32   val facts_of: Proof.context -> Facts.T
    33   val transfer: theory -> Proof.context -> Proof.context
    34   val theory: (theory -> theory) -> Proof.context -> Proof.context
    35   val theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
    36   val pretty_term_abbrev: Proof.context -> term -> Pretty.T
    37   val pretty_thm_legacy: thm -> Pretty.T
    38   val pretty_thm: Proof.context -> thm -> Pretty.T
    39   val pretty_thms: Proof.context -> thm list -> Pretty.T
    40   val pretty_fact: Proof.context -> string * thm list -> Pretty.T
    41   val pretty_proof: Proof.context -> Proofterm.proof -> Pretty.T
    42   val pretty_proof_of: Proof.context -> bool -> thm -> Pretty.T
    43   val string_of_thm: Proof.context -> thm -> string
    44   val read_typ: Proof.context -> string -> typ
    45   val read_typ_syntax: Proof.context -> string -> typ
    46   val read_typ_abbrev: Proof.context -> string -> typ
    47   val cert_typ: Proof.context -> typ -> typ
    48   val cert_typ_syntax: Proof.context -> typ -> typ
    49   val cert_typ_abbrev: Proof.context -> typ -> typ
    50   val get_skolem: Proof.context -> string -> string
    51   val revert_skolem: Proof.context -> string -> string
    52   val revert_skolems: Proof.context -> term -> term
    53   val infer_type: Proof.context -> string -> typ
    54   val inferred_param: string -> Proof.context -> (string * typ) * Proof.context
    55   val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
    56   val read_tyname: Proof.context -> string -> typ
    57   val read_const_proper: Proof.context -> string -> term
    58   val read_const: Proof.context -> string -> term
    59   val decode_term: Proof.context -> term -> term
    60   val standard_infer_types: Proof.context -> term list -> term list
    61   val read_term_pattern: Proof.context -> string -> term
    62   val read_term_schematic: Proof.context -> string -> term
    63   val read_term_abbrev: Proof.context -> string -> term
    64   val expand_abbrevs: Proof.context -> term -> term
    65   val cert_term: Proof.context -> term -> term
    66   val cert_prop: Proof.context -> term -> term
    67   val goal_export: Proof.context -> Proof.context -> thm list -> thm list
    68   val export: Proof.context -> Proof.context -> thm list -> thm list
    69   val export_morphism: Proof.context -> Proof.context -> morphism
    70   val add_binds: (indexname * string option) list -> Proof.context -> Proof.context
    71   val add_binds_i: (indexname * term option) list -> Proof.context -> Proof.context
    72   val auto_bind_goal: term list -> Proof.context -> Proof.context
    73   val auto_bind_facts: term list -> Proof.context -> Proof.context
    74   val match_bind: bool -> (string list * string) list -> Proof.context -> term list * Proof.context
    75   val match_bind_i: bool -> (term list * term) list -> Proof.context -> term list * Proof.context
    76   val read_propp: Proof.context * (string * string list) list list
    77     -> Proof.context * (term * term list) list list
    78   val cert_propp: Proof.context * (term * term list) list list
    79     -> Proof.context * (term * term list) list list
    80   val read_propp_schematic: Proof.context * (string * string list) list list
    81     -> Proof.context * (term * term list) list list
    82   val cert_propp_schematic: Proof.context * (term * term list) list list
    83     -> Proof.context * (term * term list) list list
    84   val bind_propp: Proof.context * (string * string list) list list
    85     -> Proof.context * (term list list * (Proof.context -> Proof.context))
    86   val bind_propp_i: Proof.context * (term * term list) list list
    87     -> Proof.context * (term list list * (Proof.context -> Proof.context))
    88   val bind_propp_schematic: Proof.context * (string * string list) list list
    89     -> Proof.context * (term list list * (Proof.context -> Proof.context))
    90   val bind_propp_schematic_i: Proof.context * (term * term list) list list
    91     -> Proof.context * (term list list * (Proof.context -> Proof.context))
    92   val fact_tac: thm list -> int -> tactic
    93   val some_fact_tac: Proof.context -> int -> tactic
    94   val get_fact: Proof.context -> Facts.ref -> thm list
    95   val get_fact_single: Proof.context -> Facts.ref -> thm
    96   val get_thms: Proof.context -> xstring -> thm list
    97   val get_thm: Proof.context -> xstring -> thm
    98   val valid_thms: Proof.context -> string * thm list -> bool
    99   val add_path: string -> Proof.context -> Proof.context
   100   val no_base_names: Proof.context -> Proof.context
   101   val qualified_names: Proof.context -> Proof.context
   102   val sticky_prefix: string -> Proof.context -> Proof.context
   103   val restore_naming: Proof.context -> Proof.context -> Proof.context
   104   val reset_naming: Proof.context -> Proof.context
   105   val put_thms: bool -> string * thm list option -> Proof.context -> Proof.context
   106   val note_thmss: string ->
   107     ((bstring * attribute list) * (Facts.ref * attribute list) list) list ->
   108       Proof.context -> (bstring * thm list) list * Proof.context
   109   val note_thmss_i: string ->
   110     ((bstring * attribute list) * (thm list * attribute list) list) list ->
   111       Proof.context -> (bstring * thm list) list * Proof.context
   112   val read_vars: (string * string option * mixfix) list -> Proof.context ->
   113     (string * typ option * mixfix) list * Proof.context
   114   val cert_vars: (string * typ option * mixfix) list -> Proof.context ->
   115     (string * typ option * mixfix) list * Proof.context
   116   val add_fixes: (string * string option * mixfix) list ->
   117     Proof.context -> string list * Proof.context
   118   val add_fixes_i: (string * typ option * mixfix) list ->
   119     Proof.context -> string list * Proof.context
   120   val auto_fixes: Proof.context * (term list list * 'a) -> Proof.context * (term list list * 'a)
   121   val bind_fixes: string list -> Proof.context -> (term -> term) * Proof.context
   122   val add_assms: Assumption.export ->
   123     ((string * attribute list) * (string * string list) list) list ->
   124     Proof.context -> (bstring * thm list) list * Proof.context
   125   val add_assms_i: Assumption.export ->
   126     ((string * attribute list) * (term * term list) list) list ->
   127     Proof.context -> (bstring * thm list) list * Proof.context
   128   val add_cases: bool -> (string * RuleCases.T option) list -> Proof.context -> Proof.context
   129   val apply_case: RuleCases.T -> Proof.context -> (string * term list) list * Proof.context
   130   val get_case: Proof.context -> string -> string option list -> RuleCases.T
   131   val notation: bool -> Syntax.mode -> (term * mixfix) list -> Proof.context -> Proof.context
   132   val target_notation: bool -> Syntax.mode -> (term * mixfix) list -> morphism ->
   133     Context.generic -> Context.generic
   134   val add_const_constraint: string * typ option -> Proof.context -> Proof.context
   135   val add_abbrev: string -> Markup.property list ->
   136     bstring * term -> Proof.context -> (term * term) * Proof.context
   137   val revert_abbrev: string -> string -> Proof.context -> Proof.context
   138   val verbose: bool ref
   139   val setmp_verbose: ('a -> 'b) -> 'a -> 'b
   140   val print_syntax: Proof.context -> unit
   141   val print_abbrevs: Proof.context -> unit
   142   val print_binds: Proof.context -> unit
   143   val print_lthms: Proof.context -> unit
   144   val print_cases: Proof.context -> unit
   145   val debug: bool ref
   146   val prems_limit: int ref
   147   val pretty_ctxt: Proof.context -> Pretty.T list
   148   val pretty_context: Proof.context -> Pretty.T list
   149 end;
   150 
   151 structure ProofContext: PROOF_CONTEXT =
   152 struct
   153 
   154 val theory_of = Context.theory_of_proof;
   155 val init = Context.init_proof;
   156 
   157 
   158 (** inner syntax mode **)
   159 
   160 datatype mode =
   161   Mode of
   162    {stmt: bool,                (*inner statement mode*)
   163     pattern: bool,             (*pattern binding schematic variables*)
   164     schematic: bool,           (*term referencing loose schematic variables*)
   165     abbrev: bool};             (*abbrev mode -- no normalization*)
   166 
   167 fun make_mode (stmt, pattern, schematic, abbrev) =
   168   Mode {stmt = stmt, pattern = pattern, schematic = schematic, abbrev = abbrev};
   169 
   170 val mode_default   = make_mode (false, false, false, false);
   171 val mode_stmt      = make_mode (true, false, false, false);
   172 val mode_pattern   = make_mode (false, true, false, false);
   173 val mode_schematic = make_mode (false, false, true, false);
   174 val mode_abbrev    = make_mode (false, false, false, true);
   175 
   176 
   177 
   178 (** Isar proof context information **)
   179 
   180 datatype ctxt =
   181   Ctxt of
   182    {mode: mode,                                       (*inner syntax mode*)
   183     naming: NameSpace.naming,                         (*local naming conventions*)
   184     syntax: LocalSyntax.T,                            (*local syntax*)
   185     consts: Consts.T * Consts.T,                      (*local/global consts*)
   186     facts: Facts.T,                                   (*local facts*)
   187     cases: (string * (RuleCases.T * bool)) list};     (*named case contexts*)
   188 
   189 fun make_ctxt (mode, naming, syntax, consts, facts, cases) =
   190   Ctxt {mode = mode, naming = naming, syntax = syntax,
   191     consts = consts, facts = facts, cases = cases};
   192 
   193 val local_naming = NameSpace.default_naming |> NameSpace.add_path "local";
   194 
   195 structure ContextData = ProofDataFun
   196 (
   197   type T = ctxt;
   198   fun init thy =
   199     make_ctxt (mode_default, local_naming, LocalSyntax.init thy,
   200       (Sign.consts_of thy, Sign.consts_of thy), Facts.empty, []);
   201 );
   202 
   203 fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
   204 
   205 fun map_context f =
   206   ContextData.map (fn Ctxt {mode, naming, syntax, consts, facts, cases} =>
   207     make_ctxt (f (mode, naming, syntax, consts, facts, cases)));
   208 
   209 fun set_mode mode = map_context (fn (_, naming, syntax, consts, facts, cases) =>
   210   (mode, naming, syntax, consts, facts, cases));
   211 
   212 fun map_mode f =
   213   map_context (fn (Mode {stmt, pattern, schematic, abbrev}, naming, syntax, consts, facts, cases) =>
   214     (make_mode (f (stmt, pattern, schematic, abbrev)), naming, syntax, consts, facts, cases));
   215 
   216 fun map_naming f =
   217   map_context (fn (mode, naming, syntax, consts, facts, cases) =>
   218     (mode, f naming, syntax, consts, facts, cases));
   219 
   220 fun map_syntax f =
   221   map_context (fn (mode, naming, syntax, consts, facts, cases) =>
   222     (mode, naming, f syntax, consts, facts, cases));
   223 
   224 fun map_consts f =
   225   map_context (fn (mode, naming, syntax, consts, facts, cases) =>
   226     (mode, naming, syntax, f consts, facts, cases));
   227 
   228 fun map_facts f =
   229   map_context (fn (mode, naming, syntax, consts, facts, cases) =>
   230     (mode, naming, syntax, consts, f facts, cases));
   231 
   232 fun map_cases f =
   233   map_context (fn (mode, naming, syntax, consts, facts, cases) =>
   234     (mode, naming, syntax, consts, facts, f cases));
   235 
   236 val get_mode = #mode o rep_context;
   237 fun restore_mode ctxt = set_mode (get_mode ctxt);
   238 
   239 fun set_stmt stmt =
   240   map_mode (fn (_, pattern, schematic, abbrev) => (stmt, pattern, schematic, abbrev));
   241 
   242 val naming_of = #naming o rep_context;
   243 val full_name = NameSpace.full o naming_of;
   244 
   245 val syntax_of = #syntax o rep_context;
   246 val syn_of = LocalSyntax.syn_of o syntax_of;
   247 val set_syntax_mode = map_syntax o LocalSyntax.set_mode;
   248 val restore_syntax_mode = map_syntax o LocalSyntax.restore_mode o syntax_of;
   249 
   250 val consts_of = #1 o #consts o rep_context;
   251 val const_syntax_name = Consts.syntax_name o consts_of;
   252 val the_const_constraint = Consts.the_constraint o consts_of;
   253 
   254 fun mk_const ctxt (c, Ts) = Const (c, Consts.instance (consts_of ctxt) (c, Ts));
   255 
   256 val facts_of = #facts o rep_context;
   257 val cases_of = #cases o rep_context;
   258 
   259 
   260 (* theory transfer *)
   261 
   262 fun transfer_syntax thy =
   263   map_syntax (LocalSyntax.rebuild thy) #>
   264   map_consts (fn consts as (local_consts, global_consts) =>
   265     let val thy_consts = Sign.consts_of thy in
   266       if Consts.eq_consts (thy_consts, global_consts) then consts
   267       else (Consts.merge (local_consts, thy_consts), thy_consts)
   268     end);
   269 
   270 fun transfer thy = Context.transfer_proof thy #> transfer_syntax thy;
   271 
   272 fun theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
   273 
   274 fun theory_result f ctxt =
   275   let val (res, thy') = f (theory_of ctxt)
   276   in (res, ctxt |> transfer thy') end;
   277 
   278 
   279 
   280 (** pretty printing **)
   281 
   282 fun pretty_term_abbrev ctxt = Syntax.pretty_term (set_mode mode_abbrev ctxt);
   283 
   284 fun pretty_thm_legacy th =
   285   let val thy = Thm.theory_of_thm th
   286   in Display.pretty_thm_aux (Syntax.pp (init thy)) true false [] th end;
   287 
   288 fun pretty_thm ctxt th =
   289   let val asms = map Thm.term_of (Assumption.assms_of ctxt)
   290   in Display.pretty_thm_aux (Syntax.pp ctxt) false true asms th end;
   291 
   292 fun pretty_thms ctxt [th] = pretty_thm ctxt th
   293   | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
   294 
   295 fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
   296   | pretty_fact ctxt (a, [th]) =
   297       Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th]
   298   | pretty_fact ctxt (a, ths) =
   299       Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths));
   300 
   301 fun pretty_proof ctxt prf =
   302   pretty_term_abbrev (ctxt |> transfer_syntax (ProofSyntax.proof_syntax prf (theory_of ctxt)))
   303     (ProofSyntax.term_of_proof prf);
   304 
   305 fun pretty_proof_of ctxt full th =
   306   pretty_proof ctxt (ProofSyntax.proof_of full th);
   307 
   308 val string_of_thm = Pretty.string_of oo pretty_thm;
   309 
   310 
   311 
   312 (** prepare types **)
   313 
   314 (* read_typ *)
   315 
   316 fun read_typ_mode mode ctxt s =
   317   Syntax.read_typ (Type.set_mode mode ctxt) s;
   318 
   319 val read_typ        = read_typ_mode Type.mode_default;
   320 val read_typ_syntax = read_typ_mode Type.mode_syntax;
   321 val read_typ_abbrev = read_typ_mode Type.mode_abbrev;
   322 
   323 
   324 (* cert_typ *)
   325 
   326 fun cert_typ_mode mode ctxt T =
   327   Sign.certify_typ_mode mode (theory_of ctxt) T
   328     handle TYPE (msg, _, _) => error msg;
   329 
   330 val cert_typ        = cert_typ_mode Type.mode_default;
   331 val cert_typ_syntax = cert_typ_mode Type.mode_syntax;
   332 val cert_typ_abbrev = cert_typ_mode Type.mode_abbrev;
   333 
   334 
   335 
   336 (** prepare variables **)
   337 
   338 (* internalize Skolem constants *)
   339 
   340 val lookup_skolem = AList.lookup (op =) o Variable.fixes_of;
   341 fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x);
   342 
   343 fun no_skolem internal x =
   344   if can Name.dest_skolem x then
   345     error ("Illegal reference to internal Skolem constant: " ^ quote x)
   346   else if not internal andalso can Name.dest_internal x then
   347     error ("Illegal reference to internal variable: " ^ quote x)
   348   else x;
   349 
   350 
   351 (* revert Skolem constants -- approximation only! *)
   352 
   353 fun revert_skolem ctxt =
   354   let
   355     val rev_fixes = map Library.swap (Variable.fixes_of ctxt);
   356     val revert = AList.lookup (op =) rev_fixes;
   357   in
   358     fn x =>
   359       (case revert x of
   360         SOME x' => x'
   361       | NONE => perhaps (try Name.dest_skolem) x)
   362    end;
   363 
   364 fun revert_skolems ctxt =
   365   let
   366     val revert = revert_skolem ctxt;
   367     fun reverts (Free (x, T)) = Free (revert x, T)
   368       | reverts (t $ u) = reverts t $ reverts u
   369       | reverts (Abs (x, T, t)) = Abs (x, T, reverts t)
   370       | reverts a = a;
   371   in reverts end;
   372 
   373 
   374 
   375 (** prepare terms and propositions **)
   376 
   377 (* inferred types of parameters *)
   378 
   379 fun infer_type ctxt x =
   380   Term.fastype_of (singleton (Syntax.check_terms (set_mode mode_schematic ctxt))
   381     (Free (x, dummyT)));
   382 
   383 fun inferred_param x ctxt =
   384   let val T = infer_type ctxt x
   385   in ((x, T), ctxt |> Variable.declare_term (Free (x, T))) end;
   386 
   387 fun inferred_fixes ctxt =
   388   fold_map inferred_param (rev (map #2 (Variable.fixes_of ctxt))) ctxt;
   389 
   390 
   391 (* type and constant names *)
   392 
   393 fun read_tyname ctxt c =
   394   if Syntax.is_tid c then
   395     TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (Variable.def_sort ctxt (c, ~1)))
   396   else
   397     let
   398       val thy = theory_of ctxt;
   399       val d = Sign.intern_type thy c;
   400     in Type (d, replicate (Sign.arity_number thy d) dummyT) end;
   401 
   402 fun read_const_proper ctxt c =
   403   (case Variable.lookup_const ctxt c of
   404     SOME d => Const (d, Consts.type_scheme (consts_of ctxt) d handle TYPE (msg, _, _) => error msg)
   405   | NONE => Consts.read_const (consts_of ctxt) c);
   406 
   407 fun read_const ctxt c =
   408   (case (lookup_skolem ctxt c, Variable.is_const ctxt c) of
   409     (SOME x, false) => Free (x, infer_type ctxt x)
   410   | _ => read_const_proper ctxt c);
   411 
   412 
   413 (* read_term *)
   414 
   415 fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
   416 
   417 val read_term_pattern   = read_term_mode mode_pattern;
   418 val read_term_schematic = read_term_mode mode_schematic;
   419 val read_term_abbrev    = read_term_mode mode_abbrev;
   420 
   421 
   422 (* local abbreviations *)
   423 
   424 val tsig_of = Sign.tsig_of o ProofContext.theory_of;
   425 
   426 local
   427 
   428 fun certify_consts ctxt =
   429   let val Mode {abbrev, ...} = get_mode ctxt
   430   in Consts.certify (Syntax.pp ctxt) (tsig_of ctxt) (not abbrev) (consts_of ctxt) end;
   431 
   432 fun reject_schematic (Var (xi, _)) =
   433       error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
   434   | reject_schematic (Abs (_, _, t)) = reject_schematic t
   435   | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
   436   | reject_schematic _ = ();
   437 
   438 fun expand_binds ctxt =
   439   let val Mode {pattern, schematic, ...} = get_mode ctxt in
   440     if pattern then I
   441     else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
   442   end;
   443 
   444 in
   445 
   446 fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
   447 
   448 end;
   449 
   450 
   451 fun contract_abbrevs ctxt t =
   452   let
   453     val thy = theory_of ctxt;
   454     val consts = consts_of ctxt;
   455     val Mode {abbrev, ...} = get_mode ctxt;
   456   in
   457     if abbrev orelse print_mode_active "no_abbrevs" orelse not (can Term.type_of t) then t
   458     else t |> Pattern.rewrite_term thy (Consts.abbrevs_of consts (print_mode_value () @ [""])) []
   459   end;
   460 
   461 
   462 (* patterns *)
   463 
   464 fun prepare_patternT ctxt =
   465   let val Mode {pattern, schematic, ...} = get_mode ctxt in
   466     if pattern orelse schematic then I
   467     else Term.map_atyps
   468       (fn T as TVar (xi, _) =>
   469             if not (TypeInfer.is_param xi)
   470             then error ("Illegal schematic type variable: " ^ Term.string_of_vname xi)
   471             else T
   472         | T => T)
   473   end;
   474 
   475 
   476 local
   477 
   478 fun prepare_dummies ts = #1 (fold_map Term.replace_dummy_patterns ts 1);
   479 
   480 fun reject_dummies t = Term.no_dummy_patterns t
   481   handle TERM _ => error "Illegal dummy pattern(s) in term";
   482 
   483 in
   484 
   485 fun prepare_patterns ctxt =
   486   let val Mode {pattern, ...} = get_mode ctxt in
   487     TypeInfer.fixate_params (Variable.names_of ctxt) #>
   488     pattern ? Variable.polymorphic ctxt #>
   489     (map o Term.map_types) (prepare_patternT ctxt) #>
   490     (if pattern then prepare_dummies else map reject_dummies)
   491   end;
   492 
   493 end;
   494 
   495 
   496 (* decoding raw terms (syntax trees) *)
   497 
   498 local
   499 
   500 fun intern_skolem ctxt def_type x =
   501   let
   502     val _ = no_skolem false x;
   503     val sko = lookup_skolem ctxt x;
   504     val is_const = can (read_const_proper ctxt) x orelse NameSpace.is_qualified x;
   505     val is_declared = is_some (def_type (x, ~1));
   506   in
   507     if Variable.is_const ctxt x then NONE
   508     else if is_some sko then sko
   509     else if not is_const orelse is_declared then SOME x
   510     else NONE
   511   end;
   512 
   513 in
   514 
   515 fun term_context ctxt =
   516   let val thy = theory_of ctxt in
   517    {get_sort = Sign.get_sort thy (Variable.def_sort ctxt),
   518     map_const = try (#1 o Term.dest_Const o read_const_proper ctxt),
   519     map_free = intern_skolem ctxt (Variable.def_type ctxt false),
   520     map_type = Sign.intern_tycons thy,
   521     map_sort = Sign.intern_sort thy}
   522   end;
   523 
   524 fun decode_term ctxt =
   525   let val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt
   526   in Syntax.decode_term get_sort map_const map_free map_type map_sort end;
   527 
   528 end;
   529 
   530 
   531 (* certify terms *)
   532 
   533 local
   534 
   535 fun gen_cert prop ctxt t =
   536   t
   537   |> expand_abbrevs ctxt
   538   |> (fn t' => #1 (Sign.certify' prop (Syntax.pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
   539     handle TYPE (msg, _, _) => error msg
   540       | TERM (msg, _) => error msg);
   541 
   542 in
   543 
   544 val cert_term = gen_cert false;
   545 val cert_prop = gen_cert true;
   546 
   547 end;
   548 
   549 
   550 (* type checking/inference *)
   551 
   552 fun standard_infer_types ctxt ts =
   553   let val Mode {pattern, ...} = get_mode ctxt in
   554     TypeInfer.infer_types (Syntax.pp ctxt) (tsig_of ctxt) (Syntax.check_typs ctxt)
   555       (try (Consts.the_constraint (consts_of ctxt))) (Variable.def_type ctxt pattern)
   556       (Variable.names_of ctxt) (Variable.maxidx_of ctxt) NONE (map (rpair dummyT) ts)
   557     |> #1 |> map #1
   558     handle TYPE (msg, _, _) => error msg
   559   end;
   560 
   561 local
   562 
   563 fun standard_typ_check ctxt =
   564   map (cert_typ_mode (Type.get_mode ctxt) ctxt) #>
   565   map (prepare_patternT ctxt);
   566 
   567 fun standard_term_check ctxt =
   568   standard_infer_types ctxt #>
   569   map (expand_abbrevs ctxt);
   570 
   571 fun standard_term_uncheck ctxt =
   572   map (contract_abbrevs ctxt);
   573 
   574 fun add eq what f = Context.>> (what (fn xs => fn ctxt =>
   575   let val xs' = f ctxt xs in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end));
   576 
   577 in
   578 
   579 val _ = add (op =) (Syntax.add_typ_check 0 "standard") standard_typ_check;
   580 val _ = add (op aconv) (Syntax.add_term_check 0 "standard") standard_term_check;
   581 val _ = add (op aconv) (Syntax.add_term_check 100 "fixate") prepare_patterns;
   582 
   583 val _ = add (op aconv) (Syntax.add_term_uncheck 0 "standard") standard_term_uncheck;
   584 
   585 end;
   586 
   587 
   588 
   589 (** inner syntax operations **)
   590 
   591 local
   592 
   593 fun parse_sort ctxt str =
   594   Syntax.standard_parse_sort ctxt (syn_of ctxt) (Sign.intern_sort (theory_of ctxt)) str;
   595 
   596 fun parse_typ ctxt str =
   597   let
   598     val thy = ProofContext.theory_of ctxt;
   599     val get_sort = Sign.get_sort thy (Variable.def_sort ctxt);
   600     val T = Sign.intern_tycons thy
   601       (Syntax.standard_parse_typ ctxt (syn_of ctxt) get_sort (Sign.intern_sort thy) str);
   602   in T end
   603   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   604 
   605 fun parse_term T ctxt str =
   606   let
   607     val thy = theory_of ctxt;
   608     val (T', _) = TypeInfer.paramify_dummies T 0;
   609     fun check t = (Syntax.check_term ctxt (TypeInfer.constrain T' t); NONE)
   610       handle ERROR msg => SOME msg;
   611     val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
   612     val read = Syntax.standard_parse_term (Syntax.pp ctxt) check get_sort
   613       map_const map_free map_type map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) T';
   614   in read str end;
   615 
   616 
   617 fun unparse_sort ctxt S =
   618   Syntax.standard_unparse_sort ctxt (syn_of ctxt) (Sign.extern_sort (theory_of ctxt) S);
   619 
   620 fun unparse_typ ctxt T =
   621   Syntax.standard_unparse_typ ctxt (syn_of ctxt) (Sign.extern_typ (theory_of ctxt) T);
   622 
   623 fun unparse_term ctxt t =
   624   let
   625     val thy = theory_of ctxt;
   626     val syntax = syntax_of ctxt;
   627     val consts = consts_of ctxt;
   628   in
   629     t
   630     |> Sign.extern_term (Consts.extern_early consts) thy
   631     |> LocalSyntax.extern_term syntax
   632     |> Syntax.standard_unparse_term (Consts.extern consts) ctxt (LocalSyntax.syn_of syntax)
   633         (Context.exists_name Context.CPureN thy)
   634   end;
   635 
   636 in
   637 
   638 val _ = Syntax.install_operations
   639   {parse_sort = parse_sort,
   640    parse_typ = parse_typ,
   641    parse_term = parse_term dummyT,
   642    parse_prop = parse_term propT,
   643    unparse_sort = unparse_sort,
   644    unparse_typ = unparse_typ,
   645    unparse_term = unparse_term};
   646 
   647 end;
   648 
   649 
   650 
   651 (** export results **)
   652 
   653 fun common_export is_goal inner outer =
   654   map (Assumption.export is_goal inner outer) #>
   655   Variable.export inner outer;
   656 
   657 val goal_export = common_export true;
   658 val export = common_export false;
   659 
   660 fun export_morphism inner outer =
   661   Assumption.export_morphism inner outer $>
   662   Variable.export_morphism inner outer;
   663 
   664 
   665 
   666 (** bindings **)
   667 
   668 (* simult_matches *)
   669 
   670 fun simult_matches ctxt (t, pats) =
   671   (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
   672     NONE => error "Pattern match failed!"
   673   | SOME (env, _) => map (apsnd snd) (Envir.alist_of env));
   674 
   675 
   676 (* add_binds(_i) *)
   677 
   678 local
   679 
   680 fun gen_bind prep (xi as (x, _), raw_t) ctxt =
   681   ctxt
   682   |> Variable.add_binds [(xi, Option.map (prep (set_mode mode_default ctxt)) raw_t)];
   683 
   684 in
   685 
   686 fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
   687   | drop_schematic b = b;
   688 
   689 val add_binds = fold (gen_bind Syntax.read_term);
   690 val add_binds_i = fold (gen_bind cert_term);
   691 
   692 fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
   693 val auto_bind_goal = auto_bind AutoBind.goal;
   694 val auto_bind_facts = auto_bind AutoBind.facts;
   695 
   696 end;
   697 
   698 
   699 (* match_bind(_i) *)
   700 
   701 local
   702 
   703 fun gen_bind prep_terms gen raw_binds ctxt =
   704   let
   705     fun prep_bind (raw_pats, t) ctxt1 =
   706       let
   707         val T = Term.fastype_of t;
   708         val ctxt2 = Variable.declare_term t ctxt1;
   709         val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
   710         val binds = simult_matches ctxt2 (t, pats);
   711       in (binds, ctxt2) end;
   712 
   713     val ts = prep_terms ctxt dummyT (map snd raw_binds);
   714     val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
   715     val binds' =
   716       if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
   717       else binds;
   718     val binds'' = map (apsnd SOME) binds';
   719     val ctxt'' =
   720       tap (Variable.warn_extra_tfrees ctxt)
   721        (if gen then
   722           ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> add_binds_i binds''
   723         else ctxt' |> add_binds_i binds'');
   724   in (ts, ctxt'') end;
   725 
   726 in
   727 
   728 fun read_terms ctxt T =
   729   map (Syntax.parse_term ctxt #> TypeInfer.constrain T) #> Syntax.check_terms ctxt;
   730 
   731 val match_bind = gen_bind read_terms;
   732 val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
   733 
   734 end;
   735 
   736 
   737 (* propositions with patterns *)
   738 
   739 local
   740 
   741 fun prep_propp mode prep_props (context, args) =
   742   let
   743     fun prep (_, raw_pats) (ctxt, prop :: props) =
   744       let val ctxt' = Variable.declare_term prop ctxt
   745       in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
   746 
   747     val (propp, (context', _)) = (fold_map o fold_map) prep args
   748       (context, prep_props (set_mode mode context) (maps (map fst) args));
   749   in (context', propp) end;
   750 
   751 fun gen_bind_propp mode parse_prop (ctxt, raw_args) =
   752   let
   753     val (ctxt', args) = prep_propp mode parse_prop (ctxt, raw_args);
   754     val binds = flat (flat (map (map (simult_matches ctxt')) args));
   755     val propss = map (map #1) args;
   756 
   757     (*generalize result: context evaluated now, binds added later*)
   758     val gen = Variable.exportT_terms ctxt' ctxt;
   759     fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
   760   in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
   761 
   762 in
   763 
   764 val read_propp           = prep_propp mode_default Syntax.read_props;
   765 val cert_propp           = prep_propp mode_default (map o cert_prop);
   766 val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
   767 val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
   768 
   769 val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
   770 val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
   771 val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
   772 val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
   773 
   774 end;
   775 
   776 
   777 
   778 (** theorems **)
   779 
   780 (* fact_tac *)
   781 
   782 fun comp_incr_tac [] _ st = no_tac st
   783   | comp_incr_tac (th :: ths) i st =
   784       (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
   785 
   786 fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
   787 
   788 fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
   789   fact_tac (Facts.could_unify (facts_of ctxt) (Term.strip_all_body goal)) i);
   790 
   791 
   792 (* get_thm(s) *)
   793 
   794 local
   795 
   796 fun retrieve_thms _ pick ctxt (Facts.Fact s) =
   797       let
   798         val prop = Syntax.read_prop (set_mode mode_default ctxt) s
   799           |> singleton (Variable.polymorphic ctxt);
   800         val th = Goal.prove ctxt [] [] prop (K (ALLGOALS (some_fact_tac ctxt)))
   801           handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
   802       in pick "" [th] end
   803   | retrieve_thms from_thy pick ctxt xthmref =
   804       let
   805         val thy = theory_of ctxt;
   806         val local_facts = facts_of ctxt;
   807         val space = Facts.space_of local_facts;
   808         val thmref = Facts.map_name_of_ref (NameSpace.intern space) xthmref;
   809         val name = Facts.name_of_ref thmref;
   810         val thms =
   811           if name = "" then [Thm.transfer thy Drule.dummy_thm]
   812           else
   813             (case Facts.lookup (Context.Proof ctxt) local_facts name of
   814               SOME ths => map (Thm.transfer thy) (Facts.select thmref ths)
   815             | NONE => from_thy thy xthmref);
   816       in pick name thms end;
   817 
   818 in
   819 
   820 val get_fact = retrieve_thms PureThy.get_fact (K I);
   821 val get_fact_single = retrieve_thms PureThy.get_fact Facts.the_single;
   822 
   823 fun get_thms_silent ctxt = retrieve_thms PureThy.get_fact_silent (K I) ctxt o Facts.named;
   824 fun get_thms ctxt = get_fact ctxt o Facts.named;
   825 fun get_thm ctxt = get_fact_single ctxt o Facts.named;
   826 
   827 end;
   828 
   829 
   830 (* valid_thms *)
   831 
   832 fun valid_thms ctxt (name, ths) =
   833   (case try (fn () => get_thms_silent ctxt name) () of
   834     NONE => false
   835   | SOME ths' => Thm.eq_thms (ths, ths'));
   836 
   837 
   838 (* name space operations *)
   839 
   840 val add_path        = map_naming o NameSpace.add_path;
   841 val no_base_names   = map_naming NameSpace.no_base_names;
   842 val qualified_names = map_naming NameSpace.qualified_names;
   843 val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
   844 val restore_naming  = map_naming o K o naming_of;
   845 val reset_naming    = map_naming (K local_naming);
   846 
   847 
   848 (* facts *)
   849 
   850 fun update_thms _ (bname, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt bname))
   851   | update_thms do_props (bname, SOME ths) ctxt = ctxt |> map_facts
   852       (Facts.add_local do_props (naming_of ctxt) (full_name ctxt bname, ths));
   853 
   854 fun put_thms do_props thms ctxt =
   855   ctxt |> map_naming (K local_naming) |> update_thms do_props thms |> restore_naming ctxt;
   856 
   857 local
   858 
   859 fun gen_note_thmss get k = fold_map (fn ((bname, more_attrs), raw_facts) => fn ctxt =>
   860   let
   861     val name = full_name ctxt bname;
   862     val facts = PureThy.name_thmss false name (map (apfst (get ctxt)) raw_facts);
   863     fun app (th, attrs) x =
   864       swap (foldl_map (Thm.proof_attributes (attrs @ more_attrs @ [PureThy.kind k])) (x, th));
   865     val (res, ctxt') = fold_map app facts ctxt;
   866     val thms = PureThy.name_thms false false name (flat res);
   867     val Mode {stmt, ...} = get_mode ctxt;
   868   in ((bname, thms), ctxt' |> update_thms stmt (bname, SOME thms)) end);
   869 
   870 in
   871 
   872 fun note_thmss k = gen_note_thmss get_fact k;
   873 fun note_thmss_i k = gen_note_thmss (K I) k;
   874 
   875 end;
   876 
   877 
   878 
   879 (** parameters **)
   880 
   881 (* variables *)
   882 
   883 fun declare_var (x, opt_T, mx) ctxt =
   884   let val T = (case opt_T of SOME T => T | NONE => Syntax.mixfixT mx)
   885   in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
   886 
   887 local
   888 
   889 fun prep_vars prep_typ internal =
   890   fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
   891     let
   892       val (x, mx) = Syntax.const_mixfix raw_x raw_mx;
   893       val _ = Syntax.is_identifier (no_skolem internal x) orelse
   894         error ("Illegal variable name: " ^ quote x);
   895 
   896       fun cond_tvars T =
   897         if internal then T
   898         else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
   899       val opt_T = Option.map (cond_tvars o cert_typ ctxt o prep_typ ctxt) raw_T;
   900       val var = (x, opt_T, mx);
   901     in (var, ctxt |> declare_var var |> #2) end);
   902 
   903 in
   904 
   905 val read_vars = prep_vars Syntax.parse_typ false;
   906 val cert_vars = prep_vars (K I) true;
   907 
   908 end;
   909 
   910 
   911 (* authentic constants *)
   912 
   913 local
   914 
   915 fun const_ast_tr intern ctxt [Syntax.Variable c] =
   916       let
   917         val Const (c', _) = read_const_proper ctxt c;
   918         val d = if intern then const_syntax_name ctxt c' else c;
   919       in Syntax.Constant d end
   920   | const_ast_tr _ _ asts = raise Syntax.AST ("const_ast_tr", asts);
   921 
   922 in
   923 
   924 val _ = Context.>> (Context.map_theory
   925  (Sign.add_syntax
   926    [("_context_const", "id => 'a", Delimfix "CONST _"),
   927     ("_context_const", "longid => 'a", Delimfix "CONST _"),
   928     ("_context_xconst", "id => 'a", Delimfix "XCONST _"),
   929     ("_context_xconst", "longid => 'a", Delimfix "XCONST _")] #>
   930   Sign.add_advanced_trfuns
   931     ([("_context_const", const_ast_tr true), ("_context_xconst", const_ast_tr false)], [], [], [])));
   932 
   933 end;
   934 
   935 
   936 (* notation *)
   937 
   938 local
   939 
   940 fun const_syntax ctxt (Free (x, T), mx) = SOME (true, (x, T, mx))
   941   | const_syntax ctxt (Const (c, _), mx) =
   942       Option.map (pair false) (try (Consts.syntax (consts_of ctxt)) (c, mx))
   943   | const_syntax _ _ = NONE;
   944 
   945 in
   946 
   947 fun notation add mode args ctxt =
   948   ctxt |> map_syntax
   949     (LocalSyntax.update_modesyntax (theory_of ctxt) add mode (map_filter (const_syntax ctxt) args));
   950 
   951 fun target_notation add mode args phi =
   952   let val args' = filter (fn (t, _) => Type.similar_types (t, Morphism.term phi t)) args;
   953   in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
   954 
   955 end;
   956 
   957 
   958 (* local constants *)
   959 
   960 fun add_const_constraint (c, opt_T) ctxt =
   961   let
   962     fun prepT raw_T =
   963       let val T = cert_typ ctxt raw_T
   964       in cert_term ctxt (Const (c, T)); T end;
   965   in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
   966 
   967 fun add_abbrev mode tags (c, raw_t) ctxt =
   968   let
   969     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
   970       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
   971     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
   972     val ((lhs, rhs), consts') = consts_of ctxt
   973       |> Consts.abbreviate (Syntax.pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode tags (c, t);
   974   in
   975     ctxt
   976     |> (map_consts o apfst) (K consts')
   977     |> Variable.declare_term rhs
   978     |> pair (lhs, rhs)
   979   end;
   980 
   981 fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
   982 
   983 
   984 (* fixes *)
   985 
   986 local
   987 
   988 fun prep_mixfix (x, T, mx) =
   989   if mx <> NoSyn andalso mx <> Structure andalso
   990       (can Name.dest_internal x orelse can Name.dest_skolem x) then
   991     error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
   992   else (true, (x, T, mx));
   993 
   994 fun gen_fixes prep raw_vars ctxt =
   995   let
   996     val (vars, _) = prep raw_vars ctxt;
   997     val (xs', ctxt') = Variable.add_fixes (map #1 vars) ctxt;
   998   in
   999     ctxt'
  1000     |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
  1001     |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
  1002     |> pair xs'
  1003   end;
  1004 
  1005 in
  1006 
  1007 val add_fixes = gen_fixes read_vars;
  1008 val add_fixes_i = gen_fixes cert_vars;
  1009 
  1010 end;
  1011 
  1012 
  1013 (* fixes vs. frees *)
  1014 
  1015 fun auto_fixes (arg as (ctxt, (propss, x))) =
  1016   ((fold o fold) Variable.auto_fixes propss ctxt, (propss, x));
  1017 
  1018 fun bind_fixes xs ctxt =
  1019   let
  1020     val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
  1021     fun bind (t as Free (x, T)) =
  1022           if member (op =) xs x then
  1023             (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
  1024           else t
  1025       | bind (t $ u) = bind t $ bind u
  1026       | bind (Abs (x, T, t)) = Abs (x, T, bind t)
  1027       | bind a = a;
  1028   in (bind, ctxt') end;
  1029 
  1030 
  1031 
  1032 (** assumptions **)
  1033 
  1034 local
  1035 
  1036 fun gen_assms prepp exp args ctxt =
  1037   let
  1038     val cert = Thm.cterm_of (theory_of ctxt);
  1039     val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
  1040     val _ = Variable.warn_extra_tfrees ctxt ctxt1;
  1041     val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
  1042   in
  1043     ctxt2
  1044     |> auto_bind_facts (flat propss)
  1045     |> put_thms false (AutoBind.premsN, SOME (Assumption.prems_of ctxt2))
  1046     |> note_thmss_i Thm.assumptionK (map fst args ~~ map (map (fn th => ([th], []))) premss)
  1047   end;
  1048 
  1049 in
  1050 
  1051 val add_assms = gen_assms (apsnd #1 o bind_propp);
  1052 val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
  1053 
  1054 end;
  1055 
  1056 
  1057 
  1058 (** cases **)
  1059 
  1060 local
  1061 
  1062 fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
  1063 
  1064 fun add_case _ ("", _) cases = cases
  1065   | add_case _ (name, NONE) cases = rem_case name cases
  1066   | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
  1067 
  1068 fun prep_case name fxs c =
  1069   let
  1070     fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
  1071       | replace [] ys = ys
  1072       | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
  1073     val RuleCases.Case {fixes, assumes, binds, cases} = c;
  1074     val fixes' = replace fxs fixes;
  1075     val binds' = map drop_schematic binds;
  1076   in
  1077     if null (fold (Term.add_tvarsT o snd) fixes []) andalso
  1078       null (fold (fold Term.add_vars o snd) assumes []) then
  1079         RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
  1080     else error ("Illegal schematic variable(s) in case " ^ quote name)
  1081   end;
  1082 
  1083 fun fix (x, T) ctxt =
  1084   let
  1085     val (bind, ctxt') = bind_fixes [x] ctxt;
  1086     val t = bind (Free (x, T));
  1087   in (t, ctxt' |> Variable.declare_constraints t) end;
  1088 
  1089 in
  1090 
  1091 fun add_cases is_proper = map_cases o fold (add_case is_proper);
  1092 
  1093 fun case_result c ctxt =
  1094   let
  1095     val RuleCases.Case {fixes, ...} = c;
  1096     val (ts, ctxt') = ctxt |> fold_map fix fixes;
  1097     val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
  1098   in
  1099     ctxt'
  1100     |> add_binds_i (map drop_schematic binds)
  1101     |> add_cases true (map (apsnd SOME) cases)
  1102     |> pair (assumes, (binds, cases))
  1103   end;
  1104 
  1105 val apply_case = apfst fst oo case_result;
  1106 
  1107 fun get_case ctxt name xs =
  1108   (case AList.lookup (op =) (cases_of ctxt) name of
  1109     NONE => error ("Unknown case: " ^ quote name)
  1110   | SOME (c, _) => prep_case name xs c);
  1111 
  1112 end;
  1113 
  1114 
  1115 
  1116 (** print context information **)
  1117 
  1118 val debug = ref false;
  1119 
  1120 val verbose = ref false;
  1121 fun verb f x = if ! verbose then f (x ()) else [];
  1122 
  1123 fun setmp_verbose f x = Library.setmp verbose true f x;
  1124 
  1125 
  1126 (* local syntax *)
  1127 
  1128 val print_syntax = Syntax.print_syntax o syn_of;
  1129 
  1130 
  1131 (* abbreviations *)
  1132 
  1133 fun pretty_abbrevs show_globals ctxt =
  1134   let
  1135     val ((space, consts), (_, globals)) =
  1136       pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
  1137     fun add_abbr (_, (_, NONE)) = I
  1138       | add_abbr (c, (T, SOME t)) =
  1139           if not show_globals andalso Symtab.defined globals c then I
  1140           else cons (c, Logic.mk_equals (Const (c, T), t));
  1141     val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold add_abbr consts []));
  1142   in
  1143     if null abbrevs andalso not (! verbose) then []
  1144     else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
  1145   end;
  1146 
  1147 val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
  1148 
  1149 
  1150 (* term bindings *)
  1151 
  1152 fun pretty_binds ctxt =
  1153   let
  1154     val binds = Variable.binds_of ctxt;
  1155     fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
  1156   in
  1157     if Vartab.is_empty binds andalso not (! verbose) then []
  1158     else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
  1159   end;
  1160 
  1161 val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
  1162 
  1163 
  1164 (* local theorems *)
  1165 
  1166 fun pretty_lthms ctxt =
  1167   let
  1168     val local_facts = facts_of ctxt;
  1169     val props = Facts.props local_facts;
  1170     val facts = (if null props then I else cons ("unnamed", props)) (Facts.extern local_facts);
  1171   in
  1172     if null facts andalso not (! verbose) then []
  1173     else [Pretty.big_list "facts:" (map (pretty_fact ctxt) facts)]
  1174   end;
  1175 
  1176 val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
  1177 
  1178 
  1179 (* local contexts *)
  1180 
  1181 fun pretty_cases ctxt =
  1182   let
  1183     val prt_term = Syntax.pretty_term ctxt;
  1184 
  1185     fun prt_let (xi, t) = Pretty.block
  1186       [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
  1187         Pretty.quote (prt_term t)];
  1188 
  1189     fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
  1190       ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
  1191 
  1192     fun prt_sect _ _ _ [] = []
  1193       | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
  1194             flat (Library.separate sep (map (Library.single o prt) xs))))];
  1195 
  1196     fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks
  1197       (Pretty.str (name ^ ":") ::
  1198         prt_sect "fix" [] (Pretty.str o fst) fixes @
  1199         prt_sect "let" [Pretty.str "and"] prt_let
  1200           (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
  1201         (if forall (null o #2) asms then []
  1202           else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
  1203         prt_sect "subcases:" [] (Pretty.str o fst) cs));
  1204 
  1205     fun add_case (_, (_, false)) = I
  1206       | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
  1207           cons (name, (fixes, #1 (case_result c ctxt)));
  1208     val cases = fold add_case (cases_of ctxt) [];
  1209   in
  1210     if null cases andalso not (! verbose) then []
  1211     else [Pretty.big_list "cases:" (map prt_case cases)]
  1212   end;
  1213 
  1214 val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
  1215 
  1216 
  1217 (* core context *)
  1218 
  1219 val prems_limit = ref ~1;
  1220 
  1221 fun pretty_ctxt ctxt =
  1222   if ! prems_limit < 0 andalso not (! debug) then []
  1223   else
  1224     let
  1225       val prt_term = Syntax.pretty_term ctxt;
  1226 
  1227       (*structures*)
  1228       val structs = LocalSyntax.structs_of (syntax_of ctxt);
  1229       val prt_structs = if null structs then []
  1230         else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
  1231           Pretty.commas (map Pretty.str structs))];
  1232 
  1233       (*fixes*)
  1234       fun prt_fix (x, x') =
  1235         if x = x' then Pretty.str x
  1236         else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
  1237       val fixes =
  1238         rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
  1239           (Variable.fixes_of ctxt));
  1240       val prt_fixes = if null fixes then []
  1241         else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
  1242           Pretty.commas (map prt_fix fixes))];
  1243 
  1244       (*prems*)
  1245       val prems = Assumption.prems_of ctxt;
  1246       val len = length prems;
  1247       val suppressed = len - ! prems_limit;
  1248       val prt_prems = if null prems then []
  1249         else [Pretty.big_list "prems:" ((if suppressed <= 0 then [] else [Pretty.str "..."]) @
  1250           map (pretty_thm ctxt) (Library.drop (suppressed, prems)))];
  1251     in prt_structs @ prt_fixes @ prt_prems end;
  1252 
  1253 
  1254 (* main context *)
  1255 
  1256 fun pretty_context ctxt =
  1257   let
  1258     val prt_term = Syntax.pretty_term ctxt;
  1259     val prt_typ = Syntax.pretty_typ ctxt;
  1260     val prt_sort = Syntax.pretty_sort ctxt;
  1261 
  1262     (*theory*)
  1263     val pretty_thy = Pretty.block
  1264       [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
  1265 
  1266     (*defaults*)
  1267     fun prt_atom prt prtT (x, X) = Pretty.block
  1268       [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
  1269 
  1270     fun prt_var (x, ~1) = prt_term (Syntax.free x)
  1271       | prt_var xi = prt_term (Syntax.var xi);
  1272 
  1273     fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
  1274       | prt_varT xi = prt_typ (TVar (xi, []));
  1275 
  1276     val prt_defT = prt_atom prt_var prt_typ;
  1277     val prt_defS = prt_atom prt_varT prt_sort;
  1278 
  1279     val (types, sorts) = Variable.constraints_of ctxt;
  1280   in
  1281     verb single (K pretty_thy) @
  1282     pretty_ctxt ctxt @
  1283     verb (pretty_abbrevs false) (K ctxt) @
  1284     verb pretty_binds (K ctxt) @
  1285     verb pretty_lthms (K ctxt) @
  1286     verb pretty_cases (K ctxt) @
  1287     verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
  1288     verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
  1289   end;
  1290 
  1291 end;