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