src/Pure/Isar/isar_cmd.ML
author wenzelm
Sun Nov 02 15:27:37 2014 +0100 (2014-11-02 ago)
changeset 58868 c5e1cce7ace3
parent 58201 5bf56c758e02
child 58875 ab1c65b015c3
permissions -rw-r--r--
uniform heading commands work in any context, even in theory header;
discontinued obsolete 'sect', 'subsect', 'subsubsect';
marked obsolete 'header' as legacy;
     1 (*  Title:      Pure/Isar/isar_cmd.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Miscellaneous Isar commands.
     5 *)
     6 
     7 signature ISAR_CMD =
     8 sig
     9   val global_setup: Symbol_Pos.source -> theory -> theory
    10   val local_setup: Symbol_Pos.source -> Proof.context -> Proof.context
    11   val parse_ast_translation: Symbol_Pos.source -> theory -> theory
    12   val parse_translation: Symbol_Pos.source -> theory -> theory
    13   val print_translation: Symbol_Pos.source -> theory -> theory
    14   val typed_print_translation: Symbol_Pos.source -> theory -> theory
    15   val print_ast_translation: Symbol_Pos.source -> theory -> theory
    16   val translations: (xstring * string) Syntax.trrule list -> theory -> theory
    17   val no_translations: (xstring * string) Syntax.trrule list -> theory -> theory
    18   val oracle: bstring * Position.T -> Symbol_Pos.source -> theory -> theory
    19   val add_defs: (bool * bool) * ((binding * string) * Token.src list) list -> theory -> theory
    20   val declaration: {syntax: bool, pervasive: bool} ->
    21     Symbol_Pos.source -> local_theory -> local_theory
    22   val simproc_setup: string * Position.T -> string list -> Symbol_Pos.source ->
    23     string list -> local_theory -> local_theory
    24   val have: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    25   val hence: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    26   val show: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    27   val thus: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    28   val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
    29   val terminal_proof: Method.text_range * Method.text_range option ->
    30     Toplevel.transition -> Toplevel.transition
    31   val default_proof: Toplevel.transition -> Toplevel.transition
    32   val immediate_proof: Toplevel.transition -> Toplevel.transition
    33   val done_proof: Toplevel.transition -> Toplevel.transition
    34   val skip_proof: Toplevel.transition -> Toplevel.transition
    35   val ml_diag: bool -> Symbol_Pos.source -> Toplevel.transition -> Toplevel.transition
    36   val diag_state: Proof.context -> Toplevel.state
    37   val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
    38   val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
    39   val thy_deps: Toplevel.transition -> Toplevel.transition
    40   val locale_deps: Toplevel.transition -> Toplevel.transition
    41   val print_stmts: string list * (Facts.ref * Token.src list) list
    42     -> Toplevel.transition -> Toplevel.transition
    43   val print_thms: string list * (Facts.ref * Token.src list) list
    44     -> Toplevel.transition -> Toplevel.transition
    45   val print_prfs: bool -> string list * (Facts.ref * Token.src list) list option
    46     -> Toplevel.transition -> Toplevel.transition
    47   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
    48   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
    49   val print_type: (string list * (string * string option)) ->
    50     Toplevel.transition -> Toplevel.transition
    51   val local_theory_markup: (xstring * Position.T) option * Symbol_Pos.source ->
    52     Toplevel.transition -> Toplevel.transition
    53   val proof_markup: Symbol_Pos.source -> Toplevel.transition -> Toplevel.transition
    54   val heading_markup: (xstring * Position.T) option * Symbol_Pos.source ->
    55     Toplevel.transition -> Toplevel.transition
    56 end;
    57 
    58 structure Isar_Cmd: ISAR_CMD =
    59 struct
    60 
    61 
    62 (** theory declarations **)
    63 
    64 (* generic setup *)
    65 
    66 fun global_setup source =
    67   ML_Lex.read_source false source
    68   |> ML_Context.expression (#pos source) "val setup: theory -> theory" "Context.map_theory setup"
    69   |> Context.theory_map;
    70 
    71 fun local_setup source =
    72   ML_Lex.read_source false source
    73   |> ML_Context.expression (#pos source) "val setup: local_theory -> local_theory" "Context.map_proof setup"
    74   |> Context.proof_map;
    75 
    76 
    77 (* translation functions *)
    78 
    79 fun parse_ast_translation source =
    80   ML_Lex.read_source false source
    81   |> ML_Context.expression (#pos source)
    82     "val parse_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
    83     "Context.map_theory (Sign.parse_ast_translation parse_ast_translation)"
    84   |> Context.theory_map;
    85 
    86 fun parse_translation source =
    87   ML_Lex.read_source false source
    88   |> ML_Context.expression (#pos source)
    89     "val parse_translation: (string * (Proof.context -> term list -> term)) list"
    90     "Context.map_theory (Sign.parse_translation parse_translation)"
    91   |> Context.theory_map;
    92 
    93 fun print_translation source =
    94   ML_Lex.read_source false source
    95   |> ML_Context.expression (#pos source)
    96     "val print_translation: (string * (Proof.context -> term list -> term)) list"
    97     "Context.map_theory (Sign.print_translation print_translation)"
    98   |> Context.theory_map;
    99 
   100 fun typed_print_translation source =
   101   ML_Lex.read_source false source
   102   |> ML_Context.expression (#pos source)
   103     "val typed_print_translation: (string * (Proof.context -> typ -> term list -> term)) list"
   104     "Context.map_theory (Sign.typed_print_translation typed_print_translation)"
   105   |> Context.theory_map;
   106 
   107 fun print_ast_translation source =
   108   ML_Lex.read_source false source
   109   |> ML_Context.expression (#pos source)
   110     "val print_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
   111     "Context.map_theory (Sign.print_ast_translation print_ast_translation)"
   112   |> Context.theory_map;
   113 
   114 
   115 (* translation rules *)
   116 
   117 fun read_trrules thy raw_rules =
   118   let
   119     val ctxt = Proof_Context.init_global thy;
   120     val read_root =
   121       #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} ctxt;
   122   in
   123     raw_rules
   124     |> map (Syntax.map_trrule (fn (r, s) => Syntax_Phases.parse_ast_pattern ctxt (read_root r, s)))
   125   end;
   126 
   127 fun translations args thy = Sign.add_trrules (read_trrules thy args) thy;
   128 fun no_translations args thy = Sign.del_trrules (read_trrules thy args) thy;
   129 
   130 
   131 (* oracles *)
   132 
   133 fun oracle (name, pos) source =
   134   let
   135     val body = ML_Lex.read_source false source;
   136     val ants =
   137       ML_Lex.read Position.none
   138        ("local\n\
   139         \  val binding = " ^ ML_Syntax.make_binding (name, pos) ^ ";\n\
   140         \  val body = ") @ body @ ML_Lex.read Position.none (";\n\
   141         \in\n\
   142         \  val " ^ name ^
   143         " = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, body))));\n\
   144         \end;\n");
   145   in
   146     Context.theory_map
   147       (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#pos source) ants))
   148   end;
   149 
   150 
   151 (* old-style defs *)
   152 
   153 fun add_defs ((unchecked, overloaded), args) thy =
   154  (legacy_feature "Old 'defs' command -- use 'definition' (with 'overloading') instead";
   155   thy |>
   156     (if unchecked then Global_Theory.add_defs_unchecked_cmd else Global_Theory.add_defs_cmd)
   157       overloaded
   158       (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute_cmd_global thy) srcs)) args)
   159   |> snd);
   160 
   161 
   162 (* declarations *)
   163 
   164 fun declaration {syntax, pervasive} source =
   165   ML_Lex.read_source false source
   166   |> ML_Context.expression (#pos source)
   167     "val declaration: Morphism.declaration"
   168     ("Context.map_proof (Local_Theory.declaration {syntax = " ^ Bool.toString syntax ^ ", \
   169       \pervasive = " ^ Bool.toString pervasive ^ "} declaration)")
   170   |> Context.proof_map;
   171 
   172 
   173 (* simprocs *)
   174 
   175 fun simproc_setup name lhss source identifier =
   176   ML_Lex.read_source false source
   177   |> ML_Context.expression (#pos source)
   178     "val proc: Morphism.morphism -> Proof.context -> cterm -> thm option"
   179     ("Context.map_proof (Simplifier.def_simproc_cmd {name = " ^ ML_Syntax.make_binding name ^ ", \
   180       \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   181       \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})")
   182   |> Context.proof_map;
   183 
   184 
   185 (* goals *)
   186 
   187 fun goal opt_chain goal stmt int =
   188   opt_chain #> goal NONE (K I) stmt int;
   189 
   190 val have = goal I Proof.have_cmd;
   191 val hence = goal Proof.chain Proof.have_cmd;
   192 val show = goal I Proof.show_cmd;
   193 val thus = goal Proof.chain Proof.show_cmd;
   194 
   195 
   196 (* local endings *)
   197 
   198 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   199 val local_terminal_proof = Toplevel.proof' o Proof.local_future_terminal_proof;
   200 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   201 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   202 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   203 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   204 
   205 val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
   206 
   207 
   208 (* global endings *)
   209 
   210 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   211 val global_terminal_proof = Toplevel.end_proof o Proof.global_future_terminal_proof;
   212 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   213 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   214 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   215 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   216 
   217 val skip_global_qed = Toplevel.skip_proof_to_theory (fn n => n = 1);
   218 
   219 
   220 (* common endings *)
   221 
   222 fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
   223 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   224 val default_proof = local_default_proof o global_default_proof;
   225 val immediate_proof = local_immediate_proof o global_immediate_proof;
   226 val done_proof = local_done_proof o global_done_proof;
   227 val skip_proof = local_skip_proof o global_skip_proof;
   228 
   229 
   230 (* diagnostic ML evaluation *)
   231 
   232 structure Diag_State = Proof_Data
   233 (
   234   type T = Toplevel.state;
   235   fun init _ = Toplevel.toplevel;
   236 );
   237 
   238 fun ml_diag verbose source = Toplevel.keep (fn state =>
   239   let
   240     val opt_ctxt =
   241       try Toplevel.generic_theory_of state
   242       |> Option.map (Context.proof_of #> Diag_State.put state);
   243     val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
   244   in ML_Context.eval_source_in opt_ctxt flags source end);
   245 
   246 val diag_state = Diag_State.get;
   247 
   248 fun diag_goal ctxt =
   249   Proof.goal (Toplevel.proof_of (diag_state ctxt))
   250     handle Toplevel.UNDEF => error "No goal present";
   251 
   252 val _ = Theory.setup
   253   (ML_Antiquotation.value (Binding.qualify true "Isar" @{binding state})
   254     (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
   255    ML_Antiquotation.value (Binding.qualify true "Isar" @{binding goal})
   256     (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
   257 
   258 
   259 (* theorems of theory or proof context *)
   260 
   261 fun pretty_theorems verbose st =
   262   if Toplevel.is_proof st then
   263     Proof_Context.pretty_local_facts (Toplevel.context_of st) verbose
   264   else
   265     let
   266       val thy = Toplevel.theory_of st;
   267       val prev_thys =
   268         (case Toplevel.previous_context_of st of
   269           SOME prev => [Proof_Context.theory_of prev]
   270         | NONE => Theory.parents_of thy);
   271     in Proof_Display.pretty_theorems_diff verbose prev_thys thy end;
   272 
   273 
   274 (* display dependencies *)
   275 
   276 val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   277   let
   278     val thy = Toplevel.theory_of state;
   279     val thy_session = Present.session_name thy;
   280 
   281     val gr = rev (Theory.nodes_of thy) |> map (fn node =>
   282       let
   283         val name = Context.theory_name node;
   284         val parents = map Context.theory_name (Theory.parents_of node);
   285         val session = Present.session_name node;
   286         val unfold = (session = thy_session);
   287       in
   288        {name = name, ID = name, parents = parents, dir = session,
   289         unfold = unfold, path = "", content = []}
   290       end);
   291   in Graph_Display.display_graph gr end);
   292 
   293 val locale_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   294   let
   295     val thy = Toplevel.theory_of state;
   296     val gr = Locale.pretty_locale_deps thy |> map (fn {name, parents, body} =>
   297      {name = Locale.extern thy name, ID = name, parents = parents,
   298       dir = "", unfold = true, path = "", content = [body]});
   299   in Graph_Display.display_graph gr end);
   300 
   301 
   302 (* print theorems, terms, types etc. *)
   303 
   304 local
   305 
   306 fun string_of_stmts ctxt args =
   307   Attrib.eval_thms ctxt args
   308   |> map (Element.pretty_statement ctxt Thm.theoremK)
   309   |> Pretty.chunks2 |> Pretty.string_of;
   310 
   311 fun string_of_thms ctxt args =
   312   Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
   313 
   314 fun string_of_prfs full state arg =
   315   Pretty.string_of
   316     (case arg of
   317       NONE =>
   318         let
   319           val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
   320           val thy = Proof_Context.theory_of ctxt;
   321           val prf = Thm.proof_of thm;
   322           val prop = Thm.full_prop_of thm;
   323           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   324         in
   325           Proof_Syntax.pretty_proof ctxt
   326             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   327         end
   328     | SOME srcs =>
   329         let val ctxt = Toplevel.context_of state
   330         in map (Proof_Syntax.pretty_proof_of ctxt full) (Attrib.eval_thms ctxt srcs) end
   331         |> Pretty.chunks);
   332 
   333 fun string_of_prop ctxt s =
   334   let
   335     val prop = Syntax.read_prop ctxt s;
   336     val ctxt' = Variable.auto_fixes prop ctxt;
   337   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   338 
   339 fun string_of_term ctxt s =
   340   let
   341     val t = Syntax.read_term ctxt s;
   342     val T = Term.type_of t;
   343     val ctxt' = Variable.auto_fixes t ctxt;
   344   in
   345     Pretty.string_of
   346       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   347         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   348   end;
   349 
   350 fun string_of_type ctxt (s, NONE) =
   351       let val T = Syntax.read_typ ctxt s
   352       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
   353   | string_of_type ctxt (s1, SOME s2) =
   354       let
   355         val ctxt' = Config.put show_sorts true ctxt;
   356         val raw_T = Syntax.parse_typ ctxt' s1;
   357         val S = Syntax.read_sort ctxt' s2;
   358         val T =
   359           Syntax.check_term ctxt'
   360             (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
   361           |> Logic.dest_type;
   362       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
   363 
   364 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   365   Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
   366 
   367 in
   368 
   369 val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
   370 val print_thms = print_item (string_of_thms o Toplevel.context_of);
   371 val print_prfs = print_item o string_of_prfs;
   372 val print_prop = print_item (string_of_prop o Toplevel.context_of);
   373 val print_term = print_item (string_of_term o Toplevel.context_of);
   374 val print_type = print_item (string_of_type o Toplevel.context_of);
   375 
   376 end;
   377 
   378 
   379 (* markup commands *)
   380 
   381 fun local_theory_markup (loc, txt) = Toplevel.present_local_theory loc (Thy_Output.check_text txt);
   382 val proof_markup = Toplevel.present_proof o Thy_Output.check_text;
   383 
   384 fun reject_target NONE = ()
   385   | reject_target (SOME (_, pos)) =
   386       error ("Illegal target specification -- not a theory context" ^ Position.here pos);
   387 
   388 fun heading_markup (loc, txt) =
   389   Toplevel.keep (fn state =>
   390     if Toplevel.is_toplevel state then
   391      (legacy_feature "Obsolete 'header' command -- use 'chapter', 'section' etc. instead";
   392       reject_target loc;
   393       Thy_Output.check_text txt state)
   394     else raise Toplevel.UNDEF) o
   395   local_theory_markup (loc, txt) o
   396   Toplevel.present_proof (fn state => (reject_target loc; Thy_Output.check_text txt state));
   397 
   398 end;