src/Pure/Isar/isar_thy.ML
author wenzelm
Wed Nov 18 11:00:02 1998 +0100 (1998-11-18 ago)
changeset 5925 669d0bc621e1
parent 5915 66f4bde4c6e7
child 5938 fe7640933a47
permissions -rw-r--r--
tuned comments;
     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_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
    16   val add_defs: ((bstring * string) * Args.src list) list -> theory -> theory
    17   val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
    18     -> theory -> theory
    19   val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
    20     -> theory -> theory
    21   val have_facts: (string * Args.src list) * (string * Args.src list) list
    22     -> ProofHistory.T -> ProofHistory.T
    23   val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
    24   val match_bind: (string * string) list -> ProofHistory.T -> ProofHistory.T
    25   val theorem: string -> Args.src list -> string -> theory -> ProofHistory.T
    26   val lemma: string -> Args.src list -> string -> theory -> ProofHistory.T
    27   val assume: string -> Args.src list -> string list -> ProofHistory.T -> ProofHistory.T
    28   val show: string -> Args.src list -> string -> ProofHistory.T -> ProofHistory.T
    29   val have: string -> Args.src list -> string -> ProofHistory.T -> ProofHistory.T
    30   val chain: ProofHistory.T -> ProofHistory.T
    31   val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
    32   val begin_block: ProofHistory.T -> ProofHistory.T
    33   val next_block: ProofHistory.T -> ProofHistory.T
    34   val end_block: ProofHistory.T -> ProofHistory.T
    35   val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
    36   val qed: ProofHistory.T -> theory
    37   val kill_proof: ProofHistory.T -> theory
    38   val tac: Method.text -> ProofHistory.T -> ProofHistory.T
    39   val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
    40   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
    41   val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
    42   val trivial_proof: ProofHistory.T -> ProofHistory.T
    43   val default_proof: ProofHistory.T -> ProofHistory.T
    44   val use_mltext: string -> theory option -> theory option
    45   val use_mltext_theory: string -> theory -> theory
    46   val use_setup: string -> theory -> theory
    47   val parse_ast_translation: string -> theory -> theory
    48   val parse_translation: string -> theory -> theory
    49   val print_translation: string -> theory -> theory
    50   val typed_print_translation: string -> theory -> theory
    51   val print_ast_translation: string -> theory -> theory
    52   val token_translation: string -> theory -> theory
    53   val add_oracle: bstring * string -> theory -> theory
    54   val the_theory: string -> unit -> theory
    55   val begin_theory: string * string list -> unit -> theory
    56   val end_theory: theory -> unit
    57 end;
    58 
    59 structure IsarThy: ISAR_THY =
    60 struct
    61 
    62 
    63 (** derived theory and proof operations **)
    64 
    65 (* axioms and defs *)
    66 
    67 fun add_axms f args thy =
    68   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
    69 
    70 val add_axioms = add_axms PureThy.add_axioms;
    71 val add_defs = add_axms PureThy.add_defs;
    72 
    73 
    74 (* theorems *)
    75 
    76 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
    77   f name (map (attrib x) more_srcs)
    78     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
    79 
    80 
    81 val have_theorems =
    82   #1 oo gen_have_thmss PureThy.get_tthms Attrib.global_attribute PureThy.have_tthmss;
    83 
    84 val have_lemmas =
    85   #1 oo gen_have_thmss PureThy.get_tthms Attrib.global_attribute
    86     (fn name => fn more_atts => PureThy.have_tthmss name (more_atts @ [Attribute.tag_lemma]));
    87 
    88 
    89 val have_thmss =
    90   gen_have_thmss (ProofContext.get_tthms o Proof.context_of)
    91     (Attrib.local_attribute o Proof.theory_of) Proof.have_tthmss;
    92 
    93 val have_facts = ProofHistory.apply o have_thmss;
    94 val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
    95 
    96 
    97 (* context *)
    98 
    99 val fix = ProofHistory.apply o Proof.fix;
   100 
   101 val match_bind = ProofHistory.apply o Proof.match_bind;
   102 
   103 
   104 (* statements *)
   105 
   106 fun global_statement f name tags s thy =
   107   ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
   108 
   109 fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
   110   f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
   111 
   112 val theorem = global_statement Proof.theorem;
   113 val lemma = global_statement Proof.lemma;
   114 val assume = local_statement Proof.assume;
   115 val show = local_statement Proof.show;
   116 val have = local_statement Proof.have;
   117 
   118 
   119 (* forward chaining *)
   120 
   121 val chain = ProofHistory.apply Proof.chain;
   122 
   123 
   124 (* end proof *)
   125 
   126 fun qed_with (alt_name, alt_tags) prf =
   127   let
   128     val state = ProofHistory.current prf;
   129     val thy = Proof.theory_of state;
   130     val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
   131     val (thy', (kind, name, tthm)) = Method.qed alt_name alt_atts state;
   132 
   133     val prt_result = Pretty.block
   134       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Attribute.pretty_tthm tthm];
   135   in Pretty.writeln prt_result; thy end;
   136 
   137 val qed = qed_with (None, None);
   138 
   139 val kill_proof = Proof.theory_of o ProofHistory.current;
   140 
   141 
   142 (* blocks *)
   143 
   144 val begin_block = ProofHistory.apply_open Proof.begin_block;
   145 val next_block = ProofHistory.apply Proof.next_block;
   146 
   147 
   148 (* backward steps *)
   149 
   150 val tac = ProofHistory.applys o Method.tac;
   151 val then_tac = ProofHistory.applys o Method.then_tac;
   152 val proof = ProofHistory.applys o Method.proof;
   153 val end_block = ProofHistory.applys_close Method.end_block;
   154 val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
   155 val trivial_proof = ProofHistory.applys_close Method.trivial_proof;
   156 val default_proof = ProofHistory.applys_close Method.default_proof;
   157 
   158 
   159 (* use ML text *)
   160 
   161 fun use_mltext txt opt_thy =
   162   let
   163     val save_context = Context.get_context ();
   164     val _ = Context.set_context opt_thy;
   165     val opt_exn = (transform_error (use_text false) txt; None) handle exn => Some exn;
   166     val opt_thy' = Context.get_context ();
   167   in
   168     Context.set_context save_context;
   169     (case opt_exn of
   170       None => opt_thy'
   171     | Some exn => raise exn)
   172   end;
   173 
   174 fun use_mltext_theory txt thy =
   175   (case use_mltext txt (Some thy) of
   176     Some thy' => thy'
   177   | None => error "Missing result ML theory context");
   178 
   179 
   180 fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
   181 
   182 fun use_let name body txt =
   183   use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
   184 
   185 val use_setup =
   186   use_let "setup: (theory -> theory) list" "Library.apply setup";
   187 
   188 
   189 (* translation functions *)
   190 
   191 val parse_ast_translation =
   192   use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   193     "Theory.add_trfuns (parse_ast_translation, [], [], [])";
   194 
   195 val parse_translation =
   196   use_let "parse_translation: (string * (term list -> term)) list"
   197     "Theory.add_trfuns ([], parse_translation, [], [])";
   198 
   199 val print_translation =
   200   use_let "print_translation: (string * (term list -> term)) list"
   201     "Theory.add_trfuns ([], [], print_translation, [])";
   202 
   203 val print_ast_translation =
   204   use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   205     "Theory.add_trfuns ([], [], [], print_ast_translation)";
   206 
   207 val typed_print_translation =
   208   use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
   209     "Theory.add_trfunsT typed_print_translation";
   210 
   211 val token_translation =
   212   use_let "token_translation: (string * string * (string -> string * int)) list"
   213     "Theory.add_tokentrfuns token_translation";
   214 
   215 
   216 (* add_oracle *)
   217 
   218 fun add_oracle (name, txt) =
   219   use_let
   220     "oracle: bstring * (Sign.sg * Object.T -> term)"
   221     "Theory.add_oracle oracle"
   222     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   223 
   224 
   225 (* theory init and exit *)
   226 
   227 fun the_theory name () = ThyInfo.theory name;
   228 
   229 fun begin_theory (name, names) () =
   230   PureThy.begin_theory name (map ThyInfo.theory names);
   231 
   232 
   233 fun end_theory thy =
   234   let val thy' = PureThy.end_theory thy in
   235     transform_error ThyInfo.store_theory thy'
   236       handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
   237   end;
   238 
   239 
   240 end;