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