src/Pure/Isar/isar_thy.ML
author wenzelm
Fri Mar 19 11:24:00 1999 +0100 (1999-03-19)
changeset 6404 2daaf2943c79
parent 6371 8469852acbc0
child 6483 3e5d450c2b31
permissions -rw-r--r--
common qed and end of proofs;
     1 (*  Title:      Pure/Isar/isar_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Pure/Isar derived theory operations.
     6 
     7 TODO:
     8   - 'methods' section (proof macros, ML method defs) (!?);
     9   - next_block: ProofHistory open / close (!?);
    10 *)
    11 
    12 signature ISAR_THY =
    13 sig
    14   val add_text: string -> theory -> theory
    15   val add_title: string -> string -> string -> theory -> theory
    16   val add_chapter: string -> theory -> theory
    17   val add_section: string -> theory -> theory
    18   val add_subsection: string -> theory -> theory
    19   val add_subsubsection: string -> theory -> theory
    20   val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
    21   val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory
    22   val add_defs: ((bstring * string) * Args.src list) list -> theory -> theory
    23   val add_defs_i: ((bstring * term) * theory attribute list) list -> theory -> theory
    24   val add_constdefs: ((bstring * string * mixfix) * string) list -> theory -> theory
    25   val add_constdefs_i: ((bstring * typ * mixfix) * term) list -> theory -> theory
    26   val apply_theorems: (xstring * Args.src list) list -> theory -> theory * thm list
    27   val apply_theorems_i: (thm * theory attribute list) list -> theory -> theory * thm list
    28   val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
    29     -> theory -> theory
    30   val have_theorems_i: (bstring * theory attribute list) * (thm * theory attribute list) list
    31     -> theory -> theory
    32   val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
    33     -> theory -> theory
    34   val have_lemmas_i: (bstring * theory attribute list) * (thm * theory attribute list) list
    35     -> theory -> theory
    36   val have_facts: (string * Args.src list) * (string * Args.src list) list
    37     -> ProofHistory.T -> ProofHistory.T
    38   val have_facts_i: (string * Proof.context attribute list) *
    39     (thm * Proof.context attribute list) list -> ProofHistory.T -> ProofHistory.T
    40   val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
    41   val from_facts_i: (thm * Proof.context attribute list) list -> ProofHistory.T -> ProofHistory.T
    42   val chain: ProofHistory.T -> ProofHistory.T
    43   val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
    44   val fix_i: (string * typ) list -> ProofHistory.T -> ProofHistory.T
    45   val match_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
    46   val match_bind_i: (term list * term) list -> ProofHistory.T -> ProofHistory.T
    47   val theorem: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
    48   val theorem_i: bstring -> theory attribute list -> term * term list -> theory -> ProofHistory.T
    49   val lemma: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
    50   val lemma_i: bstring -> theory attribute list -> term * term list -> theory -> ProofHistory.T
    51   val assume: string -> Args.src list -> (string * string list) list
    52     -> ProofHistory.T -> ProofHistory.T
    53   val assume_i: string -> Proof.context attribute list -> (term * term list) list
    54     -> ProofHistory.T -> ProofHistory.T
    55   val show: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    56   val show_i: string -> Proof.context attribute list -> term * term list
    57     -> ProofHistory.T -> ProofHistory.T
    58   val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    59   val have_i: string -> Proof.context attribute list -> term * term list
    60     -> ProofHistory.T -> ProofHistory.T
    61   val begin_block: ProofHistory.T -> ProofHistory.T
    62   val next_block: ProofHistory.T -> ProofHistory.T
    63   val end_block: ProofHistory.T -> ProofHistory.T
    64   val tac: Method.text -> ProofHistory.T -> ProofHistory.T
    65   val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
    66   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
    67   val kill_proof: ProofHistory.T -> theory
    68   val global_qed_with: bstring option * Args.src list option -> Method.text option
    69     -> Toplevel.transition -> Toplevel.transition
    70   val global_qed_with_i: bstring option * theory attribute list option -> Method.text option
    71     -> Toplevel.transition -> Toplevel.transition
    72   val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
    73   val terminal_proof: Method.text -> Toplevel.transition -> Toplevel.transition
    74   val immediate_proof: Toplevel.transition -> Toplevel.transition
    75   val default_proof: Toplevel.transition -> Toplevel.transition
    76   val use_mltext: string -> theory option -> theory option
    77   val use_mltext_theory: string -> theory -> theory
    78   val use_setup: string -> theory -> theory
    79   val parse_ast_translation: string -> theory -> theory
    80   val parse_translation: string -> theory -> theory
    81   val print_translation: string -> theory -> theory
    82   val typed_print_translation: string -> theory -> theory
    83   val print_ast_translation: string -> theory -> theory
    84   val token_translation: string -> theory -> theory
    85   val add_oracle: bstring * string -> theory -> theory
    86   val begin_theory: string -> string list -> (string * bool) list -> theory
    87   val end_theory: theory -> theory
    88   val theory: string * string list * (string * bool) list
    89     -> Toplevel.transition -> Toplevel.transition
    90   val context: string -> Toplevel.transition -> Toplevel.transition
    91   val update_context: string -> Toplevel.transition -> Toplevel.transition
    92 end;
    93 
    94 structure IsarThy: ISAR_THY =
    95 struct
    96 
    97 
    98 (** derived theory and proof operations **)
    99 
   100 (* formal comments *)   (* FIXME dummy *)
   101 
   102 fun add_text (txt:string) (thy:theory) = thy;
   103 fun add_title title author date thy = thy;
   104 val add_chapter = add_text;
   105 val add_section = add_text;
   106 val add_subsection = add_text;
   107 val add_subsubsection = add_text;
   108 
   109 
   110 (* axioms and defs *)
   111 
   112 fun add_axms f args thy =
   113   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
   114 
   115 val add_axioms = add_axms PureThy.add_axioms;
   116 val add_axioms_i = PureThy.add_axioms_i;
   117 val add_defs = add_axms PureThy.add_defs;
   118 val add_defs_i = PureThy.add_defs_i;
   119 
   120 
   121 (* constdefs *)
   122 
   123 fun gen_add_constdefs consts defs args thy =
   124   thy
   125   |> consts (map fst args)
   126   |> defs (map (fn ((c, _, mx), s) => ((Thm.def_name (Syntax.const_name c mx), s), [])) args);
   127 
   128 val add_constdefs = gen_add_constdefs Theory.add_consts add_defs;
   129 val add_constdefs_i = gen_add_constdefs Theory.add_consts_i add_defs_i;
   130 
   131 
   132 (* theorems *)
   133 
   134 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
   135   f name (map (attrib x) more_srcs)
   136     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
   137 
   138 fun global_have_thmss x = gen_have_thmss PureThy.get_thms Attrib.global_attribute x;
   139 
   140 fun local_have_thmss x =
   141   gen_have_thmss (ProofContext.get_thms o Proof.context_of)
   142     (Attrib.local_attribute o Proof.theory_of) x;
   143 
   144 fun have_thmss_i f ((name, more_atts), th_atts) =
   145   f name more_atts (map (apfst single) th_atts);
   146 
   147 fun have_lemss name atts = PureThy.have_thmss name (atts @ [Drule.tag_lemma]);
   148 
   149 
   150 fun apply_theorems th_srcs = global_have_thmss PureThy.have_thmss ((None, []), th_srcs);
   151 fun apply_theorems_i th_srcs = have_thmss_i PureThy.have_thmss ((None, []), th_srcs);
   152 val have_theorems = #1 oo global_have_thmss (PureThy.have_thmss o Some);
   153 val have_theorems_i = #1 oo have_thmss_i (PureThy.have_thmss o Some);
   154 val have_lemmas = #1 oo global_have_thmss (have_lemss o Some);
   155 val have_lemmas_i = #1 oo have_thmss_i (have_lemss o Some);
   156 val have_facts = ProofHistory.apply o local_have_thmss Proof.have_thmss;
   157 val have_facts_i = ProofHistory.apply o have_thmss_i Proof.have_thmss;
   158 
   159 
   160 (* forward chaining *)
   161 
   162 val from_facts =
   163   ProofHistory.apply o (Proof.chain oo curry (local_have_thmss Proof.have_thmss) ("", []));
   164 
   165 val from_facts_i =
   166   ProofHistory.apply o (Proof.chain oo curry (have_thmss_i Proof.have_thmss) ("", []));
   167 
   168 val chain = ProofHistory.apply Proof.chain;
   169 
   170 
   171 (* context *)
   172 
   173 val fix = ProofHistory.apply o Proof.fix;
   174 val fix_i = ProofHistory.apply o Proof.fix_i;
   175 val match_bind = ProofHistory.apply o Proof.match_bind;
   176 val match_bind_i = ProofHistory.apply o Proof.match_bind_i;
   177 
   178 
   179 (* statements *)
   180 
   181 fun global_statement f name src s thy =
   182   ProofHistory.init (f name (map (Attrib.global_attribute thy) src) s thy);
   183 
   184 fun local_statement do_open f name src s = ProofHistory.apply_cond_open do_open (fn state =>
   185   f name (map (Attrib.local_attribute (Proof.theory_of state)) src) s state);
   186 
   187 fun global_statement_i f name atts t thy = ProofHistory.init (f name atts t thy);
   188 fun local_statement_i do_open f name atts t = ProofHistory.apply_cond_open do_open (f name atts t);
   189 
   190 val theorem = global_statement Proof.theorem;
   191 val theorem_i = global_statement_i Proof.theorem_i;
   192 val lemma = global_statement Proof.lemma;
   193 val lemma_i = global_statement_i Proof.lemma_i;
   194 val assume = local_statement false Proof.assume;
   195 val assume_i = local_statement_i false Proof.assume_i;
   196 val show = local_statement true Proof.show;
   197 val show_i = local_statement_i true Proof.show_i;
   198 val have = local_statement true Proof.have;
   199 val have_i = local_statement_i true Proof.have_i;
   200 
   201 
   202 (* blocks *)
   203 
   204 val begin_block = ProofHistory.apply_open Proof.begin_block;
   205 val next_block = ProofHistory.apply Proof.next_block;
   206 val end_block = ProofHistory.apply_close Proof.end_block;
   207 
   208 
   209 (* backward steps *)
   210 
   211 val tac = ProofHistory.applys o Method.tac;
   212 val then_tac = ProofHistory.applys o Method.then_tac;
   213 val proof = ProofHistory.applys o Method.proof;
   214 
   215 
   216 (* local endings *)
   217 
   218 val local_qed = Toplevel.proof o ProofHistory.applys_close o Method.local_qed;
   219 val local_terminal_proof = Toplevel.proof o ProofHistory.applys_close o Method.local_terminal_proof;
   220 val local_immediate_proof = Toplevel.proof (ProofHistory.applys_close Method.local_immediate_proof);
   221 val local_default_proof = Toplevel.proof (ProofHistory.applys_close Method.local_default_proof);
   222 
   223 
   224 (* global endings *)
   225 
   226 val kill_proof = Proof.theory_of o ProofHistory.current;
   227 
   228 fun global_result finish = Toplevel.proof_to_theory (fn prf =>
   229   let
   230     val state = ProofHistory.current prf;
   231     val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
   232     val (thy, (kind, name, thm)) = finish state;
   233 
   234     val prt_result = Pretty.block
   235       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
   236   in Pretty.writeln prt_result; thy end);
   237 
   238 fun gen_global_qed_with prep_att (alt_name, raw_atts) opt_text state =
   239   let
   240     val thy = Proof.theory_of state;
   241     val alt_atts = apsome (map (prep_att thy)) raw_atts;
   242   in Method.global_qed alt_name alt_atts opt_text state end;
   243 
   244 val global_qed_with = global_result oo gen_global_qed_with Attrib.global_attribute;
   245 val global_qed_with_i = global_result oo gen_global_qed_with (K I);
   246 val global_qed = global_qed_with (None, None);
   247 
   248 val global_terminal_proof = global_result o Method.global_terminal_proof;
   249 val global_immediate_proof = global_result Method.global_immediate_proof;
   250 val global_default_proof = global_result Method.global_default_proof;
   251 
   252 
   253 (* common endings *)
   254 
   255 fun qed opt_text = local_qed opt_text o global_qed opt_text;
   256 fun terminal_proof opt_text = local_terminal_proof opt_text o global_terminal_proof opt_text;
   257 val immediate_proof = local_immediate_proof o global_immediate_proof;
   258 val default_proof = local_default_proof o global_default_proof;
   259 
   260 
   261 (* use ML text *)
   262 
   263 fun use_mltext txt opt_thy = #2 (Context.pass opt_thy (use_text false) txt);
   264 fun use_mltext_theory txt thy = #2 (Context.pass_theory thy (use_text false) txt);
   265 
   266 fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
   267 
   268 fun use_let name body txt =
   269   use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
   270 
   271 val use_setup =
   272   use_let "setup: (theory -> theory) list" "Library.apply setup";
   273 
   274 
   275 (* translation functions *)
   276 
   277 val parse_ast_translation =
   278   use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   279     "Theory.add_trfuns (parse_ast_translation, [], [], [])";
   280 
   281 val parse_translation =
   282   use_let "parse_translation: (string * (term list -> term)) list"
   283     "Theory.add_trfuns ([], parse_translation, [], [])";
   284 
   285 val print_translation =
   286   use_let "print_translation: (string * (term list -> term)) list"
   287     "Theory.add_trfuns ([], [], print_translation, [])";
   288 
   289 val print_ast_translation =
   290   use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   291     "Theory.add_trfuns ([], [], [], print_ast_translation)";
   292 
   293 val typed_print_translation =
   294   use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
   295     "Theory.add_trfunsT typed_print_translation";
   296 
   297 val token_translation =
   298   use_let "token_translation: (string * string * (string -> string * int)) list"
   299     "Theory.add_tokentrfuns token_translation";
   300 
   301 
   302 (* add_oracle *)
   303 
   304 fun add_oracle (name, txt) =
   305   use_let
   306     "oracle: bstring * (Sign.sg * Object.T -> term)"
   307     "Theory.add_oracle oracle"
   308     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   309 
   310 
   311 (* theory init and exit *)      (* FIXME move? rearrange? *)
   312 
   313 fun begin_theory name parents files =
   314   let
   315     val paths = map (apfst Path.unpack) files;
   316     val thy = ThyInfo.begin_theory name parents paths;
   317   in Present.begin_theory name parents paths; thy end;
   318 
   319 
   320 (* FIXME
   321 fun end_theory thy =
   322   let val thy' = PureThy.end_theory thy in
   323     Present.end_theory (PureThy.get_name thy');
   324     transform_error ThyInfo.put_theory thy'
   325       handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
   326   end;
   327 *)
   328 
   329 fun end_theory thy =
   330   (Present.end_theory (PureThy.get_name thy); ThyInfo.end_theory thy);
   331 
   332 fun bg_theory (name, parents, files) () = begin_theory name parents files;
   333 fun en_theory thy = (end_theory thy; ());
   334 
   335 fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory;
   336 
   337 
   338 (* context switch *)
   339 
   340 fun switch_theory load name =
   341   Toplevel.init_theory
   342     (fn () => (Context.save load name; ThyInfo.get_theory name)) (K ());
   343 
   344 val context = switch_theory ThyInfo.use_thy;
   345 val update_context = switch_theory ThyInfo.update_thy;
   346 
   347 
   348 end;