src/Pure/Isar/isar_thy.ML
author wenzelm
Fri Feb 05 21:04:58 1999 +0100 (1999-02-05 ago)
changeset 6246 0aa2e536bc20
parent 6198 7fa2deb92b39
child 6253 dbaf79ac2ff9
permissions -rw-r--r--
improved theory, context, update_context;
     1 (*  Title:      Pure/Isar/isar_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Derived theory operations.
     6 
     7 TODO:
     8   - pure_thy.ML: add_constdefs (atomic!);
     9   - 'methods' section (proof macros, ML method defs) (!?);
    10   - next_block: ProofHistory open / close (!?);
    11 *)
    12 
    13 signature ISAR_THY =
    14 sig
    15   val add_text: 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_defs: ((bstring * string) * Args.src list) list -> theory -> theory
    22   val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
    23     -> theory -> theory
    24   val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
    25     -> theory -> theory
    26   val have_facts: (string * Args.src list) * (string * Args.src list) list
    27     -> ProofHistory.T -> ProofHistory.T
    28   val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
    29   val match_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
    30   val theorem: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
    31   val lemma: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
    32   val assume: string -> Args.src list -> (string * string list) list
    33     -> ProofHistory.T -> ProofHistory.T
    34   val show: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    35   val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    36   val chain: ProofHistory.T -> ProofHistory.T
    37   val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
    38   val begin_block: ProofHistory.T -> ProofHistory.T
    39   val next_block: ProofHistory.T -> ProofHistory.T
    40   val end_block: ProofHistory.T -> ProofHistory.T
    41   val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
    42   val qed: ProofHistory.T -> theory
    43   val kill_proof: ProofHistory.T -> theory
    44   val tac: Method.text -> ProofHistory.T -> ProofHistory.T
    45   val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
    46   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
    47   val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
    48   val immediate_proof: ProofHistory.T -> ProofHistory.T
    49   val default_proof: ProofHistory.T -> ProofHistory.T
    50   val use_mltext: string -> theory option -> theory option
    51   val use_mltext_theory: string -> theory -> theory
    52   val use_setup: string -> theory -> theory
    53   val parse_ast_translation: string -> theory -> theory
    54   val parse_translation: string -> theory -> theory
    55   val print_translation: string -> theory -> theory
    56   val typed_print_translation: string -> theory -> theory
    57   val print_ast_translation: string -> theory -> theory
    58   val token_translation: string -> theory -> theory
    59   val add_oracle: bstring * string -> theory -> theory
    60   val theory: string * string list * (string * bool) list
    61     -> Toplevel.transition -> Toplevel.transition
    62   val context: string -> Toplevel.transition -> Toplevel.transition
    63   val update_context: string -> Toplevel.transition -> Toplevel.transition
    64 end;
    65 
    66 structure IsarThy: ISAR_THY =
    67 struct
    68 
    69 
    70 (** derived theory and proof operations **)
    71 
    72 (* formal comments *)	(* FIXME dummy *)
    73 
    74 fun add_text (txt:string) (thy:theory) = thy;
    75 val add_chapter = add_text;
    76 val add_section = add_text;
    77 val add_subsection = add_text;
    78 val add_subsubsection = add_text;
    79 
    80 
    81 (* axioms and defs *)
    82 
    83 fun add_axms f args thy =
    84   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
    85 
    86 val add_axioms = add_axms PureThy.add_axioms;
    87 val add_defs = add_axms PureThy.add_defs;
    88 
    89 
    90 (* theorems *)
    91 
    92 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
    93   f name (map (attrib x) more_srcs)
    94     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
    95 
    96 
    97 val have_theorems =
    98   #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute PureThy.have_thmss;
    99 
   100 val have_lemmas =
   101   #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute
   102     (fn name => fn more_atts => PureThy.have_thmss name (more_atts @ [Drule.tag_lemma]));
   103 
   104 
   105 val have_thmss =
   106   gen_have_thmss (ProofContext.get_thms o Proof.context_of)
   107     (Attrib.local_attribute o Proof.theory_of) Proof.have_thmss;
   108 
   109 val have_facts = ProofHistory.apply o have_thmss;
   110 val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
   111 
   112 
   113 (* context *)
   114 
   115 val fix = ProofHistory.apply o Proof.fix;
   116 
   117 val match_bind = ProofHistory.apply o Proof.match_bind;
   118 
   119 
   120 (* statements *)
   121 
   122 fun global_statement f name tags s thy =
   123   ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
   124 
   125 fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
   126   f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
   127 
   128 val theorem = global_statement Proof.theorem;
   129 val lemma = global_statement Proof.lemma;
   130 val assume = local_statement Proof.assume;
   131 val show = local_statement Proof.show;
   132 val have = local_statement Proof.have;
   133 
   134 
   135 (* forward chaining *)
   136 
   137 val chain = ProofHistory.apply Proof.chain;
   138 
   139 
   140 (* end proof *)
   141 
   142 fun qed_with (alt_name, alt_tags) prf =
   143   let
   144     val state = ProofHistory.current prf;
   145     val thy = Proof.theory_of state;
   146     val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
   147     val (thy', (kind, name, thm)) = Method.qed alt_name alt_atts state;
   148 
   149     val prt_result = Pretty.block
   150       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
   151   in Pretty.writeln prt_result; thy end;
   152 
   153 val qed = qed_with (None, None);
   154 
   155 val kill_proof = Proof.theory_of o ProofHistory.current;
   156 
   157 
   158 (* blocks *)
   159 
   160 val begin_block = ProofHistory.apply_open Proof.begin_block;
   161 val next_block = ProofHistory.apply Proof.next_block;
   162 
   163 
   164 (* backward steps *)
   165 
   166 val tac = ProofHistory.applys o Method.tac;
   167 val then_tac = ProofHistory.applys o Method.then_tac;
   168 val proof = ProofHistory.applys o Method.proof;
   169 val end_block = ProofHistory.applys_close Method.end_block;
   170 val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
   171 val immediate_proof = ProofHistory.applys_close Method.immediate_proof;
   172 val default_proof = ProofHistory.applys_close Method.default_proof;
   173 
   174 
   175 (* use ML text *)
   176 
   177 fun use_mltext txt opt_thy =
   178   let
   179     val save_context = Context.get_context ();
   180     val _ = Context.set_context opt_thy;
   181     val opt_exn = (transform_error (use_text false) txt; None) handle exn => Some exn;
   182     val opt_thy' = Context.get_context ();
   183   in
   184     Context.set_context save_context;
   185     (case opt_exn of
   186       None => opt_thy'
   187     | Some exn => raise exn)
   188   end;
   189 
   190 fun use_mltext_theory txt thy =
   191   (case use_mltext txt (Some thy) of
   192     Some thy' => thy'
   193   | None => error "Missing result ML theory context");
   194 
   195 
   196 fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
   197 
   198 fun use_let name body txt =
   199   use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
   200 
   201 val use_setup =
   202   use_let "setup: (theory -> theory) list" "Library.apply setup";
   203 
   204 
   205 (* translation functions *)
   206 
   207 val parse_ast_translation =
   208   use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   209     "Theory.add_trfuns (parse_ast_translation, [], [], [])";
   210 
   211 val parse_translation =
   212   use_let "parse_translation: (string * (term list -> term)) list"
   213     "Theory.add_trfuns ([], parse_translation, [], [])";
   214 
   215 val print_translation =
   216   use_let "print_translation: (string * (term list -> term)) list"
   217     "Theory.add_trfuns ([], [], print_translation, [])";
   218 
   219 val print_ast_translation =
   220   use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   221     "Theory.add_trfuns ([], [], [], print_ast_translation)";
   222 
   223 val typed_print_translation =
   224   use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
   225     "Theory.add_trfunsT typed_print_translation";
   226 
   227 val token_translation =
   228   use_let "token_translation: (string * string * (string -> string * int)) list"
   229     "Theory.add_tokentrfuns token_translation";
   230 
   231 
   232 (* add_oracle *)
   233 
   234 fun add_oracle (name, txt) =
   235   use_let
   236     "oracle: bstring * (Sign.sg * Object.T -> term)"
   237     "Theory.add_oracle oracle"
   238     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   239 
   240 
   241 (* theory init and exit *)
   242 
   243 fun begin_theory (name, parents, files) () =
   244   let val thy = ThyInfo.begin_theory name parents
   245   in seq ThyInfo.use (mapfilter (fn (x, true) => Some x | _ => None) files); thy end;
   246 
   247 fun end_theory thy =
   248   let val thy' = PureThy.end_theory thy in
   249     transform_error ThyInfo.put_theory thy'
   250       handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
   251   end;
   252 
   253 fun theory spec = Toplevel.init_theory (begin_theory spec) end_theory;
   254 
   255 
   256 (* context switch *)
   257 
   258 fun switch_theory require name =
   259   Toplevel.init_theory
   260     (fn () => (Context.save require name; ThyInfo.get_theory name)) (K ());
   261 
   262 val context = switch_theory ThyInfo.use_thy;
   263 val update_context = switch_theory ThyInfo.update_thy;
   264 
   265 
   266 end;