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