author | huffman |
Wed, 18 Nov 2009 15:54:47 -0800 | |
changeset 33777 | 69eae9bca167 |
parent 33776 | 5048b02c2bbb |
child 33778 | 9121ea165a40 |
permissions | -rw-r--r-- |
33774 | 1 |
(* Title: HOLCF/Tools/domain/domain_isomorphism.ML |
2 |
Author: Brian Huffman |
|
3 |
||
4 |
Defines new types satisfying the given domain equations. |
|
5 |
*) |
|
6 |
||
7 |
signature DOMAIN_ISOMORPHISM = |
|
8 |
sig |
|
9 |
val domain_isomorphism: |
|
10 |
(string list * binding * mixfix * typ) list -> theory -> theory |
|
11 |
val domain_isomorphism_cmd: |
|
12 |
(string list * binding * mixfix * string) list -> theory -> theory |
|
13 |
end; |
|
14 |
||
15 |
structure Domain_Isomorphism :> DOMAIN_ISOMORPHISM = |
|
16 |
struct |
|
17 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
18 |
val beta_ss = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
19 |
HOL_basic_ss |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
20 |
addsimps simp_thms |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
21 |
addsimps [@{thm beta_cfun}] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
22 |
addsimprocs [@{simproc cont_proc}]; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
23 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
24 |
val beta_tac = simp_tac beta_ss; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
25 |
|
33774 | 26 |
(******************************************************************************) |
27 |
(******************************* building types *******************************) |
|
28 |
(******************************************************************************) |
|
29 |
||
30 |
(* ->> is taken from holcf_logic.ML *) |
|
31 |
fun cfunT (T, U) = Type(@{type_name "->"}, [T, U]); |
|
32 |
||
33 |
infixr 6 ->>; val (op ->>) = cfunT; |
|
34 |
||
35 |
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U) |
|
36 |
| dest_cfunT T = raise TYPE ("dest_cfunT", [T], []); |
|
37 |
||
38 |
fun tupleT [] = HOLogic.unitT |
|
39 |
| tupleT [T] = T |
|
40 |
| tupleT (T :: Ts) = HOLogic.mk_prodT (T, tupleT Ts); |
|
41 |
||
42 |
val deflT = @{typ "udom alg_defl"}; |
|
43 |
||
44 |
(******************************************************************************) |
|
45 |
(******************************* building terms *******************************) |
|
46 |
(******************************************************************************) |
|
47 |
||
48 |
(* builds the expression (v1,v2,..,vn) *) |
|
49 |
fun mk_tuple [] = HOLogic.unit |
|
50 |
| mk_tuple (t::[]) = t |
|
51 |
| mk_tuple (t::ts) = HOLogic.mk_prod (t, mk_tuple ts); |
|
52 |
||
53 |
(* builds the expression (%(v1,v2,..,vn). rhs) *) |
|
54 |
fun lambda_tuple [] rhs = Term.lambda (Free("unit", HOLogic.unitT)) rhs |
|
55 |
| lambda_tuple (v::[]) rhs = Term.lambda v rhs |
|
56 |
| lambda_tuple (v::vs) rhs = |
|
57 |
HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs)); |
|
58 |
||
59 |
(* continuous application and abstraction *) |
|
60 |
||
61 |
fun capply_const (S, T) = |
|
62 |
Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T)); |
|
63 |
||
64 |
fun cabs_const (S, T) = |
|
65 |
Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T)); |
|
66 |
||
67 |
fun mk_cabs t = |
|
68 |
let val T = Term.fastype_of t |
|
69 |
in cabs_const (Term.domain_type T, Term.range_type T) $ t end |
|
70 |
||
71 |
(* builds the expression (LAM v. rhs) *) |
|
72 |
fun big_lambda v rhs = |
|
73 |
cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs; |
|
74 |
||
75 |
(* builds the expression (LAM v1 v2 .. vn. rhs) *) |
|
76 |
fun big_lambdas [] rhs = rhs |
|
77 |
| big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs); |
|
78 |
||
79 |
fun mk_capply (t, u) = |
|
80 |
let val (S, T) = |
|
81 |
case Term.fastype_of t of |
|
82 |
Type(@{type_name "->"}, [S, T]) => (S, T) |
|
83 |
| _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]); |
|
84 |
in capply_const (S, T) $ t $ u end; |
|
85 |
||
86 |
(* miscellaneous term constructions *) |
|
87 |
||
88 |
val mk_trp = HOLogic.mk_Trueprop; |
|
89 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
90 |
val mk_fst = HOLogic.mk_fst; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
91 |
val mk_snd = HOLogic.mk_snd; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
92 |
|
33774 | 93 |
fun mk_cont t = |
94 |
let val T = Term.fastype_of t |
|
95 |
in Const(@{const_name cont}, T --> HOLogic.boolT) $ t end; |
|
96 |
||
97 |
fun mk_fix t = |
|
98 |
let val (T, _) = dest_cfunT (Term.fastype_of t) |
|
99 |
in mk_capply (Const(@{const_name fix}, (T ->> T) ->> T), t) end; |
|
100 |
||
101 |
fun mk_Rep_of T = |
|
102 |
Const (@{const_name Rep_of}, Term.itselfT T --> deflT) $ Logic.mk_type T; |
|
103 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
104 |
(* splits a cterm into the right and lefthand sides of equality *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
105 |
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
106 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
107 |
fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u)); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
108 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
109 |
(******************************************************************************) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
110 |
(*************** fixed-point definitions and unfolding theorems ***************) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
111 |
(******************************************************************************) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
112 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
113 |
fun add_fixdefs |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
114 |
(spec : (binding * term) list) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
115 |
(thy : theory) : thm list * theory = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
116 |
let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
117 |
val binds = map fst spec; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
118 |
val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
119 |
val functional = lambda_tuple lhss (mk_tuple rhss); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
120 |
val fixpoint = mk_fix (mk_cabs functional); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
121 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
122 |
(* project components of fixpoint *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
123 |
fun mk_projs (x::[]) t = [(x, t)] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
124 |
| mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
125 |
val projs = mk_projs lhss fixpoint; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
126 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
127 |
(* convert parameters to lambda abstractions *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
128 |
fun mk_eqn (lhs, rhs) = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
129 |
case lhs of |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
130 |
Const (@{const_name Rep_CFun}, _) $ f $ (x as Free _) => |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
131 |
mk_eqn (f, big_lambda x rhs) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
132 |
| Const _ => Logic.mk_equals (lhs, rhs) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
133 |
| _ => raise TERM ("lhs not of correct form", [lhs, rhs]); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
134 |
val eqns = map mk_eqn projs; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
135 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
136 |
(* register constant definitions *) |
33777 | 137 |
val (fixdef_thms, thy) = |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
138 |
(PureThy.add_defs false o map Thm.no_attributes) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
139 |
(map (Binding.suffix_name "_def") binds ~~ eqns) thy; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
140 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
141 |
(* prove applied version of definitions *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
142 |
fun prove_proj (lhs, rhs) = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
143 |
let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
144 |
val tac = rewrite_goals_tac fixdef_thms THEN beta_tac 1; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
145 |
val goal = Logic.mk_equals (lhs, rhs); |
33777 | 146 |
in Goal.prove_global thy [] [] goal (K tac) end; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
147 |
val proj_thms = map prove_proj projs; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
148 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
149 |
(* mk_tuple lhss == fixpoint *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
150 |
fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2]; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
151 |
val tuple_fixdef_thm = foldr1 pair_equalI proj_thms; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
152 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
153 |
val cont_thm = |
33777 | 154 |
Goal.prove_global thy [] [] (mk_trp (mk_cont functional)) |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
155 |
(K (beta_tac 1)); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
156 |
val tuple_unfold_thm = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
157 |
(@{thm def_cont_fix_eq} OF [tuple_fixdef_thm, cont_thm]) |
33777 | 158 |
|> LocalDefs.unfold (ProofContext.init thy) @{thms split_conv}; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
159 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
160 |
fun mk_unfold_thms [] thm = [] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
161 |
| mk_unfold_thms (n::[]) thm = [(n, thm)] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
162 |
| mk_unfold_thms (n::ns) thm = let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
163 |
val thmL = thm RS @{thm Pair_eqD1}; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
164 |
val thmR = thm RS @{thm Pair_eqD2}; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
165 |
in (n, thmL) :: mk_unfold_thms ns thmR end; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
166 |
val unfold_binds = map (Binding.suffix_name "_unfold") binds; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
167 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
168 |
(* register unfold theorems *) |
33777 | 169 |
val (unfold_thms, thy) = |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
170 |
(PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.standard)) |
33777 | 171 |
(mk_unfold_thms unfold_binds tuple_unfold_thm) thy; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
172 |
in |
33777 | 173 |
(unfold_thms, thy) |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
174 |
end; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
175 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
176 |
|
33774 | 177 |
(******************************************************************************) |
178 |
||
179 |
fun typ_of_dtyp |
|
180 |
(descr : (string * string list) list) |
|
181 |
(sorts : (string * sort) list) |
|
182 |
: DatatypeAux.dtyp -> typ = |
|
183 |
let |
|
184 |
fun tfree a = TFree (a, the (AList.lookup (op =) sorts a)) |
|
185 |
fun typ_of (DatatypeAux.DtTFree a) = tfree a |
|
186 |
| typ_of (DatatypeAux.DtType (s, ds)) = Type (s, map typ_of ds) |
|
187 |
| typ_of (DatatypeAux.DtRec i) = |
|
188 |
let val (s, vs) = nth descr i |
|
189 |
in Type (s, map tfree vs) end |
|
190 |
in typ_of end; |
|
191 |
||
192 |
fun is_closed_dtyp (DatatypeAux.DtTFree a) = false |
|
193 |
| is_closed_dtyp (DatatypeAux.DtRec i) = false |
|
194 |
| is_closed_dtyp (DatatypeAux.DtType (s, ds)) = forall is_closed_dtyp ds; |
|
195 |
||
196 |
(* FIXME: use theory data for this *) |
|
197 |
val defl_tab : term Symtab.table = |
|
198 |
Symtab.make [(@{type_name "->"}, @{term "cfun_typ"}), |
|
199 |
(@{type_name "++"}, @{term "ssum_typ"}), |
|
200 |
(@{type_name "**"}, @{term "sprod_typ"}), |
|
201 |
(@{type_name "*"}, @{term "cprod_typ"}), |
|
202 |
(@{type_name "u"}, @{term "u_typ"}), |
|
203 |
(@{type_name "upper_pd"}, @{term "upper_typ"}), |
|
204 |
(@{type_name "lower_pd"}, @{term "lower_typ"}), |
|
205 |
(@{type_name "convex_pd"}, @{term "convex_typ"})]; |
|
206 |
||
207 |
fun defl_of_typ |
|
208 |
(tab : term Symtab.table) |
|
209 |
(free : string -> term) |
|
210 |
(T : typ) : term = |
|
211 |
let |
|
212 |
fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts |
|
213 |
| is_closed_typ _ = false; |
|
214 |
fun defl_of (TFree (a, _)) = free a |
|
215 |
| defl_of (TVar _) = error ("defl_of_typ: TVar") |
|
216 |
| defl_of (T as Type (c, Ts)) = |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
217 |
case Symtab.lookup tab c of |
33774 | 218 |
SOME t => Library.foldl mk_capply (t, map defl_of Ts) |
219 |
| NONE => if is_closed_typ T |
|
220 |
then mk_Rep_of T |
|
221 |
else error ("defl_of_typ: type variable under unsupported type constructor " ^ c); |
|
222 |
in defl_of T end; |
|
223 |
||
224 |
fun defl_of_dtyp |
|
225 |
(descr : (string * string list) list) |
|
226 |
(sorts : (string * sort) list) |
|
227 |
(f : string -> term) |
|
228 |
(r : int -> term) |
|
229 |
(dt : DatatypeAux.dtyp) : term = |
|
230 |
let |
|
231 |
fun tfree a = TFree (a, the (AList.lookup (op =) sorts a)) |
|
232 |
fun defl_of (DatatypeAux.DtTFree a) = f a |
|
233 |
| defl_of (DatatypeAux.DtRec i) = r i |
|
234 |
| defl_of (dt as DatatypeAux.DtType (s, ds)) = |
|
235 |
case Symtab.lookup defl_tab s of |
|
236 |
SOME t => Library.foldl mk_capply (t, map defl_of ds) |
|
237 |
| NONE => if DatatypeAux.is_rec_type dt |
|
238 |
then error ("defl_of_dtyp: recursion under unsupported type constructor " ^ s) |
|
239 |
else if is_closed_dtyp dt |
|
240 |
then mk_Rep_of (typ_of_dtyp descr sorts dt) |
|
241 |
else error ("defl_of_dtyp: type variable under unsupported type constructor " ^ s); |
|
242 |
in defl_of dt end; |
|
243 |
||
244 |
(******************************************************************************) |
|
245 |
(* prepare datatype specifications *) |
|
246 |
||
247 |
fun read_typ thy str sorts = |
|
248 |
let |
|
249 |
val ctxt = ProofContext.init thy |
|
250 |
|> fold (Variable.declare_typ o TFree) sorts; |
|
251 |
val T = Syntax.read_typ ctxt str; |
|
252 |
in (T, Term.add_tfreesT T sorts) end; |
|
253 |
||
254 |
fun cert_typ sign raw_T sorts = |
|
255 |
let |
|
256 |
val T = Type.no_tvars (Sign.certify_typ sign raw_T) |
|
257 |
handle TYPE (msg, _, _) => error msg; |
|
258 |
val sorts' = Term.add_tfreesT T sorts; |
|
259 |
val _ = |
|
260 |
case duplicates (op =) (map fst sorts') of |
|
261 |
[] => () |
|
262 |
| dups => error ("Inconsistent sort constraints for " ^ commas dups) |
|
263 |
in (T, sorts') end; |
|
264 |
||
265 |
fun gen_domain_isomorphism |
|
266 |
(prep_typ: theory -> 'a -> (string * sort) list -> typ * (string * sort) list) |
|
267 |
(doms_raw: (string list * binding * mixfix * 'a) list) |
|
268 |
(thy: theory) |
|
269 |
: theory = |
|
270 |
let |
|
271 |
val _ = Theory.requires thy "Domain" "domain definitions"; |
|
272 |
||
273 |
(* this theory is used just for parsing *) |
|
274 |
val tmp_thy = thy |> |
|
275 |
Theory.copy |> |
|
276 |
Sign.add_types (map (fn (tvs, tname, mx, _) => |
|
277 |
(tname, length tvs, mx)) doms_raw); |
|
278 |
||
279 |
fun prep_dom thy (vs, t, mx, typ_raw) sorts = |
|
280 |
let val (typ, sorts') = prep_typ thy typ_raw sorts |
|
281 |
in ((vs, t, mx, typ), sorts') end; |
|
282 |
||
283 |
val (doms : (string list * binding * mixfix * typ) list, |
|
284 |
sorts : (string * sort) list) = |
|
285 |
fold_map (prep_dom tmp_thy) doms_raw []; |
|
286 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
287 |
(* domain equations *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
288 |
fun mk_dom_eqn (vs, tbind, mx, rhs) = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
289 |
let fun arg v = TFree (v, the (AList.lookup (op =) sorts v)); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
290 |
in (Type (Sign.full_name tmp_thy tbind, map arg vs), rhs) end; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
291 |
val dom_eqns = map mk_dom_eqn doms; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
292 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
293 |
(* check for valid type parameters *) |
33774 | 294 |
val (tyvars, _, _, _)::_ = doms; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
295 |
val new_doms = map (fn (tvs, tname, mx, _) => |
33774 | 296 |
let val full_tname = Sign.full_name tmp_thy tname |
297 |
in |
|
298 |
(case duplicates (op =) tvs of |
|
299 |
[] => |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
300 |
if eq_set (op =) (tyvars, tvs) then (full_tname, tvs) |
33774 | 301 |
else error ("Mutually recursive domains must have same type parameters") |
302 |
| dups => error ("Duplicate parameter(s) for domain " ^ quote (Binding.str_of tname) ^ |
|
303 |
" : " ^ commas dups)) |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
304 |
end) doms; |
33774 | 305 |
val dom_names = map fst new_doms; |
306 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
307 |
(* declare type combinator constants *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
308 |
fun declare_typ_const (vs, tbind, mx, rhs) thy = |
33774 | 309 |
let |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
310 |
val typ_type = Library.foldr cfunT (map (K deflT) vs, deflT); |
33774 | 311 |
val typ_bind = Binding.suffix_name "_typ" tbind; |
312 |
in |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
313 |
Sign.declare_const ((typ_bind, typ_type), NoSyn) thy |
33774 | 314 |
end; |
33777 | 315 |
val (typ_consts, thy) = fold_map declare_typ_const doms thy; |
33774 | 316 |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
317 |
(* defining equations for type combinators *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
318 |
val defl_tab1 = defl_tab; (* FIXME: use theory data *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
319 |
val defl_tab2 = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
320 |
Symtab.make (map (fst o dest_Type o fst) dom_eqns ~~ typ_consts); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
321 |
val defl_tab' = Symtab.merge (K true) (defl_tab1, defl_tab2); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
322 |
fun free a = Free (Library.unprefix "'" a, deflT); |
33776 | 323 |
fun mk_defl_spec (lhsT, rhsT) = |
324 |
mk_eqs (defl_of_typ defl_tab' free lhsT, |
|
325 |
defl_of_typ defl_tab' free rhsT); |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
326 |
val defl_specs = map mk_defl_spec dom_eqns; |
33774 | 327 |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
328 |
(* register recursive definition of type combinators *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
329 |
val typ_binds = map (Binding.suffix_name "_typ" o #2) doms; |
33777 | 330 |
val (typ_unfold_thms, thy) = add_fixdefs (typ_binds ~~ defl_specs) thy; |
33774 | 331 |
|
33776 | 332 |
(* define types using deflation combinators *) |
33774 | 333 |
fun make_repdef ((vs, tbind, mx, _), typ_const) thy = |
334 |
let |
|
335 |
fun tfree a = TFree (a, the (AList.lookup (op =) sorts a)) |
|
336 |
val reps = map (mk_Rep_of o tfree) vs; |
|
337 |
val defl = Library.foldl mk_capply (typ_const, reps); |
|
33777 | 338 |
val ((_, _, _, {REP, ...}), thy) = |
33774 | 339 |
Repdef.add_repdef false NONE (tbind, vs, mx) defl NONE thy; |
340 |
in |
|
33777 | 341 |
(REP, thy) |
33774 | 342 |
end; |
33777 | 343 |
val (REP_thms, thy) = |
344 |
fold_map make_repdef (doms ~~ typ_consts) thy; |
|
33774 | 345 |
|
33776 | 346 |
(* FIXME: use theory data for this *) |
347 |
val REP_simps = REP_thms @ |
|
348 |
@{thms REP_cfun REP_ssum REP_sprod REP_cprod REP_up |
|
349 |
REP_upper REP_lower REP_convex}; |
|
350 |
||
351 |
(* prove REP equations *) |
|
352 |
fun mk_REP_eqn_thm (lhsT, rhsT) = |
|
353 |
let |
|
354 |
val goal = mk_eqs (mk_Rep_of lhsT, mk_Rep_of rhsT); |
|
355 |
val tac = |
|
356 |
simp_tac (HOL_basic_ss addsimps REP_simps) 1 |
|
357 |
THEN resolve_tac typ_unfold_thms 1; |
|
358 |
in |
|
33777 | 359 |
Goal.prove_global thy [] [] goal (K tac) |
33776 | 360 |
end; |
361 |
val REP_eqn_thms = map mk_REP_eqn_thm dom_eqns; |
|
362 |
||
33774 | 363 |
in |
33777 | 364 |
thy |
33774 | 365 |
end; |
366 |
||
367 |
val domain_isomorphism = gen_domain_isomorphism cert_typ; |
|
368 |
val domain_isomorphism_cmd = gen_domain_isomorphism read_typ; |
|
369 |
||
370 |
(******************************************************************************) |
|
371 |
(******************************** outer syntax ********************************) |
|
372 |
(******************************************************************************) |
|
373 |
||
374 |
local |
|
375 |
||
376 |
structure P = OuterParse and K = OuterKeyword |
|
377 |
||
378 |
val parse_domain_iso : (string list * binding * mixfix * string) parser = |
|
379 |
(P.type_args -- P.binding -- P.opt_infix -- (P.$$$ "=" |-- P.typ)) |
|
380 |
>> (fn (((vs, t), mx), rhs) => (vs, t, mx, rhs)); |
|
381 |
||
382 |
val parse_domain_isos = P.and_list1 parse_domain_iso; |
|
383 |
||
384 |
in |
|
385 |
||
386 |
val _ = |
|
387 |
OuterSyntax.command "domain_isomorphism" "define domain isomorphisms (HOLCF)" K.thy_decl |
|
388 |
(parse_domain_isos >> (Toplevel.theory o domain_isomorphism_cmd)); |
|
389 |
||
390 |
end; |
|
391 |
||
392 |
end; |