author  paulson 
Tue, 22 Jul 1997 11:14:18 +0200  
changeset 3538  ed9de44032e0 
parent 2979  db6941221197 
child 3767  e2bb53d8dd26 
permissions  rwrr 
1526  1 
(* Title: Pure/theory.ML 
2 
ID: $Id$ 

3 
Author: Lawrence C Paulson and Markus Wenzel 

4 
Copyright 1996 University of Cambridge 

5 

6 
Theories 

7 
*) 

8 

9 
signature THEORY = 

10 
sig 

11 
type theory 

12 
exception THEORY of string * theory list 

13 
val rep_theory : theory > 

1539  14 
{sign: Sign.sg, oraopt: (Sign.sg * exn > term) option, 
15 
new_axioms: term Symtab.table, parents: theory list} 

1526  16 
val sign_of : theory > Sign.sg 
17 
val syn_of : theory > Syntax.syntax 

18 
val stamps_of_thy : theory > string ref list 

19 
val parents_of : theory > theory list 

20 
val subthy : theory * theory > bool 

21 
val eq_thy : theory * theory > bool 

22 
val proto_pure_thy : theory 

23 
val pure_thy : theory 

24 
val cpure_thy : theory 

25 
(*theory primitives*) 

1539  26 
val add_classes : (class * class list) list > theory > theory 
27 
val add_classrel : (class * class) list > theory > theory 

28 
val add_defsort : sort > theory > theory 

29 
val add_types : (string * int * mixfix) list > theory > theory 

30 
val add_tyabbrs : (string * string list * string * mixfix) list 

1526  31 
> theory > theory 
1539  32 
val add_tyabbrs_i : (string * string list * typ * mixfix) list 
1526  33 
> theory > theory 
1539  34 
val add_arities : (string * sort list * sort) list > theory > theory 
35 
val add_consts : (string * string * mixfix) list > theory > theory 

36 
val add_consts_i : (string * typ * mixfix) list > theory > theory 

37 
val add_syntax : (string * string * mixfix) list > theory > theory 

38 
val add_syntax_i : (string * typ * mixfix) list > theory > theory 

2359  39 
val add_modesyntax : string * bool > (string * string * mixfix) list > theory > theory 
40 
val add_modesyntax_i : string * bool > (string * typ * mixfix) list > theory > theory 

1539  41 
val add_trfuns : 
1526  42 
(string * (Syntax.ast list > Syntax.ast)) list * 
43 
(string * (term list > term)) list * 

44 
(string * (term list > term)) list * 

45 
(string * (Syntax.ast list > Syntax.ast)) list > theory > theory 

2385  46 
val add_trfunsT : 
47 
(string * (typ > term list > term)) list > theory > theory 

2693  48 
val add_tokentrfuns: 
49 
(string * string * (string > string * int)) list > theory > theory 

1526  50 
val add_trrules : (string * string)Syntax.trrule list > theory > theory 
1539  51 
val add_trrules_i : Syntax.ast Syntax.trrule list > theory > theory 
1526  52 
val cert_axm : Sign.sg > string * term > string * term 
53 
val read_axm : Sign.sg > string * string > string * term 

54 
val inferT_axm : Sign.sg > string * term > string * term 

1539  55 
val add_axioms : (string * string) list > theory > theory 
56 
val add_axioms_i : (string * term) list > theory > theory 

57 
val add_thyname : string > theory > theory 

58 

59 
val set_oracle : (Sign.sg * exn > term) > theory > theory 

1526  60 

61 
val merge_theories : theory * theory > theory 

62 
val merge_thy_list : bool > theory list > theory 

63 
end; 

64 

65 

66 
structure Theory : THEORY = 

67 
struct 

2206  68 

69 
(** datatype theory **) 

1526  70 

71 
datatype theory = 

