src/Pure/Isar/isar_cmd.ML
author wenzelm
Sat Mar 09 13:19:13 2019 +0100 (3 months ago ago)
changeset 70064 f8293bf510a0
parent 69268 c546e37f6cb9
child 70067 0cb8753bdb50
permissions -rw-r--r--
clarified Toplevel.state: more explicit types;
presentation context is always present, with default to Pure.thy and fall-back to Pure bootstrap theory;
     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 declaration: {syntax: bool, pervasive: bool} -> Input.source -> local_theory -> local_theory
    20   val simproc_setup: string * Position.T -> string list -> Input.source ->
    21     local_theory -> local_theory
    22   val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
    23   val terminal_proof: Method.text_range * Method.text_range option ->
    24     Toplevel.transition -> Toplevel.transition
    25   val default_proof: Toplevel.transition -> Toplevel.transition
    26   val immediate_proof: Toplevel.transition -> Toplevel.transition
    27   val done_proof: Toplevel.transition -> Toplevel.transition
    28   val skip_proof: Toplevel.transition -> Toplevel.transition
    29   val ml_diag: bool -> Input.source -> Toplevel.transition -> Toplevel.transition
    30   val diag_state: Proof.context -> Toplevel.state
    31   val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
    32   val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
    33   val print_stmts: string list * (Facts.ref * Token.src list) list
    34     -> Toplevel.transition -> Toplevel.transition
    35   val print_thms: string list * (Facts.ref * Token.src list) list
    36     -> Toplevel.transition -> Toplevel.transition
    37   val print_prfs: bool -> string list * (Facts.ref * Token.src list) list option
    38     -> Toplevel.transition -> Toplevel.transition
    39   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
    40   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
    41   val print_type: (string list * (string * string option)) ->
    42     Toplevel.transition -> Toplevel.transition
    43 end;
    44 
    45 structure Isar_Cmd: ISAR_CMD =
    46 struct
    47 
    48 
    49 (** theory declarations **)
    50 
    51 (* generic setup *)
    52 
    53 fun setup source =
    54   ML_Context.expression (Input.pos_of source)
    55     (ML_Lex.read "Theory.setup (" @ ML_Lex.read_source source @ ML_Lex.read ")")
    56   |> Context.theory_map;
    57 
    58 fun local_setup source =
    59   ML_Context.expression (Input.pos_of source)
    60     (ML_Lex.read "Theory.local_setup (" @ ML_Lex.read_source source @ ML_Lex.read ")")
    61   |> Context.proof_map;
    62 
    63 
    64 (* translation functions *)
    65 
    66 fun parse_ast_translation source =
    67   ML_Context.expression (Input.pos_of source)
    68     (ML_Lex.read "Theory.setup (Sign.parse_ast_translation (" @
    69       ML_Lex.read_source source @ ML_Lex.read "))")
    70   |> Context.theory_map;
    71 
    72 fun parse_translation source =
    73   ML_Context.expression (Input.pos_of source)
    74     (ML_Lex.read "Theory.setup (Sign.parse_translation (" @
    75       ML_Lex.read_source source @ ML_Lex.read "))")
    76   |> Context.theory_map;
    77 
    78 fun print_translation source =
    79   ML_Context.expression (Input.pos_of source)
    80     (ML_Lex.read "Theory.setup (Sign.print_translation (" @
    81       ML_Lex.read_source source @ ML_Lex.read "))")
    82   |> Context.theory_map;
    83 
    84 fun typed_print_translation source =
    85   ML_Context.expression (Input.pos_of source)
    86     (ML_Lex.read "Theory.setup (Sign.typed_print_translation (" @
    87       ML_Lex.read_source source @ ML_Lex.read "))")
    88   |> Context.theory_map;
    89 
    90 fun print_ast_translation source =
    91   ML_Context.expression (Input.pos_of source)
    92     (ML_Lex.read "Theory.setup (Sign.print_ast_translation (" @
    93       ML_Lex.read_source source @ ML_Lex.read "))")
    94   |> Context.theory_map;
    95 
    96 
    97 (* translation rules *)
    98 
    99 fun read_trrules thy raw_rules =
   100   let
   101     val ctxt = Proof_Context.init_global thy;
   102     val read_root =
   103       #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} ctxt;
   104   in
   105     raw_rules
   106     |> map (Syntax.map_trrule (fn (r, s) => Syntax_Phases.parse_ast_pattern ctxt (read_root r, s)))
   107   end;
   108 
   109 fun translations args thy = Sign.add_trrules (read_trrules thy args) thy;
   110 fun no_translations args thy = Sign.del_trrules (read_trrules thy args) thy;
   111 
   112 
   113 (* oracles *)
   114 
   115 fun oracle (name, range) source =
   116   ML_Context.expression (Input.pos_of source)
   117     (ML_Lex.read "val " @
   118      ML_Lex.read_set_range range name @
   119      ML_Lex.read
   120       (" = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (" ^
   121         ML_Syntax.make_binding (name, #1 range) ^ ", ") @
   122      ML_Lex.read_source source @
   123      ML_Lex.read "))))")
   124   |> Context.theory_map;
   125 
   126 
   127 (* declarations *)
   128 
   129 fun declaration {syntax, pervasive} source =
   130   ML_Context.expression (Input.pos_of source)
   131     (ML_Lex.read
   132       ("Theory.local_setup (Local_Theory.declaration {syntax = " ^
   133         Bool.toString syntax ^ ", pervasive = " ^ Bool.toString pervasive ^ "} (") @
   134      ML_Lex.read_source source @ ML_Lex.read "))")
   135   |> Context.proof_map;
   136 
   137 
   138 (* simprocs *)
   139 
   140 fun simproc_setup name lhss source =
   141   ML_Context.expression (Input.pos_of source)
   142     (ML_Lex.read
   143       ("Theory.local_setup (Simplifier.define_simproc_cmd (" ^
   144         ML_Syntax.make_binding name ^ ") {lhss = " ^ ML_Syntax.print_strings lhss ^
   145         ", proc = (") @ ML_Lex.read_source source @ ML_Lex.read ")})")
   146   |> Context.proof_map;
   147 
   148 
   149 (* local endings *)
   150 
   151 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   152 val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
   153 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   154 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   155 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   156 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   157 
   158 
   159 (* global endings *)
   160 
   161 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   162 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
   163 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   164 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   165 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   166 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   167 
   168 
   169 (* common endings *)
   170 
   171 fun qed m = local_qed m o global_qed m;
   172 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   173 val default_proof = local_default_proof o global_default_proof;
   174 val immediate_proof = local_immediate_proof o global_immediate_proof;
   175 val done_proof = local_done_proof o global_done_proof;
   176 val skip_proof = local_skip_proof o global_skip_proof;
   177 
   178 
   179 (* diagnostic ML evaluation *)
   180 
   181 structure Diag_State = Proof_Data
   182 (
   183   type T = Toplevel.state option;
   184   fun init _ = NONE;
   185 );
   186 
   187 fun ml_diag verbose source = Toplevel.keep (fn state =>
   188   let
   189     val opt_ctxt =
   190       try Toplevel.generic_theory_of state
   191       |> Option.map (Context.proof_of #> Diag_State.put (SOME state));
   192     val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
   193   in ML_Context.eval_source_in opt_ctxt flags source end);
   194 
   195 fun diag_state ctxt =
   196   (case Diag_State.get ctxt of
   197     SOME st => st
   198   | NONE => Toplevel.init ());
   199 
   200 val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
   201 
   202 val _ = Theory.setup
   203   (ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>state\<close>)
   204     (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
   205    ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>goal\<close>)
   206     (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
   207 
   208 
   209 (* theorems of theory or proof context *)
   210 
   211 fun pretty_theorems verbose st =
   212   if Toplevel.is_proof st then
   213     Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
   214   else
   215     let
   216       val ctxt = Toplevel.context_of st;
   217       val prev_thys =
   218         (case Toplevel.previous_theory_of st of
   219           SOME thy => [thy]
   220         | NONE => Theory.parents_of (Proof_Context.theory_of ctxt));
   221     in Proof_Display.pretty_theorems_diff verbose prev_thys ctxt end;
   222 
   223 
   224 (* print theorems, terms, types etc. *)
   225 
   226 local
   227 
   228 fun string_of_stmts ctxt args =
   229   Attrib.eval_thms ctxt args
   230   |> map (Element.pretty_statement ctxt Thm.theoremK)
   231   |> Pretty.chunks2 |> Pretty.string_of;
   232 
   233 fun string_of_thms ctxt args =
   234   Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
   235 
   236 fun string_of_prfs full state arg =
   237   Pretty.string_of
   238     (case arg of
   239       NONE =>
   240         let
   241           val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
   242           val prf = Thm.proof_of thm;
   243           val prop = Thm.full_prop_of thm;
   244           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   245         in
   246           Proof_Syntax.pretty_proof ctxt
   247             (if full then Reconstruct.reconstruct_proof ctxt prop prf' else prf')
   248         end
   249     | SOME srcs =>
   250         let
   251           val ctxt = Toplevel.context_of state;
   252           val pretty_proof = Proof_Syntax.pretty_clean_proof_of ctxt full;
   253         in Pretty.chunks (map pretty_proof (Attrib.eval_thms ctxt srcs)) end);
   254 
   255 fun string_of_prop ctxt s =
   256   let
   257     val prop = Syntax.read_prop ctxt s;
   258     val ctxt' = Variable.auto_fixes prop ctxt;
   259   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   260 
   261 fun string_of_term ctxt s =
   262   let
   263     val t = Syntax.read_term ctxt s;
   264     val T = Term.type_of t;
   265     val ctxt' = Variable.auto_fixes t ctxt;
   266   in
   267     Pretty.string_of
   268       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   269         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   270   end;
   271 
   272 fun string_of_type ctxt (s, NONE) =
   273       let val T = Syntax.read_typ ctxt s
   274       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
   275   | string_of_type ctxt (s1, SOME s2) =
   276       let
   277         val ctxt' = Config.put show_sorts true ctxt;
   278         val raw_T = Syntax.parse_typ ctxt' s1;
   279         val S = Syntax.read_sort ctxt' s2;
   280         val T =
   281           Syntax.check_term ctxt'
   282             (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
   283           |> Logic.dest_type;
   284       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
   285 
   286 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   287   Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
   288 
   289 in
   290 
   291 val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
   292 val print_thms = print_item (string_of_thms o Toplevel.context_of);
   293 val print_prfs = print_item o string_of_prfs;
   294 val print_prop = print_item (string_of_prop o Toplevel.context_of);
   295 val print_term = print_item (string_of_term o Toplevel.context_of);
   296 val print_type = print_item (string_of_type o Toplevel.context_of);
   297 
   298 end;
   299 
   300 end;