src/Pure/Isar/isar_cmd.ML
author wenzelm
Wed Oct 31 15:53:32 2018 +0100 (7 months ago ago)
changeset 69221 1a52baa70aed
parent 69188 d8849cfad60f
child 69268 c546e37f6cb9
permissions -rw-r--r--
clarified ML_Context.expression: it is a closed expression, not a let-declaration -- thus source positions are more accurate (amending d8849cfad60f, 162a4c2e97bc);
     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   let
   117     val body_range = Input.range_of source;
   118     val body = ML_Lex.read_source source;
   119 
   120     val ants =
   121       ML_Lex.read
   122        ("local\n\
   123         \  val binding = " ^ ML_Syntax.make_binding (name, #1 range) ^ ";\n\
   124         \  val") @ ML_Lex.read_set_range body_range "oracle" @ ML_Lex.read "=" @ body @
   125         ML_Lex.read (";\nin\n\
   126         \  val") @ ML_Lex.read_set_range range name @ ML_Lex.read "=\
   127         \ snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, oracle))));\n\
   128         \end;\n";
   129   in
   130     Context.theory_map
   131       (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#1 body_range) ants))
   132   end;
   133 
   134 
   135 (* declarations *)
   136 
   137 fun declaration {syntax, pervasive} source =
   138   ML_Context.expression (Input.pos_of source)
   139     (ML_Lex.read
   140       ("Theory.local_setup (Local_Theory.declaration {syntax = " ^
   141         Bool.toString syntax ^ ", pervasive = " ^ Bool.toString pervasive ^ "} (") @
   142      ML_Lex.read_source source @ ML_Lex.read "))")
   143   |> Context.proof_map;
   144 
   145 
   146 (* simprocs *)
   147 
   148 fun simproc_setup name lhss source =
   149   ML_Context.expression (Input.pos_of source)
   150     (ML_Lex.read
   151       ("Theory.local_setup (Simplifier.define_simproc_cmd (" ^
   152         ML_Syntax.make_binding name ^ ") {lhss = " ^ ML_Syntax.print_strings lhss ^
   153         ", proc = (") @ ML_Lex.read_source source @ ML_Lex.read ")})")
   154   |> Context.proof_map;
   155 
   156 
   157 (* local endings *)
   158 
   159 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   160 val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
   161 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   162 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   163 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   164 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   165 
   166 
   167 (* global endings *)
   168 
   169 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   170 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
   171 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   172 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   173 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   174 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   175 
   176 
   177 (* common endings *)
   178 
   179 fun qed m = local_qed m o global_qed m;
   180 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   181 val default_proof = local_default_proof o global_default_proof;
   182 val immediate_proof = local_immediate_proof o global_immediate_proof;
   183 val done_proof = local_done_proof o global_done_proof;
   184 val skip_proof = local_skip_proof o global_skip_proof;
   185 
   186 
   187 (* diagnostic ML evaluation *)
   188 
   189 structure Diag_State = Proof_Data
   190 (
   191   type T = Toplevel.state;
   192   fun init _ = Toplevel.toplevel;
   193 );
   194 
   195 fun ml_diag verbose source = Toplevel.keep (fn state =>
   196   let
   197     val opt_ctxt =
   198       try Toplevel.generic_theory_of state
   199       |> Option.map (Context.proof_of #> Diag_State.put state);
   200     val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
   201   in ML_Context.eval_source_in opt_ctxt flags source end);
   202 
   203 val diag_state = Diag_State.get;
   204 val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
   205 
   206 val _ = Theory.setup
   207   (ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>state\<close>)
   208     (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
   209    ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>goal\<close>)
   210     (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
   211 
   212 
   213 (* theorems of theory or proof context *)
   214 
   215 fun pretty_theorems verbose st =
   216   if Toplevel.is_proof st then
   217     Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
   218   else
   219     let
   220       val ctxt = Toplevel.context_of st;
   221       val prev_thys =
   222         (case Toplevel.previous_theory_of st of
   223           SOME thy => [thy]
   224         | NONE => Theory.parents_of (Proof_Context.theory_of ctxt));
   225     in Proof_Display.pretty_theorems_diff verbose prev_thys ctxt end;
   226 
   227 
   228 (* print theorems, terms, types etc. *)
   229 
   230 local
   231 
   232 fun string_of_stmts ctxt args =
   233   Attrib.eval_thms ctxt args
   234   |> map (Element.pretty_statement ctxt Thm.theoremK)
   235   |> Pretty.chunks2 |> Pretty.string_of;
   236 
   237 fun string_of_thms ctxt args =
   238   Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
   239 
   240 fun string_of_prfs full state arg =
   241   Pretty.string_of
   242     (case arg of
   243       NONE =>
   244         let
   245           val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
   246           val prf = Thm.proof_of thm;
   247           val prop = Thm.full_prop_of thm;
   248           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   249         in
   250           Proof_Syntax.pretty_proof ctxt
   251             (if full then Reconstruct.reconstruct_proof ctxt prop prf' else prf')
   252         end
   253     | SOME srcs =>
   254         let
   255           val ctxt = Toplevel.context_of state;
   256           val pretty_proof = Proof_Syntax.pretty_clean_proof_of ctxt full;
   257         in Pretty.chunks (map pretty_proof (Attrib.eval_thms ctxt srcs)) end);
   258 
   259 fun string_of_prop ctxt s =
   260   let
   261     val prop = Syntax.read_prop ctxt s;
   262     val ctxt' = Variable.auto_fixes prop ctxt;
   263   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   264 
   265 fun string_of_term ctxt s =
   266   let
   267     val t = Syntax.read_term ctxt s;
   268     val T = Term.type_of t;
   269     val ctxt' = Variable.auto_fixes t ctxt;
   270   in
   271     Pretty.string_of
   272       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   273         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   274   end;
   275 
   276 fun string_of_type ctxt (s, NONE) =
   277       let val T = Syntax.read_typ ctxt s
   278       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
   279   | string_of_type ctxt (s1, SOME s2) =
   280       let
   281         val ctxt' = Config.put show_sorts true ctxt;
   282         val raw_T = Syntax.parse_typ ctxt' s1;
   283         val S = Syntax.read_sort ctxt' s2;
   284         val T =
   285           Syntax.check_term ctxt'
   286             (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
   287           |> Logic.dest_type;
   288       in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
   289 
   290 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   291   Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
   292 
   293 in
   294 
   295 val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
   296 val print_thms = print_item (string_of_thms o Toplevel.context_of);
   297 val print_prfs = print_item o string_of_prfs;
   298 val print_prop = print_item (string_of_prop o Toplevel.context_of);
   299 val print_term = print_item (string_of_term o Toplevel.context_of);
   300 val print_type = print_item (string_of_type o Toplevel.context_of);
   301 
   302 end;
   303 
   304 end;