author  wenzelm 
Mon, 17 Aug 1998 20:32:24 +0200  
changeset 5328  ac539483ad09 
parent 5280  6055775a151b 
child 5686  1f053d05f571 
permissions  rwrr 
3987  1 
(* Title: Pure/pure_thy.ML 
2 
ID: $Id$ 

3 
Author: Markus Wenzel, TU Muenchen 

4 

5091  5 
Theorem database, derived theory operations, and the ProtoPure theory. 
3987  6 
*) 
7 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

8 
signature BASIC_PURE_THY = 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

9 
sig 
5000  10 
val print_theorems: theory > unit 
11 
val print_theory: theory > unit 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

12 
val get_thm: theory > xstring > thm 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

13 
val get_thms: theory > xstring > thm list 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

14 
val thms_of: theory > (string * thm) list 
5091  15 
structure ProtoPure: 
16 
sig 

17 
val thy: theory 

18 
val flexpair_def: thm 

19 
val Goal_def: thm 

20 
end 

4853  21 
end; 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

22 

3987  23 
signature PURE_THY = 
24 
sig 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

25 
include BASIC_PURE_THY 
4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

26 
val thms_closure: theory > xstring > tthm list option 
4783  27 
val get_tthm: theory > xstring > tthm 
28 
val get_tthms: theory > xstring > tthm list 

5328  29 
val get_tthmss: theory > xstring list > tthm list 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

30 
val thms_containing: theory > string list > (string * thm) list 
5280  31 
val store_tthm: (bstring * tthm) * theory attribute list > theory > theory * tthm 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

32 
val smart_store_thm: (bstring * thm) > thm 
4853  33 
val add_tthms: ((bstring * tthm) * theory attribute list) list > theory > theory 
34 
val add_tthmss: ((bstring * tthm list) * theory attribute list) list > theory > theory 

35 
val add_axioms: ((bstring * string) * theory attribute list) list > theory > theory 

36 
val add_axioms_i: ((bstring * term) * theory attribute list) list > theory > theory 

37 
val add_axiomss: ((bstring * string list) * theory attribute list) list > theory > theory 

38 
val add_axiomss_i: ((bstring * term list) * theory attribute list) list > theory > theory 

39 
val add_defs: ((bstring * string) * theory attribute list) list > theory > theory 

40 
val add_defs_i: ((bstring * term) * theory attribute list) list > theory > theory 

41 
val add_defss: ((bstring * string list) * theory attribute list) list > theory > theory 

42 
val add_defss_i: ((bstring * term list) * theory attribute list) list > theory > theory 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

43 
val get_name: theory > string 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

44 
val put_name: string > theory > theory 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

45 
val global_path: theory > theory 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

46 
val local_path: theory > theory 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

47 
val begin_theory: string > theory list > theory 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

48 
val end_theory: theory > theory 
5091  49 
exception ROLLBACK of theory * exn option 
50 
val transaction: (theory > theory) > theory > theory 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

51 
val add_typedecls: (bstring * string list * mixfix) list > theory > theory 
3987  52 
end; 
53 

54 
structure PureThy: PURE_THY = 

55 
struct 

56 

57 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

58 
(*** theorem database ***) 
3987  59 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

60 
(** data kind 'Pure/theorems' **) 
3987  61 

5005  62 
structure TheoremsDataArgs = 
63 
struct 

64 
val name = "Pure/theorems"; 

3987  65 

5005  66 
type T = 
67 
{space: NameSpace.T, 

68 
thms_tab: tthm list Symtab.table, 

69 
const_idx: int * (int * tthm) list Symtab.table} ref; 

3987  70 

4853  71 
fun mk_empty _ = 
5005  72 
ref {space = NameSpace.empty, thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)} : T; 
3987  73 

5005  74 
val empty = mk_empty (); 
75 
val prep_ext = mk_empty; 

76 
val merge = mk_empty; 

77 

78 
fun print sg (ref {space, thms_tab, const_idx = _}) = 

4853  79 
let 
80 
val prt_thm = Attribute.pretty_tthm o apfst (Thm.transfer_sg sg); 

81 
fun prt_thms (name, [th]) = 

