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