src/Pure/Isar/isar_thy.ML
changeset 6371 8469852acbc0
parent 6354 a4c75cbd2fbf
child 6404 2daaf2943c79
equal deleted inserted replaced
6370:e71ac23a9111 6371:8469852acbc0
     1 (*  Title:      Pure/Isar/isar_thy.ML
     1 (*  Title:      Pure/Isar/isar_thy.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 Derived theory operations.
     5 Pure/Isar derived theory operations.
     6 
     6 
     7 TODO:
     7 TODO:
     8   - pure_thy.ML: add_constdefs (atomic!);
       
     9   - 'methods' section (proof macros, ML method defs) (!?);
     8   - 'methods' section (proof macros, ML method defs) (!?);
    10   - next_block: ProofHistory open / close (!?);
     9   - next_block: ProofHistory open / close (!?);
    11 *)
    10 *)
    12 
    11 
    13 signature ISAR_THY =
    12 signature ISAR_THY =
    17   val add_chapter: string -> theory -> theory
    16   val add_chapter: string -> theory -> theory
    18   val add_section: string -> theory -> theory
    17   val add_section: string -> theory -> theory
    19   val add_subsection: string -> theory -> theory
    18   val add_subsection: string -> theory -> theory
    20   val add_subsubsection: string -> theory -> theory
    19   val add_subsubsection: string -> theory -> theory
    21   val add_axioms: ((bstring * string) * Args.src list) list -> 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
    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
    23   val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) 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
    24     -> theory -> theory
    31     -> theory -> theory
    25   val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
    32   val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
    26     -> theory -> theory
    33     -> theory -> theory
       
    34   val have_lemmas_i: (bstring * theory attribute list) * (thm * theory attribute list) list
       
    35     -> theory -> theory
    27   val have_facts: (string * Args.src list) * (string * Args.src list) list
    36   val have_facts: (string * Args.src list) * (string * Args.src list) list
    28     -> ProofHistory.T -> ProofHistory.T
    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
    29   val fix: (string * string option) list -> 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
    30   val match_bind: (string list * string) 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
    31   val theorem: string -> Args.src list -> string * string list -> theory -> 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
    32   val lemma: string -> Args.src list -> string * string 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
    33   val assume: string -> Args.src list -> (string * string list) list
    51   val assume: string -> Args.src list -> (string * string list) list
    34     -> ProofHistory.T -> ProofHistory.T
    52     -> ProofHistory.T -> ProofHistory.T
       
    53   val assume_i: string -> Proof.context attribute list -> (term * term list) list
       
    54     -> ProofHistory.T -> ProofHistory.T
    35   val show: string -> Args.src list -> string * string list -> 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
    36   val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    58   val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
    37   val chain: ProofHistory.T -> ProofHistory.T
    59   val have_i: string -> Proof.context attribute list -> term * term list
    38   val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
    60     -> ProofHistory.T -> ProofHistory.T
    39   val begin_block: ProofHistory.T -> ProofHistory.T
    61   val begin_block: ProofHistory.T -> ProofHistory.T
    40   val next_block: ProofHistory.T -> ProofHistory.T
    62   val next_block: ProofHistory.T -> ProofHistory.T
    41   val end_block: ProofHistory.T -> ProofHistory.T
    63   val end_block: ProofHistory.T -> ProofHistory.T
    42   val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
    64   val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
       
    65   val qed_with_i: bstring option * theory attribute list option -> ProofHistory.T -> theory
    43   val qed: ProofHistory.T -> theory
    66   val qed: ProofHistory.T -> theory
    44   val kill_proof: ProofHistory.T -> theory
    67   val kill_proof: ProofHistory.T -> theory
    45   val tac: Method.text -> ProofHistory.T -> ProofHistory.T
    68   val tac: Method.text -> ProofHistory.T -> ProofHistory.T
       
    69   val tac_i: (Proof.context -> Proof.method) -> ProofHistory.T -> ProofHistory.T
    46   val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
    70   val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
       
    71   val then_tac_i: (Proof.context -> Proof.method) -> ProofHistory.T -> ProofHistory.T
    47   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
    72   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
       
    73   val proof_i: (Proof.context -> Proof.method) option -> ProofHistory.T -> ProofHistory.T
    48   val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
    74   val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
       
    75   val terminal_proof_i: (Proof.context -> Proof.method) -> ProofHistory.T -> ProofHistory.T
    49   val immediate_proof: ProofHistory.T -> ProofHistory.T
    76   val immediate_proof: ProofHistory.T -> ProofHistory.T
    50   val default_proof: ProofHistory.T -> ProofHistory.T
    77   val default_proof: ProofHistory.T -> ProofHistory.T
    51   val use_mltext: string -> theory option -> theory option
    78   val use_mltext: string -> theory option -> theory option
    52   val use_mltext_theory: string -> theory -> theory
    79   val use_mltext_theory: string -> theory -> theory
    53   val use_setup: string -> theory -> theory
    80   val use_setup: string -> theory -> theory
    70 struct
    97 struct
    71 
    98 
    72 
    99 
    73 (** derived theory and proof operations **)
   100 (** derived theory and proof operations **)
    74 
   101 
    75 (* formal comments *)	(* FIXME dummy *)
   102 (* formal comments *)   (* FIXME dummy *)
    76 
   103 
    77 fun add_text (txt:string) (thy:theory) = thy;
   104 fun add_text (txt:string) (thy:theory) = thy;
    78 fun add_title title author date thy = thy;
   105 fun add_title title author date thy = thy;
    79 val add_chapter = add_text;
   106 val add_chapter = add_text;
    80 val add_section = add_text;
   107 val add_section = add_text;
    86 
   113 
    87 fun add_axms f args thy =
   114 fun add_axms f args thy =
    88   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
   115   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
    89 
   116 
    90 val add_axioms = add_axms PureThy.add_axioms;
   117 val add_axioms = add_axms PureThy.add_axioms;
       
   118 val add_axioms_i = PureThy.add_axioms_i;
    91 val add_defs = add_axms PureThy.add_defs;
   119 val add_defs = add_axms PureThy.add_defs;
       
   120 val add_defs_i = PureThy.add_defs_i;
       
   121 
       
   122 
       
   123 (* constdefs *)
       
   124 
       
   125 fun gen_add_constdefs consts defs args thy =
       
   126   thy
       
   127   |> consts (map fst args)
       
   128   |> defs (map (fn ((c, _, mx), s) => ((Thm.def_name (Syntax.const_name c mx), s), [])) args);
       
   129 
       
   130 val add_constdefs = gen_add_constdefs Theory.add_consts add_defs;
       
   131 val add_constdefs_i = gen_add_constdefs Theory.add_consts_i add_defs_i;
    92 
   132 
    93 
   133 
    94 (* theorems *)
   134 (* theorems *)
    95 
   135 
    96 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
   136 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
    97   f name (map (attrib x) more_srcs)
   137   f name (map (attrib x) more_srcs)
    98     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
   138     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
    99 
   139 
   100 
   140 fun global_have_thmss x = gen_have_thmss PureThy.get_thms Attrib.global_attribute x;
   101 val have_theorems =
   141 
   102   #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute PureThy.have_thmss;
   142 fun local_have_thmss x =
   103 
       
   104 val have_lemmas =
       
   105   #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute
       
   106     (fn name => fn more_atts => PureThy.have_thmss name (more_atts @ [Drule.tag_lemma]));
       
   107 
       
   108 
       
   109 val have_thmss =
       
   110   gen_have_thmss (ProofContext.get_thms o Proof.context_of)
   143   gen_have_thmss (ProofContext.get_thms o Proof.context_of)
   111     (Attrib.local_attribute o Proof.theory_of) Proof.have_thmss;
   144     (Attrib.local_attribute o Proof.theory_of) x;
   112 
   145 
   113 val have_facts = ProofHistory.apply o have_thmss;
   146 fun have_thmss_i f ((name, more_atts), th_atts) =
   114 val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
   147   f name more_atts (map (apfst single) th_atts);
       
   148 
       
   149 fun have_lemss name atts = PureThy.have_thmss name (atts @ [Drule.tag_lemma]);
       
   150 
       
   151 
       
   152 fun apply_theorems th_srcs = global_have_thmss PureThy.have_thmss ((None, []), th_srcs);
       
   153 fun apply_theorems_i th_srcs = have_thmss_i PureThy.have_thmss ((None, []), th_srcs);
       
   154 val have_theorems = #1 oo global_have_thmss (PureThy.have_thmss o Some);
       
   155 val have_theorems_i = #1 oo have_thmss_i (PureThy.have_thmss o Some);
       
   156 val have_lemmas = #1 oo global_have_thmss (have_lemss o Some);
       
   157 val have_lemmas_i = #1 oo have_thmss_i (have_lemss o Some);
       
   158 val have_facts = ProofHistory.apply o local_have_thmss Proof.have_thmss;
       
   159 val have_facts_i = ProofHistory.apply o have_thmss_i Proof.have_thmss;
       
   160 
       
   161 
       
   162 (* forward chaining *)
       
   163 
       
   164 val from_facts =
       
   165   ProofHistory.apply o (Proof.chain oo curry (local_have_thmss Proof.have_thmss) ("", []));
       
   166 
       
   167 val from_facts_i =
       
   168   ProofHistory.apply o (Proof.chain oo curry (have_thmss_i Proof.have_thmss) ("", []));
       
   169 
       
   170 val chain = ProofHistory.apply Proof.chain;
   115 
   171 
   116 
   172 
   117 (* context *)
   173 (* context *)
   118 
   174 
   119 val fix = ProofHistory.apply o Proof.fix;
   175 val fix = ProofHistory.apply o Proof.fix;
   120 
   176 val fix_i = ProofHistory.apply o Proof.fix_i;
   121 val match_bind = ProofHistory.apply o Proof.match_bind;
   177 val match_bind = ProofHistory.apply o Proof.match_bind;
       
   178 val match_bind_i = ProofHistory.apply o Proof.match_bind_i;
   122 
   179 
   123 
   180 
   124 (* statements *)
   181 (* statements *)
   125 
   182 
   126 fun global_statement f name tags s thy =
   183 fun global_statement f name src s thy =
   127   ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
   184   ProofHistory.init (f name (map (Attrib.global_attribute thy) src) s thy);
   128 
   185 
   129 fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
   186 fun local_statement do_open f name src s = ProofHistory.apply_cond_open do_open (fn state =>
   130   f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
   187   f name (map (Attrib.local_attribute (Proof.theory_of state)) src) s state);
       
   188 
       
   189 fun global_statement_i f name atts t thy = ProofHistory.init (f name atts t thy);
       
   190 fun local_statement_i do_open f name atts t = ProofHistory.apply_cond_open do_open (f name atts t);
   131 
   191 
   132 val theorem = global_statement Proof.theorem;
   192 val theorem = global_statement Proof.theorem;
       
   193 val theorem_i = global_statement_i Proof.theorem_i;
   133 val lemma = global_statement Proof.lemma;
   194 val lemma = global_statement Proof.lemma;
   134 val assume = local_statement Proof.assume;
   195 val lemma_i = global_statement_i Proof.lemma_i;
   135 val show = local_statement Proof.show;
   196 val assume = local_statement false Proof.assume;
   136 val have = local_statement Proof.have;
   197 val assume_i = local_statement_i false Proof.assume_i;
   137 
   198 val show = local_statement true Proof.show;
   138 
   199 val show_i = local_statement_i true Proof.show_i;
   139 (* forward chaining *)
   200 val have = local_statement true Proof.have;
   140 
   201 val have_i = local_statement_i true Proof.have_i;
   141 val chain = ProofHistory.apply Proof.chain;
       
   142 
   202 
   143 
   203 
   144 (* end proof *)
   204 (* end proof *)
   145 
   205 
   146 fun qed_with (alt_name, alt_tags) prf =
   206 fun gen_qed_with prep_att (alt_name, raw_atts) prf =
   147   let
   207   let
   148     val state = ProofHistory.current prf;
   208     val state = ProofHistory.current prf;
   149     val thy = Proof.theory_of state;
   209     val thy = Proof.theory_of state;
   150     val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
   210     val alt_atts = apsome (map (prep_att thy)) raw_atts;
   151     val (thy', (kind, name, thm)) = Method.qed alt_name alt_atts state;
   211     val (thy', (kind, name, thm)) = Method.qed alt_name alt_atts state;
   152 
   212 
   153     val prt_result = Pretty.block
   213     val prt_result = Pretty.block
   154       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
   214       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
   155   in Pretty.writeln prt_result; thy end;
   215   in Pretty.writeln prt_result; thy end;
   156 
   216 
       
   217 val qed_with = gen_qed_with Attrib.global_attribute;
       
   218 val qed_with_i = gen_qed_with (K I);
       
   219 
   157 val qed = qed_with (None, None);
   220 val qed = qed_with (None, None);
   158 
   221 
   159 val kill_proof = Proof.theory_of o ProofHistory.current;
   222 val kill_proof = Proof.theory_of o ProofHistory.current;
   160 
   223 
   161 
   224 
   166 
   229 
   167 
   230 
   168 (* backward steps *)
   231 (* backward steps *)
   169 
   232 
   170 val tac = ProofHistory.applys o Method.tac;
   233 val tac = ProofHistory.applys o Method.tac;
       
   234 val tac_i = tac o Method.Basic;
   171 val then_tac = ProofHistory.applys o Method.then_tac;
   235 val then_tac = ProofHistory.applys o Method.then_tac;
       
   236 val then_tac_i = then_tac o Method.Basic;
   172 val proof = ProofHistory.applys o Method.proof;
   237 val proof = ProofHistory.applys o Method.proof;
       
   238 val proof_i = proof o apsome Method.Basic;
   173 val end_block = ProofHistory.applys_close Method.end_block;
   239 val end_block = ProofHistory.applys_close Method.end_block;
   174 val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
   240 val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
       
   241 val terminal_proof_i = terminal_proof o Method.Basic;
   175 val immediate_proof = ProofHistory.applys_close Method.immediate_proof;
   242 val immediate_proof = ProofHistory.applys_close Method.immediate_proof;
   176 val default_proof = ProofHistory.applys_close Method.default_proof;
   243 val default_proof = ProofHistory.applys_close Method.default_proof;
   177 
   244 
   178 
   245 
   179 (* use ML text *)
   246 (* use ML text *)
   224     "oracle: bstring * (Sign.sg * Object.T -> term)"
   291     "oracle: bstring * (Sign.sg * Object.T -> term)"
   225     "Theory.add_oracle oracle"
   292     "Theory.add_oracle oracle"
   226     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   293     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   227 
   294 
   228 
   295 
   229 (* theory init and exit *)	(* FIXME move? rearrange? *)
   296 (* theory init and exit *)      (* FIXME move? rearrange? *)
   230 
   297 
   231 fun begin_theory name parents files =
   298 fun begin_theory name parents files =
   232   let
   299   let
   233     val paths = map (apfst Path.unpack) files;
   300     val paths = map (apfst Path.unpack) files;
   234     val thy = ThyInfo.begin_theory name parents paths;
   301     val thy = ThyInfo.begin_theory name parents paths;