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