author  clasohm 
Tue, 06 Sep 1994 14:44:10 +0200  
changeset 586  201e115d8031 
parent 570  6333c181a3f7 
child 587  3ba470399605 
permissions  rwrr 
389  1 
(* Title: Pure/Thy/thy_parse.ML 
2 
ID: $Id$ 

3 
Author: Markus Wenzel, TU Muenchen 

4 

5 
The parser for theory files. 

6 
*) 

7 

8 
infix 5  $$ $$ ^^; 

9 
infix 3 >>; 

10 
infix 0 ; 

11 

12 
signature THY_PARSE = 

13 
sig 

14 
type token 

15 
val !! : ('a > 'b * 'c) > 'a > 'b * 'c 

16 
val >> : ('a > 'b * 'c) * ('b > 'd) > 'a > 'd * 'c 

17 
val  : ('a > 'b) * ('a > 'b) > 'a > 'b 

18 
val  : ('a > 'b * 'c) * ('c > 'd * 'e) > 'a > ('b * 'd) * 'e 

19 
val ^^ : ('a > string * 'b) * ('b > string * 'c) > 'a > string * 'c 

20 
val $$ : string > token list > string * token list 

21 
val $$ : string * (token list > 'b * 'c) > token list > 'b * 'c 

22 
val $$ : ('a > 'b * token list) * string > 'a > 'b * token list 

23 
val ident: token list > string * token list 

24 
val long_ident: token list > string * token list 

25 
val long_id: token list > string * token list 

26 
val type_var: token list > string * token list 

27 
val nat: token list > string * token list 

28 
val string: token list > string * token list 

29 
val verbatim: token list > string * token list 

30 
val empty: 'a > 'b list * 'a 

31 
val optional: ('a > 'b * 'a) > 'b > 'a > 'b * 'a 

32 
val repeat: ('a > 'b * 'a) > 'a > 'b list * 'a 

33 
val repeat1: ('a > 'b * 'a) > 'a > 'b list * 'a 

34 
val enum: string > (token list > 'a * token list) 

35 
> token list > 'a list * token list 

36 
val enum1: string > (token list > 'a * token list) 

37 
> token list > 'a list * token list 

38 
val list: (token list > 'a * token list) 

39 
> token list > 'a list * token list 

40 
val list1: (token list > 'a * token list) 

41 
> token list > 'a list * token list 

42 
val name: token list > string * token list 

43 
val sort: token list > string * token list 

451  44 
val opt_infix: token list > string * token list 
45 
val opt_mixfix: token list > string * token list 

389  46 
type syntax 
47 
val make_syntax: string list > 

48 
(string * (token list > (string * string) * token list)) list > syntax 

476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

49 
val parse_thy: syntax > string > string > string 
389  50 
val section: string > string > (token list > string * token list) 
51 
> (string * (token list > (string * string) * token list)) 

52 
val axm_section: string > string 

53 
> (token list > (string * string list) * token list) 

54 
> (string * (token list > (string * string) * token list)) 

55 
val pure_keywords: string list 

56 
val pure_sections: 

57 
(string * (token list > (string * string) * token list)) list 

570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

58 
(*items for building strings*) 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

59 
val parens : string > string 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

60 
val brackets : string > string 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

61 
val mk_list : string list > string 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

62 
val mk_big_list : string list > string 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

63 
val mk_pair : string * string > string 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

64 
val mk_triple : string * string * string > string 
389  65 
end; 
66 

414  67 
functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN): THY_PARSE = 
389  68 
struct 
69 

70 
open ThyScan; 

71 

72 

73 
(** parser toolbox **) 

74 

75 
type token = token_kind * string * int; 

76 

77 

78 
(* errors *) 

79 

80 
exception SYNTAX_ERROR of string * string * int; 

81 

82 
fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n); 

83 

84 
fun eof_err () = error "Unexpected endoffile"; 

85 

570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

86 
(*Similar to Prolog's cut: reports any syntax error instead of backtracking 
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset

87 
through a superior  *) 
389  88 
fun !! parse toks = parse toks 
89 
handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^ 

90 
string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found"); 

91 

92 

93 
(* parser combinators *) 

94 

95 
fun (parse >> f) toks = apfst f (parse toks); 

96 

97 
fun (parse1  parse2) toks = 

98 
parse1 toks handle SYNTAX_ERROR _ => parse2 toks; 

99 

100 
fun (parse1  parse2) toks = 

101 
let 

102 
val (x, toks') = parse1 toks; 

103 
val (y, toks'') = parse2 toks'; 

104 
in 

105 
((x, y), toks'') 

106 
end; 

107 

108 
fun (parse1 ^^ parse2) = parse1  parse2 >> op ^; 

109 

110 

111 
(* generic parsers *) 

112 

113 
fun $$ a ((k, b, n) :: toks) = 

114 
if k = Keyword andalso a = b then (a, toks) 

115 
else syn_err (quote a) (quote b) n 

116 
 $$ _ [] = eof_err (); 

117 

118 
fun (a $$ parse) = $$ a  parse >> #2; 

119 

120 
fun (parse $$ a) = parse  $$ a >> #1; 

121 

122 

123 
fun kind k1 ((k2, s, n) :: toks) = 

124 
if k1 = k2 then (s, toks) 

125 
else syn_err (name_of_kind k1) (quote s) n 

126 
 kind _ [] = eof_err (); 

127 

128 
val ident = kind Ident; 

129 
val long_ident = kind LongIdent; 

130 
val long_id = ident  long_ident; 

131 
val type_var = kind TypeVar >> quote; 

132 
val nat = kind Nat; 

133 
val string = kind String; 

134 
val verbatim = kind Verbatim; 

135 
val eof = kind EOF; 

136 

137 
fun empty toks = ([], toks); 

138 

139 
fun optional parse def = parse  empty >> K def; 

140 

141 
fun repeat parse toks = (parse  repeat parse >> op ::  empty) toks; 

142 
fun repeat1 parse = parse  repeat parse >> op ::; 

143 

144 
fun enum1 sep parse = parse  repeat (sep $$ parse) >> op ::; 

145 
fun enum sep parse = enum1 sep parse  empty; 

146 

147 
val list = enum ","; 

148 
val list1 = enum1 ","; 

149 

150 

151 

152 
(** theory parsers **) 

153 

154 
(* misc utilities *) 

155 

156 
fun cat s1 s2 = s1 ^ " " ^ s2; 

157 

558  158 
val parens = enclose "(" ")"; 
159 
val brackets = enclose "[" "]"; 

389  160 

161 
val mk_list = brackets o commas; 

162 
val mk_big_list = brackets o space_implode ",\n "; 

163 

558  164 
fun mk_pair (x, y) = parens (commas [x, y]); 
165 
fun mk_triple (x, y, z) = parens (commas [x, y, z]); 

389  166 
fun mk_triple1 ((x, y), z) = mk_triple (x, y, z); 
167 
fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z); 

168 

169 
val split_decls = flat o map (fn (xs, y) => map (rpair y) xs); 

170 

171 
fun strip_quotes str = 

172 
implode (tl (take (size str  1, explode str))); 

173 

174 

175 
(* names *) 

176 

177 
val name = ident >> quote  string; 

178 
val names = list name; 

179 
val names1 = list1 name; 

180 
val name_list = names >> mk_list; 

181 
val name_list1 = names1 >> mk_list; 

182 

183 

184 
(* classes *) 

185 

186 
val subclass = name  optional ("<" $$ !! name_list1) "[]"; 

187 

558  188 
val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list; 
389  189 

190 

191 
(* arities *) 

192 

193 
val sort = 

194 
name >> brackets  

195 
"{" $$ name_list $$ "}"; 

196 

197 
val sort_list1 = list1 sort >> mk_list; 

198 

199 

200 
val arity = optional ("(" $$ !! (sort_list1 $$")")) "[]"  sort; 

201 

202 
val arity_decls = repeat1 (names1 $$ "::"  !! arity) 

203 
>> (mk_big_list o map mk_triple2 o split_decls); 

204 

205 

206 
(* mixfix annotations *) 

207 

208 
val infxl = "infixl" $$ !! nat >> cat "Infixl"; 

209 
val infxr = "infixr" $$ !! nat >> cat "Infixr"; 

210 

211 
val binder = "binder" $$ !! (string  nat) >> (cat "Binder" o mk_pair); 

212 

213 
val opt_pris = optional ("[" $$ !! (list nat $$ "]")) [] >> mk_list; 

214 

215 
val mixfix = string  !! (opt_pris  optional nat "max_pri") 

216 
>> (cat "Mixfix" o mk_triple2); 

217 

558  218 
fun opt_syn fx = optional ("(" $$ fx $$ ")") "NoSyn"; 
389  219 

220 
val opt_infix = opt_syn (infxl  infxr); 

221 
val opt_mixfix = opt_syn (mixfix  infxl  infxr  binder); 

222 

223 

224 
(* types *) 

225 

226 
fun mk_old_type_decl ((ts, n), syn) = 

227 
map (fn t => (mk_triple (t, n, syn), false)) ts; 

228 

229 
fun mk_type_decl (((xs, t), None), syn) = 

230 
[(mk_triple (t, string_of_int (length xs), syn), false)] 

231 
 mk_type_decl (((xs, t), Some rhs), syn) = 

558  232 
[(parens (commas [t, mk_list xs, rhs, syn]), true)]; 
389  233 

234 
fun mk_type_decls tys = 

414  235 
"> add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\ 
236 
\> add_tyabbrs\n" ^ mk_big_list (keyfilter tys true); 

389  237 

238 

425  239 
val old_type_decl = names1  nat  opt_infix >> mk_old_type_decl; 
389  240 

241 
val type_args = 

242 
type_var >> (fn x => [x])  

243 
"(" $$ !! (list1 type_var $$ ")")  

244 
empty >> K []; 

245 

246 
val type_decl = type_args  name  optional ("=" $$ !! string >> Some) None 

247 
 opt_infix >> mk_type_decl; 

248 

249 
val type_decls = repeat1 (old_type_decl  type_decl) 

250 
>> (rpair "" o mk_type_decls o flat); 

251 

252 

253 
(* consts *) 

254 

255 
val const_decls = repeat1 (names1 $$ "::"  !! (string  opt_mixfix)) 

256 
>> (mk_big_list o map mk_triple2 o split_decls); 

257 

258 

259 
(* translations *) 

260 

261 
val trans_pat = 

262 
optional ("(" $$ !! (name $$ ")")) "\"logic\""  string >> mk_pair; 

263 

264 
val trans_arrow = 

265 
$$ "=>" >> K " > "  

266 
$$ "<=" >> K " < "  

267 
$$ "==" >> K " <> "; 

268 

269 
val trans_decls = repeat1 (trans_pat ^^ !! (trans_arrow ^^ trans_pat)) 

270 
>> mk_big_list; 

271 

272 

273 
(* ML translations *) 

274 

275 
val trfun_defs = 

276 
" val parse_ast_translation = [];\n\ 

277 
\ val parse_translation = [];\n\ 

278 
\ val print_translation = [];\n\ 

279 
\ val print_ast_translation = [];"; 

280 

281 
val trfun_args = 

282 
"(parse_ast_translation, parse_translation, \ 

283 
\print_translation, print_ast_translation)"; 

284 

285 
fun mk_mltrans txt = 

286 
"let\n" 

287 
^ trfun_defs ^ "\n" 

288 
^ txt ^ "\n\ 

289 
\in\n\ 

290 
\ " ^ trfun_args ^ "\n\ 

291 
\end"; 

292 

293 
val mltrans = verbatim >> mk_mltrans; 

294 

295 

296 
(* axioms *) 

297 

298 
val mk_axms = mk_big_list o map (mk_pair o apfst quote); 

299 

300 
fun mk_axiom_decls axms = (mk_axms axms, map fst axms); 

301 

302 
val axiom_decls = repeat1 (ident  !! string) >> mk_axiom_decls; 

303 

304 

305 
(* axclass *) 

306 

307 
fun mk_axclass_decl ((c, cs), axms) = 

308 
(mk_pair (c, cs) ^ "\n" ^ mk_axms axms, 

309 
(strip_quotes c ^ "I") :: map fst axms); 

310 

311 
val axclass_decl = subclass  repeat (ident  !! string) >> mk_axclass_decl; 

312 

313 

451  314 
(* instance *) 
389  315 

425  316 
fun mk_witness (axths, opt_tac) = 
389  317 
mk_list (keyfilter axths false) ^ "\n" ^ 
318 
mk_list (keyfilter axths true) ^ "\n" ^ 

319 
opt_tac; 

320 

321 
val axm_or_thm = 

322 
string >> rpair false  

323 
long_id >> rpair true; 

324 

451  325 

425  326 
val opt_witness = 
389  327 
optional ("(" $$ list1 axm_or_thm $$ ")") []  
558  328 
optional (verbatim >> (parens o cat "Some" o parens)) "None" 
425  329 
>> mk_witness; 
330 

331 
val instance_decl = 

451  332 
(name $$ "<"  name >> (pair "> add_inst_subclass" o mk_pair)  
333 
name $$ "::"  arity >> (pair "> add_inst_arity" o mk_triple2)) 

334 
 opt_witness 

335 
>> (fn ((x, y), z) => (cat_lines [x, y, z], "")); 

389  336 

337 

338 

339 
(** theory syntax **) 

340 

341 
type syntax = 

342 
lexicon * (token list > (string * string) * token list) Symtab.table; 

343 

344 
fun make_syntax keywords sects = 

451  345 
(make_lexicon (map fst sects @ keywords), 
346 
Symtab.make sects handle Symtab.DUPS dups => 

347 
error ("Duplicate sections in theory file syntax: " ^ commas_quote dups)); 

389  348 

349 

350 
(* header *) 

351 

352 
fun mk_header (thy_name, bases) = 

586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

353 
(thy_name, "mk_base " ^ mk_list bases ^ " " ^ quote thy_name); 
389  354 

355 
val base = 

356 
ident >> (cat "Thy" o quote)  

357 
string >> cat "File"; 

358 

359 
val header = ident $$ "="  enum1 "+" base >> mk_header; 

360 

361 

362 
(* extension *) 

363 

364 
fun mk_extension (txts, mltxt) = 

365 
let 

366 
val cat_sects = space_implode "\n\n" o filter_out (equal ""); 

367 
val (extxts, postxts) = split_list txts; 

368 
in 

369 
(cat_sects extxts, cat_sects postxts, mltxt) 

370 
end; 

371 

372 
fun sect tab ((Keyword, s, n) :: toks) = 

373 
(case Symtab.lookup (tab, s) of 

374 
Some parse => !! parse toks 

375 
 None => syn_err "section" s n) 

376 
 sect _ ((_, s, n) :: _) = syn_err "section" s n 

377 
 sect _ [] = eof_err (); 

378 

379 
fun extension sectab = "+" $$ !! (repeat (sect sectab) $$ "end")  

380 
optional ("ML" $$ verbatim) "" >> mk_extension; 

381 

382 

383 
(* theory definition *) 

384 

558  385 
fun mk_structure tname ((thy_name, old_thys), opt_txts) = 
386 
if thy_name <> tname then 

387 
error ("Filename \"" ^ tname ^ ".thy\" and theory name " 

388 
^ quote thy_name ^ " are different") 

389 
else 

390 
(case opt_txts of 

391 
Some (extxt, postxt, mltxt) => 

586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

392 
"val base = " ^ old_thys ^ " true;\n\n\ 
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

393 
\structure " ^ thy_name ^ " =\n\ 
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

394 
\struct\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

395 
\\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

396 
\local\n" 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

397 
^ trfun_defs ^ "\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

398 
\in\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

399 
\\n" 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

400 
^ mltxt ^ "\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

401 
\\n\ 
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

402 
\val thy = base\n\n\ 
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

403 
\> add_trfuns\n" 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

404 
^ trfun_args ^ "\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

405 
\\n" 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

406 
^ extxt ^ "\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

407 
\\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

408 
\> add_thyname " ^ quote thy_name ^ ";\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

409 
\\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

410 
\\n" 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

411 
^ postxt ^ "\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

412 
\\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

413 
\end;\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

414 
\end;\n" 
558  415 
 None => 
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

416 
"val base = " ^ old_thys ^ " false;\n\n\ 
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

417 
\structure " ^ thy_name ^ " =\n\ 
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

418 
\struct\n\ 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

419 
\\n\ 
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset

420 
\val thy = base\n\ 
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

421 
\\n\ 
558  422 
\end;\n"); 
389  423 

476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

424 
fun theory_defn sectab tname = 
389  425 
header  optional (extension sectab >> Some) None  eof 
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

426 
>> (mk_structure tname o #1); 
389  427 

476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

428 
fun parse_thy (lex, sectab) tname str = 
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset

429 
#1 (!! (theory_defn sectab tname) (tokenize lex str)); 
389  430 

431 

432 
(* standard sections *) 

433 

434 
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";"; 

435 

436 
fun mk_axm_sect pretxt (txt, axs) = 

437 
(pretxt ^ "\n" ^ txt, cat_lines (map mk_val axs)); 

438 

439 
fun axm_section name pretxt parse = 

440 
(name, parse >> mk_axm_sect pretxt); 

441 

442 
fun section name pretxt parse = 

443 
axm_section name pretxt (parse >> rpair []); 

444 

445 

446 
val pure_keywords = 

451  447 
["end", "ML", "mixfix", "infixr", "infixl", "binder", "=", "+", ",", "<", 
448 
"{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="]; 

389  449 

450 
val pure_sections = 

414  451 
[section "classes" "> add_classes" class_decls, 
452 
section "default" "> add_defsort" sort, 

389  453 
("types", type_decls), 
414  454 
section "arities" "> add_arities" arity_decls, 
455 
section "consts" "> add_consts" const_decls, 

456 
section "syntax" "> add_syntax" const_decls, 

457 
section "translations" "> add_trrules" trans_decls, 

458 
section "MLtrans" "> add_trfuns" mltrans, 

389  459 
("MLtext", verbatim >> rpair ""), 
414  460 
axm_section "rules" "> add_axioms" axiom_decls, 
558  461 
axm_section "defs" "> add_defs" axiom_decls, 
414  462 
axm_section "axclass" "> add_axclass" axclass_decl, 
451  463 
("instance", instance_decl)]; 
389  464 

465 

466 
end; 

467 