82 
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th] 

83 
 prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths); 

3987  84 

5175  85 
val thmss = sort_wrt fst (map (apfst (NameSpace.cond_extern space)) (Symtab.dest thms_tab)); 
4853  86 
in 
87 
Pretty.writeln (Display.pretty_name_space ("theorem name space", space)); 

88 
Pretty.writeln (Pretty.big_list "theorems:" (map prt_thms thmss)) 

89 
end; 

3987  90 
end; 
91 

5005  92 
structure TheoremsData = TheoryDataFun(TheoremsDataArgs); 
93 
val get_theorems_sg = TheoremsData.get_sg; 

94 
val get_theorems = TheoremsData.get; 

95 

3987  96 

5000  97 
(* print theory *) 
3987  98 

5005  99 
val print_theorems = TheoremsData.print; 
5000  100 
fun print_theory thy = 
101 
(Display.print_theory thy; print_theorems thy); 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

102 

3987  103 

104 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

105 
(** retrieve theorems **) 
3987  106 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

107 
(* thms_closure *) 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

108 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

109 
(*note: we avoid life references to the theory, so users may safely 
5000  110 
keep thms_closure with moderate space consumption*) 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

111 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

112 
fun thms_closure_aux thy = 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

113 
let val ref {space, thms_tab, ...} = get_theorems thy 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

114 
in fn name => Symtab.lookup (thms_tab, NameSpace.intern space name) end; 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

115 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

116 
fun thms_closure thy = 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

117 
let val closures = map thms_closure_aux (thy :: Theory.ancestors_of thy) 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

118 
in fn name => get_first (fn f => f name) closures end; 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

119 

3987  120 

4783  121 
(* get_thms etc. *) 
4037  122 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

123 
fun lookup_thms name thy = thms_closure_aux thy name; 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

124 

4783  125 
fun get_tthms thy name = 
4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

126 
(case get_first (lookup_thms name) (thy :: Theory.ancestors_of thy) of 
4590  127 
None => raise THEORY ("Unknown theorem(s) " ^ quote name, [thy]) 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

128 
 Some thms => thms); 
3987  129 

4783  130 
fun get_tthm thy name = 
131 
(case get_tthms thy name of 

3987  132 
[thm] => thm 
4590  133 
 _ => raise THEORY ("Single theorem expected " ^ quote name, [thy])); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

134 

5328  135 
fun get_tthmss thy names = flat (map (get_tthms thy) names); 
136 

4783  137 
fun get_thms thy = map Attribute.thm_of o get_tthms thy; 
138 
fun get_thm thy = Attribute.thm_of o get_tthm thy; 

139 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

140 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

141 
(* thms_of *) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

142 

4783  143 
fun attach_name (thm, _) = (Thm.name_of_thm thm, thm); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

144 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

145 
fun thms_of thy = 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

146 
let val ref {thms_tab, ...} = get_theorems thy in 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

147 
map attach_name (flat (map snd (Symtab.dest thms_tab))) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

148 
end; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

149 

3987  150 

151 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

152 
(** theorems indexed by constants **) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

153 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

154 
(* make index *) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

155 

4037  156 
val ignore = ["Trueprop", "all", "==>", "=="]; 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

157 

4783  158 
fun add_const_idx ((next, table), tthm as (thm, _)) = 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

159 
let 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

160 
val {hyps, prop, ...} = Thm.rep_thm thm; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

161 
val consts = 
4037  162 
foldr add_term_consts (hyps, add_term_consts (prop, [])) \\ ignore; 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

163 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

164 
fun add (tab, c) = 
4783  165 
Symtab.update ((c, (next, tthm) :: Symtab.lookup_multi (tab, c)), tab); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

166 
in (next + 1, foldl add (table, consts)) end; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

167 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

168 
fun make_const_idx thm_tab = 
4487  169 
foldl (foldl add_const_idx) ((0, Symtab.empty), map snd (Symtab.dest thm_tab)); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

170 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

171 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

172 
(* lookup index *) 
3987  173 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

174 
(*search locally*) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

175 
fun containing [] thy = thms_of thy 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

176 
 containing consts thy = 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

