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