|
1 (* Title: HOLCF/Tools/holcf_library.ML |
|
2 Author: Brian Huffman |
|
3 |
|
4 Functions for constructing HOLCF types and terms. |
|
5 *) |
|
6 |
|
7 structure HOLCF_Library = |
|
8 struct |
|
9 |
|
10 (*** Operations from Isabelle/HOL ***) |
|
11 |
|
12 val boolT = HOLogic.boolT; |
|
13 |
|
14 val mk_equals = Logic.mk_equals; |
|
15 val mk_eq = HOLogic.mk_eq; |
|
16 val mk_trp = HOLogic.mk_Trueprop; |
|
17 val mk_fst = HOLogic.mk_fst; |
|
18 val mk_snd = HOLogic.mk_snd; |
|
19 val mk_not = HOLogic.mk_not; |
|
20 val mk_conj = HOLogic.mk_conj; |
|
21 val mk_disj = HOLogic.mk_disj; |
|
22 |
|
23 fun mk_ex (x, t) = HOLogic.exists_const (fastype_of x) $ Term.lambda x t; |
|
24 |
|
25 |
|
26 (*** Basic HOLCF concepts ***) |
|
27 |
|
28 fun mk_bottom T = Const (@{const_name UU}, T); |
|
29 |
|
30 fun below_const T = Const (@{const_name below}, [T, T] ---> boolT); |
|
31 fun mk_below (t, u) = below_const (fastype_of t) $ t $ u; |
|
32 |
|
33 fun mk_undef t = mk_eq (t, mk_bottom (fastype_of t)); |
|
34 |
|
35 fun mk_defined t = mk_not (mk_undef t); |
|
36 |
|
37 fun mk_compact t = |
|
38 Const (@{const_name compact}, fastype_of t --> boolT) $ t; |
|
39 |
|
40 fun mk_cont t = |
|
41 Const (@{const_name cont}, fastype_of t --> boolT) $ t; |
|
42 |
|
43 |
|
44 (*** Continuous function space ***) |
|
45 |
|
46 (* ->> is taken from holcf_logic.ML *) |
|
47 fun mk_cfunT (T, U) = Type(@{type_name "->"}, [T, U]); |
|
48 |
|
49 infixr 6 ->>; val (op ->>) = mk_cfunT; |
|
50 infix -->>; val (op -->>) = Library.foldr mk_cfunT; |
|
51 |
|
52 fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U) |
|
53 | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []); |
|
54 |
|
55 fun capply_const (S, T) = |
|
56 Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T)); |
|
57 |
|
58 fun cabs_const (S, T) = |
|
59 Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T)); |
|
60 |
|
61 fun mk_cabs t = |
|
62 let val T = fastype_of t |
|
63 in cabs_const (Term.domain_type T, Term.range_type T) $ t end |
|
64 |
|
65 (* builds the expression (% v1 v2 .. vn. rhs) *) |
|
66 fun lambdas [] rhs = rhs |
|
67 | lambdas (v::vs) rhs = Term.lambda v (lambdas vs rhs); |
|
68 |
|
69 (* builds the expression (LAM v. rhs) *) |
|
70 fun big_lambda v rhs = |
|
71 cabs_const (fastype_of v, fastype_of rhs) $ Term.lambda v rhs; |
|
72 |
|
73 (* builds the expression (LAM v1 v2 .. vn. rhs) *) |
|
74 fun big_lambdas [] rhs = rhs |
|
75 | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs); |
|
76 |
|
77 fun mk_capply (t, u) = |
|
78 let val (S, T) = |
|
79 case fastype_of t of |
|
80 Type(@{type_name "->"}, [S, T]) => (S, T) |
|
81 | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]); |
|
82 in capply_const (S, T) $ t $ u end; |
|
83 |
|
84 infix 9 ` ; val (op `) = mk_capply; |
|
85 |
|
86 val list_ccomb : term * term list -> term = Library.foldl mk_capply; |
|
87 |
|
88 fun mk_ID T = Const (@{const_name ID}, T ->> T); |
|
89 |
|
90 fun cfcomp_const (T, U, V) = |
|
91 Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V)); |
|
92 |
|
93 fun mk_cfcomp (f, g) = |
|
94 let |
|
95 val (U, V) = dest_cfunT (fastype_of f); |
|
96 val (T, U') = dest_cfunT (fastype_of g); |
|
97 in |
|
98 if U = U' |
|
99 then mk_capply (mk_capply (cfcomp_const (T, U, V), f), g) |
|
100 else raise TYPE ("mk_cfcomp", [U, U'], [f, g]) |
|
101 end; |
|
102 |
|
103 fun mk_strict t = |
|
104 let val (T, U) = dest_cfunT (fastype_of t); |
|
105 in mk_eq (t ` mk_bottom T, mk_bottom U) end; |
|
106 |
|
107 |
|
108 (*** Product type ***) |
|
109 |
|
110 val mk_prodT = HOLogic.mk_prodT |
|
111 |
|
112 fun mk_tupleT [] = HOLogic.unitT |
|
113 | mk_tupleT [T] = T |
|
114 | mk_tupleT (T :: Ts) = mk_prodT (T, mk_tupleT Ts); |
|
115 |
|
116 (* builds the expression (v1,v2,..,vn) *) |
|
117 fun mk_tuple [] = HOLogic.unit |
|
118 | mk_tuple (t::[]) = t |
|
119 | mk_tuple (t::ts) = HOLogic.mk_prod (t, mk_tuple ts); |
|
120 |
|
121 (* builds the expression (%(v1,v2,..,vn). rhs) *) |
|
122 fun lambda_tuple [] rhs = Term.lambda (Free("unit", HOLogic.unitT)) rhs |
|
123 | lambda_tuple (v::[]) rhs = Term.lambda v rhs |
|
124 | lambda_tuple (v::vs) rhs = |
|
125 HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs)); |
|
126 |
|
127 |
|
128 (*** Lifted cpo type ***) |
|
129 |
|
130 fun mk_upT T = Type(@{type_name "u"}, [T]); |
|
131 |
|
132 fun dest_upT (Type(@{type_name "u"}, [T])) = T |
|
133 | dest_upT T = raise TYPE ("dest_upT", [T], []); |
|
134 |
|
135 fun up_const T = Const(@{const_name up}, T ->> mk_upT T); |
|
136 |
|
137 fun mk_up t = up_const (fastype_of t) ` t; |
|
138 |
|
139 fun fup_const (T, U) = |
|
140 Const(@{const_name fup}, (T ->> U) ->> mk_upT T ->> U); |
|
141 |
|
142 fun from_up T = fup_const (T, T) ` mk_ID T; |
|
143 |
|
144 |
|
145 (*** Strict product type ***) |
|
146 |
|
147 val oneT = @{typ "one"}; |
|
148 |
|
149 fun mk_sprodT (T, U) = Type(@{type_name "**"}, [T, U]); |
|
150 |
|
151 fun dest_sprodT (Type(@{type_name "**"}, [T, U])) = (T, U) |
|
152 | dest_sprodT T = raise TYPE ("dest_sprodT", [T], []); |
|
153 |
|
154 fun spair_const (T, U) = |
|
155 Const(@{const_name spair}, T ->> U ->> mk_sprodT (T, U)); |
|
156 |
|
157 (* builds the expression (:t, u:) *) |
|
158 fun mk_spair (t, u) = |
|
159 spair_const (fastype_of t, fastype_of u) ` t ` u; |
|
160 |
|
161 (* builds the expression (:t1,t2,..,tn:) *) |
|
162 fun mk_stuple [] = @{term "ONE"} |
|
163 | mk_stuple (t::[]) = t |
|
164 | mk_stuple (t::ts) = mk_spair (t, mk_stuple ts); |
|
165 |
|
166 fun sfst_const (T, U) = |
|
167 Const(@{const_name sfst}, mk_sprodT (T, U) ->> T); |
|
168 |
|
169 fun ssnd_const (T, U) = |
|
170 Const(@{const_name ssnd}, mk_sprodT (T, U) ->> U); |
|
171 |
|
172 |
|
173 (*** Strict sum type ***) |
|
174 |
|
175 fun mk_ssumT (T, U) = Type(@{type_name "++"}, [T, U]); |
|
176 |
|
177 fun dest_ssumT (Type(@{type_name "++"}, [T, U])) = (T, U) |
|
178 | dest_ssumT T = raise TYPE ("dest_ssumT", [T], []); |
|
179 |
|
180 fun sinl_const (T, U) = Const(@{const_name sinl}, T ->> mk_ssumT (T, U)); |
|
181 fun sinr_const (T, U) = Const(@{const_name sinr}, U ->> mk_ssumT (T, U)); |
|
182 |
|
183 (* builds the list [sinl(t1), sinl(sinr(t2)), ... sinr(...sinr(tn))] *) |
|
184 fun mk_sinjects ts = |
|
185 let |
|
186 val Ts = map fastype_of ts; |
|
187 fun combine (t, T) (us, U) = |
|
188 let |
|
189 val v = sinl_const (T, U) ` t; |
|
190 val vs = map (fn u => sinr_const (T, U) ` u) us; |
|
191 in |
|
192 (v::vs, mk_ssumT (T, U)) |
|
193 end |
|
194 fun inj [] = error "mk_sinjects: empty list" |
|
195 | inj ((t, T)::[]) = ([t], T) |
|
196 | inj ((t, T)::ts) = combine (t, T) (inj ts); |
|
197 in |
|
198 fst (inj (ts ~~ Ts)) |
|
199 end; |
|
200 |
|
201 fun sscase_const (T, U, V) = |
|
202 Const(@{const_name sscase}, |
|
203 (T ->> V) ->> (U ->> V) ->> mk_ssumT (T, U) ->> V); |
|
204 |
|
205 fun from_sinl (T, U) = |
|
206 sscase_const (T, U, T) ` mk_ID T ` mk_bottom (U ->> T); |
|
207 |
|
208 fun from_sinr (T, U) = |
|
209 sscase_const (T, U, U) ` mk_bottom (T ->> U) ` mk_ID U; |
|
210 |
|
211 |
|
212 (*** pattern match monad type ***) |
|
213 |
|
214 fun mk_matchT T = Type (@{type_name "maybe"}, [T]); |
|
215 |
|
216 fun dest_matchT (Type(@{type_name "maybe"}, [T])) = T |
|
217 | dest_matchT T = raise TYPE ("dest_matchT", [T], []); |
|
218 |
|
219 fun mk_fail T = Const (@{const_name "Fixrec.fail"}, mk_matchT T); |
|
220 |
|
221 fun return_const T = Const (@{const_name "Fixrec.return"}, T ->> mk_matchT T); |
|
222 fun mk_return t = return_const (fastype_of t) ` t; |
|
223 |
|
224 |
|
225 (*** lifted boolean type ***) |
|
226 |
|
227 val trT = @{typ "tr"}; |
|
228 |
|
229 |
|
230 (*** theory of fixed points ***) |
|
231 |
|
232 fun mk_fix t = |
|
233 let val (T, _) = dest_cfunT (fastype_of t) |
|
234 in mk_capply (Const(@{const_name fix}, (T ->> T) ->> T), t) end; |
|
235 |
|
236 |
|
237 end; |