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