src/Pure/Isar/proof_context.ML
author wenzelm
Thu Mar 20 00:20:51 2008 +0100 (2008-03-20 ago)
changeset 26346 17debd2fff8e
parent 26336 a0e2b706ce73
child 26361 7946f459c6c8
permissions -rw-r--r--
simplified get_thm(s): back to plain name argument;
renamed former get_thm to get_fact_single, and get_thms to get_fact;
     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.add_setup
   575   (Context.theory_map (what (fn xs => fn ctxt =>
   576     let val xs' = f ctxt xs in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end)));
   577 
   578 in
   579 
   580 val _ = add (op =) (Syntax.add_typ_check 0 "standard") standard_typ_check;
   581 val _ = add (op aconv) (Syntax.add_term_check 0 "standard") standard_term_check;
   582 val _ = add (op aconv) (Syntax.add_term_check 100 "fixate") prepare_patterns;
   583 
   584 val _ = add (op aconv) (Syntax.add_term_uncheck 0 "standard") standard_term_uncheck;
   585 
   586 end;
   587 
   588 
   589 
   590 (** inner syntax operations **)
   591 
   592 local
   593 
   594 fun parse_sort ctxt str =
   595   Syntax.standard_parse_sort ctxt (syn_of ctxt) (Sign.intern_sort (theory_of ctxt)) str;
   596 
   597 fun parse_typ ctxt str =
   598   let
   599     val thy = ProofContext.theory_of ctxt;
   600     val get_sort = Sign.get_sort thy (Variable.def_sort ctxt);
   601     val T = Sign.intern_tycons thy
   602       (Syntax.standard_parse_typ ctxt (syn_of ctxt) get_sort (Sign.intern_sort thy) str);
   603   in T end
   604   handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
   605 
   606 fun parse_term T ctxt str =
   607   let
   608     val thy = theory_of ctxt;
   609     val (T', _) = TypeInfer.paramify_dummies T 0;
   610     fun check t = (Syntax.check_term ctxt (TypeInfer.constrain T' t); NONE)
   611       handle ERROR msg => SOME msg;
   612     val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
   613     val read = Syntax.standard_parse_term (Syntax.pp ctxt) check get_sort
   614       map_const map_free map_type map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) T';
   615   in read str end;
   616 
   617 
   618 fun unparse_sort ctxt S =
   619   Syntax.standard_unparse_sort ctxt (syn_of ctxt) (Sign.extern_sort (theory_of ctxt) S);
   620 
   621 fun unparse_typ ctxt T =
   622   Syntax.standard_unparse_typ ctxt (syn_of ctxt) (Sign.extern_typ (theory_of ctxt) T);
   623 
   624 fun unparse_term ctxt t =
   625   let
   626     val thy = theory_of ctxt;
   627     val syntax = syntax_of ctxt;
   628     val consts = consts_of ctxt;
   629   in
   630     t
   631     |> Sign.extern_term (Consts.extern_early consts) thy
   632     |> LocalSyntax.extern_term syntax
   633     |> Syntax.standard_unparse_term (Consts.extern consts) ctxt (LocalSyntax.syn_of syntax)
   634         (Context.exists_name Context.CPureN thy)
   635   end;
   636 
   637 in
   638 
   639 val _ = Syntax.install_operations
   640   {parse_sort = parse_sort,
   641    parse_typ = parse_typ,
   642    parse_term = parse_term dummyT,
   643    parse_prop = parse_term propT,
   644    unparse_sort = unparse_sort,
   645    unparse_typ = unparse_typ,
   646    unparse_term = unparse_term};
   647 
   648 end;
   649 
   650 
   651 
   652 (** export results **)
   653 
   654 fun common_export is_goal inner outer =
   655   map (Assumption.export is_goal inner outer) #>
   656   Variable.export inner outer;
   657 
   658 val goal_export = common_export true;
   659 val export = common_export false;
   660 
   661 fun export_morphism inner outer =
   662   Assumption.export_morphism inner outer $>
   663   Variable.export_morphism inner outer;
   664 
   665 
   666 
   667 (** bindings **)
   668 
   669 (* simult_matches *)
   670 
   671 fun simult_matches ctxt (t, pats) =
   672   (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
   673     NONE => error "Pattern match failed!"
   674   | SOME (env, _) => map (apsnd snd) (Envir.alist_of env));
   675 
   676 
   677 (* add_binds(_i) *)
   678 
   679 local
   680 
   681 fun gen_bind prep (xi as (x, _), raw_t) ctxt =
   682   ctxt
   683   |> Variable.add_binds [(xi, Option.map (prep (set_mode mode_default ctxt)) raw_t)];
   684 
   685 in
   686 
   687 fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
   688   | drop_schematic b = b;
   689 
   690 val add_binds = fold (gen_bind Syntax.read_term);
   691 val add_binds_i = fold (gen_bind cert_term);
   692 
   693 fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
   694 val auto_bind_goal = auto_bind AutoBind.goal;
   695 val auto_bind_facts = auto_bind AutoBind.facts;
   696 
   697 end;
   698 
   699 
   700 (* match_bind(_i) *)
   701 
   702 local
   703 
   704 fun gen_bind prep_terms gen raw_binds ctxt =
   705   let
   706     fun prep_bind (raw_pats, t) ctxt1 =
   707       let
   708         val T = Term.fastype_of t;
   709         val ctxt2 = Variable.declare_term t ctxt1;
   710         val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
   711         val binds = simult_matches ctxt2 (t, pats);
   712       in (binds, ctxt2) end;
   713 
   714     val ts = prep_terms ctxt dummyT (map snd raw_binds);
   715     val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
   716     val binds' =
   717       if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
   718       else binds;
   719     val binds'' = map (apsnd SOME) binds';
   720     val ctxt'' =
   721       tap (Variable.warn_extra_tfrees ctxt)
   722        (if gen then
   723           ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> add_binds_i binds''
   724         else ctxt' |> add_binds_i binds'');
   725   in (ts, ctxt'') end;
   726 
   727 in
   728 
   729 fun read_terms ctxt T =
   730   map (Syntax.parse_term ctxt #> TypeInfer.constrain T) #> Syntax.check_terms ctxt;
   731 
   732 val match_bind = gen_bind read_terms;
   733 val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
   734 
   735 end;
   736 
   737 
   738 (* propositions with patterns *)
   739 
   740 local
   741 
   742 fun prep_propp mode prep_props (context, args) =
   743   let
   744     fun prep (_, raw_pats) (ctxt, prop :: props) =
   745       let val ctxt' = Variable.declare_term prop ctxt
   746       in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
   747 
   748     val (propp, (context', _)) = (fold_map o fold_map) prep args
   749       (context, prep_props (set_mode mode context) (maps (map fst) args));
   750   in (context', propp) end;
   751 
   752 fun gen_bind_propp mode parse_prop (ctxt, raw_args) =
   753   let
   754     val (ctxt', args) = prep_propp mode parse_prop (ctxt, raw_args);
   755     val binds = flat (flat (map (map (simult_matches ctxt')) args));
   756     val propss = map (map #1) args;
   757 
   758     (*generalize result: context evaluated now, binds added later*)
   759     val gen = Variable.exportT_terms ctxt' ctxt;
   760     fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
   761   in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
   762 
   763 in
   764 
   765 val read_propp           = prep_propp mode_default Syntax.read_props;
   766 val cert_propp           = prep_propp mode_default (map o cert_prop);
   767 val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
   768 val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
   769 
   770 val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
   771 val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
   772 val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
   773 val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
   774 
   775 end;
   776 
   777 
   778 
   779 (** theorems **)
   780 
   781 (* fact_tac *)
   782 
   783 fun comp_incr_tac [] _ st = no_tac st
   784   | comp_incr_tac (th :: ths) i st =
   785       (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
   786 
   787 fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
   788 
   789 fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
   790   fact_tac (Facts.could_unify (facts_of ctxt) (Term.strip_all_body goal)) i);
   791 
   792 
   793 (* get_thm(s) *)
   794 
   795 fun retrieve_thms _ pick ctxt (Facts.Fact s) =
   796       let
   797         val prop = Syntax.read_prop (set_mode mode_default ctxt) s
   798           |> singleton (Variable.polymorphic ctxt);
   799         val th = Goal.prove ctxt [] [] prop (K (ALLGOALS (some_fact_tac ctxt)))
   800           handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
   801       in pick "" [th] end
   802   | retrieve_thms from_thy pick ctxt xthmref =
   803       let
   804         val thy = theory_of ctxt;
   805         val local_facts = facts_of ctxt;
   806         val space = Facts.space_of local_facts;
   807         val thmref = Facts.map_name_of_ref (NameSpace.intern space) xthmref;
   808         val name = Facts.name_of_ref thmref;
   809         val thms =
   810           if name = "" then [Thm.transfer thy Drule.dummy_thm]
   811           else
   812             (case Facts.lookup local_facts name of
   813               SOME ths => map (Thm.transfer thy) (Facts.select thmref ths)
   814             | NONE => from_thy thy xthmref);
   815       in pick name thms end;
   816 
   817 val get_fact_silent = retrieve_thms PureThy.get_fact_silent (K I);
   818 
   819 val get_fact = retrieve_thms PureThy.get_fact (K I);
   820 val get_fact_single = retrieve_thms PureThy.get_fact Facts.the_single;
   821 
   822 fun get_thms ctxt name = get_fact ctxt (Facts.Named (name, NONE));
   823 fun get_thm ctxt name = get_fact_single ctxt (Facts.Named (name, NONE));
   824 
   825 
   826 (* valid_thms *)
   827 
   828 fun valid_thms ctxt (name, ths) =
   829   (case try (fn () => get_fact_silent ctxt (Facts.Named (name, NONE))) () of
   830     NONE => false
   831   | SOME ths' => Thm.eq_thms (ths, ths'));
   832 
   833 
   834 (* name space operations *)
   835 
   836 val add_path        = map_naming o NameSpace.add_path;
   837 val no_base_names   = map_naming NameSpace.no_base_names;
   838 val qualified_names = map_naming NameSpace.qualified_names;
   839 val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
   840 val restore_naming  = map_naming o K o naming_of;
   841 val reset_naming    = map_naming (K local_naming);
   842 
   843 
   844 (* facts *)
   845 
   846 fun update_thms _ (bname, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt bname))
   847   | update_thms do_props (bname, SOME ths) ctxt = ctxt |> map_facts
   848       (Facts.add_local do_props (naming_of ctxt) (full_name ctxt bname, ths));
   849 
   850 fun put_thms do_props thms ctxt =
   851   ctxt |> map_naming (K local_naming) |> update_thms do_props thms |> restore_naming ctxt;
   852 
   853 local
   854 
   855 fun gen_note_thmss get k = fold_map (fn ((bname, more_attrs), raw_facts) => fn ctxt =>
   856   let
   857     val name = full_name ctxt bname;
   858     val facts = PureThy.name_thmss false name (map (apfst (get ctxt)) raw_facts);
   859     fun app (th, attrs) x =
   860       swap (foldl_map (Thm.proof_attributes (attrs @ more_attrs @ [PureThy.kind k])) (x, th));
   861     val (res, ctxt') = fold_map app facts ctxt;
   862     val thms = PureThy.name_thms false false name (flat res);
   863     val Mode {stmt, ...} = get_mode ctxt;
   864   in ((bname, thms), ctxt' |> update_thms stmt (bname, SOME thms)) end);
   865 
   866 in
   867 
   868 fun note_thmss k = gen_note_thmss get_fact k;
   869 fun note_thmss_i k = gen_note_thmss (K I) k;
   870 
   871 end;
   872 
   873 
   874 
   875 (** parameters **)
   876 
   877 (* variables *)
   878 
   879 fun declare_var (x, opt_T, mx) ctxt =
   880   let val T = (case opt_T of SOME T => T | NONE => Syntax.mixfixT mx)
   881   in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
   882 
   883 local
   884 
   885 fun prep_vars prep_typ internal =
   886   fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
   887     let
   888       val (x, mx) = Syntax.const_mixfix raw_x raw_mx;
   889       val _ = Syntax.is_identifier (no_skolem internal x) orelse
   890         error ("Illegal variable name: " ^ quote x);
   891 
   892       fun cond_tvars T =
   893         if internal then T
   894         else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
   895       val opt_T = Option.map (cond_tvars o cert_typ ctxt o prep_typ ctxt) raw_T;
   896       val var = (x, opt_T, mx);
   897     in (var, ctxt |> declare_var var |> #2) end);
   898 
   899 in
   900 
   901 val read_vars = prep_vars Syntax.parse_typ false;
   902 val cert_vars = prep_vars (K I) true;
   903 
   904 end;
   905 
   906 
   907 (* authentic constants *)
   908 
   909 local
   910 
   911 fun const_ast_tr intern ctxt [Syntax.Variable c] =
   912       let
   913         val Const (c', _) = read_const_proper ctxt c;
   914         val d = if intern then const_syntax_name ctxt c' else c;
   915       in Syntax.Constant d end
   916   | const_ast_tr _ _ asts = raise Syntax.AST ("const_ast_tr", asts);
   917 
   918 in
   919 
   920 val _ = Context.add_setup
   921  (Sign.add_syntax
   922    [("_context_const", "id => 'a", Delimfix "CONST _"),
   923     ("_context_const", "longid => 'a", Delimfix "CONST _"),
   924     ("_context_xconst", "id => 'a", Delimfix "XCONST _"),
   925     ("_context_xconst", "longid => 'a", Delimfix "XCONST _")] #>
   926   Sign.add_advanced_trfuns
   927     ([("_context_const", const_ast_tr true), ("_context_xconst", const_ast_tr false)], [], [], []));
   928 
   929 end;
   930 
   931 
   932 (* notation *)
   933 
   934 local
   935 
   936 fun const_syntax ctxt (Free (x, T), mx) = SOME (true, (x, T, mx))
   937   | const_syntax ctxt (Const (c, _), mx) =
   938       Option.map (pair false) (try (Consts.syntax (consts_of ctxt)) (c, mx))
   939   | const_syntax _ _ = NONE;
   940 
   941 in
   942 
   943 fun notation add mode args ctxt =
   944   ctxt |> map_syntax
   945     (LocalSyntax.update_modesyntax (theory_of ctxt) add mode (map_filter (const_syntax ctxt) args));
   946 
   947 fun target_notation add mode args phi =
   948   let val args' = filter (fn (t, _) => Type.similar_types (t, Morphism.term phi t)) args;
   949   in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
   950 
   951 end;
   952 
   953 
   954 (* local constants *)
   955 
   956 fun add_const_constraint (c, opt_T) ctxt =
   957   let
   958     fun prepT raw_T =
   959       let val T = cert_typ ctxt raw_T
   960       in cert_term ctxt (Const (c, T)); T end;
   961   in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
   962 
   963 fun add_abbrev mode tags (c, raw_t) ctxt =
   964   let
   965     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
   966       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
   967     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
   968     val ((lhs, rhs), consts') = consts_of ctxt
   969       |> Consts.abbreviate (Syntax.pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode tags (c, t);
   970   in
   971     ctxt
   972     |> (map_consts o apfst) (K consts')
   973     |> Variable.declare_term rhs
   974     |> pair (lhs, rhs)
   975   end;
   976 
   977 fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
   978 
   979 
   980 (* fixes *)
   981 
   982 local
   983 
   984 fun prep_mixfix (x, T, mx) =
   985   if mx <> NoSyn andalso mx <> Structure andalso
   986       (can Name.dest_internal x orelse can Name.dest_skolem x) then
   987     error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
   988   else (true, (x, T, mx));
   989 
   990 fun gen_fixes prep raw_vars ctxt =
   991   let
   992     val (vars, _) = prep raw_vars ctxt;
   993     val (xs', ctxt') = Variable.add_fixes (map #1 vars) ctxt;
   994   in
   995     ctxt'
   996     |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
   997     |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
   998     |> pair xs'
   999   end;
  1000 
  1001 in
  1002 
  1003 val add_fixes = gen_fixes read_vars;
  1004 val add_fixes_i = gen_fixes cert_vars;
  1005 
  1006 end;
  1007 
  1008 
  1009 (* fixes vs. frees *)
  1010 
  1011 fun auto_fixes (arg as (ctxt, (propss, x))) =
  1012   ((fold o fold) Variable.auto_fixes propss ctxt, (propss, x));
  1013 
  1014 fun bind_fixes xs ctxt =
  1015   let
  1016     val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
  1017     fun bind (t as Free (x, T)) =
  1018           if member (op =) xs x then
  1019             (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
  1020           else t
  1021       | bind (t $ u) = bind t $ bind u
  1022       | bind (Abs (x, T, t)) = Abs (x, T, bind t)
  1023       | bind a = a;
  1024   in (bind, ctxt') end;
  1025 
  1026 
  1027 
  1028 (** assumptions **)
  1029 
  1030 local
  1031 
  1032 fun gen_assms prepp exp args ctxt =
  1033   let
  1034     val cert = Thm.cterm_of (theory_of ctxt);
  1035     val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
  1036     val _ = Variable.warn_extra_tfrees ctxt ctxt1;
  1037     val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
  1038   in
  1039     ctxt2
  1040     |> auto_bind_facts (flat propss)
  1041     |> put_thms false (AutoBind.premsN, SOME (Assumption.prems_of ctxt2))
  1042     |> note_thmss_i Thm.assumptionK (map fst args ~~ map (map (fn th => ([th], []))) premss)
  1043   end;
  1044 
  1045 in
  1046 
  1047 val add_assms = gen_assms (apsnd #1 o bind_propp);
  1048 val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
  1049 
  1050 end;
  1051 
  1052 
  1053 
  1054 (** cases **)
  1055 
  1056 local
  1057 
  1058 fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
  1059 
  1060 fun add_case _ ("", _) cases = cases
  1061   | add_case _ (name, NONE) cases = rem_case name cases
  1062   | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
  1063 
  1064 fun prep_case name fxs c =
  1065   let
  1066     fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
  1067       | replace [] ys = ys
  1068       | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
  1069     val RuleCases.Case {fixes, assumes, binds, cases} = c;
  1070     val fixes' = replace fxs fixes;
  1071     val binds' = map drop_schematic binds;
  1072   in
  1073     if null (fold (Term.add_tvarsT o snd) fixes []) andalso
  1074       null (fold (fold Term.add_vars o snd) assumes []) then
  1075         RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
  1076     else error ("Illegal schematic variable(s) in case " ^ quote name)
  1077   end;
  1078 
  1079 fun fix (x, T) ctxt =
  1080   let
  1081     val (bind, ctxt') = bind_fixes [x] ctxt;
  1082     val t = bind (Free (x, T));
  1083   in (t, ctxt' |> Variable.declare_constraints t) end;
  1084 
  1085 in
  1086 
  1087 fun add_cases is_proper = map_cases o fold (add_case is_proper);
  1088 
  1089 fun case_result c ctxt =
  1090   let
  1091     val RuleCases.Case {fixes, ...} = c;
  1092     val (ts, ctxt') = ctxt |> fold_map fix fixes;
  1093     val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
  1094   in
  1095     ctxt'
  1096     |> add_binds_i (map drop_schematic binds)
  1097     |> add_cases true (map (apsnd SOME) cases)
  1098     |> pair (assumes, (binds, cases))
  1099   end;
  1100 
  1101 val apply_case = apfst fst oo case_result;
  1102 
  1103 fun get_case ctxt name xs =
  1104   (case AList.lookup (op =) (cases_of ctxt) name of
  1105     NONE => error ("Unknown case: " ^ quote name)
  1106   | SOME (c, _) => prep_case name xs c);
  1107 
  1108 end;
  1109 
  1110 
  1111 
  1112 (** print context information **)
  1113 
  1114 val debug = ref false;
  1115 
  1116 val verbose = ref false;
  1117 fun verb f x = if ! verbose then f (x ()) else [];
  1118 
  1119 fun setmp_verbose f x = Library.setmp verbose true f x;
  1120 
  1121 
  1122 (* local syntax *)
  1123 
  1124 val print_syntax = Syntax.print_syntax o syn_of;
  1125 
  1126 
  1127 (* abbreviations *)
  1128 
  1129 fun pretty_abbrevs show_globals ctxt =
  1130   let
  1131     val ((space, consts), (_, globals)) =
  1132       pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
  1133     fun add_abbr (_, (_, NONE)) = I
  1134       | add_abbr (c, (T, SOME t)) =
  1135           if not show_globals andalso Symtab.defined globals c then I
  1136           else cons (c, Logic.mk_equals (Const (c, T), t));
  1137     val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold add_abbr consts []));
  1138   in
  1139     if null abbrevs andalso not (! verbose) then []
  1140     else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
  1141   end;
  1142 
  1143 val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
  1144 
  1145 
  1146 (* term bindings *)
  1147 
  1148 fun pretty_binds ctxt =
  1149   let
  1150     val binds = Variable.binds_of ctxt;
  1151     fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
  1152   in
  1153     if Vartab.is_empty binds andalso not (! verbose) then []
  1154     else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
  1155   end;
  1156 
  1157 val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
  1158 
  1159 
  1160 (* local theorems *)
  1161 
  1162 fun pretty_lthms ctxt =
  1163   let
  1164     val local_facts = facts_of ctxt;
  1165     val props = Facts.props local_facts;
  1166     val facts = (if null props then I else cons ("unnamed", props)) (Facts.extern local_facts);
  1167   in
  1168     if null facts andalso not (! verbose) then []
  1169     else [Pretty.big_list "facts:" (map (pretty_fact ctxt) facts)]
  1170   end;
  1171 
  1172 val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
  1173 
  1174 
  1175 (* local contexts *)
  1176 
  1177 fun pretty_cases ctxt =
  1178   let
  1179     val prt_term = Syntax.pretty_term ctxt;
  1180 
  1181     fun prt_let (xi, t) = Pretty.block
  1182       [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
  1183         Pretty.quote (prt_term t)];
  1184 
  1185     fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
  1186       ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
  1187 
  1188     fun prt_sect _ _ _ [] = []
  1189       | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
  1190             flat (Library.separate sep (map (Library.single o prt) xs))))];
  1191 
  1192     fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks
  1193       (Pretty.str (name ^ ":") ::
  1194         prt_sect "fix" [] (Pretty.str o fst) fixes @
  1195         prt_sect "let" [Pretty.str "and"] prt_let
  1196           (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
  1197         (if forall (null o #2) asms then []
  1198           else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
  1199         prt_sect "subcases:" [] (Pretty.str o fst) cs));
  1200 
  1201     fun add_case (_, (_, false)) = I
  1202       | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
  1203           cons (name, (fixes, #1 (case_result c ctxt)));
  1204     val cases = fold add_case (cases_of ctxt) [];
  1205   in
  1206     if null cases andalso not (! verbose) then []
  1207     else [Pretty.big_list "cases:" (map prt_case cases)]
  1208   end;
  1209 
  1210 val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
  1211 
  1212 
  1213 (* core context *)
  1214 
  1215 val prems_limit = ref ~1;
  1216 
  1217 fun pretty_ctxt ctxt =
  1218   if ! prems_limit < 0 andalso not (! debug) then []
  1219   else
  1220     let
  1221       val prt_term = Syntax.pretty_term ctxt;
  1222 
  1223       (*structures*)
  1224       val structs = LocalSyntax.structs_of (syntax_of ctxt);
  1225       val prt_structs = if null structs then []
  1226         else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
  1227           Pretty.commas (map Pretty.str structs))];
  1228 
  1229       (*fixes*)
  1230       fun prt_fix (x, x') =
  1231         if x = x' then Pretty.str x
  1232         else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
  1233       val fixes =
  1234         rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
  1235           (Variable.fixes_of ctxt));
  1236       val prt_fixes = if null fixes then []
  1237         else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
  1238           Pretty.commas (map prt_fix fixes))];
  1239 
  1240       (*prems*)
  1241       val prems = Assumption.prems_of ctxt;
  1242       val len = length prems;
  1243       val suppressed = len - ! prems_limit;
  1244       val prt_prems = if null prems then []
  1245         else [Pretty.big_list "prems:" ((if suppressed <= 0 then [] else [Pretty.str "..."]) @
  1246           map (pretty_thm ctxt) (Library.drop (suppressed, prems)))];
  1247     in prt_structs @ prt_fixes @ prt_prems end;
  1248 
  1249 
  1250 (* main context *)
  1251 
  1252 fun pretty_context ctxt =
  1253   let
  1254     val prt_term = Syntax.pretty_term ctxt;
  1255     val prt_typ = Syntax.pretty_typ ctxt;
  1256     val prt_sort = Syntax.pretty_sort ctxt;
  1257 
  1258     (*theory*)
  1259     val pretty_thy = Pretty.block
  1260       [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
  1261 
  1262     (*defaults*)
  1263     fun prt_atom prt prtT (x, X) = Pretty.block
  1264       [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
  1265 
  1266     fun prt_var (x, ~1) = prt_term (Syntax.free x)
  1267       | prt_var xi = prt_term (Syntax.var xi);
  1268 
  1269     fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
  1270       | prt_varT xi = prt_typ (TVar (xi, []));
  1271 
  1272     val prt_defT = prt_atom prt_var prt_typ;
  1273     val prt_defS = prt_atom prt_varT prt_sort;
  1274 
  1275     val (types, sorts) = Variable.constraints_of ctxt;
  1276   in
  1277     verb single (K pretty_thy) @
  1278     pretty_ctxt ctxt @
  1279     verb (pretty_abbrevs false) (K ctxt) @
  1280     verb pretty_binds (K ctxt) @
  1281     verb pretty_lthms (K ctxt) @
  1282     verb pretty_cases (K ctxt) @
  1283     verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
  1284     verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
  1285   end;
  1286 
  1287 end;