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