author | Christian Urban <urbanc@in.tum.de> |
Wed, 28 Apr 2010 08:22:20 +0200 | |
changeset 1971 | 8daf6ff5e11a |
parent 1970 | 90758c187861 |
child 1981 | 9f9c4965b608 |
permissions | -rw-r--r-- |
1941 | 1 |
theory NewParser |
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
2 |
imports "../Nominal-General/Nominal2_Base" |
1941 | 3 |
"../Nominal-General/Nominal2_Eqvt" |
4 |
"../Nominal-General/Nominal2_Supp" |
|
1960
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
5 |
"Perm" "NewFv" |
1941 | 6 |
begin |
7 |
||
8 |
section{* Interface for nominal_datatype *} |
|
9 |
||
10 |
||
11 |
ML {* |
|
12 |
(* nominal datatype parser *) |
|
13 |
local |
|
14 |
structure P = OuterParse; |
|
15 |
structure S = Scan |
|
16 |
||
17 |
fun triple1 ((x, y), z) = (x, y, z) |
|
18 |
fun triple2 (x, (y, z)) = (x, y, z) |
|
19 |
fun tuple ((x, y, z), u) = (x, y, z, u) |
|
20 |
fun tswap (((x, y), z), u) = (x, y, u, z) |
|
21 |
in |
|
22 |
||
23 |
val _ = OuterKeyword.keyword "bind" |
|
24 |
val _ = OuterKeyword.keyword "bind_set" |
|
25 |
val _ = OuterKeyword.keyword "bind_res" |
|
26 |
||
1943 | 27 |
val anno_typ = S.option (P.name --| P.$$$ "::") -- P.typ |
1941 | 28 |
|
29 |
val bind_mode = P.$$$ "bind" || P.$$$ "bind_set" || P.$$$ "bind_res" |
|
30 |
||
31 |
val bind_clauses = |
|
32 |
P.enum "," (bind_mode -- S.repeat1 P.term -- (P.$$$ "in" |-- S.repeat1 P.name) >> triple1) |
|
33 |
||
34 |
val cnstr_parser = |
|
1943 | 35 |
P.binding -- S.repeat anno_typ -- bind_clauses -- P.opt_mixfix >> tswap |
1941 | 36 |
|
37 |
(* datatype parser *) |
|
38 |
val dt_parser = |
|
39 |
(P.type_args -- P.binding -- P.opt_mixfix >> triple1) -- |
|
1943 | 40 |
(P.$$$ "=" |-- P.enum1 "|" cnstr_parser) >> tuple |
1941 | 41 |
|
42 |
(* binding function parser *) |
|
43 |
val bnfun_parser = |
|
1944 | 44 |
S.optional (P.$$$ "binder" |-- P.fixes -- SpecParse.where_alt_specs) ([], []) |
1941 | 45 |
|
46 |
(* main parser *) |
|
47 |
val main_parser = |
|
1944 | 48 |
P.and_list1 dt_parser -- bnfun_parser >> triple2 |
1941 | 49 |
|
50 |
end |
|
51 |
*} |
|
52 |
||
53 |
ML {* |
|
54 |
fun get_cnstrs dts = |
|
55 |
map (fn (_, _, _, constrs) => constrs) dts |
|
56 |
||
57 |
fun get_typed_cnstrs dts = |
|
58 |
flat (map (fn (_, bn, _, constrs) => |
|
59 |
(map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts) |
|
60 |
||
61 |
fun get_cnstr_strs dts = |
|
62 |
map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts)) |
|
63 |
||
64 |
fun get_bn_fun_strs bn_funs = |
|
65 |
map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs |
|
66 |
*} |
|
67 |
||
68 |
ML {* |
|
69 |
fun add_primrec_wrapper funs eqs lthy = |
|
70 |
if null funs then (([], []), lthy) |
|
71 |
else |
|
72 |
let |
|
73 |
val eqs' = map (fn (_, eq) => (Attrib.empty_binding, eq)) eqs |
|
74 |
val funs' = map (fn (bn, ty, mx) => (bn, SOME ty, mx)) funs |
|
75 |
in |
|
76 |
Primrec.add_primrec funs' eqs' lthy |
|
77 |
end |
|
78 |
*} |
|
79 |
||
80 |
ML {* |
|
81 |
fun add_datatype_wrapper dt_names dts = |
|
82 |
let |
|
83 |
val conf = Datatype.default_config |
|
84 |
in |
|
85 |
Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts) |
|
86 |
end |
|
87 |
*} |
|
88 |
||
1944 | 89 |
|
90 |
text {* Infrastructure for adding "_raw" to types and terms *} |
|
91 |
||
1941 | 92 |
ML {* |
93 |
fun add_raw s = s ^ "_raw" |
|
94 |
fun add_raws ss = map add_raw ss |
|
95 |
fun raw_bind bn = Binding.suffix_name "_raw" bn |
|
96 |
||
97 |
fun replace_str ss s = |
|
98 |
case (AList.lookup (op=) ss s) of |
|
99 |
SOME s' => s' |
|
100 |
| NONE => s |
|
101 |
||
102 |
fun replace_typ ty_ss (Type (a, Ts)) = Type (replace_str ty_ss a, map (replace_typ ty_ss) Ts) |
|
103 |
| replace_typ ty_ss T = T |
|
104 |
||
105 |
fun raw_dts ty_ss dts = |
|
106 |
let |
|
107 |
fun raw_dts_aux1 (bind, tys, mx) = |
|
108 |
(raw_bind bind, map (replace_typ ty_ss) tys, mx) |
|
109 |
||
110 |
fun raw_dts_aux2 (ty_args, bind, mx, constrs) = |
|
111 |
(ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs) |
|
112 |
in |
|
113 |
map raw_dts_aux2 dts |
|
114 |
end |
|
115 |
||
116 |
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T) |
|
117 |
| replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T) |
|
118 |
| replace_aterm trm_ss trm = trm |
|
119 |
||
120 |
fun replace_term trm_ss ty_ss trm = |
|
121 |
trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss) |
|
122 |
*} |
|
123 |
||
124 |
ML {* |
|
125 |
fun rawify_dts dt_names dts dts_env = |
|
126 |
let |
|
127 |
val raw_dts = raw_dts dts_env dts |
|
128 |
val raw_dt_names = add_raws dt_names |
|
129 |
in |
|
130 |
(raw_dt_names, raw_dts) |
|
131 |
end |
|
132 |
*} |
|
133 |
||
134 |
ML {* |
|
135 |
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs = |
|
136 |
let |
|
137 |
val bn_funs' = map (fn (bn, ty, mx) => |
|
138 |
(raw_bind bn, replace_typ dts_env ty, mx)) bn_funs |
|
139 |
||
140 |
val bn_eqs' = map (fn (attr, trm) => |
|
141 |
(attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs |
|
142 |
in |
|
143 |
(bn_funs', bn_eqs') |
|
144 |
end |
|
145 |
*} |
|
146 |
||
147 |
ML {* |
|
148 |
fun rawify_bclauses dts_env cnstrs_env bn_fun_env bclauses = |
|
149 |
let |
|
150 |
fun rawify_bnds bnds = |
|
151 |
map (apfst (Option.map (replace_term (cnstrs_env @ bn_fun_env) dts_env))) bnds |
|
152 |
||
153 |
fun rawify_bclause (BEmy n) = BEmy n |
|
154 |
| rawify_bclause (BLst (bnds, bdys)) = BLst (rawify_bnds bnds, bdys) |
|
155 |
| rawify_bclause (BSet (bnds, bdys)) = BSet (rawify_bnds bnds, bdys) |
|
156 |
| rawify_bclause (BRes (bnds, bdys)) = BRes (rawify_bnds bnds, bdys) |
|
157 |
in |
|
158 |
map (map (map rawify_bclause)) bclauses |
|
159 |
end |
|
160 |
*} |
|
161 |
||
1942
d3b89f6c086a
added a comment about a function where I am not sure who wrote it.
Christian Urban <urbanc@in.tum.de>
parents:
1941
diff
changeset
|
162 |
text {* What does the prep_bn code do? Cezary's Function? *} |
1941 | 163 |
|
164 |
ML {* |
|
165 |
fun strip_bn_fun t = |
|
166 |
case t of |
|
167 |
Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r |
|
168 |
| Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r |
|
169 |
| Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y => |
|
170 |
(i, NONE) :: strip_bn_fun y |
|
171 |
| Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y => |
|
172 |
(i, NONE) :: strip_bn_fun y |
|
173 |
| Const (@{const_name bot}, _) => [] |
|
174 |
| Const (@{const_name Nil}, _) => [] |
|
175 |
| (f as Free _) $ Bound i => [(i, SOME f)] |
|
176 |
| _ => error ("Unsupported binding function: " ^ (PolyML.makestring t)) |
|
177 |
*} |
|
178 |
||
179 |
ML {* |
|
180 |
fun find [] _ = error ("cannot find element") |
|
181 |
| find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y |
|
182 |
*} |
|
183 |
||
184 |
ML {* |
|
185 |
fun prep_bn dt_names dts eqs = |
|
186 |
let |
|
187 |
fun aux eq = |
|
188 |
let |
|
189 |
val (lhs, rhs) = eq |
|
190 |
|> strip_qnt_body "all" |
|
191 |
|> HOLogic.dest_Trueprop |
|
192 |
|> HOLogic.dest_eq |
|
193 |
val (bn_fun, [cnstr]) = strip_comb lhs |
|
194 |
val (_, ty) = dest_Free bn_fun |
|
195 |
val (ty_name, _) = dest_Type (domain_type ty) |
|
196 |
val dt_index = find_index (fn x => x = ty_name) dt_names |
|
197 |
val (cnstr_head, cnstr_args) = strip_comb cnstr |
|
198 |
val rhs_elements = strip_bn_fun rhs |
|
199 |
val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements |
|
200 |
in |
|
201 |
(dt_index, (bn_fun, (cnstr_head, included))) |
|
202 |
end |
|
203 |
fun order dts i ts = |
|
204 |
let |
|
205 |
val dt = nth dts i |
|
206 |
val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt) |
|
207 |
val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts |
|
208 |
in |
|
209 |
map (find ts') cts |
|
210 |
end |
|
211 |
||
212 |
val unordered = AList.group (op=) (map aux eqs) |
|
213 |
val unordered' = map (fn (x, y) => (x, AList.group (op=) y)) unordered |
|
214 |
val ordered = map (fn (x, y) => (x, map (fn (v, z) => (v, order dts x z)) y)) unordered' |
|
215 |
in |
|
216 |
ordered |
|
217 |
end |
|
218 |
*} |
|
219 |
||
220 |
||
221 |
ML {* |
|
222 |
fun raw_nominal_decls dts bn_funs bn_eqs binds lthy = |
|
223 |
let |
|
224 |
val thy = ProofContext.theory_of lthy |
|
225 |
val thy_name = Context.theory_name thy |
|
226 |
||
227 |
val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts |
|
228 |
val dt_full_names = map (Long_Name.qualify thy_name) dt_names |
|
229 |
val dt_full_names' = add_raws dt_full_names |
|
230 |
val dts_env = dt_full_names ~~ dt_full_names' |
|
231 |
||
232 |
val cnstrs = get_cnstr_strs dts |
|
233 |
val cnstrs_ty = get_typed_cnstrs dts |
|
234 |
val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs |
|
235 |
val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name |
|
236 |
(Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty |
|
237 |
val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names' |
|
238 |
||
239 |
val bn_fun_strs = get_bn_fun_strs bn_funs |
|
240 |
val bn_fun_strs' = add_raws bn_fun_strs |
|
241 |
val bn_fun_env = bn_fun_strs ~~ bn_fun_strs' |
|
242 |
val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) |
|
243 |
(bn_fun_strs ~~ bn_fun_strs') |
|
244 |
||
245 |
val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env |
|
246 |
||
247 |
val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs |
|
248 |
||
249 |
val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env binds |
|
250 |
||
251 |
val raw_bns = prep_bn dt_full_names' raw_dts (map snd raw_bn_eqs) |
|
252 |
in |
|
253 |
lthy |
|
254 |
|> add_datatype_wrapper raw_dt_names raw_dts |
|
255 |
||>> add_primrec_wrapper raw_bn_funs raw_bn_eqs |
|
256 |
||>> pair raw_bclauses |
|
257 |
||>> pair raw_bns |
|
258 |
end |
|
259 |
*} |
|
260 |
||
261 |
ML {* |
|
262 |
fun nominal_datatype2 dts bn_funs bn_eqs bclauses lthy = |
|
263 |
let |
|
1945
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
264 |
val ((((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_bclauses), raw_bns), lthy1) = |
1941 | 265 |
raw_nominal_decls dts bn_funs bn_eqs bclauses lthy |
266 |
||
1945
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
267 |
val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names); |
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
268 |
val {descr, sorts, ...} = dtinfo; |
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
269 |
|
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
270 |
val ((raw_perm_def, raw_perm_simps, perms), lthy2) = |
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
271 |
Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy1; |
1960
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
272 |
|
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
273 |
val morphism_2_0 = ProofContext.export_morphism lthy2 lthy |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
274 |
fun export_fun f (t, l) = (f t, map (map (apsnd (Option.map f))) l); |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
275 |
val raw_bns_exp = map (apsnd (map (export_fun (Morphism.term morphism_2_0)))) raw_bns; |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
276 |
val bn_funs_decls = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) raw_bns_exp); |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
277 |
val thy = Local_Theory.exit_global lthy2; |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
278 |
val lthy3 = Theory_Target.init NONE thy; |
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
279 |
|
1963 | 280 |
val (_, lthy4) = define_raw_fv dtinfo bn_funs_decls raw_bclauses lthy3; |
1941 | 281 |
in |
1960
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
282 |
((raw_dt_names, raw_bn_funs_loc, raw_bn_eqs_loc, raw_bclauses, raw_bns), lthy4) |
1941 | 283 |
end |
284 |
*} |
|
285 |
||
286 |
section {* Preparing and parsing of the specification *} |
|
287 |
||
288 |
ML {* |
|
289 |
(* parsing the datatypes and declaring *) |
|
290 |
(* constructors in the local theory *) |
|
291 |
fun prepare_dts dt_strs lthy = |
|
292 |
let |
|
293 |
val thy = ProofContext.theory_of lthy |
|
294 |
||
295 |
fun mk_type full_tname tvrs = |
|
296 |
Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs) |
|
297 |
||
298 |
fun prep_cnstr full_tname tvs (cname, anno_tys, mx, _) = |
|
299 |
let |
|
300 |
val tys = map (Syntax.read_typ lthy o snd) anno_tys |
|
301 |
val ty = mk_type full_tname tvs |
|
302 |
in |
|
303 |
((cname, tys ---> ty, mx), (cname, tys, mx)) |
|
304 |
end |
|
305 |
||
306 |
fun prep_dt (tvs, tname, mx, cnstrs) = |
|
307 |
let |
|
308 |
val full_tname = Sign.full_name thy tname |
|
309 |
val (cnstrs', cnstrs'') = |
|
310 |
split_list (map (prep_cnstr full_tname tvs) cnstrs) |
|
311 |
in |
|
312 |
(cnstrs', (tvs, tname, mx, cnstrs'')) |
|
313 |
end |
|
314 |
||
315 |
val (cnstrs, dts) = split_list (map prep_dt dt_strs) |
|
316 |
in |
|
317 |
lthy |
|
318 |
|> Local_Theory.theory (Sign.add_consts_i (flat cnstrs)) |
|
319 |
|> pair dts |
|
320 |
end |
|
321 |
*} |
|
322 |
||
323 |
ML {* |
|
324 |
(* parsing the binding function specification and *) |
|
325 |
(* declaring the functions in the local theory *) |
|
326 |
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy = |
|
327 |
let |
|
328 |
val ((bn_funs, bn_eqs), _) = |
|
329 |
Specification.read_spec bn_fun_strs bn_eq_strs lthy |
|
330 |
||
331 |
fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) |
|
332 |
||
333 |
val bn_funs' = map prep_bn_fun bn_funs |
|
334 |
in |
|
335 |
lthy |
|
336 |
|> Local_Theory.theory (Sign.add_consts_i bn_funs') |
|
337 |
|> pair (bn_funs', bn_eqs) |
|
338 |
end |
|
339 |
*} |
|
340 |
||
341 |
text {* associates every SOME with the index in the list; drops NONEs *} |
|
342 |
ML {* |
|
343 |
fun indexify xs = |
|
344 |
let |
|
345 |
fun mapp _ [] = [] |
|
346 |
| mapp i (NONE :: xs) = mapp (i + 1) xs |
|
347 |
| mapp i (SOME x :: xs) = (x, i) :: mapp (i + 1) xs |
|
348 |
in |
|
349 |
mapp 0 xs |
|
350 |
end |
|
351 |
||
352 |
fun index_lookup xs x = |
|
353 |
case AList.lookup (op=) xs x of |
|
354 |
SOME x => x |
|
355 |
| NONE => error ("Cannot find " ^ x ^ " as argument annotation."); |
|
356 |
*} |
|
357 |
||
358 |
ML {* |
|
359 |
fun prepare_bclauses dt_strs lthy = |
|
360 |
let |
|
361 |
val annos_bclauses = |
|
362 |
get_cnstrs dt_strs |
|
363 |
|> map (map (fn (_, antys, _, bns) => (map fst antys, bns))) |
|
364 |
||
365 |
fun prep_binder env bn_str = |
|
366 |
case (Syntax.read_term lthy bn_str) of |
|
367 |
Free (x, _) => (NONE, index_lookup env x) |
|
368 |
| Const (a, T) $ Free (x, _) => (SOME (Const (a, T)), index_lookup env x) |
|
369 |
| _ => error ("The term " ^ bn_str ^ " is not allowed as binding function.") |
|
370 |
||
371 |
fun prep_body env bn_str = index_lookup env bn_str |
|
372 |
||
373 |
fun prep_mode "bind" = BLst |
|
374 |
| prep_mode "bind_set" = BSet |
|
375 |
| prep_mode "bind_res" = BRes |
|
376 |
||
377 |
fun prep_bclause env (mode, binders, bodies) = |
|
378 |
let |
|
379 |
val binders' = map (prep_binder env) binders |
|
380 |
val bodies' = map (prep_body env) bodies |
|
381 |
in |
|
382 |
prep_mode mode (binders', bodies') |
|
383 |
end |
|
384 |
||
385 |
fun prep_bclauses (annos, bclause_strs) = |
|
386 |
let |
|
387 |
val env = indexify annos (* for every label, associate the index *) |
|
388 |
in |
|
389 |
map (prep_bclause env) bclause_strs |
|
390 |
end |
|
391 |
in |
|
392 |
map (map prep_bclauses) annos_bclauses |
|
393 |
end |
|
394 |
*} |
|
395 |
||
1943 | 396 |
text {* |
397 |
adds an empty binding clause for every argument |
|
398 |
that is not already part of a binding clause |
|
399 |
*} |
|
400 |
||
1941 | 401 |
ML {* |
402 |
fun included i bcs = |
|
403 |
let |
|
404 |
fun incl (BEmy j) = i = j |
|
405 |
| incl (BLst (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) |
|
406 |
| incl (BSet (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) |
|
407 |
| incl (BRes (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) |
|
408 |
in |
|
409 |
exists incl bcs |
|
410 |
end |
|
411 |
*} |
|
412 |
||
413 |
ML {* |
|
414 |
fun complete dt_strs bclauses = |
|
415 |
let |
|
416 |
val args = |
|
417 |
get_cnstrs dt_strs |
|
418 |
|> map (map (fn (_, antys, _, _) => length antys)) |
|
419 |
||
420 |
fun complt n bcs = |
|
421 |
let |
|
422 |
fun add bcs i = (if included i bcs then [] else [BEmy i]) |
|
423 |
in |
|
424 |
bcs @ (flat (map_range (add bcs) n)) |
|
425 |
end |
|
426 |
in |
|
427 |
map2 (map2 complt) args bclauses |
|
428 |
end |
|
429 |
*} |
|
430 |
||
431 |
ML {* |
|
432 |
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy = |
|
433 |
let |
|
434 |
fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx) |
|
435 |
val lthy0 = |
|
436 |
Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy |
|
1944 | 437 |
val (dts, lthy1) = prepare_dts dt_strs lthy0 |
438 |
val ((bn_funs, bn_eqs), lthy2) = prepare_bn_funs bn_fun_strs bn_eq_strs lthy1 |
|
1941 | 439 |
val bclauses = prepare_bclauses dt_strs lthy2 |
440 |
val bclauses' = complete dt_strs bclauses |
|
441 |
in |
|
442 |
nominal_datatype2 dts bn_funs bn_eqs bclauses' lthy |> snd |
|
443 |
end |
|
444 |
||
445 |
||
446 |
(* Command Keyword *) |
|
447 |
||
448 |
val _ = OuterSyntax.local_theory "nominal_datatype" "test" OuterKeyword.thy_decl |
|
449 |
(main_parser >> nominal_datatype2_cmd) |
|
450 |
*} |
|
451 |
||
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
452 |
atom_decl name |
1941 | 453 |
|
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
454 |
nominal_datatype lam = |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
455 |
Var name |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
456 |
| App lam lam |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
457 |
| Lam x::name t::lam bind_set x in t |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
458 |
| Let p::pt t::lam bind_set "bn p" in t |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
459 |
and pt = |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
460 |
P1 name |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
461 |
| P2 pt pt |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
462 |
binder |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
463 |
bn::"pt \<Rightarrow> atom set" |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
464 |
where |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
465 |
"bn (P1 x) = {atom x}" |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
466 |
| "bn (P2 p1 p2) = bn p1 \<union> bn p2" |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
467 |
|
1941 | 468 |
|
469 |
nominal_datatype exp = |
|
470 |
EVar name |
|
471 |
| EUnit |
|
472 |
| EPair q1::exp q2::exp bind_set q1 q2 in q1 q2 |
|
473 |
| ELetRec l::lrbs e::exp bind "b_lrbs l" in e l |
|
474 |
||
475 |
and fnclause = |
|
476 |
K x::name p::pat f::exp bind_res "b_pat p" in f |
|
477 |
||
478 |
and fnclauses = |
|
479 |
S fnclause |
|
480 |
| ORs fnclause fnclauses |
|
481 |
||
482 |
and lrb = |
|
483 |
Clause fnclauses |
|
484 |
||
485 |
and lrbs = |
|
486 |
Single lrb |
|
487 |
| More lrb lrbs |
|
488 |
||
489 |
and pat = |
|
490 |
PVar name |
|
491 |
| PUnit |
|
492 |
| PPair pat pat |
|
493 |
||
494 |
binder |
|
495 |
b_lrbs :: "lrbs \<Rightarrow> atom set" and |
|
496 |
b_pat :: "pat \<Rightarrow> atom set" and |
|
497 |
b_fnclauses :: "fnclauses \<Rightarrow> atom set" and |
|
498 |
b_fnclause :: "fnclause \<Rightarrow> atom set" and |
|
499 |
b_lrb :: "lrb \<Rightarrow> atom set" |
|
500 |
||
501 |
where |
|
502 |
"b_lrbs (Single l) = b_lrb l" |
|
503 |
| "b_lrbs (More l ls) = b_lrb l \<union> b_lrbs ls" |
|
504 |
| "b_pat (PVar x) = {atom x}" |
|
505 |
| "b_pat (PUnit) = {}" |
|
506 |
| "b_pat (PPair p1 p2) = b_pat p1 \<union> b_pat p2" |
|
507 |
| "b_fnclauses (S fc) = (b_fnclause fc)" |
|
508 |
| "b_fnclauses (ORs fc fcs) = (b_fnclause fc) \<union> (b_fnclauses fcs)" |
|
509 |
| "b_lrb (Clause fcs) = (b_fnclauses fcs)" |
|
510 |
| "b_fnclause (K x pat exp) = {atom x}" |
|
511 |
||
1945
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
512 |
|
1941 | 513 |
typ exp_raw |
1970
90758c187861
use sort at_base instead of at
Christian Urban <urbanc@in.tum.de>
parents:
1964
diff
changeset
|
514 |
typ pat_raw |
1941 | 515 |
thm exp_raw_fnclause_raw_fnclauses_raw_lrb_raw_lrbs_raw_pat_raw.induct[no_vars] |
516 |
thm b_fnclause_raw_b_fnclauses_raw_b_lrb_raw_b_lrbs_raw_b_pat_raw.simps[no_vars] |
|
1945
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
517 |
thm permute_exp_raw_permute_fnclause_raw_permute_fnclauses_raw_permute_lrb_raw_permute_lrbs_raw_permute_pat_raw.simps[no_vars] |
1960
47e2e91705f3
Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1945
diff
changeset
|
518 |
thm fv_exp_raw.simps fv_fnclause_raw.simps fv_fnclauses_raw.simps fv_lrb_raw.simps fv_lrbs_raw.simps fv_pat_raw.simps fv_b_lrbs_raw.simps fv_b_pat_raw.simps fv_b_fnclauses_raw.simps fv_b_lrb_raw.simps fv_b_fnclause_raw.simps |
1945
93e5a31ba230
added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents:
1944
diff
changeset
|
519 |
|
1941 | 520 |
|
1964
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
521 |
(* some further tests *) |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
522 |
|
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
523 |
nominal_datatype lam2 = |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
524 |
Var2 "name" |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
525 |
| App2 "lam2" "lam2 list" |
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents:
1970
diff
changeset
|
526 |
| Lam2 x::"name" t::"lam2" bind x in t |
1964
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
527 |
|
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
528 |
nominal_datatype ty = |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
529 |
Var "name" |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
530 |
| Fun "ty" "ty" |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
531 |
|
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
532 |
nominal_datatype tys = |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
533 |
All xs::"name fset" ty::"ty_raw" bind xs in ty |
209ee65b2395
added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents:
1963
diff
changeset
|
534 |
|
1941 | 535 |
|
536 |
||
537 |
end |
|
538 |
||
539 |
||
540 |