src/Pure/Isar/proof_context.ML
author wenzelm
Thu Sep 24 23:33:29 2015 +0200 (2015-09-24)
changeset 61261 ddb2da7cb2e4
parent 61168 dcdfb6355a05
child 61262 7bd1eb4b056e
permissions -rw-r--r--
more explicit Defs.context: use proper name spaces as far as possible;
     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   val get_global: theory -> string -> Proof.context
    14   type mode
    15   val mode_default: mode
    16   val mode_stmt: mode
    17   val mode_pattern: mode
    18   val mode_schematic: mode
    19   val mode_abbrev: mode
    20   val set_mode: mode -> Proof.context -> Proof.context
    21   val get_mode: Proof.context -> mode
    22   val restore_mode: Proof.context -> Proof.context -> Proof.context
    23   val abbrev_mode: Proof.context -> bool
    24   val set_stmt: bool -> Proof.context -> Proof.context
    25   val syn_of: Proof.context -> Syntax.syntax
    26   val tsig_of: Proof.context -> Type.tsig
    27   val set_defsort: sort -> Proof.context -> Proof.context
    28   val default_sort: Proof.context -> indexname -> sort
    29   val arity_sorts: Proof.context -> string -> sort -> sort list
    30   val consts_of: Proof.context -> Consts.T
    31   val set_syntax_mode: Syntax.mode -> Proof.context -> Proof.context
    32   val restore_syntax_mode: Proof.context -> Proof.context -> Proof.context
    33   val map_naming: (Name_Space.naming -> Name_Space.naming) -> Proof.context -> Proof.context
    34   val naming_of: Proof.context -> Name_Space.naming
    35   val restore_naming: Proof.context -> Proof.context -> Proof.context
    36   val full_name: Proof.context -> binding -> string
    37   val get_scope: Proof.context -> Binding.scope option
    38   val new_scope: Proof.context -> Binding.scope * Proof.context
    39   val private_scope: Binding.scope -> Proof.context -> Proof.context
    40   val private: Position.T -> Proof.context -> Proof.context
    41   val qualified_scope: Binding.scope -> Proof.context -> Proof.context
    42   val qualified: Position.T -> Proof.context -> Proof.context
    43   val concealed: Proof.context -> Proof.context
    44   val class_space: Proof.context -> Name_Space.T
    45   val type_space: Proof.context -> Name_Space.T
    46   val const_space: Proof.context -> Name_Space.T
    47   val defs_context: Proof.context -> Defs.context
    48   val intern_class: Proof.context -> xstring -> string
    49   val intern_type: Proof.context -> xstring -> string
    50   val intern_const: Proof.context -> xstring -> string
    51   val extern_class: Proof.context -> string -> xstring
    52   val markup_class: Proof.context -> string -> string
    53   val pretty_class: Proof.context -> string -> Pretty.T
    54   val extern_type: Proof.context -> string -> xstring
    55   val markup_type: Proof.context -> string -> string
    56   val pretty_type: Proof.context -> string -> Pretty.T
    57   val extern_const: Proof.context -> string -> xstring
    58   val markup_const: Proof.context -> string -> string
    59   val pretty_const: Proof.context -> string -> Pretty.T
    60   val transfer: theory -> Proof.context -> Proof.context
    61   val transfer_facts: theory -> Proof.context -> Proof.context
    62   val background_theory: (theory -> theory) -> Proof.context -> Proof.context
    63   val background_theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
    64   val facts_of: Proof.context -> Facts.T
    65   val facts_of_fact: Proof.context -> string -> Facts.T
    66   val markup_extern_fact: Proof.context -> string -> Markup.T * xstring
    67   val pretty_term_abbrev: Proof.context -> term -> Pretty.T
    68   val pretty_fact: Proof.context -> string * thm list -> Pretty.T
    69   val check_class: Proof.context -> xstring * Position.T -> class * Position.report list
    70   val read_class: Proof.context -> string -> class
    71   val read_typ: Proof.context -> string -> typ
    72   val read_typ_syntax: Proof.context -> string -> typ
    73   val read_typ_abbrev: Proof.context -> string -> typ
    74   val cert_typ: Proof.context -> typ -> typ
    75   val cert_typ_syntax: Proof.context -> typ -> typ
    76   val cert_typ_abbrev: Proof.context -> typ -> typ
    77   val infer_type: Proof.context -> string * typ -> typ
    78   val inferred_param: string -> Proof.context -> (string * typ) * Proof.context
    79   val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
    80   val check_type_name: {proper: bool, strict: bool} -> Proof.context ->
    81     xstring * Position.T -> typ * Position.report list
    82   val read_type_name: {proper: bool, strict: bool} -> Proof.context -> string -> typ
    83   val consts_completion_message: Proof.context -> xstring * Position.T list -> string
    84   val check_const: {proper: bool, strict: bool} -> Proof.context ->
    85     xstring * Position.T list -> term * Position.report list
    86   val read_const: {proper: bool, strict: bool} -> Proof.context -> string -> term
    87   val read_arity: Proof.context -> xstring * string list * string -> arity
    88   val cert_arity: Proof.context -> arity -> arity
    89   val allow_dummies: Proof.context -> Proof.context
    90   val prepare_sortsT: Proof.context -> typ list -> string list * typ list
    91   val prepare_sorts: Proof.context -> term list -> string list * term list
    92   val check_tfree: Proof.context -> string * sort -> string * sort
    93   val read_term_pattern: Proof.context -> string -> term
    94   val read_term_schematic: Proof.context -> string -> term
    95   val read_term_abbrev: Proof.context -> string -> term
    96   val show_abbrevs_raw: Config.raw
    97   val show_abbrevs: bool Config.T
    98   val expand_abbrevs: Proof.context -> term -> term
    99   val cert_term: Proof.context -> term -> term
   100   val cert_prop: Proof.context -> term -> term
   101   val def_type: Proof.context -> indexname -> typ option
   102   val standard_typ_check: Proof.context -> typ list -> typ list
   103   val standard_term_check_finish: Proof.context -> term list -> term list
   104   val standard_term_uncheck: Proof.context -> term list -> term list
   105   val goal_export: Proof.context -> Proof.context -> thm list -> thm list
   106   val export: Proof.context -> Proof.context -> thm list -> thm list
   107   val export_morphism: Proof.context -> Proof.context -> morphism
   108   val norm_export_morphism: Proof.context -> Proof.context -> morphism
   109   val auto_bind_goal: term list -> Proof.context -> Proof.context
   110   val auto_bind_facts: term list -> Proof.context -> Proof.context
   111   val match_bind: bool -> (term list * term) list -> Proof.context ->
   112     term list * Proof.context
   113   val match_bind_cmd: bool -> (string list * string) list -> Proof.context ->
   114     term list * Proof.context
   115   val cert_propp: Proof.context -> (term * term list) list list ->
   116     (term list list * (indexname * term) list)
   117   val read_propp: Proof.context -> (string * string list) list list ->
   118     (term list list * (indexname * term) list)
   119   val fact_tac: Proof.context -> thm list -> int -> tactic
   120   val some_fact_tac: Proof.context -> int -> tactic
   121   val get_fact_generic: Context.generic -> Facts.ref -> string option * thm list
   122   val get_fact: Proof.context -> Facts.ref -> thm list
   123   val get_fact_single: Proof.context -> Facts.ref -> thm
   124   val get_thms: Proof.context -> xstring -> thm list
   125   val get_thm: Proof.context -> xstring -> thm
   126   val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list ->
   127     Proof.context -> (string * thm list) list * Proof.context
   128   val put_thms: bool -> string * thm list option -> Proof.context -> Proof.context
   129   val read_var: binding * string option * mixfix -> Proof.context ->
   130     (binding * typ option * mixfix) * Proof.context
   131   val cert_var: binding * typ option * mixfix -> Proof.context ->
   132     (binding * typ option * mixfix) * Proof.context
   133   val add_fixes: (binding * typ option * mixfix) list -> Proof.context ->
   134     string list * Proof.context
   135   val add_fixes_cmd: (binding * string option * mixfix) list -> Proof.context ->
   136     string list * Proof.context
   137   val add_assms: Assumption.export ->
   138     (Thm.binding * (term * term list) list) list ->
   139     Proof.context -> (string * thm list) list * Proof.context
   140   val add_assms_cmd: Assumption.export ->
   141     (Thm.binding * (string * string list) list) list ->
   142     Proof.context -> (string * thm list) list * Proof.context
   143   val dest_cases: Proof.context -> (string * (Rule_Cases.T * {legacy: bool})) list
   144   val update_cases: (string * Rule_Cases.T option) list -> Proof.context -> Proof.context
   145   val update_cases_legacy: (string * Rule_Cases.T option) list -> Proof.context -> Proof.context
   146   val apply_case: Rule_Cases.T -> Proof.context -> (string * term list) list * Proof.context
   147   val check_case: Proof.context -> bool ->
   148     string * Position.T -> binding option list -> Rule_Cases.T
   149   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> Proof.context -> Proof.context
   150   val notation: bool -> Syntax.mode -> (term * mixfix) list -> Proof.context -> Proof.context
   151   val generic_type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> morphism ->
   152     Context.generic -> Context.generic
   153   val generic_notation: bool -> Syntax.mode -> (term * mixfix) list -> morphism ->
   154     Context.generic -> Context.generic
   155   val class_alias: binding -> class -> Proof.context -> Proof.context
   156   val type_alias: binding -> string -> Proof.context -> Proof.context
   157   val const_alias: binding -> string -> Proof.context -> Proof.context
   158   val fact_alias: binding -> string -> Proof.context -> Proof.context
   159   val add_const_constraint: string * typ option -> Proof.context -> Proof.context
   160   val add_abbrev: string -> binding * term -> Proof.context -> (term * term) * Proof.context
   161   val revert_abbrev: string -> string -> Proof.context -> Proof.context
   162   val generic_add_abbrev: string -> binding * term -> Context.generic ->
   163     (term * term) * Context.generic
   164   val generic_revert_abbrev: string -> string -> Context.generic -> Context.generic
   165   val print_syntax: Proof.context -> unit
   166   val print_abbrevs: bool -> Proof.context -> unit
   167   val pretty_term_bindings: Proof.context -> Pretty.T list
   168   val pretty_local_facts: bool -> Proof.context -> Pretty.T list
   169   val print_local_facts: bool -> Proof.context -> unit
   170   val pretty_cases: Proof.context -> Pretty.T list
   171   val debug: bool Config.T
   172   val verbose: bool Config.T
   173   val pretty_ctxt: Proof.context -> Pretty.T list
   174   val pretty_context: Proof.context -> Pretty.T list
   175 end;
   176 
   177 structure Proof_Context: PROOF_CONTEXT =
   178 struct
   179 
   180 val theory_of = Proof_Context.theory_of;
   181 val init_global = Proof_Context.init_global;
   182 val get_global = Proof_Context.get_global;
   183 
   184 
   185 
   186 (** inner syntax mode **)
   187 
   188 datatype mode =
   189   Mode of
   190    {stmt: bool,                (*inner statement mode*)
   191     pattern: bool,             (*pattern binding schematic variables*)
   192     schematic: bool,           (*term referencing loose schematic variables*)
   193     abbrev: bool};             (*abbrev mode -- no normalization*)
   194 
   195 fun make_mode (stmt, pattern, schematic, abbrev) =
   196   Mode {stmt = stmt, pattern = pattern, schematic = schematic, abbrev = abbrev};
   197 
   198 val mode_default   = make_mode (false, false, false, false);
   199 val mode_stmt      = make_mode (true, false, false, false);
   200 val mode_pattern   = make_mode (false, true, false, false);
   201 val mode_schematic = make_mode (false, false, true, false);
   202 val mode_abbrev    = make_mode (false, false, false, true);
   203 
   204 
   205 
   206 (** Isar proof context information **)
   207 
   208 type cases = ((Rule_Cases.T * {legacy: bool}) * int) Name_Space.table;
   209 val empty_cases: cases = Name_Space.empty_table Markup.caseN;
   210 
   211 datatype data =
   212   Data of
   213    {mode: mode,                  (*inner syntax mode*)
   214     syntax: Local_Syntax.T,      (*local syntax*)
   215     tsig: Type.tsig * Type.tsig, (*local/global type signature -- local name space / defsort only*)
   216     consts: Consts.T * Consts.T, (*local/global consts -- local name space / abbrevs only*)
   217     facts: Facts.T,              (*local facts, based on initial global facts*)
   218     cases: cases};               (*named case contexts: case, legacy, running index*)
   219 
   220 fun make_data (mode, syntax, tsig, consts, facts, cases) =
   221   Data {mode = mode, syntax = syntax, tsig = tsig, consts = consts, facts = facts, cases = cases};
   222 
   223 structure Data = Proof_Data
   224 (
   225   type T = data;
   226   fun init thy =
   227     make_data (mode_default,
   228       Local_Syntax.init thy,
   229       (Type.change_ignore (Sign.tsig_of thy), Sign.tsig_of thy),
   230       (Consts.change_ignore (Sign.consts_of thy), Sign.consts_of thy),
   231       Global_Theory.facts_of thy,
   232       empty_cases);
   233 );
   234 
   235 fun rep_data ctxt = Data.get ctxt |> (fn Data rep => rep);
   236 
   237 fun map_data f =
   238   Data.map (fn Data {mode, syntax, tsig, consts, facts, cases} =>
   239     make_data (f (mode, syntax, tsig, consts, facts, cases)));
   240 
   241 fun set_mode mode = map_data (fn (_, syntax, tsig, consts, facts, cases) =>
   242   (mode, syntax, tsig, consts, facts, cases));
   243 
   244 fun map_mode f =
   245   map_data (fn (Mode {stmt, pattern, schematic, abbrev}, syntax, tsig, consts, facts, cases) =>
   246     (make_mode (f (stmt, pattern, schematic, abbrev)), syntax, tsig, consts, facts, cases));
   247 
   248 fun map_syntax f =
   249   map_data (fn (mode, syntax, tsig, consts, facts, cases) =>
   250     (mode, f syntax, tsig, consts, facts, cases));
   251 
   252 fun map_syntax_idents f ctxt =
   253   let val (opt_idents', syntax') = f (#syntax (rep_data ctxt)) in
   254     ctxt
   255     |> map_syntax (K syntax')
   256     |> (case opt_idents' of NONE => I | SOME idents' => Syntax_Trans.put_idents idents')
   257   end;
   258 
   259 fun map_tsig f =
   260   map_data (fn (mode, syntax, tsig, consts, facts, cases) =>
   261     (mode, syntax, f tsig, consts, facts, cases));
   262 
   263 fun map_consts f =
   264   map_data (fn (mode, syntax, tsig, consts, facts, cases) =>
   265     (mode, syntax, tsig, f consts, facts, cases));
   266 
   267 fun map_facts f =
   268   map_data (fn (mode, syntax, tsig, consts, facts, cases) =>
   269     (mode, syntax, tsig, consts, f facts, cases));
   270 
   271 fun map_cases f =
   272   map_data (fn (mode, syntax, tsig, consts, facts, cases) =>
   273     (mode, syntax, tsig, consts, facts, f cases));
   274 
   275 val get_mode = #mode o rep_data;
   276 val restore_mode = set_mode o get_mode;
   277 val abbrev_mode = get_mode #> (fn Mode {abbrev, ...} => abbrev);
   278 
   279 fun set_stmt stmt =
   280   map_mode (fn (_, pattern, schematic, abbrev) => (stmt, pattern, schematic, abbrev));
   281 
   282 val syntax_of = #syntax o rep_data;
   283 val syn_of = Local_Syntax.syn_of o syntax_of;
   284 val set_syntax_mode = map_syntax o Local_Syntax.set_mode;
   285 val restore_syntax_mode = map_syntax o Local_Syntax.restore_mode o syntax_of;
   286 
   287 val tsig_of = #1 o #tsig o rep_data;
   288 val set_defsort = map_tsig o apfst o Type.set_defsort;
   289 fun default_sort ctxt = the_default (Type.defaultS (tsig_of ctxt)) o Variable.def_sort ctxt;
   290 fun arity_sorts ctxt = Type.arity_sorts (Context.pretty ctxt) (tsig_of ctxt);
   291 
   292 val consts_of = #1 o #consts o rep_data;
   293 val cases_of = #cases o rep_data;
   294 
   295 
   296 (* naming *)
   297 
   298 val naming_of = Name_Space.naming_of o Context.Proof;
   299 val map_naming = Context.proof_map o Name_Space.map_naming;
   300 val restore_naming = map_naming o K o naming_of;
   301 
   302 val full_name = Name_Space.full_name o naming_of;
   303 
   304 val get_scope = Name_Space.get_scope o naming_of;
   305 
   306 fun new_scope ctxt =
   307   let
   308     val (scope, naming') = Name_Space.new_scope (naming_of ctxt);
   309     val ctxt' = map_naming (K naming') ctxt;
   310   in (scope, ctxt') end;
   311 
   312 val private_scope = map_naming o Name_Space.private_scope;
   313 val private = map_naming o Name_Space.private;
   314 val qualified_scope = map_naming o Name_Space.qualified_scope;
   315 val qualified = map_naming o Name_Space.qualified;
   316 
   317 val concealed = map_naming Name_Space.concealed;
   318 
   319 
   320 (* name spaces *)
   321 
   322 val class_space = Type.class_space o tsig_of;
   323 val type_space = Type.type_space o tsig_of;
   324 val const_space = Consts.space_of o consts_of;
   325 
   326 fun defs_context ctxt = (ctxt, SOME (const_space ctxt, type_space ctxt));
   327 
   328 val intern_class = Name_Space.intern o class_space;
   329 val intern_type = Name_Space.intern o type_space;
   330 val intern_const = Name_Space.intern o const_space;
   331 
   332 fun extern_class ctxt = Name_Space.extern ctxt (class_space ctxt);
   333 fun extern_type ctxt = Name_Space.extern ctxt (type_space ctxt);
   334 fun extern_const ctxt = Name_Space.extern ctxt (const_space ctxt);
   335 
   336 fun markup_class ctxt c = Name_Space.markup_extern ctxt (class_space ctxt) c |-> Markup.markup;
   337 fun markup_type ctxt c = Name_Space.markup_extern ctxt (type_space ctxt) c |-> Markup.markup;
   338 fun markup_const ctxt c = Name_Space.markup_extern ctxt (const_space ctxt) c |-> Markup.markup;
   339 
   340 fun pretty_class ctxt c = Name_Space.markup_extern ctxt (class_space ctxt) c |> Pretty.mark_str;
   341 fun pretty_type ctxt c = Name_Space.markup_extern ctxt (type_space ctxt) c |> Pretty.mark_str;
   342 fun pretty_const ctxt c = Name_Space.markup_extern ctxt (const_space ctxt) c |> Pretty.mark_str;
   343 
   344 
   345 (* theory transfer *)
   346 
   347 fun transfer_syntax thy ctxt = ctxt |>
   348   map_syntax (Local_Syntax.rebuild thy) |>
   349   map_tsig (fn tsig as (local_tsig, global_tsig) =>
   350     let val thy_tsig = Sign.tsig_of thy in
   351       if Type.eq_tsig (thy_tsig, global_tsig) then tsig
   352       else (Type.merge_tsig (Context.pretty ctxt) (local_tsig, thy_tsig), thy_tsig)  (*historic merge order*)
   353     end) |>
   354   map_consts (fn consts as (local_consts, global_consts) =>
   355     let val thy_consts = Sign.consts_of thy in
   356       if Consts.eq_consts (thy_consts, global_consts) then consts
   357       else (Consts.merge (local_consts, thy_consts), thy_consts)  (*historic merge order*)
   358     end);
   359 
   360 fun transfer thy = Context.raw_transfer thy #> transfer_syntax thy;
   361 
   362 fun transfer_facts thy =
   363   map_facts (fn local_facts => Facts.merge (Global_Theory.facts_of thy, local_facts));
   364 
   365 fun background_theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
   366 
   367 fun background_theory_result f ctxt =
   368   let val (res, thy') = f (theory_of ctxt)
   369   in (res, ctxt |> transfer thy') end;
   370 
   371 
   372 (* hybrid facts *)
   373 
   374 val facts_of = #facts o rep_data;
   375 
   376 fun facts_of_fact ctxt name =
   377   let
   378     val local_facts = facts_of ctxt;
   379     val global_facts = Global_Theory.facts_of (theory_of ctxt);
   380   in
   381     if Facts.defined local_facts name
   382     then local_facts else global_facts
   383   end;
   384 
   385 fun markup_extern_fact ctxt name =
   386   Facts.markup_extern ctxt (facts_of_fact ctxt name) name;
   387 
   388 
   389 
   390 (** pretty printing **)
   391 
   392 fun pretty_term_abbrev ctxt = Syntax.pretty_term (set_mode mode_abbrev ctxt);
   393 
   394 fun pretty_fact_name ctxt a =
   395   Pretty.block [Pretty.mark_str (markup_extern_fact ctxt a), Pretty.str ":"];
   396 
   397 fun pretty_fact ctxt =
   398   let
   399     val pretty_thm = Display.pretty_thm ctxt;
   400     val pretty_thms = map (Display.pretty_thm_item ctxt);
   401   in
   402     fn ("", [th]) => pretty_thm th
   403      | ("", ths) => Pretty.blk (0, Pretty.fbreaks (pretty_thms ths))
   404      | (a, [th]) => Pretty.block [pretty_fact_name ctxt a, Pretty.brk 1, pretty_thm th]
   405      | (a, ths) => Pretty.block (Pretty.fbreaks (pretty_fact_name ctxt a :: pretty_thms ths))
   406   end;
   407 
   408 
   409 
   410 (** prepare types **)
   411 
   412 (* classes *)
   413 
   414 fun check_class ctxt (xname, pos) =
   415   let
   416     val tsig = tsig_of ctxt;
   417     val class_space = Type.class_space tsig;
   418 
   419     val name = Type.cert_class tsig (Name_Space.intern class_space xname)
   420       handle TYPE (msg, _, _) =>
   421         error (msg ^ Position.here pos ^
   422           Markup.markup_report (Completion.reported_text
   423             (Name_Space.completion (Context.Proof ctxt) class_space (xname, pos))));
   424     val reports =
   425       if Context_Position.is_reported ctxt pos
   426       then [(pos, Name_Space.markup class_space name)] else [];
   427   in (name, reports) end;
   428 
   429 fun read_class ctxt text =
   430   let
   431     val source = Syntax.read_input text;
   432     val (c, reports) = check_class ctxt (Input.source_content source, Input.pos_of source);
   433     val _ = Position.reports reports;
   434   in c end;
   435 
   436 
   437 (* types *)
   438 
   439 fun read_typ_mode mode ctxt s =
   440   Syntax.read_typ (Type.set_mode mode ctxt) s;
   441 
   442 val read_typ = read_typ_mode Type.mode_default;
   443 val read_typ_syntax = read_typ_mode Type.mode_syntax;
   444 val read_typ_abbrev = read_typ_mode Type.mode_abbrev;
   445 
   446 
   447 fun cert_typ_mode mode ctxt T =
   448   Type.cert_typ_mode mode (tsig_of ctxt) T
   449     handle TYPE (msg, _, _) => error msg;
   450 
   451 val cert_typ = cert_typ_mode Type.mode_default;
   452 val cert_typ_syntax = cert_typ_mode Type.mode_syntax;
   453 val cert_typ_abbrev = cert_typ_mode Type.mode_abbrev;
   454 
   455 
   456 
   457 (** prepare terms and propositions **)
   458 
   459 (* inferred types of parameters *)
   460 
   461 fun infer_type ctxt x =
   462   Term.fastype_of (singleton (Syntax.check_terms (set_mode mode_schematic ctxt)) (Free x));
   463 
   464 fun inferred_param x ctxt =
   465   let val p = (x, infer_type ctxt (x, dummyT))
   466   in (p, ctxt |> Variable.declare_term (Free p)) end;
   467 
   468 fun inferred_fixes ctxt =
   469   fold_map inferred_param (map #2 (Variable.dest_fixes ctxt)) ctxt;
   470 
   471 
   472 (* type names *)
   473 
   474 fun check_type_name {proper, strict} ctxt (c, pos) =
   475   if Lexicon.is_tid c then
   476     if proper then error ("Not a type constructor: " ^ quote c ^ Position.here pos)
   477     else
   478       let
   479         val reports =
   480           if Context_Position.is_reported ctxt pos
   481           then [(pos, Markup.tfree)] else [];
   482       in (TFree (c, default_sort ctxt (c, ~1)), reports) end
   483   else
   484     let
   485       val ((d, reports), decl) = Type.check_decl (Context.Proof ctxt) (tsig_of ctxt) (c, pos);
   486       fun err () = error ("Bad type name: " ^ quote d ^ Position.here pos);
   487       val args =
   488         (case decl of
   489           Type.LogicalType n => n
   490         | Type.Abbreviation (vs, _, _) => if strict then err () else length vs
   491         | Type.Nonterminal => if strict then err () else 0);
   492     in (Type (d, replicate args dummyT), reports) end;
   493 
   494 fun read_type_name ctxt flags text =
   495   let
   496     val source = Syntax.read_input text;
   497     val (T, reports) =
   498       check_type_name ctxt flags (Input.source_content source, Input.pos_of source);
   499     val _ = Position.reports reports;
   500   in T end;
   501 
   502 
   503 (* constant names *)
   504 
   505 fun consts_completion_message ctxt (c, ps) =
   506   ps |> map (fn pos =>
   507     Name_Space.completion (Context.Proof ctxt) (Consts.space_of (consts_of ctxt)) (c, pos)
   508     |> Completion.reported_text)
   509   |> implode
   510   |> Markup.markup_report;
   511 
   512 fun check_const {proper, strict} ctxt (c, ps) =
   513   let
   514     val _ =
   515       Name.reject_internal (c, ps) handle ERROR msg =>
   516         error (msg ^ consts_completion_message ctxt (c, ps));
   517     fun err msg = error (msg ^ Position.here_list ps);
   518     val consts = consts_of ctxt;
   519     val fixed = if proper then NONE else Variable.lookup_fixed ctxt c;
   520     val (t, reports) =
   521       (case (fixed, Variable.lookup_const ctxt c) of
   522         (SOME x, NONE) =>
   523           let
   524             val reports = ps
   525               |> filter (Context_Position.is_reported ctxt)
   526               |> map (fn pos =>
   527                 (pos, Markup.name x (if Name.is_skolem x then Markup.skolem else Markup.free)));
   528           in (Free (x, infer_type ctxt (x, dummyT)), reports) end
   529       | (_, SOME d) =>
   530           let
   531             val T = Consts.type_scheme consts d handle TYPE (msg, _, _) => err msg;
   532             val reports = ps
   533               |> filter (Context_Position.is_reported ctxt)
   534               |> map (fn pos => (pos, Name_Space.markup (Consts.space_of consts) d));
   535           in (Const (d, T), reports) end
   536       | _ => Consts.check_const (Context.Proof ctxt) consts (c, ps));
   537     val _ =
   538       (case (strict, t) of
   539         (true, Const (d, _)) =>
   540           (ignore (Consts.the_const consts d) handle TYPE (msg, _, _) => err msg)
   541       | _ => ());
   542   in (t, reports) end;
   543 
   544 fun read_const ctxt flags text =
   545   let
   546     val source = Syntax.read_input text;
   547     val (t, reports) = check_const ctxt flags (Input.source_content source, [Input.pos_of source]);
   548     val _ = Position.reports reports;
   549   in t end;
   550 
   551 
   552 (* type arities *)
   553 
   554 local
   555 
   556 fun prep_arity prep_tycon prep_sort ctxt (t, Ss, S) =
   557   let val arity = (prep_tycon ctxt t, map (prep_sort ctxt) Ss, prep_sort ctxt S)
   558   in Type.add_arity (Context.pretty ctxt) arity (tsig_of ctxt); arity end;
   559 
   560 in
   561 
   562 val read_arity =
   563   prep_arity ((#1 o dest_Type) oo read_type_name {proper = true, strict = true}) Syntax.read_sort;
   564 
   565 val cert_arity = prep_arity (K I) (Type.cert_sort o tsig_of);
   566 
   567 end;
   568 
   569 
   570 (* read_term *)
   571 
   572 fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
   573 
   574 val read_term_pattern   = read_term_mode mode_pattern;
   575 val read_term_schematic = read_term_mode mode_schematic;
   576 val read_term_abbrev    = read_term_mode mode_abbrev;
   577 
   578 
   579 (* local abbreviations *)
   580 
   581 local
   582 
   583 fun certify_consts ctxt = Consts.certify (Context.pretty ctxt) (tsig_of ctxt)
   584   (not (abbrev_mode ctxt)) (consts_of ctxt);
   585 
   586 fun expand_binds ctxt =
   587   let
   588     val Mode {pattern, schematic, ...} = get_mode ctxt;
   589 
   590     fun reject_schematic (t as Var _) =
   591           error ("Unbound schematic variable: " ^ Syntax.string_of_term ctxt t)
   592       | reject_schematic (Abs (_, _, t)) = reject_schematic t
   593       | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
   594       | reject_schematic _ = ();
   595   in
   596     if pattern then I
   597     else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
   598   end;
   599 
   600 in
   601 
   602 fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
   603 
   604 end;
   605 
   606 val show_abbrevs_raw = Config.declare ("show_abbrevs", @{here}) (fn _ => Config.Bool true);
   607 val show_abbrevs = Config.bool show_abbrevs_raw;
   608 
   609 fun contract_abbrevs ctxt t =
   610   let
   611     val thy = theory_of ctxt;
   612     val consts = consts_of ctxt;
   613     val Mode {abbrev, ...} = get_mode ctxt;
   614     val retrieve = Consts.retrieve_abbrevs consts (print_mode_value () @ [""]);
   615     fun match_abbrev u = Option.map #1 (get_first (Pattern.match_rew thy u) (retrieve u));
   616   in
   617     if abbrev orelse not (Config.get ctxt show_abbrevs) orelse not (can Term.type_of t) then t
   618     else Pattern.rewrite_term_top thy [] [match_abbrev] t
   619   end;
   620 
   621 
   622 (* patterns *)
   623 
   624 fun prepare_patternT ctxt T =
   625   let
   626     val Mode {pattern, schematic, ...} = get_mode ctxt;
   627     val _ =
   628       pattern orelse schematic orelse
   629         T |> Term.exists_subtype
   630           (fn T as TVar (xi, _) =>
   631             not (Type_Infer.is_param xi) andalso
   632               error ("Illegal schematic type variable: " ^ Syntax.string_of_typ ctxt T)
   633           | _ => false)
   634   in T end;
   635 
   636 
   637 local
   638 
   639 val dummies =
   640   Config.bool (Config.declare ("Proof_Context.dummies", @{here}) (K (Config.Bool false)));
   641 
   642 fun check_dummies ctxt t =
   643   if Config.get ctxt dummies then t
   644   else Term.no_dummy_patterns t handle TERM _ => error "Illegal dummy pattern(s) in term";
   645 
   646 fun prepare_dummies ts = #1 (fold_map Term.replace_dummy_patterns ts 1);
   647 
   648 in
   649 
   650 val allow_dummies = Config.put dummies true;
   651 
   652 fun prepare_patterns ctxt =
   653   let val Mode {pattern, ...} = get_mode ctxt in
   654     Type_Infer.fixate ctxt #>
   655     pattern ? Variable.polymorphic ctxt #>
   656     (map o Term.map_types) (prepare_patternT ctxt) #>
   657     (if pattern then prepare_dummies else map (check_dummies ctxt))
   658   end;
   659 
   660 end;
   661 
   662 
   663 (* sort constraints *)
   664 
   665 local
   666 
   667 fun prepare_sorts_env ctxt tys =
   668   let
   669     val tsig = tsig_of ctxt;
   670     val defaultS = Type.defaultS tsig;
   671 
   672     val dummy_var = ("'_dummy_", ~1);
   673 
   674     fun constraint (xi, raw_S) env =
   675       let val (ps, S) = Term_Position.decode_positionS raw_S in
   676         if xi = dummy_var orelse S = dummyS then env
   677         else
   678           Vartab.insert (op =) (xi, Type.minimize_sort tsig S) env
   679             handle Vartab.DUP _ =>
   680               error ("Inconsistent sort constraints for type variable " ^
   681                 quote (Term.string_of_vname' xi) ^ Position.here_list ps)
   682       end;
   683 
   684     val env =
   685       (fold o fold_atyps)
   686         (fn TFree (x, S) => constraint ((x, ~1), S)
   687           | TVar v => constraint v
   688           | _ => I) tys Vartab.empty;
   689 
   690     fun get_sort xi raw_S =
   691       if xi = dummy_var then
   692         Type.minimize_sort tsig (#2 (Term_Position.decode_positionS raw_S))
   693       else
   694         (case (Vartab.lookup env xi, Variable.def_sort ctxt xi) of
   695           (NONE, NONE) => defaultS
   696         | (NONE, SOME S) => S
   697         | (SOME S, NONE) => S
   698         | (SOME S, SOME S') =>
   699             if Type.eq_sort tsig (S, S') then S'
   700             else
   701               error ("Sort constraint " ^ Syntax.string_of_sort ctxt S ^
   702                 " inconsistent with default " ^ Syntax.string_of_sort ctxt S' ^
   703                 " for type variable " ^ quote (Term.string_of_vname' xi)));
   704 
   705     fun add_report S pos reports =
   706       if Position.is_reported pos andalso not (AList.defined (op =) reports pos) then
   707         (pos, Position.reported_text pos Markup.sorting (Syntax.string_of_sort ctxt S)) :: reports
   708       else reports;
   709 
   710     fun get_sort_reports xi raw_S =
   711       let
   712         val ps = #1 (Term_Position.decode_positionS raw_S);
   713         val S = get_sort xi raw_S handle ERROR msg => error (msg ^ Position.here_list ps);
   714       in fold (add_report S) ps end;
   715 
   716     val reports =
   717       (fold o fold_atyps)
   718         (fn T =>
   719           if Term_Position.is_positionT T then I
   720           else
   721             (case T of
   722               TFree (x, raw_S) => get_sort_reports (x, ~1) raw_S
   723             | TVar (xi, raw_S) => get_sort_reports xi raw_S
   724             | _ => I)) tys [];
   725 
   726   in (map #2 reports, get_sort) end;
   727 
   728 fun replace_sortsT get_sort =
   729   map_atyps
   730     (fn T =>
   731       if Term_Position.is_positionT T then T
   732       else
   733         (case T of
   734           TFree (x, raw_S) => TFree (x, get_sort (x, ~1) raw_S)
   735         | TVar (xi, raw_S) => TVar (xi, get_sort xi raw_S)
   736         | _ => T));
   737 
   738 in
   739 
   740 fun prepare_sortsT ctxt tys =
   741   let val (sorting_report, get_sort) = prepare_sorts_env ctxt tys
   742   in (sorting_report, map (replace_sortsT get_sort) tys) end;
   743 
   744 fun prepare_sorts ctxt tms =
   745   let
   746     val tys = rev ((fold o fold_types) cons tms []);
   747     val (sorting_report, get_sort) = prepare_sorts_env ctxt tys;
   748   in (sorting_report, (map o map_types) (replace_sortsT get_sort) tms) end;
   749 
   750 fun check_tfree ctxt v =
   751   let
   752     val (sorting_report, [TFree a]) = prepare_sortsT ctxt [TFree v];
   753     val _ = if Context_Position.is_visible ctxt then Output.report sorting_report else ();
   754   in a end;
   755 
   756 end;
   757 
   758 
   759 (* certify terms *)
   760 
   761 local
   762 
   763 fun gen_cert prop ctxt t =
   764   t
   765   |> expand_abbrevs ctxt
   766   |> (fn t' =>
   767       #1 (Sign.certify' prop (Context.pretty ctxt) false (consts_of ctxt) (theory_of ctxt) t')
   768         handle TYPE (msg, _, _) => error msg | TERM (msg, _) => error msg);
   769 
   770 in
   771 
   772 val cert_term = gen_cert false;
   773 val cert_prop = gen_cert true;
   774 
   775 end;
   776 
   777 
   778 (* check/uncheck *)
   779 
   780 fun def_type ctxt =
   781   let val Mode {pattern, ...} = get_mode ctxt
   782   in Variable.def_type ctxt pattern end;
   783 
   784 fun standard_typ_check ctxt =
   785   map (cert_typ_mode (Type.get_mode ctxt) ctxt #> prepare_patternT ctxt);
   786 
   787 val standard_term_check_finish = prepare_patterns;
   788 
   789 fun standard_term_uncheck ctxt = map (contract_abbrevs ctxt);
   790 
   791 
   792 
   793 (** export results **)
   794 
   795 fun common_export is_goal inner outer =
   796   map (Assumption.export is_goal inner outer) #>
   797   Variable.export inner outer;
   798 
   799 val goal_export = common_export true;
   800 val export = common_export false;
   801 
   802 fun export_morphism inner outer =
   803   Assumption.export_morphism inner outer $>
   804   Variable.export_morphism inner outer;
   805 
   806 fun norm_export_morphism inner outer =
   807   export_morphism inner outer $>
   808   Morphism.thm_morphism "Proof_Context.norm_export" (Goal.norm_result outer);
   809 
   810 
   811 
   812 (** term bindings **)
   813 
   814 (* simult_matches *)
   815 
   816 fun simult_matches ctxt (t, pats) =
   817   (case Seq.pull (Unify.matchers (Context.Proof ctxt) (map (rpair t) pats)) of
   818     NONE => error "Pattern match failed!"
   819   | SOME (env, _) => Vartab.fold (fn (v, (_, t)) => cons (v, t)) (Envir.term_env env) []);
   820 
   821 
   822 (* auto_bind *)
   823 
   824 fun auto_bind f props ctxt = fold Variable.maybe_bind_term (f ctxt props) ctxt;
   825 
   826 val auto_bind_goal = auto_bind Auto_Bind.goal;
   827 val auto_bind_facts = auto_bind Auto_Bind.facts;
   828 
   829 
   830 (* bind terms (non-schematic) *)
   831 
   832 fun cert_maybe_bind_term (xi, t) ctxt =
   833   ctxt
   834   |> Variable.maybe_bind_term (xi, Option.map (cert_term (set_mode mode_default ctxt)) t);
   835 
   836 val cert_bind_term = cert_maybe_bind_term o apsnd SOME;
   837 
   838 
   839 (* match_bind *)
   840 
   841 local
   842 
   843 fun gen_bind prep_terms gen raw_binds ctxt =
   844   let
   845     fun prep_bind (raw_pats, t) ctxt1 =
   846       let
   847         val T = Term.fastype_of t;
   848         val ctxt2 = Variable.declare_term t ctxt1;
   849         val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
   850         val binds = simult_matches ctxt2 (t, pats);
   851       in (binds, ctxt2) end;
   852 
   853     val ts = prep_terms ctxt dummyT (map snd raw_binds);
   854     val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
   855     val binds' =
   856       if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
   857       else binds;
   858     val ctxt'' =
   859       tap (Variable.warn_extra_tfrees ctxt)
   860        (if gen then
   861           ctxt (*sic!*)
   862           |> fold Variable.declare_term (map #2 binds')
   863           |> fold cert_bind_term binds'
   864         else ctxt' |> fold cert_bind_term binds');
   865   in (ts, ctxt'') end;
   866 
   867 in
   868 
   869 fun read_terms ctxt T =
   870   map (Syntax.parse_term ctxt #> Type.constraint T) #> Syntax.check_terms ctxt;
   871 
   872 val match_bind = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
   873 val match_bind_cmd = gen_bind read_terms;
   874 
   875 end;
   876 
   877 
   878 (* propositions with patterns *)
   879 
   880 local
   881 
   882 fun prep_propp prep_props ctxt raw_args =
   883   let
   884     val props = prep_props ctxt (maps (map fst) raw_args);
   885     val props_ctxt = fold Variable.declare_term props ctxt;
   886     val patss = maps (map (prep_props (set_mode mode_pattern props_ctxt) o snd)) raw_args;
   887 
   888     val propps = unflat raw_args (props ~~ patss);
   889     val binds = (maps o maps) (simult_matches props_ctxt) propps;
   890   in (map (map fst) propps, binds) end;
   891 
   892 in
   893 
   894 val cert_propp = prep_propp (map o cert_prop);
   895 val read_propp = prep_propp Syntax.read_props;
   896 
   897 end;
   898 
   899 
   900 
   901 (** theorems **)
   902 
   903 (* fact_tac *)
   904 
   905 local
   906 
   907 fun comp_hhf_tac ctxt th i st =
   908   PRIMSEQ (Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = true}
   909     (false, Drule.lift_all ctxt (Thm.cprem_of st i) th, 0) i) st;
   910 
   911 fun comp_incr_tac _ [] _ = no_tac
   912   | comp_incr_tac ctxt (th :: ths) i =
   913       (fn st => comp_hhf_tac ctxt (Drule.incr_indexes st th) i st) APPEND
   914       comp_incr_tac ctxt ths i;
   915 
   916 val vacuous_facts = [Drule.termI];
   917 
   918 in
   919 
   920 fun fact_tac ctxt facts = Goal.norm_hhf_tac ctxt THEN' comp_incr_tac ctxt facts;
   921 
   922 fun potential_facts ctxt prop =
   923   let
   924     val body = Term.strip_all_body prop;
   925     val vacuous = filter (fn th => Term.could_unify (body, Thm.concl_of th)) vacuous_facts;
   926   in Facts.could_unify (facts_of ctxt) body @ vacuous end;
   927 
   928 fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) => fact_tac ctxt (potential_facts ctxt goal) i);
   929 
   930 end;
   931 
   932 
   933 (* get facts *)
   934 
   935 local
   936 
   937 fun retrieve_global context =
   938   Facts.retrieve context (Global_Theory.facts_of (Context.theory_of context));
   939 
   940 fun retrieve_generic (context as Context.Proof ctxt) arg =
   941       (Facts.retrieve context (facts_of ctxt) arg handle ERROR local_msg =>
   942         (retrieve_global context arg handle ERROR _ => error local_msg))
   943   | retrieve_generic context arg = retrieve_global context arg;
   944 
   945 fun retrieve pick context (Facts.Fact s) =
   946       let
   947         val ctxt = Context.the_proof context;
   948         val pos = Syntax.read_input_pos s;
   949         val prop =
   950           Syntax.read_prop (ctxt |> set_mode mode_default |> allow_dummies) s
   951           |> singleton (Variable.polymorphic ctxt);
   952         fun err msg = error (msg ^ Position.here pos ^ ":\n" ^ Syntax.string_of_term ctxt prop);
   953 
   954         val (prop', _) = Term.replace_dummy_patterns prop (Variable.maxidx_of ctxt + 1);
   955         fun prove_fact th =
   956           Goal.prove ctxt [] [] prop' (K (ALLGOALS (fact_tac ctxt [th])));
   957         val results = map_filter (try prove_fact) (potential_facts ctxt prop');
   958         val thm =
   959           (case distinct Thm.eq_thm_prop results of
   960             [thm] => thm
   961           | [] => err "Failed to retrieve literal fact"
   962           | _ => err "Ambiguous specification of literal fact");
   963       in pick true ("", Position.none) [thm] end
   964   | retrieve pick context (Facts.Named ((xname, pos), sel)) =
   965       let
   966         val thy = Context.theory_of context;
   967         fun immediate thm = {name = xname, static = true, thms = [Thm.transfer thy thm]};
   968         val {name, static, thms} =
   969           (case xname of
   970             "" => immediate Drule.dummy_thm
   971           | "_" => immediate Drule.asm_rl
   972           | _ => retrieve_generic context (xname, pos));
   973         val thms' = Facts.select (Facts.Named ((name, pos), sel)) thms;
   974       in pick (static orelse is_some sel) (name, pos) thms' end;
   975 
   976 in
   977 
   978 val get_fact_generic =
   979   retrieve (fn static => fn (name, _) => fn thms =>
   980     (if static then NONE else SOME name, thms));
   981 
   982 val get_fact = retrieve (K (K I)) o Context.Proof;
   983 val get_fact_single = retrieve (K Facts.the_single) o Context.Proof;
   984 
   985 fun get_thms ctxt = get_fact ctxt o Facts.named;
   986 fun get_thm ctxt = get_fact_single ctxt o Facts.named;
   987 
   988 end;
   989 
   990 
   991 (* facts *)
   992 
   993 local
   994 
   995 fun update_thms _ (b, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt b))
   996   | update_thms flags (b, SOME ths) ctxt = ctxt |> map_facts
   997       (Facts.add_static (Context.Proof ctxt) flags (b, ths) #> snd);
   998 
   999 in
  1000 
  1001 fun note_thmss kind = fold_map (fn ((b, more_atts), raw_facts) => fn ctxt =>
  1002   let
  1003     val name = full_name ctxt b;
  1004     val facts = Global_Theory.name_thmss false name raw_facts;
  1005     fun app (ths, atts) =
  1006       fold_map (Thm.proof_attributes (surround (Thm.kind kind) (atts @ more_atts))) ths;
  1007     val (res, ctxt') = fold_map app facts ctxt;
  1008     val thms = Global_Theory.name_thms false false name (flat res);
  1009     val Mode {stmt, ...} = get_mode ctxt;
  1010   in ((name, thms), ctxt' |> update_thms {strict = false, index = stmt} (b, SOME thms)) end);
  1011 
  1012 fun put_thms index thms ctxt = ctxt
  1013   |> map_naming (K Name_Space.local_naming)
  1014   |> Context_Position.set_visible false
  1015   |> update_thms {strict = false, index = index} (apfst Binding.name thms)
  1016   |> Context_Position.restore_visible ctxt
  1017   |> restore_naming ctxt;
  1018 
  1019 end;
  1020 
  1021 
  1022 
  1023 (** basic logical entities **)
  1024 
  1025 (* variables *)
  1026 
  1027 fun declare_var (x, opt_T, mx) ctxt =
  1028   let val T = (case opt_T of SOME T => T | NONE => Mixfix.mixfixT mx)
  1029   in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
  1030 
  1031 fun check_var internal b =
  1032   let
  1033     val x = Variable.check_name b;
  1034     val check = if internal then Name.reject_skolem else Name.reject_internal;
  1035     val _ =
  1036       if can check (x, []) andalso Symbol_Pos.is_identifier x then ()
  1037       else error ("Bad name: " ^ Binding.print b);
  1038   in x end;
  1039 
  1040 local
  1041 
  1042 fun prep_var prep_typ internal (b, raw_T, mx) ctxt =
  1043   let
  1044     val x = check_var internal b;
  1045     fun cond_tvars T =
  1046       if internal then T
  1047       else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
  1048     val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T;
  1049     val (_, ctxt') = ctxt |> declare_var (x, opt_T, mx);
  1050   in ((b, opt_T, mx), ctxt') end;
  1051 
  1052 in
  1053 
  1054 val read_var = prep_var Syntax.read_typ false;
  1055 val cert_var = prep_var cert_typ true;
  1056 
  1057 end;
  1058 
  1059 
  1060 (* notation *)
  1061 
  1062 local
  1063 
  1064 fun type_syntax (Type (c, args), mx) =
  1065       SOME (Local_Syntax.Type, (Lexicon.mark_type c, Mixfix.make_type (length args), mx))
  1066   | type_syntax _ = NONE;
  1067 
  1068 fun const_syntax _ (Free (x, T), mx) = SOME (Local_Syntax.Fixed, (x, T, mx))
  1069   | const_syntax ctxt (Const (c, _), mx) =
  1070       (case try (Consts.type_scheme (consts_of ctxt)) c of
  1071         SOME T => SOME (Local_Syntax.Const, (Lexicon.mark_const c, T, mx))
  1072       | NONE => NONE)
  1073   | const_syntax _ _ = NONE;
  1074 
  1075 fun gen_notation syntax add mode args ctxt =
  1076   ctxt |> map_syntax_idents
  1077     (Local_Syntax.update_modesyntax ctxt add mode (map_filter (syntax ctxt) args));
  1078 
  1079 in
  1080 
  1081 val type_notation = gen_notation (K type_syntax);
  1082 val notation = gen_notation const_syntax;
  1083 
  1084 fun generic_type_notation add mode args phi =
  1085   let
  1086     val args' = args |> map_filter (fn (T, mx) =>
  1087       let
  1088         val T' = Morphism.typ phi T;
  1089         val similar = (case (T, T') of (Type (c, _), Type (c', _)) => c = c' | _ => false);
  1090       in if similar then SOME (T', mx) else NONE end);
  1091   in Context.mapping (Sign.type_notation add mode args') (type_notation add mode args') end;
  1092 
  1093 fun generic_notation add mode args phi =
  1094   let
  1095     val args' = args |> map_filter (fn (t, mx) =>
  1096       let val t' = Morphism.term phi t
  1097       in if Term.aconv_untyped (t, t') then SOME (t', mx) else NONE end);
  1098   in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
  1099 
  1100 end;
  1101 
  1102 
  1103 (* aliases *)
  1104 
  1105 fun class_alias b c ctxt = (map_tsig o apfst) (Type.class_alias (naming_of ctxt) b c) ctxt;
  1106 fun type_alias b c ctxt = (map_tsig o apfst) (Type.type_alias (naming_of ctxt) b c) ctxt;
  1107 fun const_alias b c ctxt = (map_consts o apfst) (Consts.alias (naming_of ctxt) b c) ctxt;
  1108 fun fact_alias b c ctxt = map_facts (Facts.alias (naming_of ctxt) b c) ctxt;
  1109 
  1110 
  1111 (* local constants *)
  1112 
  1113 fun add_const_constraint (c, opt_T) ctxt =
  1114   let
  1115     fun prepT raw_T =
  1116       let val T = cert_typ ctxt raw_T
  1117       in cert_term ctxt (Const (c, T)); T end;
  1118   in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
  1119 
  1120 fun add_abbrev mode (b, raw_t) ctxt =
  1121   let
  1122     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
  1123       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ Binding.print b);
  1124     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
  1125     val ((lhs, rhs), consts') = consts_of ctxt
  1126       |> Consts.abbreviate (Context.Proof ctxt) (tsig_of ctxt) mode (b, t);
  1127   in
  1128     ctxt
  1129     |> (map_consts o apfst) (K consts')
  1130     |> Variable.declare_term rhs
  1131     |> pair (lhs, rhs)
  1132   end;
  1133 
  1134 fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
  1135 
  1136 fun generic_add_abbrev mode arg =
  1137   Context.mapping_result (Sign.add_abbrev mode arg) (add_abbrev mode arg);
  1138 
  1139 fun generic_revert_abbrev mode arg =
  1140   Context.mapping (Sign.revert_abbrev mode arg) (revert_abbrev mode arg);
  1141 
  1142 
  1143 (* fixes *)
  1144 
  1145 local
  1146 
  1147 fun gen_fixes prep_var raw_vars ctxt =
  1148   let
  1149     val (vars, _) = fold_map prep_var raw_vars ctxt;
  1150     val (xs, ctxt') = Variable.add_fixes_binding (map #1 vars) ctxt;
  1151   in
  1152     ctxt'
  1153     |> fold_map declare_var (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars)
  1154     |-> (map_syntax_idents o Local_Syntax.add_syntax ctxt o map (pair Local_Syntax.Fixed))
  1155     |> pair xs
  1156   end;
  1157 
  1158 in
  1159 
  1160 val add_fixes = gen_fixes cert_var;
  1161 val add_fixes_cmd = gen_fixes read_var;
  1162 
  1163 end;
  1164 
  1165 
  1166 
  1167 (** assumptions **)
  1168 
  1169 local
  1170 
  1171 fun gen_assms prep_propp exp args ctxt =
  1172   let
  1173     val (propss, binds) = prep_propp ctxt (map snd args);
  1174     val props = flat propss;
  1175   in
  1176     ctxt
  1177     |> fold Variable.declare_term props
  1178     |> tap (Variable.warn_extra_tfrees ctxt)
  1179     |> fold_burrow (Assumption.add_assms exp o map (Thm.cterm_of ctxt)) propss
  1180     |-> (fn premss =>
  1181       auto_bind_facts props
  1182       #> fold Variable.bind_term binds
  1183       #> note_thmss "" (map fst args ~~ map (map (fn th => ([th], []))) premss))
  1184   end;
  1185 
  1186 in
  1187 
  1188 val add_assms = gen_assms cert_propp;
  1189 val add_assms_cmd = gen_assms read_propp;
  1190 
  1191 end;
  1192 
  1193 
  1194 
  1195 (** cases **)
  1196 
  1197 fun dest_cases ctxt =
  1198   Name_Space.fold_table (fn (a, (c, i)) => cons (i, (a, c))) (cases_of ctxt) []
  1199   |> sort (int_ord o apply2 #1) |> map #2;
  1200 
  1201 local
  1202 
  1203 fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
  1204   | drop_schematic b = b;
  1205 
  1206 fun update_case _ _ ("", _) res = res
  1207   | update_case _ _ (name, NONE) (cases, index) =
  1208       (Name_Space.del_table name cases, index)
  1209   | update_case context legacy (name, SOME c) (cases, index) =
  1210       let
  1211         val binding = Binding.name name |> legacy ? Binding.concealed;
  1212         val (_, cases') = cases
  1213           |> Name_Space.define context false (binding, ((c, {legacy = legacy}), index));
  1214         val index' = index + 1;
  1215       in (cases', index') end;
  1216 
  1217 fun update_cases' legacy args ctxt =
  1218   let
  1219     val context = Context.Proof ctxt |> Name_Space.map_naming (K Name_Space.global_naming);
  1220     val cases = cases_of ctxt;
  1221     val index = Name_Space.fold_table (fn _ => Integer.add 1) cases 0;
  1222     val (cases', _) = fold (update_case context legacy) args (cases, index);
  1223   in map_cases (K cases') ctxt end;
  1224 
  1225 fun fix (b, T) ctxt =
  1226   let val ([x], ctxt') = add_fixes [(b, SOME T, NoSyn)] ctxt
  1227   in (Free (x, T), ctxt') end;
  1228 
  1229 in
  1230 
  1231 val update_cases = update_cases' false;
  1232 val update_cases_legacy = update_cases' true;
  1233 
  1234 fun case_result c ctxt =
  1235   let
  1236     val Rule_Cases.Case {fixes, ...} = c;
  1237     val (ts, ctxt') = ctxt |> fold_map fix fixes;
  1238     val Rule_Cases.Case {assumes, binds, cases, ...} = Rule_Cases.apply ts c;
  1239   in
  1240     ctxt'
  1241     |> fold (cert_maybe_bind_term o drop_schematic) binds
  1242     |> update_cases (map (apsnd SOME) cases)
  1243     |> pair (assumes, (binds, cases))
  1244   end;
  1245 
  1246 val apply_case = apfst fst oo case_result;
  1247 
  1248 fun check_case ctxt internal (name, pos) param_specs =
  1249   let
  1250     val (_, ((Rule_Cases.Case {fixes, assumes, binds, cases}, {legacy}), _)) =
  1251       Name_Space.check (Context.Proof ctxt) (cases_of ctxt) (name, pos);
  1252     val _ =
  1253       if legacy then
  1254         legacy_feature ("Bad case " ^ quote name ^ Position.here pos ^
  1255           " -- use proof method \"goal_cases\" instead")
  1256       else ();
  1257 
  1258     val _ = List.app (fn NONE => () | SOME b => ignore (check_var internal b)) param_specs;
  1259     fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
  1260       | replace [] ys = ys
  1261       | replace (_ :: _) [] =
  1262           error ("Too many parameters for case " ^ quote name ^ Position.here pos);
  1263     val fixes' = replace param_specs fixes;
  1264     val binds' = map drop_schematic binds;
  1265   in
  1266     if null (fold (Term.add_tvarsT o snd) fixes []) andalso
  1267       null (fold (fold Term.add_vars o snd) assumes []) then
  1268         Rule_Cases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
  1269     else error ("Illegal schematic variable(s) in case " ^ quote name ^ Position.here pos)
  1270   end;
  1271 
  1272 end;
  1273 
  1274 
  1275 
  1276 (** print context information **)
  1277 
  1278 (* local syntax *)
  1279 
  1280 val print_syntax = Syntax.print_syntax o syn_of;
  1281 
  1282 
  1283 (* abbreviations *)
  1284 
  1285 fun pretty_abbrevs verbose show_globals ctxt =
  1286   let
  1287     val space = const_space ctxt;
  1288     val (constants, global_constants) =
  1289       apply2 (#constants o Consts.dest) (#consts (rep_data ctxt));
  1290     val globals = Symtab.make global_constants;
  1291     fun add_abbr (_, (_, NONE)) = I
  1292       | add_abbr (c, (T, SOME t)) =
  1293           if not show_globals andalso Symtab.defined globals c then I
  1294           else cons (c, Logic.mk_equals (Const (c, T), t));
  1295     val abbrevs = Name_Space.markup_entries verbose ctxt space (fold add_abbr constants []);
  1296   in
  1297     if null abbrevs then []
  1298     else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
  1299   end;
  1300 
  1301 fun print_abbrevs verbose = Pretty.writeln_chunks o pretty_abbrevs verbose true;
  1302 
  1303 
  1304 (* term bindings *)
  1305 
  1306 fun pretty_term_bindings ctxt =
  1307   let
  1308     val binds = Variable.binds_of ctxt;
  1309     fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
  1310   in
  1311     if Vartab.is_empty binds then []
  1312     else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
  1313   end;
  1314 
  1315 
  1316 (* local facts *)
  1317 
  1318 fun pretty_local_facts verbose ctxt =
  1319   let
  1320     val facts = facts_of ctxt;
  1321     val props = Facts.props facts;
  1322     val local_facts =
  1323       (if null props then [] else [("<unnamed>", props)]) @
  1324       Facts.dest_static verbose [Global_Theory.facts_of (theory_of ctxt)] facts;
  1325   in
  1326     if null local_facts then []
  1327     else
  1328       [Pretty.big_list "local facts:"
  1329         (map #1 (sort_by (#1 o #2) (map (`(pretty_fact ctxt)) local_facts)))]
  1330   end;
  1331 
  1332 fun print_local_facts verbose ctxt =
  1333   Pretty.writeln_chunks (pretty_local_facts verbose ctxt);
  1334 
  1335 
  1336 (* local contexts *)
  1337 
  1338 local
  1339 
  1340 fun pretty_case (name, (fixes, ((asms, (lets, cs)), ctxt))) =
  1341   let
  1342     val prt_term = Syntax.pretty_term ctxt;
  1343 
  1344     fun prt_let (xi, t) = Pretty.block
  1345       [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
  1346         Pretty.quote (prt_term t)];
  1347 
  1348     fun prt_asm (a, ts) =
  1349       Pretty.block (Pretty.breaks
  1350         ((if a = "" then [] else [Pretty.str a, Pretty.str ":"]) @
  1351           map (Pretty.quote o prt_term) ts));
  1352 
  1353     fun prt_sect _ _ _ [] = []
  1354       | prt_sect head sep prt xs =
  1355           [Pretty.block (Pretty.breaks (head ::
  1356             flat (separate sep (map (single o prt) xs))))];
  1357   in
  1358     Pretty.block (Pretty.fbreaks
  1359       (Pretty.str (name ^ ":") ::
  1360         prt_sect (Pretty.keyword1 "fix") [] (Pretty.str o Binding.name_of o fst) fixes @
  1361         prt_sect (Pretty.keyword1 "let") [Pretty.keyword2 "and"] prt_let
  1362           (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
  1363         (if forall (null o #2) asms then []
  1364           else prt_sect (Pretty.keyword1 "assume") [Pretty.keyword2 "and"] prt_asm asms) @
  1365         prt_sect (Pretty.str "subcases:") [] (Pretty.str o fst) cs))
  1366   end;
  1367 
  1368 in
  1369 
  1370 fun pretty_cases ctxt =
  1371   let
  1372     fun mk_case (_, (_, {legacy = true})) = NONE
  1373       | mk_case (name, (c as Rule_Cases.Case {fixes, ...}, {legacy = false})) =
  1374           SOME (name, (fixes, case_result c ctxt));
  1375     val cases = dest_cases ctxt |> map_filter mk_case;
  1376   in
  1377     if null cases then []
  1378     else [Pretty.big_list "cases:" (map pretty_case cases)]
  1379   end;
  1380 
  1381 end;
  1382 
  1383 
  1384 (* core context *)
  1385 
  1386 val debug =
  1387   Config.bool (Config.declare ("Proof_Context.debug", @{here}) (K (Config.Bool false)));
  1388 
  1389 val verbose =
  1390   Config.bool (Config.declare ("Proof_Context.verbose", @{here}) (K (Config.Bool false)));
  1391 
  1392 fun pretty_ctxt ctxt =
  1393   if not (Config.get ctxt debug) then []
  1394   else
  1395     let
  1396       val prt_term = Syntax.pretty_term ctxt;
  1397 
  1398       (*structures*)
  1399       val {structs, ...} = Syntax_Trans.get_idents ctxt;
  1400       val prt_structs =
  1401         if null structs then []
  1402         else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
  1403           Pretty.commas (map Pretty.str structs))];
  1404 
  1405       (*fixes*)
  1406       fun prt_fix (x, x') =
  1407         if x = x' then Pretty.str x
  1408         else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
  1409       val fixes =
  1410         filter_out ((Name.is_internal orf member (op =) structs) o #1)
  1411           (Variable.dest_fixes ctxt);
  1412       val prt_fixes =
  1413         if null fixes then []
  1414         else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
  1415           Pretty.commas (map prt_fix fixes))];
  1416 
  1417       (*assumptions*)
  1418       val prt_assms =
  1419         (case Assumption.all_prems_of ctxt of
  1420           [] => []
  1421         | prems => [Pretty.big_list "assumptions:" [pretty_fact ctxt ("", prems)]]);
  1422     in prt_structs @ prt_fixes @ prt_assms end;
  1423 
  1424 
  1425 (* main context *)
  1426 
  1427 fun pretty_context ctxt =
  1428   let
  1429     val verbose = Config.get ctxt verbose;
  1430     fun verb f x = if verbose then f (x ()) else [];
  1431 
  1432     val prt_term = Syntax.pretty_term ctxt;
  1433     val prt_typ = Syntax.pretty_typ ctxt;
  1434     val prt_sort = Syntax.pretty_sort ctxt;
  1435 
  1436     (*theory*)
  1437     val pretty_thy = Pretty.block
  1438       [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
  1439 
  1440     (*defaults*)
  1441     fun prt_atom prt prtT (x, X) = Pretty.block
  1442       [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
  1443 
  1444     fun prt_var (x, ~1) = prt_term (Syntax.free x)
  1445       | prt_var xi = prt_term (Syntax.var xi);
  1446 
  1447     fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
  1448       | prt_varT xi = prt_typ (TVar (xi, []));
  1449 
  1450     val prt_defT = prt_atom prt_var prt_typ;
  1451     val prt_defS = prt_atom prt_varT prt_sort;
  1452 
  1453     val (types, sorts) = Variable.constraints_of ctxt;
  1454   in
  1455     verb single (K pretty_thy) @
  1456     pretty_ctxt ctxt @
  1457     verb (pretty_abbrevs true false) (K ctxt) @
  1458     verb pretty_term_bindings (K ctxt) @
  1459     verb (pretty_local_facts true) (K ctxt) @
  1460     verb pretty_cases (K ctxt) @
  1461     verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
  1462     verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
  1463   end;
  1464 
  1465 end;
  1466 
  1467 val show_abbrevs = Proof_Context.show_abbrevs;