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