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