author | wenzelm |
Sat, 06 Oct 2007 16:50:04 +0200 | |
changeset 24867 | e5b55d7be9bb |
parent 24680 | 0d355aa59e67 |
child 25701 | 73fbe868b4e7 |
permissions | -rw-r--r-- |
23152 | 1 |
(* Title: HOLCF/Tools/domain/domain_library.ML |
2 |
ID: $Id$ |
|
3 |
Author: David von Oheimb |
|
4 |
||
5 |
Library for domain command. |
|
6 |
*) |
|
7 |
||
8 |
||
9 |
(* ----- general support ---------------------------------------------------- *) |
|
10 |
||
11 |
fun mapn f n [] = [] |
|
12 |
| mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs; |
|
13 |
||
14 |
fun foldr'' f (l,f2) = let fun itr [] = raise Fail "foldr''" |
|
15 |
| itr [a] = f2 a |
|
16 |
| itr (a::l) = f(a, itr l) |
|
17 |
in itr l end; |
|
18 |
fun map_cumulr f start xs = foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) => |
|
19 |
(y::ys,res2)) ([],start) xs; |
|
20 |
||
21 |
||
22 |
fun first (x,_,_) = x; fun second (_,x,_) = x; fun third (_,_,x) = x; |
|
23 |
fun upd_first f (x,y,z) = (f x, y, z); |
|
24 |
fun upd_second f (x,y,z) = ( x, f y, z); |
|
25 |
fun upd_third f (x,y,z) = ( x, y, f z); |
|
26 |
||
27 |
fun atomize thm = let val r_inst = read_instantiate; |
|
28 |
fun at thm = case concl_of thm of |
|
29 |
_$(Const("op &",_)$_$_) => at(thm RS conjunct1)@at(thm RS conjunct2) |
|
30 |
| _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [("x","?"^s)] spec)) |
|
31 |
| _ => [thm]; |
|
32 |
in map zero_var_indexes (at thm) end; |
|
33 |
||
34 |
(* ----- specific support for domain ---------------------------------------- *) |
|
35 |
||
36 |
structure Domain_Library = struct |
|
37 |
||
38 |
open HOLCFLogic; |
|
39 |
||
40 |
exception Impossible of string; |
|
41 |
fun Imposs msg = raise Impossible ("Domain:"^msg); |
|
42 |
||
43 |
(* ----- name handling ----- *) |
|
44 |
||
45 |
val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs |
|
46 |
| strip ["'"] = [] |
|
47 |
| strip (c :: cs) = c :: strip cs |
|
48 |
| strip [] = []; |
|
49 |
in implode o strip o Symbol.explode end; |
|
50 |
||
51 |
fun extern_name con = case Symbol.explode con of |
|
52 |
("o"::"p"::" "::rest) => implode rest |
|
53 |
| _ => con; |
|
54 |
fun dis_name con = "is_"^ (extern_name con); |
|
55 |
fun dis_name_ con = "is_"^ (strip_esc con); |
|
56 |
fun mat_name con = "match_"^ (extern_name con); |
|
57 |
fun mat_name_ con = "match_"^ (strip_esc con); |
|
58 |
fun pat_name con = (extern_name con) ^ "_pat"; |
|
59 |
fun pat_name_ con = (strip_esc con) ^ "_pat"; |
|
60 |
||
61 |
(* make distinct names out of the type list, |
|
62 |
forbidding "o","n..","x..","f..","P.." as names *) |
|
63 |
(* a number string is added if necessary *) |
|
64 |
fun mk_var_names ids : string list = let |
|
65 |
fun nonreserved s = if s mem ["n","x","f","P"] then s^"'" else s; |
|
66 |
fun index_vnames(vn::vns,occupied) = |
|
67 |
(case AList.lookup (op =) occupied vn of |
|
68 |
NONE => if vn mem vns |
|
69 |
then (vn^"1") :: index_vnames(vns,(vn,1) ::occupied) |
|
70 |
else vn :: index_vnames(vns, occupied) |
|
71 |
| SOME(i) => (vn^(string_of_int (i+1))) |
|
72 |
:: index_vnames(vns,(vn,i+1)::occupied)) |
|
73 |
| index_vnames([],occupied) = []; |
|
74 |
in index_vnames(map nonreserved ids, [("O",0),("o",0)]) end; |
|
75 |
||
76 |
fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, pcpoS); |
|
77 |
fun string_of_typ sg = Sign.string_of_typ sg o Sign.certify_typ sg; |
|
78 |
||
79 |
(* ----- constructor list handling ----- *) |
|
80 |
||
81 |
type cons = (string * (* operator name of constr *) |
|
82 |
((bool*int*DatatypeAux.dtyp)* (* (lazy,recursive element or ~1) *) |
|
83 |
string option* (* selector name *) |
|
84 |
string) (* argument name *) |
|
85 |
list); (* argument list *) |
|
86 |
type eq = (string * (* name of abstracted type *) |
|
87 |
typ list) * (* arguments of abstracted type *) |
|
88 |
cons list; (* represented type, as a constructor list *) |
|
89 |
||
90 |
fun rec_of arg = second (first arg); |
|
91 |
fun is_lazy arg = first (first arg); |
|
92 |
val sel_of = second; |
|
93 |
val vname = third; |
|
94 |
val upd_vname = upd_third; |
|
95 |
fun is_rec arg = rec_of arg >=0; |
|
96 |
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg); |
|
97 |
fun nonlazy args = map vname (filter_out is_lazy args); |
|
98 |
fun nonlazy_rec args = map vname (List.filter is_nonlazy_rec args); |
|
99 |
||
100 |
(* ----- qualified names of HOLCF constants ----- *) |
|
101 |
||
23284
07ae93e58fea
use new-style class for sq_ord; rename op << to sq_le
huffman
parents:
23152
diff
changeset
|
102 |
val lessN = @{const_name Porder.sq_le}; |
23152 | 103 |
val UU_N = "Pcpo.UU"; |
104 |
val admN = "Adm.adm"; |
|
105 |
val compactN = "Adm.compact"; |
|
106 |
val Rep_CFunN = "Cfun.Rep_CFun"; |
|
107 |
val Abs_CFunN = "Cfun.Abs_CFun"; |
|
108 |
val ID_N = "Cfun.ID"; |
|
109 |
val cfcompN = "Cfun.cfcomp"; |
|
110 |
val strictifyN = "Cfun.strictify"; |
|
111 |
val cpairN = "Cprod.cpair"; |
|
112 |
val cfstN = "Cprod.cfst"; |
|
113 |
val csndN = "Cprod.csnd"; |
|
114 |
val csplitN = "Cprod.csplit"; |
|
115 |
val spairN = "Sprod.spair"; |
|
116 |
val sfstN = "Sprod.sfst"; |
|
117 |
val ssndN = "Sprod.ssnd"; |
|
118 |
val ssplitN = "Sprod.ssplit"; |
|
119 |
val sinlN = "Ssum.sinl"; |
|
120 |
val sinrN = "Ssum.sinr"; |
|
121 |
val sscaseN = "Ssum.sscase"; |
|
122 |
val upN = "Up.up"; |
|
123 |
val fupN = "Up.fup"; |
|
124 |
val ONE_N = "One.ONE"; |
|
125 |
val TT_N = "Tr.TT"; |
|
126 |
val FF_N = "Tr.FF"; |
|
127 |
val iterateN = "Fix.iterate"; |
|
128 |
val fixN = "Fix.fix"; |
|
129 |
val returnN = "Fixrec.return"; |
|
130 |
val failN = "Fixrec.fail"; |
|
131 |
val cpair_patN = "Fixrec.cpair_pat"; |
|
132 |
val branchN = "Fixrec.branch"; |
|
133 |
||
134 |
val pcpoN = "Pcpo.pcpo" |
|
135 |
val pcpoS = [pcpoN]; |
|
136 |
||
137 |
||
138 |
(* ----- support for type and mixfix expressions ----- *) |
|
139 |
||
140 |
infixr 5 -->; |
|
141 |
||
142 |
(* ----- support for term expressions ----- *) |
|
143 |
||
144 |
fun %: s = Free(s,dummyT); |
|
145 |
fun %# arg = %:(vname arg); |
|
146 |
fun %%: s = Const(s,dummyT); |
|
147 |
||
148 |
local open HOLogic in |
|
149 |
val mk_trp = mk_Trueprop; |
|
150 |
fun mk_conj (S,T) = conj $ S $ T; |
|
151 |
fun mk_disj (S,T) = disj $ S $ T; |
|
152 |
fun mk_imp (S,T) = imp $ S $ T; |
|
153 |
fun mk_lam (x,T) = Abs(x,dummyT,T); |
|
154 |
fun mk_all (x,P) = HOLogic.mk_all (x,dummyT,P); |
|
155 |
fun mk_ex (x,P) = mk_exists (x,dummyT,P); |
|
24680 | 156 |
val mk_constrain = uncurry TypeInfer.constrain; |
157 |
fun mk_constrainall (x,typ,P) = %%:"All" $ (TypeInfer.constrain (typ --> boolT) (mk_lam(x,P))); |
|
23152 | 158 |
end |
159 |
||
160 |
fun mk_All (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *) |
|
161 |
||
162 |
infixr 0 ===>; fun S ===> T = %%:"==>" $ S $ T; |
|
163 |
infixr 0 ==>; fun S ==> T = mk_trp S ===> mk_trp T; |
|
164 |
infix 0 ==; fun S == T = %%:"==" $ S $ T; |
|
165 |
infix 1 ===; fun S === T = %%:"op =" $ S $ T; |
|
166 |
infix 1 ~=; fun S ~= T = HOLogic.mk_not (S === T); |
|
167 |
infix 1 <<; fun S << T = %%:lessN $ S $ T; |
|
168 |
infix 1 ~<<; fun S ~<< T = HOLogic.mk_not (S << T); |
|
169 |
||
170 |
infix 9 ` ; fun f` x = %%:Rep_CFunN $ f $ x; |
|
171 |
infix 9 `% ; fun f`% s = f` %: s; |
|
172 |
infix 9 `%%; fun f`%%s = f` %%:s; |
|
173 |
val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *) |
|
174 |
fun con_app2 con f args = list_ccomb(%%:con,map f args); |
|
175 |
fun con_app con = con_app2 con %#; |
|
176 |
fun if_rec arg f y = if is_rec arg then f (rec_of arg) else y; |
|
177 |
fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) I (%# arg); |
|
178 |
fun prj _ _ x ( _::[]) _ = x |
|
179 |
| prj f1 _ x (_::y::ys) 0 = f1 x y |
|
180 |
| prj f1 f2 x (y:: ys) j = prj f1 f2 (f2 x y) ys (j-1); |
|
181 |
fun proj x = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x; |
|
182 |
fun cproj x = prj (fn S => K(%%:cfstN`S)) (fn S => K(%%:csndN`S)) x; |
|
183 |
fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t)); |
|
184 |
||
185 |
fun /\ v T = %%:Abs_CFunN $ mk_lam(v,T); |
|
186 |
fun /\# (arg,T) = /\ (vname arg) T; |
|
187 |
infixr 9 oo; fun S oo T = %%:cfcompN`S`T; |
|
188 |
val UU = %%:UU_N; |
|
189 |
fun strict f = f`UU === UU; |
|
190 |
fun defined t = t ~= UU; |
|
191 |
fun cpair (t,u) = %%:cpairN`t`u; |
|
192 |
fun spair (t,u) = %%:spairN`t`u; |
|
193 |
fun mk_ctuple [] = HOLogic.unit (* used in match_defs *) |
|
194 |
| mk_ctuple ts = foldr1 cpair ts; |
|
195 |
fun mk_stuple [] = %%:ONE_N |
|
196 |
| mk_stuple ts = foldr1 spair ts; |
|
197 |
fun mk_ctupleT [] = HOLogic.unitT (* used in match_defs *) |
|
198 |
| mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts; |
|
199 |
fun mk_maybeT T = Type ("Fixrec.maybe",[T]); |
|
200 |
fun cpair_pat (p1,p2) = %%:cpair_patN $ p1 $ p2; |
|
201 |
fun lift_defined f = lift (fn x => defined (f x)); |
|
202 |
fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1); |
|
203 |
||
204 |
fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) = |
|
205 |
(case cont_eta_contract body of |
|
206 |
body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) => |
|
207 |
if not (0 mem loose_bnos f) then incr_boundvars ~1 f |
|
208 |
else Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body') |
|
209 |
| body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')) |
|
210 |
| cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t |
|
211 |
| cont_eta_contract t = t; |
|
212 |
||
213 |
fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n); |
|
214 |
fun when_funs cons = if length cons = 1 then ["f"] |
|
215 |
else mapn (fn n => K("f"^(string_of_int n))) 1 cons; |
|
216 |
fun when_body cons funarg = let |
|
217 |
fun one_fun n (_,[] ) = /\ "dummy" (funarg(1,n)) |
|
218 |
| one_fun n (_,args) = let |
|
219 |
val l2 = length args; |
|
220 |
fun idxs m arg = (if is_lazy arg then fn x=> %%:fupN` %%:ID_N`x |
|
221 |
else I) (Bound(l2-m)); |
|
222 |
in cont_eta_contract (foldr'' |
|
223 |
(fn (a,t) => %%:ssplitN`(/\# (a,t))) |
|
224 |
(args, |
|
225 |
fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args)))) |
|
226 |
) end; |
|
227 |
in (if length cons = 1 andalso length(snd(hd cons)) <= 1 |
|
228 |
then fn t => %%:strictifyN`t else I) |
|
229 |
(foldr1 (fn (x,y)=> %%:sscaseN`x`y) (mapn one_fun 1 cons)) end; |
|
230 |
end; (* struct *) |