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