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