1 (* Title: HOLCF/Tools/domain/domain_library.ML
3 Author: David von Oheimb
5 Library for domain command.
9 (* ----- general support ---------------------------------------------------- *)
12 | mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs;
14 fun foldr'' f (l,f2) = let fun itr [] = raise Fail "foldr''"
16 | itr (a::l) = f(a, itr l)
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;
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);
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))
32 in map zero_var_indexes (at thm) end;
34 (* ----- specific support for domain ---------------------------------------- *)
36 structure Domain_Library = struct
40 exception Impossible of string;
41 fun Imposs msg = raise Impossible ("Domain:"^msg);
43 (* ----- name handling ----- *)
45 val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs
47 | strip (c :: cs) = c :: strip cs
49 in implode o strip o Symbol.explode end;
51 fun extern_name con = case Symbol.explode con of
52 ("o"::"p"::" "::rest) => implode rest
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";
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
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;
76 fun pcpo_type sg t = Sign.of_sort sg (Sign.certify_typ sg t, pcpoS);
77 fun string_of_typ sg = Syntax.string_of_typ_global sg o Sign.certify_typ sg;
79 (* ----- constructor list handling ----- *)
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 *)
90 fun rec_of arg = second (first arg);
91 fun is_lazy arg = first (first arg);
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);
100 (* ----- support for type and mixfix expressions ----- *)
104 (* ----- support for term expressions ----- *)
106 fun %: s = Free(s,dummyT);
107 fun %# arg = %:(vname arg);
108 fun %%: s = Const(s,dummyT);
110 local open HOLogic in
111 val mk_trp = mk_Trueprop;
112 fun mk_conj (S,T) = conj $ S $ T;
113 fun mk_disj (S,T) = disj $ S $ T;
114 fun mk_imp (S,T) = imp $ S $ T;
115 fun mk_lam (x,T) = Abs(x,dummyT,T);
116 fun mk_all (x,P) = HOLogic.mk_all (x,dummyT,P);
117 fun mk_ex (x,P) = mk_exists (x,dummyT,P);
118 val mk_constrain = uncurry TypeInfer.constrain;
119 fun mk_constrainall (x,typ,P) = %%:"All" $ (TypeInfer.constrain (typ --> boolT) (mk_lam(x,P)));
122 fun mk_All (x,P) = %%:"all" $ mk_lam(x,P); (* meta universal quantification *)
124 infixr 0 ===>; fun S ===> T = %%:"==>" $ S $ T;
125 infixr 0 ==>; fun S ==> T = mk_trp S ===> mk_trp T;
126 infix 0 ==; fun S == T = %%:"==" $ S $ T;
127 infix 1 ===; fun S === T = %%:"op =" $ S $ T;
128 infix 1 ~=; fun S ~= T = HOLogic.mk_not (S === T);
129 infix 1 <<; fun S << T = %%:@{const_name Porder.sq_le} $ S $ T;
130 infix 1 ~<<; fun S ~<< T = HOLogic.mk_not (S << T);
132 infix 9 ` ; fun f ` x = %%:@{const_name Rep_CFun} $ f $ x;
133 infix 9 `% ; fun f`% s = f` %: s;
134 infix 9 `%%; fun f`%%s = f` %%:s;
136 fun mk_adm t = %%:@{const_name adm} $ t;
137 fun mk_compact t = %%:@{const_name compact} $ t;
138 val ID = %%:@{const_name ID};
139 fun mk_strictify t = %%:@{const_name strictify}`t;
140 fun mk_cfst t = %%:@{const_name cfst}`t;
141 fun mk_csnd t = %%:@{const_name csnd}`t;
142 (*val csplitN = "Cprod.csplit";*)
143 (*val sfstN = "Sprod.sfst";*)
144 (*val ssndN = "Sprod.ssnd";*)
145 fun mk_ssplit t = %%:@{const_name ssplit}`t;
146 fun mk_sinl t = %%:@{const_name sinl}`t;
147 fun mk_sinr t = %%:@{const_name sinr}`t;
148 fun mk_sscase (x, y) = %%:@{const_name sscase}`x`y;
149 fun mk_up t = %%:@{const_name up}`t;
150 fun mk_fup (t,u) = %%:@{const_name fup} ` t ` u;
151 val ONE = @{term ONE};
154 fun mk_iterate (n,f,z) = %%:@{const_name iterate} $ n ` f ` z;
155 fun mk_fix t = %%:@{const_name fix}`t;
156 fun mk_return t = %%:@{const_name Fixrec.return}`t;
157 val mk_fail = %%:@{const_name Fixrec.fail};
159 fun mk_branch t = %%:@{const_name Fixrec.branch} $ t;
161 val pcpoS = @{sort pcpo};
163 val list_ccomb = Library.foldl (op `); (* continuous version of list_comb *)
164 fun con_app2 con f args = list_ccomb(%%:con,map f args);
165 fun con_app con = con_app2 con %#;
166 fun if_rec arg f y = if is_rec arg then f (rec_of arg) else y;
167 fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) I (%# arg);
168 fun prj _ _ x ( _::[]) _ = x
169 | prj f1 _ x (_::y::ys) 0 = f1 x y
170 | prj f1 f2 x (y:: ys) j = prj f1 f2 (f2 x y) ys (j-1);
171 fun proj x = prj (fn S => K(%%:"fst" $S)) (fn S => K(%%:"snd" $S)) x;
172 fun cproj x = prj (fn S => K(mk_cfst S)) (fn S => K(mk_csnd S)) x;
173 fun lift tfn = Library.foldr (fn (x,t)=> (mk_trp(tfn x) ===> t));
175 fun /\ v T = %%:@{const_name Abs_CFun} $ mk_lam(v,T);
176 fun /\# (arg,T) = /\ (vname arg) T;
177 infixr 9 oo; fun S oo T = %%:@{const_name cfcomp}`S`T;
178 val UU = %%:@{const_name UU};
179 fun strict f = f`UU === UU;
180 fun defined t = t ~= UU;
181 fun cpair (t,u) = %%:@{const_name cpair}`t`u;
182 fun spair (t,u) = %%:@{const_name spair}`t`u;
183 fun mk_ctuple [] = HOLogic.unit (* used in match_defs *)
184 | mk_ctuple ts = foldr1 cpair ts;
185 fun mk_stuple [] = ONE
186 | mk_stuple ts = foldr1 spair ts;
187 fun mk_ctupleT [] = HOLogic.unitT (* used in match_defs *)
188 | mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts;
189 fun mk_maybeT T = Type ("Fixrec.maybe",[T]);
190 fun cpair_pat (p1,p2) = %%:@{const_name cpair_pat} $ p1 $ p2;
191 val mk_ctuple_pat = foldr1 cpair_pat;
192 fun lift_defined f = lift (fn x => defined (f x));
193 fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1);
195 fun cont_eta_contract (Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body)) =
196 (case cont_eta_contract body of
197 body' as (Const("Cfun.Rep_CFun",Ta) $ f $ Bound 0) =>
198 if not (0 mem loose_bnos f) then incr_boundvars ~1 f
199 else Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body')
200 | body' => Const("Cfun.Abs_CFun",TT) $ Abs(a,T,body'))
201 | cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t
202 | cont_eta_contract t = t;
204 fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n);
205 fun when_funs cons = if length cons = 1 then ["f"]
206 else mapn (fn n => K("f"^(string_of_int n))) 1 cons;
207 fun when_body cons funarg = let
208 fun one_fun n (_,[] ) = /\ "dummy" (funarg(1,n))
209 | one_fun n (_,args) = let
210 val l2 = length args;
211 fun idxs m arg = (if is_lazy arg then (fn t => mk_fup (ID, t))
212 else I) (Bound(l2-m));
213 in cont_eta_contract (foldr''
214 (fn (a,t) => mk_ssplit (/\# (a,t)))
216 fn a=> /\#(a,(list_ccomb(funarg(l2,n),mapn idxs 1 args))))
218 in (if length cons = 1 andalso length(snd(hd cons)) <= 1
219 then mk_strictify else I)
220 (foldr1 mk_sscase (mapn one_fun 1 cons)) end;