177 
let 
4037  178 
fun int ([], _) = [] 
179 
 int (_, []) = [] 

180 
 int (xxs as ((x as (i:int, _)) :: xs), yys as ((y as (j, _)) :: ys)) = 

181 
if i = j then x :: int (xs, ys) 

182 
else if i > j then int (xs, yys) 

183 
else int (xxs, ys); 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

184 

4037  185 
fun ints [xs] = xs 
186 
 ints xss = if exists null xss then [] else foldl int (hd xss, tl xss); 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

187 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

188 
val ref {const_idx = (_, ctab), ...} = get_theorems thy; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

189 
val ithmss = map (fn c => Symtab.lookup_multi (ctab, c)) consts; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

190 
in 
4037  191 
map (attach_name o snd) (ints ithmss) 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

192 
end; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

193 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

194 
(*search globally*) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

195 
fun thms_containing thy consts = 
4037  196 
flat (map (containing (consts \\ ignore)) (thy :: Theory.ancestors_of thy)); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

197 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

198 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

199 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

200 
(** store theorems **) (*DESTRUCTIVE*) 
3987  201 

4853  202 
(* naming *) 
203 

204 
fun gen_names len name = 

205 
map (fn i => name ^ "_" ^ string_of_int i) (1 upto len); 

206 

207 
fun name_single name x = [(name, x)]; 

208 
fun name_multi name xs = gen_names (length xs) name ~~ xs; 

209 

210 

211 
(* enter_tthmx *) 

212 

3987  213 
fun warn_overwrite name = 
214 
warning ("Replaced old copy of theorems " ^ quote name); 

215 

216 
fun warn_same name = 

5005  217 
warning ("Theorem database already contains a copy of " ^ quote name); 
3987  218 

4853  219 
fun enter_tthmx sg app_name (bname, tthmx) = 
3987  220 
let 
4853  221 
val name = Sign.full_name sg bname; 
222 
fun name_tthm (nm, (thm, tgs)) = (Thm.name_thm (nm, thm), tgs); 

223 
val named_tthms = map name_tthm (app_name name tthmx); 

3987  224 

4783  225 
fun eq_tthm ((th1, _), (th2, _)) = Thm.eq_thm (th1, th2); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

226 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

227 
val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

228 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

229 
val overwrite = 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

230 
(case Symtab.lookup (thms_tab, name) of 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

231 
None => false 
4783  232 
 Some tthms' => 
4853  233 
if length tthms' = length named_tthms andalso forall2 eq_tthm (tthms', named_tthms) then 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

234 
(warn_same name; false) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

235 
else (warn_overwrite name; true)); 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

236 

4487  237 
val space' = NameSpace.extend (space, [name]); 
4783  238 
val thms_tab' = Symtab.update ((name, named_tthms), thms_tab); 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

239 
val const_idx' = 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

240 
if overwrite then make_const_idx thms_tab' 
4783  241 
else foldl add_const_idx (const_idx, named_tthms); 
3987  242 
in 
4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