72 
Theory of { 

73 
sign: Sign.sg, 

1539  74 
oraopt: (Sign.sg * exn > term) option, 
1526  75 
new_axioms: term Symtab.table, 
76 
parents: theory list}; 

77 

78 
fun rep_theory (Theory args) = args; 

79 

80 
(*errors involving theories*) 

81 
exception THEORY of string * theory list; 

82 

83 

84 
val sign_of = #sign o rep_theory; 

85 
val syn_of = #syn o Sign.rep_sg o sign_of; 

86 

87 
(*stamps associated with a theory*) 

88 
val stamps_of_thy = #stamps o Sign.rep_sg o sign_of; 

89 

90 
(*return the immediate ancestors*) 

91 
val parents_of = #parents o rep_theory; 

92 

93 

94 
(*compare theories*) 

95 
val subthy = Sign.subsig o pairself sign_of; 

96 
val eq_thy = Sign.eq_sg o pairself sign_of; 

97 

98 

99 
(* the Pure theories *) 

100 

101 
val proto_pure_thy = 

1539  102 
Theory {sign = Sign.proto_pure, oraopt = None, 
103 
new_axioms = Symtab.null, parents = []}; 

1526  104 

105 
val pure_thy = 

1539  106 
Theory {sign = Sign.pure, oraopt = None, 
107 
new_axioms = Symtab.null, parents = []}; 

1526  108 

109 
val cpure_thy = 

1539  110 
Theory {sign = Sign.cpure, oraopt = None, 
111 
new_axioms = Symtab.null, parents = []}; 

1526  112 

113 

114 

115 
(** extend theory **) 

116 

117 
fun err_dup_axms names = 

118 
error ("Duplicate axiom name(s) " ^ commas_quote names); 

119 

1539  120 
fun ext_thy (thy as Theory {sign, oraopt, new_axioms, parents}) 
121 
sign1 new_axms = 

1526  122 
let 
123 
val draft = Sign.is_draft sign; 

124 
val new_axioms1 = 

125 
Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms) 

126 
handle Symtab.DUPS names => err_dup_axms names; 

127 
val parents1 = if draft then parents else [thy]; 

128 
in 

1539  129 
Theory {sign = sign1, oraopt = oraopt, 
130 
new_axioms = new_axioms1, parents = parents1} 

1526  131 
end; 
132 

133 

134 
(* extend signature of a theory *) 

135 

136 
fun ext_sg extfun decls (thy as Theory {sign, ...}) = 

137 
ext_thy thy (extfun decls sign) []; 

138 

2206  139 
val add_classes = ext_sg Sign.add_classes; 
140 
val add_classrel = ext_sg Sign.add_classrel; 

141 
val add_defsort = ext_sg Sign.add_defsort; 

142 
val add_types = ext_sg Sign.add_types; 

143 
val add_tyabbrs = ext_sg Sign.add_tyabbrs; 

144 
val add_tyabbrs_i = ext_sg Sign.add_tyabbrs_i; 

145 
val add_arities = ext_sg Sign.add_arities; 

146 
val add_consts = ext_sg Sign.add_consts; 

147 
val add_consts_i = ext_sg Sign.add_consts_i; 

148 
val add_syntax = ext_sg Sign.add_syntax; 

149 
val add_syntax_i = ext_sg Sign.add_syntax_i; 

150 
val add_modesyntax = curry (ext_sg Sign.add_modesyntax); 

151 
val add_modesyntax_i = curry (ext_sg Sign.add_modesyntax_i); 

152 
val add_trfuns = ext_sg Sign.add_trfuns; 

2385  153 
val add_trfunsT = ext_sg Sign.add_trfunsT; 
2693  154 
val add_tokentrfuns = ext_sg Sign.add_tokentrfuns; 
2206  155 
val add_trrules = ext_sg Sign.add_trrules; 
156 
val add_trrules_i = ext_sg Sign.add_trrules_i; 

157 
val add_thyname = ext_sg Sign.add_name; 

1526  158 

159 

