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