src/Pure/Isar/isar_cmd.ML
author wenzelm
Wed Sep 09 20:57:21 2015 +0200 (2015-09-09 ago)
changeset 61144 5e94dfead1c2
parent 61086 fc7ab11128dc
child 61266 eb9522a9d997
permissions -rw-r--r--
simplified simproc programming interfaces;
     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     Position.here (Position.thread_data ()));
   148   thy |>
   149     (if unchecked then Global_Theory.add_defs_unchecked_cmd else Global_Theory.add_defs_cmd)
   150       overloaded
   151       (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute_cmd_global thy) srcs)) args)
   152   |> snd);
   153 
   154 
   155 (* declarations *)
   156 
   157 fun declaration {syntax, pervasive} source =
   158   ML_Lex.read_source false source
   159   |> ML_Context.expression (Input.range_of source) "declaration" "Morphism.declaration"
   160     ("Context.map_proof (Local_Theory.declaration {syntax = " ^ Bool.toString syntax ^ ", \
   161       \pervasive = " ^ Bool.toString pervasive ^ "} declaration)")
   162   |> Context.proof_map;
   163 
   164 
   165 (* simprocs *)
   166 
   167 fun simproc_setup name lhss source identifier =
   168   ML_Lex.read_source false source
   169   |> ML_Context.expression (Input.range_of source) "proc"
   170     "Morphism.morphism -> Proof.context -> cterm -> thm option"
   171     ("Context.map_proof (Simplifier.define_simproc_cmd " ^
   172       ML_Syntax.atomic (ML_Syntax.make_binding name) ^
   173       "{lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   174       \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})")
   175   |> Context.proof_map;
   176 
   177 
   178 (* local endings *)
   179 
   180 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   181 val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
   182 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   183 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   184 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   185 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   186 
   187 
   188 (* global endings *)
   189 
   190 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   191 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
   192 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   193 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   194 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   195 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   196 
   197 
   198 (* common endings *)
   199 
   200 fun qed m = local_qed m o global_qed m;
   201 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   202 val default_proof = local_default_proof o global_default_proof;
   203 val immediate_proof = local_immediate_proof o global_immediate_proof;
   204 val done_proof = local_done_proof o global_done_proof;
   205 val skip_proof = local_skip_proof o global_skip_proof;
   206 
   207 
   208 (* diagnostic ML evaluation *)
   209 
   210 structure Diag_State = Proof_Data
   211 (
   212   type T = Toplevel.state;
   213   fun init _ = Toplevel.toplevel;
   214 );
   215 
   216 fun ml_diag verbose source = Toplevel.keep (fn state =>
   217   let
   218     val opt_ctxt =
   219       try Toplevel.generic_theory_of state
   220       |> Option.map (Context.proof_of #> Diag_State.put state);
   221     val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
   222   in ML_Context.eval_source_in opt_ctxt flags source end);
   223 
   224 val diag_state = Diag_State.get;
   225 val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
   226 
   227 val _ = Theory.setup
   228   (ML_Antiquotation.value (Binding.qualify true "Isar" @{binding state})
   229     (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
   230    ML_Antiquotation.value (Binding.qualify true "Isar" @{binding goal})
   231     (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
   232 
   233 
   234 (* theorems of theory or proof context *)
   235 
   236 fun pretty_theorems verbose st =
   237   if Toplevel.is_proof st then
   238     Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
   239   else
   240     let
   241       val thy = Toplevel.theory_of st;
   242       val prev_thys =
   243         (case Toplevel.previous_context_of st of
   244           SOME prev => [Proof_Context.theory_of prev]
   245         | NONE => Theory.parents_of thy);
   246     in Proof_Display.pretty_theorems_diff verbose prev_thys thy end;
   247 
   248 
   249 (* print theorems, terms, types etc. *)
   250 
   251 local
   252 
   253 fun string_of_stmts ctxt args =
   254   Attrib.eval_thms ctxt args
   255   |> map (Element.pretty_statement ctxt Thm.theoremK)
   256   |> Pretty.chunks2 |> Pretty.string_of;
   257 
   258 fun string_of_thms ctxt args =
   259   Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
   260 
   261 fun string_of_prfs full state arg =
   262   Pretty.string_of
   263     (case arg of
   264       NONE =>
   265         let
   266           val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
   267           val thy = Proof_Context.theory_of ctxt;
   268           val prf = Thm.proof_of thm;
   269           val prop = Thm.full_prop_of thm;
   270           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   271         in
   272           Proof_Syntax.pretty_proof ctxt
   273             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   274         end
   275     | SOME srcs =>
   276         let val ctxt = Toplevel.context_of state
   277         in map (Proof_Syntax.pretty_proof_of ctxt full) (Attrib.eval_thms ctxt srcs) end
   278         |> Pretty.chunks);
   279 
   280 fun string_of_prop ctxt s =
   281   let
   282     val prop = Syntax.read_prop ctxt s;
   283     val ctxt' = Variable.auto_fixes prop ctxt;
   284   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   285 
   286 fun string_of_term ctxt s =
   287   let
   288     val t = Syntax.read_term ctxt s;
   289     val T = Term.type_of t;
   290     val ctxt' = Variable.auto_fixes t ctxt;
   291   in
   292     Pretty.string_of
   293       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   294         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   295   end;
   296 
   297 fun string_of_type ctxt (s, NONE) =
   298       let val T = Syntax.read_typ ctxt s
   299       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
   300   | string_of_type ctxt (s1, SOME s2) =
   301       let
   302         val ctxt' = Config.put show_sorts true ctxt;
   303         val raw_T = Syntax.parse_typ ctxt' s1;
   304         val S = Syntax.read_sort ctxt' s2;
   305         val T =
   306           Syntax.check_term ctxt'
   307             (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
   308           |> Logic.dest_type;
   309       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
   310 
   311 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   312   Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
   313 
   314 in
   315 
   316 val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
   317 val print_thms = print_item (string_of_thms o Toplevel.context_of);
   318 val print_prfs = print_item o string_of_prfs;
   319 val print_prop = print_item (string_of_prop o Toplevel.context_of);
   320 val print_term = print_item (string_of_term o Toplevel.context_of);
   321 val print_type = print_item (string_of_type o Toplevel.context_of);
   322 
   323 end;
   324 
   325 end;