160 
(* prepare axioms *) 

161 

162 
fun err_in_axm name = 

163 
error ("The error(s) above occurred in axiom " ^ quote name); 

164 

165 
fun no_vars tm = 

166 
if null (term_vars tm) andalso null (term_tvars tm) then tm 

167 
else error "Illegal schematic variable(s) in term"; 

168 

169 
fun cert_axm sg (name, raw_tm) = 

170 
let 

171 
val (t, T, _) = Sign.certify_term sg raw_tm 

2979  172 
handle TYPE (msg, _, _) => error msg 
1526  173 
 TERM (msg, _) => error msg; 
174 
in 

175 
assert (T = propT) "Term not of type prop"; 

176 
(name, no_vars t) 

177 
end 

178 
handle ERROR => err_in_axm name; 

179 

180 
(*Some duplication of code with read_def_cterm*) 

181 
fun read_axm sg (name, str) = 

182 
let val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str; 

183 
val (_, t, _) = 

184 
Sign.infer_types sg (K None) (K None) [] true (ts,propT); 

1960
ae390b599213
Improved error handling: if there are syntax or typechecking
paulson
parents:
1539
diff
changeset

185 
in cert_axm sg (name,t) end 
ae390b599213
Improved error handling: if there are syntax or typechecking
paulson
parents:
1539
diff
changeset

186 
handle ERROR => err_in_axm name; 
1526  187 

188 
fun inferT_axm sg (name, pre_tm) = 

189 
let val t = #2(Sign.infer_types sg (K None) (K None) [] true 

190 
([pre_tm], propT)) 

191 
in (name, no_vars t) end 

192 
handle ERROR => err_in_axm name; 

193 

194 

195 
(* extend axioms of a theory *) 

196 

197 
fun ext_axms prep_axm axms (thy as Theory {sign, ...}) = 

198 
let 

199 
val sign1 = Sign.make_draft sign; 

200 
val axioms = map (apsnd (Term.compress_term o Logic.varify) o 

201 
prep_axm sign) 

202 
axms; 

203 
in 

204 
ext_thy thy sign1 axioms 

205 
end; 

206 

207 
val add_axioms = ext_axms read_axm; 

208 
val add_axioms_i = ext_axms cert_axm; 

209 

210 

1539  211 
(** Set oracle of theory **) 
212 

213 
fun set_oracle oracle 

214 
(thy as Theory {sign, oraopt = None, new_axioms, parents}) = 

215 
if Sign.is_draft sign then 

216 
Theory {sign = sign, 

217 
oraopt = Some oracle, 

218 
new_axioms = new_axioms, 

219 
parents = parents} 

220 
else raise THEORY ("Can only set oracle of a draft", [thy]) 

221 
 set_oracle _ thy = raise THEORY ("Oracle already set", [thy]); 

222 

1526  223 

224 
(** merge theories **) 

225 

226 
fun merge_thy_list mk_draft thys = 

227 
let 

228 
fun is_union thy = forall (fn t => subthy (t, thy)) thys; 

229 
val is_draft = Sign.is_draft o sign_of; 

230 

231 
fun add_sign (sg, Theory {sign, ...}) = 

232 
Sign.merge (sg, sign) handle TERM (msg, _) => error msg; 

233 
in 

1539  234 
case (find_first is_union thys, exists is_draft thys) of 
1526  235 
(Some thy, _) => thy 
236 
 (None, true) => raise THEORY ("Illegal merge of draft theories", thys) 

237 
 (None, false) => Theory { 

238 
sign = 

239 
(if mk_draft then Sign.make_draft else I) 

240 
(foldl add_sign (Sign.proto_pure, thys)), 

1539  241 
oraopt = None, 
1526  242 
new_axioms = Symtab.null, 
1539  243 
parents = thys} 
1526  244 
end; 
245 

246 
fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2]; 

247 

248 

249 
end; 

250 

251 
open Theory; 