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