243 
r := {space = space', thms_tab = thms_tab', const_idx = const_idx'}; 
4783  244 
named_tthms 
3987  245 
end; 
246 

4853  247 

248 
(* add_tthms(s) *) 

249 

250 
fun add_tthmx app_name app_att ((bname, tthmx), atts) thy = 

5280  251 
let 
252 
val (thy', tthmx') = app_att ((thy, tthmx), atts); 

253 
val tthms'' = enter_tthmx (Theory.sign_of thy') app_name (bname, tthmx'); 

254 
in (thy', tthms'') end; 

4853  255 

5280  256 
val add_tthms = 
257 
Theory.apply o map (fn th_atts => fst o add_tthmx name_single Attribute.apply th_atts); 

258 
val add_tthmss = 

259 
Theory.apply o map (fn th_atts => fst o add_tthmx name_multi Attribute.applys th_atts); 

260 

261 

262 
(* store_tthm *) 

263 

264 
fun store_tthm th_atts thy = 

265 
let val (thy', [th']) = add_tthmx name_single Attribute.apply th_atts thy 

266 
in (thy', th') end; 

3987  267 

268 

4853  269 
(* smart_store_thm *) 
3987  270 

4012  271 
fun smart_store_thm (name, thm) = 
4853  272 
let val [(thm', _)] = enter_tthmx (Thm.sign_of_thm thm) name_single (name, Attribute.tthm_of thm) 
4783  273 
in thm' end; 
3987  274 

275 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

276 
(* store axioms as theorems *) 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

277 

4853  278 
local 
279 
fun add_ax app_name add ((name, axs), atts) thy = 

280 
let 

281 
val named_axs = app_name name axs; 

282 
val thy' = add named_axs thy; 

283 
val tthms = map (Attribute.tthm_of o Thm.get_axiom thy' o fst) named_axs; 

284 
in add_tthmss [((name, tthms), atts)] thy' end; 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

285 

4853  286 
fun add_axs app_name add = Theory.apply o map (add_ax app_name add); 
287 
in 

288 
val add_axioms = add_axs name_single Theory.add_axioms; 

289 
val add_axioms_i = add_axs name_single Theory.add_axioms_i; 

290 
val add_axiomss = add_axs name_multi Theory.add_axioms; 

291 
val add_axiomss_i = add_axs name_multi Theory.add_axioms_i; 

292 
val add_defs = add_axs name_single Theory.add_defs; 

293 
val add_defs_i = add_axs name_single Theory.add_defs_i; 

294 
val add_defss = add_axs name_multi Theory.add_defs; 

295 
val add_defss_i = add_axs name_multi Theory.add_defs_i; 

296 
end; 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

297 

0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

298 

3987  299 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

300 
(*** derived theory operations ***) 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

301 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

302 
(** theory management **) 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

303 

5005  304 
(* data kind 'Pure/theory_management' *) 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

305 

5005  306 
structure TheoryManagementDataArgs = 
307 
struct 

308 
val name = "Pure/theory_management"; 

309 
type T = {name: string, generation: int}; 

5000  310 

5005  311 
val empty = {name = "", generation = 0}; 
312 
val prep_ext = I; 

5000  313 
fun merge _ = empty; 
5005  314 
fun print _ _ = (); 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

315 
end; 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

316 

5005  317 
structure TheoryManagementData = TheoryDataFun(TheoryManagementDataArgs); 
318 
val get_info = TheoryManagementData.get; 

319 
val put_info = TheoryManagementData.put; 

320 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

321 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

322 
(* get / put name *) 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

323 

5000  324 
val get_name = #name o get_info; 
325 
fun put_name name = put_info {name = name, generation = 0}; 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

326 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

327 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

328 
(* control prefixing of theory name *) 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

329 

5210  330 
val global_path = Theory.root_path; 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

331 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

332 
fun local_path thy = 
5210  333 
thy > Theory.root_path > Theory.add_path (get_name thy); 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

334 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

335 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

336 
(* begin / end theory *) 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

337 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

338 
fun begin_theory name thys = 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

339 
Theory.prep_ext_merge thys 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

340 
> put_name name 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

341 
> local_path; 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

342 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

343 
fun end_theory thy = Theory.add_name (get_name thy) thy; 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

344 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

345 

5000  346 
(* atomic transactions *) 
347 

5091  348 
exception ROLLBACK of theory * exn option; 
349 

5000  350 
fun transaction f thy = 
351 
let 

352 
val {name, generation} = get_info thy; 

353 
val copy_thy = 

354 
thy 

355 
> Theory.prep_ext 

5005  356 
> Theory.add_name ("#" ^ name ^ ":" ^ string_of_int generation) (* FIXME !!?? *) 
5000  357 
> put_info {name = name, generation = generation + 1}; 
5026  358 
val (thy', opt_exn) = (transform_error f thy, None) handle exn => (thy, Some exn); 
5000  359 
in 
5091  360 
if Sign.is_stale (Theory.sign_of thy') then raise ROLLBACK (copy_thy, opt_exn) 
361 
else (case opt_exn of Some exn => raise exn  _ => thy') 

5000  362 
end; 
363 

364 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

365 

38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

366 
(** add logical types **) 
4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

367 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

368 
fun add_typedecls decls thy = 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

369 
let 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

370 
val full = Sign.full_name (Theory.sign_of thy); 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

371 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

372 
fun type_of (raw_name, vs, mx) = 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

373 
if null (duplicates vs) then (raw_name, length vs, mx) 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

374 
else error ("Duplicate parameters in type declaration: " ^ quote raw_name); 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

375 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

376 
fun arity_of (raw_name, len, mx) = 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

377 
(full (Syntax.type_name raw_name mx), replicate len logicS, logicS); 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

378 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

379 
val types = map type_of decls; 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

380 
val arities = map arity_of types; 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

381 
in 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

382 
thy 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

383 
> Theory.add_types types 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

384 
> Theory.add_arities_i arities 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

385 
end; 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

386 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

387 

03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

388 

5091  389 
(*** the ProtoPure theory ***) 
3987  390 

391 
val proto_pure = 

392 
Theory.pre_pure 

5022
b4bd7e6402fe
moved add_axioms_x, add_defs_x to Isar/isar_thy.ML;
wenzelm
parents:
5005
diff
changeset

393 
> Theory.apply [TheoremsData.init, TheoryManagementData.init] 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

394 
> put_name "ProtoPure" 
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

395 
> global_path 
3987  396 
> Theory.add_types 
4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

397 
[("fun", 2, NoSyn), 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

398 
("prop", 0, NoSyn), 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

399 
("itself", 1, NoSyn), 
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

400 
("dummy", 0, NoSyn)] 
3987  401 
> Theory.add_classes_i [(logicC, [])] 
402 
> Theory.add_defsort_i logicS 

403 
> Theory.add_arities_i 

404 
[("fun", [logicS, logicS], logicS), 

405 
("prop", [], logicS), 

406 
("itself", [logicS], logicS)] 

4922
03b81b6e1baa
added thms_closure: theory > xstring > tthm list option;
wenzelm
parents:
4853
diff
changeset

407 
> Theory.add_nonterminals Syntax.pure_nonterms 
3987  408 
> Theory.add_syntax Syntax.pure_syntax 
409 
> Theory.add_modesyntax ("symbols", true) Syntax.pure_sym_syntax 

410 
> Theory.add_trfuns Syntax.pure_trfuns 

411 
> Theory.add_trfunsT Syntax.pure_trfunsT 

412 
> Theory.add_syntax 

413 
[("==>", "[prop, prop] => prop", Delimfix "op ==>")] 

414 
> Theory.add_consts 

415 
[("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)), 

416 
("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)), 

417 
("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)), 

418 
("all", "('a => prop) => prop", Binder ("!!", 0, 0)), 

4788  419 
("Goal", "prop => prop", Mixfix ("GOAL _", [999], 1000)), 
3987  420 
("TYPE", "'a itself", NoSyn)] 
5041
a1d0a6d555cd
Goals may now contain assumptions, which are not returned.
nipkow
parents:
5026
diff
changeset

421 
> Theory.add_modesyntax ("", false) 
a1d0a6d555cd
Goals may now contain assumptions, which are not returned.
nipkow
parents:
5026
diff
changeset

422 
[("Goal", "prop => prop", Mixfix ("_", [0], 0))] 
4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

423 
> local_path 
4853  424 
> (add_defs o map Attribute.none) 
4788  425 
[("flexpair_def", "(t =?= u) == (t == u::'a::{})"), 
426 
("Goal_def", "GOAL (PROP A) == PROP A")] 

4963
38aa2d56e28c
added get_name, put_name, global_path, local_path, begin_theory,
wenzelm
parents:
4933
diff
changeset

427 
> end_theory; 
3987  428 

5091  429 
structure ProtoPure = 
430 
struct 

431 
val thy = proto_pure; 

432 
val flexpair_def = get_axiom thy "flexpair_def"; 

433 
val Goal_def = get_axiom thy "Goal_def"; 

434 
end; 

3987  435 

436 

437 
end; 

438 

439 

4022
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

440 
structure BasicPureThy: BASIC_PURE_THY = PureThy; 
0770a19c48d3
added ignored_consts, thms_containing, add_store_axioms(_i),
wenzelm
parents:
4013
diff
changeset

441 
open BasicPureThy; 