src/Pure/Isar/isar_cmd.ML
author wenzelm
Thu Apr 16 13:48:10 2015 +0200 (2015-04-16 ago)
changeset 60095 c48d536231fe
parent 60082 d3573eb7728f
child 60096 96a4765ba7d1
permissions -rw-r--r--
clarified thy_deps;
     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 setup: Input.source -> theory -> theory
    10   val local_setup: Input.source -> Proof.context -> Proof.context
    11   val parse_ast_translation: Input.source -> theory -> theory
    12   val parse_translation: Input.source -> theory -> theory
    13   val print_translation: Input.source -> theory -> theory
    14   val typed_print_translation: Input.source -> theory -> theory
    15   val print_ast_translation: Input.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.range -> Input.source -> theory -> theory
    19   val add_defs: (bool * bool) * ((binding * string) * Token.src list) list -> theory -> theory
    20   val declaration: {syntax: bool, pervasive: bool} -> Input.source -> local_theory -> local_theory
    21   val simproc_setup: string * Position.T -> string list -> Input.source ->
    22     string list -> local_theory -> local_theory
    23   val have: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    24   val hence: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    25   val show: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    26   val thus: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    27   val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
    28   val terminal_proof: Method.text_range * Method.text_range option ->
    29     Toplevel.transition -> Toplevel.transition
    30   val default_proof: Toplevel.transition -> Toplevel.transition
    31   val immediate_proof: Toplevel.transition -> Toplevel.transition
    32   val done_proof: Toplevel.transition -> Toplevel.transition
    33   val skip_proof: Toplevel.transition -> Toplevel.transition
    34   val ml_diag: bool -> Input.source -> Toplevel.transition -> Toplevel.transition
    35   val diag_state: Proof.context -> Toplevel.state
    36   val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
    37   val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
    38   val locale_deps: Toplevel.transition -> Toplevel.transition
    39   val print_stmts: string list * (Facts.ref * Token.src list) list
    40     -> Toplevel.transition -> Toplevel.transition
    41   val print_thms: string list * (Facts.ref * Token.src list) list
    42     -> Toplevel.transition -> Toplevel.transition
    43   val print_prfs: bool -> string list * (Facts.ref * Token.src list) list option
    44     -> Toplevel.transition -> Toplevel.transition
    45   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
    46   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
    47   val print_type: (string list * (string * string option)) ->
    48     Toplevel.transition -> Toplevel.transition
    49 end;
    50 
    51 structure Isar_Cmd: ISAR_CMD =
    52 struct
    53 
    54 
    55 (** theory declarations **)
    56 
    57 (* generic setup *)
    58 
    59 fun setup source =
    60   ML_Lex.read_source false source
    61   |> ML_Context.expression (Input.range_of source) "setup" "theory -> theory"
    62     "Context.map_theory setup"
    63   |> Context.theory_map;
    64 
    65 fun local_setup source =
    66   ML_Lex.read_source false source
    67   |> ML_Context.expression (Input.range_of source) "local_setup" "local_theory -> local_theory"
    68     "Context.map_proof local_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 (Input.range_of source) "parse_ast_translation"
    77     "(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 (Input.range_of source) "parse_translation"
    84     "(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 (Input.range_of source) "print_translation"
    91     "(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 (Input.range_of source) "typed_print_translation"
    98     "(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 (Input.range_of source) "print_ast_translation"
   105     "(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, range) source =
   129   let
   130     val body_range = Input.range_of source;
   131     val body = ML_Lex.read_source false source;
   132 
   133     val ants =
   134       ML_Lex.read
   135        ("local\n\
   136         \  val binding = " ^ ML_Syntax.make_binding (name, #1 range) ^ ";\n\
   137         \  val") @ ML_Lex.read_set_range body_range "oracle" @ ML_Lex.read "=" @ body @
   138         ML_Lex.read (";\nin\n\
   139         \  val") @ ML_Lex.read_set_range range name @ ML_Lex.read "=\
   140         \ snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, oracle))));\n\
   141         \end;\n";
   142   in
   143     Context.theory_map
   144       (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#1 body_range) ants))
   145   end;
   146 
   147 
   148 (* old-style defs *)
   149 
   150 fun add_defs ((unchecked, overloaded), args) thy =
   151  (legacy_feature "Old 'defs' command -- use 'definition' (with 'overloading') instead";
   152   thy |>
   153     (if unchecked then Global_Theory.add_defs_unchecked_cmd else Global_Theory.add_defs_cmd)
   154       overloaded
   155       (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute_cmd_global thy) srcs)) args)
   156   |> snd);
   157 
   158 
   159 (* declarations *)
   160 
   161 fun declaration {syntax, pervasive} source =
   162   ML_Lex.read_source false source
   163   |> ML_Context.expression (Input.range_of source) "declaration" "Morphism.declaration"
   164     ("Context.map_proof (Local_Theory.declaration {syntax = " ^ Bool.toString syntax ^ ", \
   165       \pervasive = " ^ Bool.toString pervasive ^ "} declaration)")
   166   |> Context.proof_map;
   167 
   168 
   169 (* simprocs *)
   170 
   171 fun simproc_setup name lhss source identifier =
   172   ML_Lex.read_source false source
   173   |> ML_Context.expression (Input.range_of source) "proc"
   174     "Morphism.morphism -> Proof.context -> cterm -> thm option"
   175     ("Context.map_proof (Simplifier.def_simproc_cmd {name = " ^ ML_Syntax.make_binding name ^ ", \
   176       \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   177       \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})")
   178   |> Context.proof_map;
   179 
   180 
   181 (* goals *)
   182 
   183 fun goal opt_chain goal stmt int =
   184   opt_chain #> goal NONE (K I) stmt int;
   185 
   186 val have = goal I Proof.have_cmd;
   187 val hence = goal Proof.chain Proof.have_cmd;
   188 val show = goal I Proof.show_cmd;
   189 val thus = goal Proof.chain Proof.show_cmd;
   190 
   191 
   192 (* local endings *)
   193 
   194 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   195 val local_terminal_proof = Toplevel.proof' o Proof.local_future_terminal_proof;
   196 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   197 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   198 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   199 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   200 
   201 val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
   202 
   203 
   204 (* global endings *)
   205 
   206 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   207 val global_terminal_proof = Toplevel.end_proof o Proof.global_future_terminal_proof;
   208 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   209 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   210 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   211 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   212 
   213 val skip_global_qed = Toplevel.skip_proof_to_theory (fn n => n = 1);
   214 
   215 
   216 (* common endings *)
   217 
   218 fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
   219 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   220 val default_proof = local_default_proof o global_default_proof;
   221 val immediate_proof = local_immediate_proof o global_immediate_proof;
   222 val done_proof = local_done_proof o global_done_proof;
   223 val skip_proof = local_skip_proof o global_skip_proof;
   224 
   225 
   226 (* diagnostic ML evaluation *)
   227 
   228 structure Diag_State = Proof_Data
   229 (
   230   type T = Toplevel.state;
   231   fun init _ = Toplevel.toplevel;
   232 );
   233 
   234 fun ml_diag verbose source = Toplevel.keep (fn state =>
   235   let
   236     val opt_ctxt =
   237       try Toplevel.generic_theory_of state
   238       |> Option.map (Context.proof_of #> Diag_State.put state);
   239     val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
   240   in ML_Context.eval_source_in opt_ctxt flags source end);
   241 
   242 val diag_state = Diag_State.get;
   243 
   244 fun diag_goal ctxt =
   245   Proof.goal (Toplevel.proof_of (diag_state ctxt))
   246     handle Toplevel.UNDEF => error "No goal present";
   247 
   248 val _ = Theory.setup
   249   (ML_Antiquotation.value (Binding.qualify true "Isar" @{binding state})
   250     (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
   251    ML_Antiquotation.value (Binding.qualify true "Isar" @{binding goal})
   252     (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
   253 
   254 
   255 (* theorems of theory or proof context *)
   256 
   257 fun pretty_theorems verbose st =
   258   if Toplevel.is_proof st then
   259     Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
   260   else
   261     let
   262       val thy = Toplevel.theory_of st;
   263       val prev_thys =
   264         (case Toplevel.previous_context_of st of
   265           SOME prev => [Proof_Context.theory_of prev]
   266         | NONE => Theory.parents_of thy);
   267     in Proof_Display.pretty_theorems_diff verbose prev_thys thy end;
   268 
   269 
   270 (* display dependencies *)
   271 
   272 val locale_deps =
   273   Toplevel.unknown_theory o
   274   Toplevel.keep (Toplevel.theory_of #> (fn thy =>
   275     Locale.pretty_locale_deps thy
   276     |> map (fn {name, parents, body} =>
   277       ((name, Graph_Display.content_node (Locale.extern thy name) [body]), parents))
   278     |> Graph_Display.display_graph));
   279 
   280 
   281 (* print theorems, terms, types etc. *)
   282 
   283 local
   284 
   285 fun string_of_stmts ctxt args =
   286   Attrib.eval_thms ctxt args
   287   |> map (Element.pretty_statement ctxt Thm.theoremK)
   288   |> Pretty.chunks2 |> Pretty.string_of;
   289 
   290 fun string_of_thms ctxt args =
   291   Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
   292 
   293 fun string_of_prfs full state arg =
   294   Pretty.string_of
   295     (case arg of
   296       NONE =>
   297         let
   298           val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
   299           val thy = Proof_Context.theory_of ctxt;
   300           val prf = Thm.proof_of thm;
   301           val prop = Thm.full_prop_of thm;
   302           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   303         in
   304           Proof_Syntax.pretty_proof ctxt
   305             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   306         end
   307     | SOME srcs =>
   308         let val ctxt = Toplevel.context_of state
   309         in map (Proof_Syntax.pretty_proof_of ctxt full) (Attrib.eval_thms ctxt srcs) end
   310         |> Pretty.chunks);
   311 
   312 fun string_of_prop ctxt s =
   313   let
   314     val prop = Syntax.read_prop ctxt s;
   315     val ctxt' = Variable.auto_fixes prop ctxt;
   316   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   317 
   318 fun string_of_term ctxt s =
   319   let
   320     val t = Syntax.read_term ctxt s;
   321     val T = Term.type_of t;
   322     val ctxt' = Variable.auto_fixes t ctxt;
   323   in
   324     Pretty.string_of
   325       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   326         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   327   end;
   328 
   329 fun string_of_type ctxt (s, NONE) =
   330       let val T = Syntax.read_typ ctxt s
   331       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
   332   | string_of_type ctxt (s1, SOME s2) =
   333       let
   334         val ctxt' = Config.put show_sorts true ctxt;
   335         val raw_T = Syntax.parse_typ ctxt' s1;
   336         val S = Syntax.read_sort ctxt' s2;
   337         val T =
   338           Syntax.check_term ctxt'
   339             (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
   340           |> Logic.dest_type;
   341       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
   342 
   343 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   344   Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
   345 
   346 in
   347 
   348 val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
   349 val print_thms = print_item (string_of_thms o Toplevel.context_of);
   350 val print_prfs = print_item o string_of_prfs;
   351 val print_prop = print_item (string_of_prop o Toplevel.context_of);
   352 val print_term = print_item (string_of_term o Toplevel.context_of);
   353 val print_type = print_item (string_of_type o Toplevel.context_of);
   354 
   355 end;
   356 
   357 end;