6 |
6 |
7 |
7 |
8 (* ----- general support ---------------------------------------------------- *) |
8 (* ----- general support ---------------------------------------------------- *) |
9 |
9 |
10 fun mapn f n [] = [] |
10 fun mapn f n [] = [] |
11 | mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs; |
11 | mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs; |
12 |
12 |
13 fun foldr'' f (l,f2) = let fun itr [] = raise Fail "foldr''" |
13 fun foldr'' f (l,f2) = |
14 | itr [a] = f2 a |
14 let fun itr [] = raise Fail "foldr''" |
15 | itr (a::l) = f(a, itr l) |
15 | itr [a] = f2 a |
16 in itr l end; |
16 | itr (a::l) = f(a, itr l) |
17 fun map_cumulr f start xs = List.foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) => |
17 in itr l end; |
18 (y::ys,res2)) ([],start) xs; |
18 |
19 |
19 fun map_cumulr f start xs = |
|
20 List.foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) => |
|
21 (y::ys,res2)) ([],start) xs; |
20 |
22 |
21 fun first (x,_,_) = x; fun second (_,x,_) = x; fun third (_,_,x) = x; |
23 fun first (x,_,_) = x; fun second (_,x,_) = x; fun third (_,_,x) = x; |
22 fun upd_first f (x,y,z) = (f x, y, z); |
24 fun upd_first f (x,y,z) = (f x, y, z); |
23 fun upd_second f (x,y,z) = ( x, f y, z); |
25 fun upd_second f (x,y,z) = ( x, f y, z); |
24 fun upd_third f (x,y,z) = ( x, y, f z); |
26 fun upd_third f (x,y,z) = ( x, y, f z); |
25 |
27 |
26 fun atomize ctxt thm = let val r_inst = read_instantiate ctxt; |
28 fun atomize ctxt thm = |
27 fun at thm = case concl_of thm of |
29 let |
28 _$(Const("op &",_)$_$_) => at(thm RS conjunct1)@at(thm RS conjunct2) |
30 val r_inst = read_instantiate ctxt; |
29 | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [(("x", 0), "?" ^ s)] spec)) |
31 fun at thm = |
30 | _ => [thm]; |
32 case concl_of thm of |
31 in map zero_var_indexes (at thm) end; |
33 _$(Const("op &",_)$_$_) => at(thm RS conjunct1)@at(thm RS conjunct2) |
|
34 | _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [(("x", 0), "?" ^ s)] spec)) |
|
35 | _ => [thm]; |
|
36 in map zero_var_indexes (at thm) end; |
32 |
37 |
33 (* infix syntax *) |
38 (* infix syntax *) |
34 |
39 |
35 infixr 5 -->; |
40 infixr 5 -->; |
36 infixr 6 ->>; |
41 infixr 6 ->>; |
167 exception Impossible of string; |
172 exception Impossible of string; |
168 fun Imposs msg = raise Impossible ("Domain:"^msg); |
173 fun Imposs msg = raise Impossible ("Domain:"^msg); |
169 |
174 |
170 (* ----- name handling ----- *) |
175 (* ----- name handling ----- *) |
171 |
176 |
172 val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs |
177 val strip_esc = |
173 | strip ["'"] = [] |
178 let fun strip ("'" :: c :: cs) = c :: strip cs |
174 | strip (c :: cs) = c :: strip cs |
179 | strip ["'"] = [] |
175 | strip [] = []; |
180 | strip (c :: cs) = c :: strip cs |
176 in implode o strip o Symbol.explode end; |
181 | strip [] = []; |
177 |
182 in implode o strip o Symbol.explode end; |
178 fun extern_name con = case Symbol.explode con of |
183 |
179 ("o"::"p"::" "::rest) => implode rest |
184 fun extern_name con = |
180 | _ => con; |
185 case Symbol.explode con of |
|
186 ("o"::"p"::" "::rest) => implode rest |
|
187 | _ => con; |
181 fun dis_name con = "is_"^ (extern_name con); |
188 fun dis_name con = "is_"^ (extern_name con); |
182 fun dis_name_ con = "is_"^ (strip_esc con); |
189 fun dis_name_ con = "is_"^ (strip_esc con); |
183 fun mat_name con = "match_"^ (extern_name con); |
190 fun mat_name con = "match_"^ (extern_name con); |
184 fun mat_name_ con = "match_"^ (strip_esc con); |
191 fun mat_name_ con = "match_"^ (strip_esc con); |
185 fun pat_name con = (extern_name con) ^ "_pat"; |
192 fun pat_name con = (extern_name con) ^ "_pat"; |
186 fun pat_name_ con = (strip_esc con) ^ "_pat"; |
193 fun pat_name_ con = (strip_esc con) ^ "_pat"; |
187 |
194 |
188 (* make distinct names out of the type list, |
195 (* make distinct names out of the type list, |
189 forbidding "o","n..","x..","f..","P.." as names *) |
196 forbidding "o","n..","x..","f..","P.." as names *) |
190 (* a number string is added if necessary *) |
197 (* a number string is added if necessary *) |
191 fun mk_var_names ids : string list = let |
198 fun mk_var_names ids : string list = |
192 fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s; |
199 let |
193 fun index_vnames(vn::vns,occupied) = |
200 fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s; |
|
201 fun index_vnames(vn::vns,occupied) = |
194 (case AList.lookup (op =) occupied vn of |
202 (case AList.lookup (op =) occupied vn of |
195 NONE => if vn mem vns |
203 NONE => if vn mem vns |
196 then (vn^"1") :: index_vnames(vns,(vn,1) ::occupied) |
204 then (vn^"1") :: index_vnames(vns,(vn,1) ::occupied) |
197 else vn :: index_vnames(vns, occupied) |
205 else vn :: index_vnames(vns, occupied) |
198 | SOME(i) => (vn^(string_of_int (i+1))) |
206 | SOME(i) => (vn^(string_of_int (i+1))) |
199 :: index_vnames(vns,(vn,i+1)::occupied)) |
207 :: index_vnames(vns,(vn,i+1)::occupied)) |
200 | index_vnames([],occupied) = []; |
208 | index_vnames([],occupied) = []; |
201 in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end; |
209 in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end; |
202 |
210 |
203 fun cpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort cpo}); |
211 fun cpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort cpo}); |
204 fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort pcpo}); |
212 fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, @{sort pcpo}); |
205 fun string_of_typ sg = Syntax.string_of_typ_global sg o Sign.certify_typ sg; |
213 fun string_of_typ sg = Syntax.string_of_typ_global sg o Sign.certify_typ sg; |
206 |
214 |
207 (* ----- constructor list handling ----- *) |
215 (* ----- constructor list handling ----- *) |
208 |
216 |
209 type arg = |
217 type arg = |
210 (bool * DatatypeAux.dtyp) * (* (lazy, recursive element) *) |
218 (bool * DatatypeAux.dtyp) * (* (lazy, recursive element) *) |
211 string option * (* selector name *) |
219 string option * (* selector name *) |
212 string; (* argument name *) |
220 string; (* argument name *) |
213 |
221 |
214 type cons = |
222 type cons = |
215 string * (* operator name of constr *) |
223 string * (* operator name of constr *) |
216 arg list; (* argument list *) |
224 arg list; (* argument list *) |
217 |
225 |
218 type eq = |
226 type eq = |
219 (string * (* name of abstracted type *) |
227 (string * (* name of abstracted type *) |
220 typ list) * (* arguments of abstracted type *) |
228 typ list) * (* arguments of abstracted type *) |
221 cons list; (* represented type, as a constructor list *) |
229 cons list; (* represented type, as a constructor list *) |
222 |
230 |
223 val mk_arg = I; |
231 val mk_arg = I; |
224 |
232 |
225 fun rec_of ((_,dtyp),_,_) = |
233 fun rec_of ((_,dtyp),_,_) = |
226 case dtyp of DatatypeAux.DtRec i => i | _ => ~1; |
234 case dtyp of DatatypeAux.DtRec i => i | _ => ~1; |
227 (* FIXME: what about indirect recursion? *) |
235 (* FIXME: what about indirect recursion? *) |
228 |
236 |
229 fun is_lazy arg = fst (first arg); |
237 fun is_lazy arg = fst (first arg); |
230 fun dtyp_of arg = snd (first arg); |
238 fun dtyp_of arg = snd (first arg); |
231 val sel_of = second; |
239 val sel_of = second; |
331 fun con_app2 con f args = list_ccomb(%%:con,map f args); |
339 fun con_app2 con f args = list_ccomb(%%:con,map f args); |
332 fun con_app con = con_app2 con %#; |
340 fun con_app con = con_app2 con %#; |
333 fun if_rec arg f y = if is_rec arg then f (rec_of arg) else y; |
341 fun if_rec arg f y = if is_rec arg then f (rec_of arg) else y; |
334 fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) I (%# arg); |
342 fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) I (%# arg); |
335 fun prj _ _ x ( _::[]) _ = x |
343 fun prj _ _ x ( _::[]) _ = x |
336 | prj f1 _ x (_::y::ys) 0 = f1 x y |
344 | prj f1 _ x (_::y::ys) 0 = f1 x y |
337 | prj f1 f2 x (y:: ys) j = prj f1 f2 (f2 x y) ys (j-1); |
345 | prj f1 f2 x (y:: ys) j = prj f1 f2 (f2 x y) ys (j-1); |
338 fun proj x = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x; |
346 fun proj x = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x; |
339 fun cproj x = prj (fn S => K(mk_cfst S)) (fn S => K(mk_csnd S)) x; |
347 fun cproj x = prj (fn S => K(mk_cfst S)) (fn S => K(mk_csnd S)) x; |
340 fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t)); |
348 fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t)); |
341 |
349 |
342 fun /\ v T = %%: @{const_name Abs_CFun} $ mk_lam(v,T); |
350 fun /\ v T = %%: @{const_name Abs_CFun} $ mk_lam(v,T); |
346 fun strict f = f`UU === UU; |
354 fun strict f = f`UU === UU; |
347 fun defined t = t ~= UU; |
355 fun defined t = t ~= UU; |
348 fun cpair (t,u) = %%: @{const_name cpair}`t`u; |
356 fun cpair (t,u) = %%: @{const_name cpair}`t`u; |
349 fun spair (t,u) = %%: @{const_name spair}`t`u; |
357 fun spair (t,u) = %%: @{const_name spair}`t`u; |
350 fun mk_ctuple [] = HOLogic.unit (* used in match_defs *) |
358 fun mk_ctuple [] = HOLogic.unit (* used in match_defs *) |
351 | mk_ctuple ts = foldr1 cpair ts; |
359 | mk_ctuple ts = foldr1 cpair ts; |
352 fun mk_stuple [] = ONE |
360 fun mk_stuple [] = ONE |
353 | mk_stuple ts = foldr1 spair ts; |
361 | mk_stuple ts = foldr1 spair ts; |
354 fun mk_ctupleT [] = HOLogic.unitT (* used in match_defs *) |
362 fun mk_ctupleT [] = HOLogic.unitT (* used in match_defs *) |
355 | mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts; |
363 | mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts; |
356 fun mk_maybeT T = Type ("Fixrec.maybe",[T]); |
364 fun mk_maybeT T = Type ("Fixrec.maybe",[T]); |
357 fun cpair_pat (p1,p2) = %%: @{const_name cpair_pat} $ p1 $ p2; |
365 fun cpair_pat (p1,p2) = %%: @{const_name cpair_pat} $ p1 $ p2; |
358 val mk_ctuple_pat = foldr1 cpair_pat; |
366 val mk_ctuple_pat = foldr1 cpair_pat; |
359 fun lift_defined f = lift (fn x => defined (f x)); |
367 fun lift_defined f = lift (fn x => defined (f x)); |
360 fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1); |
368 fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1); |
361 |
369 |
362 fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = |
370 fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = |
363 (case cont_eta_contract body of |
371 (case cont_eta_contract body of |
364 body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => |
372 body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => |
365 if not (0 mem loose_bnos f) then incr_boundvars ~1 f |
373 if not (0 mem loose_bnos f) then incr_boundvars ~1 f |
366 else Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body') |
374 else Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body') |
367 | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')) |
375 | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')) |
368 | cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t |
376 | cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t |
369 | cont_eta_contract t = t; |
377 | cont_eta_contract t = t; |
370 |
378 |
371 fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n); |
379 fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n); |
372 fun when_funs cons = if length cons = 1 then ["f"] |
380 fun when_funs cons = if length cons = 1 then ["f"] |
373 else mapn (fn n => K("f"^(string_of_int n))) 1 cons; |
381 else mapn (fn n => K("f"^(string_of_int n))) 1 cons; |
374 fun when_body cons funarg = let |
382 fun when_body cons funarg = |
375 fun one_fun n (_,[] ) = /\ "dummy" (funarg(1,n)) |
383 let |
376 | one_fun n (_,args) = let |
384 fun one_fun n (_,[] ) = /\ "dummy" (funarg(1,n)) |
377 val l2 = length args; |
385 | one_fun n (_,args) = let |
378 fun idxs m arg = (if is_lazy arg then (fn t => mk_fup (ID, t)) |
386 val l2 = length args; |
379 else I) (Bound(l2-m)); |
387 fun idxs m arg = (if is_lazy arg then (fn t => mk_fup (ID, t)) |
380 in cont_eta_contract (foldr'' |
388 else I) (Bound(l2-m)); |
381 (fn (a,t) => mk_ssplit (/\# (a,t))) |
389 in cont_eta_contract |
382 (args, |
390 (foldr'' |
383 fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args)))) |
391 (fn (a,t) => mk_ssplit (/\# (a,t))) |
384 ) end; |
392 (args, |
385 in (if length cons = 1 andalso length(snd(hd cons)) <= 1 |
393 fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args)))) |
386 then mk_strictify else I) |
394 ) end; |
387 (foldr1 mk_sscase (mapn one_fun 1 cons)) end; |
395 in (if length cons = 1 andalso length(snd(hd cons)) <= 1 |
|
396 then mk_strictify else I) |
|
397 (foldr1 mk_sscase (mapn one_fun 1 cons)) end; |
|
398 |
388 end; (* struct *) |
399 end; (* struct *) |