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