author | huffman |
Mon, 01 Mar 2010 16:58:16 -0800 | |
changeset 35490 | 63f8121c6585 |
parent 35489 | dd02201d95b6 |
child 35494 | 45c9a8278faf |
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 |
|
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
9 |
type iso_info = |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
10 |
{ |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
11 |
repT : typ, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
12 |
absT : typ, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
13 |
rep_const : term, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
14 |
abs_const : term, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
15 |
rep_inverse : thm, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
16 |
abs_inverse : thm |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
17 |
} |
35489 | 18 |
val domain_isomorphism : |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
19 |
(string list * binding * mixfix * typ * (binding * binding) option) list |
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
20 |
-> theory -> iso_info list * theory |
35489 | 21 |
val domain_isomorphism_cmd : |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
22 |
(string list * binding * mixfix * string * (binding * binding) option) list |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
23 |
-> theory -> theory |
35489 | 24 |
val add_type_constructor : |
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
25 |
(string * term * string * thm * thm * thm * thm) -> theory -> theory |
35489 | 26 |
val get_map_tab : |
33801 | 27 |
theory -> string Symtab.table |
35489 | 28 |
val define_take_functions : |
29 |
(binding * iso_info) list -> theory -> |
|
30 |
{ take_consts : term list, |
|
31 |
take_defs : thm list, |
|
32 |
chain_take_thms : thm list, |
|
33 |
take_0_thms : thm list, |
|
34 |
take_Suc_thms : thm list, |
|
35 |
deflation_take_thms : thm list |
|
36 |
} * theory; |
|
33774 | 37 |
end; |
38 |
||
39 |
structure Domain_Isomorphism :> DOMAIN_ISOMORPHISM = |
|
40 |
struct |
|
41 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
42 |
val beta_ss = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
43 |
HOL_basic_ss |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
44 |
addsimps simp_thms |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
45 |
addsimps [@{thm beta_cfun}] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
46 |
addsimprocs [@{simproc cont_proc}]; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
47 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
48 |
val beta_tac = simp_tac beta_ss; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
49 |
|
33774 | 50 |
(******************************************************************************) |
33791 | 51 |
(******************************** theory data *********************************) |
52 |
(******************************************************************************) |
|
53 |
||
54 |
structure DeflData = Theory_Data |
|
55 |
( |
|
56 |
type T = term Symtab.table; |
|
57 |
val empty = Symtab.empty; |
|
58 |
val extend = I; |
|
59 |
fun merge data = Symtab.merge (K true) data; |
|
60 |
); |
|
61 |
||
62 |
structure MapData = Theory_Data |
|
63 |
( |
|
64 |
type T = string Symtab.table; |
|
65 |
val empty = Symtab.empty; |
|
66 |
val extend = I; |
|
67 |
fun merge data = Symtab.merge (K true) data; |
|
68 |
); |
|
69 |
||
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
70 |
structure Thm_List : THEORY_DATA_ARGS = |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
71 |
struct |
33791 | 72 |
type T = thm list; |
73 |
val empty = []; |
|
74 |
val extend = I; |
|
75 |
val merge = Thm.merge_thms; |
|
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
76 |
end; |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
77 |
|
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
78 |
structure RepData = Theory_Data (Thm_List); |
33791 | 79 |
|
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
80 |
structure IsodeflData = Theory_Data (Thm_List); |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
81 |
|
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
82 |
structure MapIdData = Theory_Data (Thm_List); |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
83 |
|
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
84 |
structure DeflMapData = Theory_Data (Thm_List); |
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
85 |
|
33791 | 86 |
fun add_type_constructor |
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
87 |
(tname, defl_const, map_name, REP_thm, |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
88 |
isodefl_thm, map_ID_thm, defl_map_thm) = |
33791 | 89 |
DeflData.map (Symtab.insert (K true) (tname, defl_const)) |
90 |
#> MapData.map (Symtab.insert (K true) (tname, map_name)) |
|
91 |
#> RepData.map (Thm.add_thm REP_thm) |
|
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
92 |
#> IsodeflData.map (Thm.add_thm isodefl_thm) |
35479
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
93 |
#> MapIdData.map (Thm.add_thm map_ID_thm) |
dffffe36344a
store deflation thms for map functions in theory data
huffman
parents:
35478
diff
changeset
|
94 |
#> DeflMapData.map (Thm.add_thm defl_map_thm); |
33791 | 95 |
|
33801 | 96 |
val get_map_tab = MapData.get; |
97 |
||
33791 | 98 |
|
99 |
(******************************************************************************) |
|
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
100 |
(************************** building types and terms **************************) |
33774 | 101 |
(******************************************************************************) |
102 |
||
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
103 |
open HOLCF_Library; |
33774 | 104 |
|
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
105 |
infixr 6 ->>; |
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
106 |
infix -->>; |
33774 | 107 |
|
108 |
val deflT = @{typ "udom alg_defl"}; |
|
109 |
||
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
110 |
fun mapT (T as Type (_, Ts)) = |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
111 |
(map (fn T => T ->> T) Ts) -->> (T ->> T) |
35474 | 112 |
| mapT T = T ->> T; |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
113 |
|
33774 | 114 |
fun mk_Rep_of T = |
115 |
Const (@{const_name Rep_of}, Term.itselfT T --> deflT) $ Logic.mk_type T; |
|
116 |
||
33778 | 117 |
fun coerce_const T = Const (@{const_name coerce}, T); |
118 |
||
33788 | 119 |
fun isodefl_const T = |
120 |
Const (@{const_name isodefl}, (T ->> T) --> deflT --> HOLogic.boolT); |
|
121 |
||
35480
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
122 |
fun mk_deflation t = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
123 |
Const (@{const_name deflation}, Term.fastype_of t --> boolT) $ t; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
124 |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
125 |
(* 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
|
126 |
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
|
127 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
128 |
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
|
129 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
130 |
(******************************************************************************) |
35489 | 131 |
(****************************** isomorphism info ******************************) |
132 |
(******************************************************************************) |
|
133 |
||
134 |
type iso_info = |
|
135 |
{ |
|
136 |
absT : typ, |
|
137 |
repT : typ, |
|
138 |
abs_const : term, |
|
139 |
rep_const : term, |
|
140 |
abs_inverse : thm, |
|
141 |
rep_inverse : thm |
|
142 |
} |
|
143 |
||
144 |
fun deflation_abs_rep (info : iso_info) : thm = |
|
145 |
let |
|
146 |
val abs_iso = #abs_inverse info; |
|
147 |
val rep_iso = #rep_inverse info; |
|
148 |
val thm = @{thm deflation_abs_rep} OF [abs_iso, rep_iso]; |
|
149 |
in |
|
150 |
Drule.export_without_context thm |
|
151 |
end |
|
152 |
||
153 |
(******************************************************************************) |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
154 |
(*************** fixed-point definitions and unfolding theorems ***************) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
155 |
(******************************************************************************) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
156 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
157 |
fun add_fixdefs |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
158 |
(spec : (binding * term) list) |
33788 | 159 |
(thy : theory) : (thm list * thm list) * theory = |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
160 |
let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
161 |
val binds = map fst spec; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
162 |
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
|
163 |
val functional = lambda_tuple lhss (mk_tuple rhss); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
164 |
val fixpoint = mk_fix (mk_cabs functional); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
165 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
166 |
(* project components of fixpoint *) |
35474 | 167 |
fun mk_projs [] t = [] |
168 |
| mk_projs (x::[]) t = [(x, t)] |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
169 |
| 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
|
170 |
val projs = mk_projs lhss fixpoint; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
171 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
172 |
(* convert parameters to lambda abstractions *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
173 |
fun mk_eqn (lhs, rhs) = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
174 |
case lhs of |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
175 |
Const (@{const_name Rep_CFun}, _) $ f $ (x as Free _) => |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
176 |
mk_eqn (f, big_lambda x rhs) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
177 |
| Const _ => Logic.mk_equals (lhs, rhs) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
178 |
| _ => raise TERM ("lhs not of correct form", [lhs, rhs]); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
179 |
val eqns = map mk_eqn projs; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
180 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
181 |
(* register constant definitions *) |
33777 | 182 |
val (fixdef_thms, thy) = |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
183 |
(PureThy.add_defs false o map Thm.no_attributes) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
184 |
(map (Binding.suffix_name "_def") binds ~~ eqns) thy; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
185 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
186 |
(* prove applied version of definitions *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
187 |
fun prove_proj (lhs, rhs) = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
188 |
let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
189 |
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
|
190 |
val goal = Logic.mk_equals (lhs, rhs); |
33777 | 191 |
in Goal.prove_global thy [] [] goal (K tac) end; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
192 |
val proj_thms = map prove_proj projs; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
193 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
194 |
(* mk_tuple lhss == fixpoint *) |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
195 |
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
|
196 |
val tuple_fixdef_thm = foldr1 pair_equalI proj_thms; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
197 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
198 |
val cont_thm = |
33777 | 199 |
Goal.prove_global thy [] [] (mk_trp (mk_cont functional)) |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
200 |
(K (beta_tac 1)); |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
201 |
val tuple_unfold_thm = |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
202 |
(@{thm def_cont_fix_eq} OF [tuple_fixdef_thm, cont_thm]) |
33777 | 203 |
|> LocalDefs.unfold (ProofContext.init thy) @{thms split_conv}; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
204 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
205 |
fun mk_unfold_thms [] thm = [] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
206 |
| mk_unfold_thms (n::[]) thm = [(n, thm)] |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
207 |
| mk_unfold_thms (n::ns) thm = let |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
208 |
val thmL = thm RS @{thm Pair_eqD1}; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
209 |
val thmR = thm RS @{thm Pair_eqD2}; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
210 |
in (n, thmL) :: mk_unfold_thms ns thmR end; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
211 |
val unfold_binds = map (Binding.suffix_name "_unfold") binds; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
212 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
213 |
(* register unfold theorems *) |
33777 | 214 |
val (unfold_thms, thy) = |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
34149
diff
changeset
|
215 |
(PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.export_without_context)) |
33777 | 216 |
(mk_unfold_thms unfold_binds tuple_unfold_thm) thy; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
217 |
in |
33788 | 218 |
((proj_thms, unfold_thms), thy) |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
219 |
end; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
220 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
221 |
|
33774 | 222 |
(******************************************************************************) |
33790 | 223 |
(****************** deflation combinators and map functions *******************) |
224 |
(******************************************************************************) |
|
33774 | 225 |
|
226 |
fun defl_of_typ |
|
227 |
(tab : term Symtab.table) |
|
228 |
(T : typ) : term = |
|
229 |
let |
|
230 |
fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts |
|
231 |
| is_closed_typ _ = false; |
|
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
232 |
fun defl_of (TFree (a, _)) = Free (Library.unprefix "'" a, deflT) |
33774 | 233 |
| defl_of (TVar _) = error ("defl_of_typ: TVar") |
234 |
| defl_of (T as Type (c, Ts)) = |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
235 |
case Symtab.lookup tab c of |
35478 | 236 |
SOME t => list_ccomb (t, map defl_of Ts) |
33774 | 237 |
| NONE => if is_closed_typ T |
238 |
then mk_Rep_of T |
|
239 |
else error ("defl_of_typ: type variable under unsupported type constructor " ^ c); |
|
240 |
in defl_of T end; |
|
241 |
||
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
242 |
fun map_of_typ |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
243 |
(tab : string Symtab.table) |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
244 |
(T : typ) : term = |
33774 | 245 |
let |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
246 |
fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
247 |
| is_closed_typ _ = false; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
248 |
fun map_of (T as TFree (a, _)) = Free (Library.unprefix "'" a, T ->> T) |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
249 |
| map_of (T as TVar _) = error ("map_of_typ: TVar") |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
250 |
| map_of (T as Type (c, Ts)) = |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
251 |
case Symtab.lookup tab c of |
35478 | 252 |
SOME t => list_ccomb (Const (t, mapT T), map map_of Ts) |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
253 |
| NONE => if is_closed_typ T |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
254 |
then mk_ID T |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
255 |
else error ("map_of_typ: type variable under unsupported type constructor " ^ c); |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
256 |
in map_of T end; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
257 |
|
33774 | 258 |
|
259 |
(******************************************************************************) |
|
35477 | 260 |
(********************* declaring definitions and theorems *********************) |
261 |
(******************************************************************************) |
|
262 |
||
263 |
fun define_const |
|
264 |
(bind : binding, rhs : term) |
|
265 |
(thy : theory) |
|
266 |
: (term * thm) * theory = |
|
267 |
let |
|
268 |
val typ = Term.fastype_of rhs; |
|
269 |
val (const, thy) = Sign.declare_const ((bind, typ), NoSyn) thy; |
|
270 |
val eqn = Logic.mk_equals (const, rhs); |
|
271 |
val def = Thm.no_attributes (Binding.suffix_name "_def" bind, eqn); |
|
272 |
val (def_thm, thy) = yield_singleton (PureThy.add_defs false) def thy; |
|
273 |
in |
|
274 |
((const, def_thm), thy) |
|
275 |
end; |
|
276 |
||
35489 | 277 |
fun add_qualified_thm name (path, thm) thy = |
278 |
thy |
|
279 |
|> Sign.add_path path |
|
280 |
|> yield_singleton PureThy.add_thms |
|
281 |
(Thm.no_attributes (Binding.name name, thm)) |
|
282 |
||> Sign.parent_path; |
|
283 |
||
284 |
(******************************************************************************) |
|
285 |
(************************** defining take functions ***************************) |
|
286 |
(******************************************************************************) |
|
287 |
||
288 |
fun define_take_functions |
|
289 |
(spec : (binding * iso_info) list) |
|
290 |
(thy : theory) = |
|
291 |
let |
|
292 |
||
293 |
(* retrieve components of spec *) |
|
294 |
val dom_binds = map fst spec; |
|
295 |
val iso_infos = map snd spec; |
|
296 |
val dom_eqns = map (fn x => (#absT x, #repT x)) iso_infos; |
|
297 |
val rep_abs_consts = map (fn x => (#rep_const x, #abs_const x)) iso_infos; |
|
298 |
val dnames = map Binding.name_of dom_binds; |
|
299 |
||
300 |
(* get table of map functions *) |
|
301 |
val map_tab = MapData.get thy; |
|
302 |
||
303 |
fun mk_projs [] t = [] |
|
304 |
| mk_projs (x::[]) t = [(x, t)] |
|
305 |
| mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t); |
|
306 |
||
307 |
fun mk_cfcomp2 ((rep_const, abs_const), f) = |
|
308 |
mk_cfcomp (abs_const, mk_cfcomp (f, rep_const)); |
|
309 |
||
310 |
(* defining map functions over dtyps *) |
|
311 |
fun copy_of_dtyp recs (T, dt) = |
|
312 |
if Datatype_Aux.is_rec_type dt |
|
313 |
then copy_of_dtyp' recs (T, dt) |
|
314 |
else mk_ID T |
|
315 |
and copy_of_dtyp' recs (T, Datatype_Aux.DtRec i) = nth recs i |
|
316 |
| copy_of_dtyp' recs (T, Datatype_Aux.DtTFree a) = mk_ID T |
|
317 |
| copy_of_dtyp' recs (T, Datatype_Aux.DtType (c, ds)) = |
|
318 |
case Symtab.lookup map_tab c of |
|
319 |
SOME f => |
|
320 |
list_ccomb |
|
321 |
(Const (f, mapT T), |
|
322 |
map (copy_of_dtyp recs) (snd (dest_Type T) ~~ ds)) |
|
323 |
| NONE => |
|
324 |
(warning ("copy_of_dtyp: unknown type constructor " ^ c); mk_ID T); |
|
325 |
||
326 |
(* define take functional *) |
|
327 |
val new_dts : (string * string list) list = |
|
328 |
map (apsnd (map (fst o dest_TFree)) o dest_Type o fst) dom_eqns; |
|
329 |
val copy_arg_type = mk_tupleT (map (fn (T, _) => T ->> T) dom_eqns); |
|
330 |
val copy_arg = Free ("f", copy_arg_type); |
|
331 |
val copy_args = map snd (mk_projs dom_binds copy_arg); |
|
332 |
fun one_copy_rhs (rep_abs, (lhsT, rhsT)) = |
|
333 |
let |
|
334 |
val dtyp = Datatype_Aux.dtyp_of_typ new_dts rhsT; |
|
335 |
val body = copy_of_dtyp copy_args (rhsT, dtyp); |
|
336 |
in |
|
337 |
mk_cfcomp2 (rep_abs, body) |
|
338 |
end; |
|
339 |
val take_functional = |
|
340 |
big_lambda copy_arg |
|
341 |
(mk_tuple (map one_copy_rhs (rep_abs_consts ~~ dom_eqns))); |
|
342 |
val take_rhss = |
|
343 |
let |
|
344 |
val i = Free ("i", HOLogic.natT); |
|
345 |
val rhs = mk_iterate (i, take_functional) |
|
346 |
in |
|
347 |
map (Term.lambda i o snd) (mk_projs dom_binds rhs) |
|
348 |
end; |
|
349 |
||
350 |
(* define take constants *) |
|
351 |
fun define_take_const ((tbind, take_rhs), (lhsT, rhsT)) thy = |
|
352 |
let |
|
353 |
val take_type = HOLogic.natT --> lhsT ->> lhsT; |
|
354 |
val take_bind = Binding.suffix_name "_take" tbind; |
|
355 |
val (take_const, thy) = |
|
356 |
Sign.declare_const ((take_bind, take_type), NoSyn) thy; |
|
357 |
val take_eqn = Logic.mk_equals (take_const, take_rhs); |
|
358 |
val (take_def_thm, thy) = |
|
359 |
thy |
|
360 |
|> Sign.add_path (Binding.name_of tbind) |
|
361 |
|> yield_singleton |
|
362 |
(PureThy.add_defs false o map Thm.no_attributes) |
|
363 |
(Binding.name "take_def", take_eqn) |
|
364 |
||> Sign.parent_path; |
|
365 |
in ((take_const, take_def_thm), thy) end; |
|
366 |
val ((take_consts, take_defs), thy) = thy |
|
367 |
|> fold_map define_take_const (dom_binds ~~ take_rhss ~~ dom_eqns) |
|
368 |
|>> ListPair.unzip; |
|
369 |
||
370 |
(* prove chain_take lemmas *) |
|
371 |
fun prove_chain_take (take_const, dname) thy = |
|
372 |
let |
|
373 |
val goal = mk_trp (mk_chain take_const); |
|
374 |
val rules = take_defs @ @{thms chain_iterate ch2ch_fst ch2ch_snd}; |
|
375 |
val tac = simp_tac (HOL_basic_ss addsimps rules) 1; |
|
376 |
val chain_take_thm = Goal.prove_global thy [] [] goal (K tac); |
|
377 |
in |
|
378 |
add_qualified_thm "chain_take" (dname, chain_take_thm) thy |
|
379 |
end; |
|
380 |
val (chain_take_thms, thy) = |
|
381 |
fold_map prove_chain_take (take_consts ~~ dnames) thy; |
|
382 |
||
383 |
(* prove take_0 lemmas *) |
|
384 |
fun prove_take_0 ((take_const, dname), (lhsT, rhsT)) thy = |
|
385 |
let |
|
386 |
val lhs = take_const $ @{term "0::nat"}; |
|
387 |
val goal = mk_eqs (lhs, mk_bottom (lhsT ->> lhsT)); |
|
388 |
val rules = take_defs @ @{thms iterate_0 fst_strict snd_strict}; |
|
389 |
val tac = simp_tac (HOL_basic_ss addsimps rules) 1; |
|
390 |
val take_0_thm = Goal.prove_global thy [] [] goal (K tac); |
|
391 |
in |
|
392 |
add_qualified_thm "take_0" (dname, take_0_thm) thy |
|
393 |
end; |
|
394 |
val (take_0_thms, thy) = |
|
395 |
fold_map prove_take_0 (take_consts ~~ dnames ~~ dom_eqns) thy; |
|
396 |
||
397 |
(* prove take_Suc lemmas *) |
|
398 |
val i = Free ("i", natT); |
|
399 |
val take_is = map (fn t => t $ i) take_consts; |
|
400 |
fun prove_take_Suc |
|
401 |
(((take_const, rep_abs), dname), (lhsT, rhsT)) thy = |
|
402 |
let |
|
403 |
val lhs = take_const $ (@{term Suc} $ i); |
|
404 |
val dtyp = Datatype_Aux.dtyp_of_typ new_dts rhsT; |
|
405 |
val body = copy_of_dtyp take_is (rhsT, dtyp); |
|
406 |
val rhs = mk_cfcomp2 (rep_abs, body); |
|
407 |
val goal = mk_eqs (lhs, rhs); |
|
408 |
val simps = @{thms iterate_Suc fst_conv snd_conv} |
|
409 |
val rules = take_defs @ simps; |
|
410 |
val tac = simp_tac (beta_ss addsimps rules) 1; |
|
411 |
val take_Suc_thm = Goal.prove_global thy [] [] goal (K tac); |
|
412 |
in |
|
413 |
add_qualified_thm "take_Suc" (dname, take_Suc_thm) thy |
|
414 |
end; |
|
415 |
val (take_Suc_thms, thy) = |
|
416 |
fold_map prove_take_Suc |
|
417 |
(take_consts ~~ rep_abs_consts ~~ dnames ~~ dom_eqns) thy; |
|
418 |
||
419 |
(* prove deflation theorems for take functions *) |
|
420 |
val deflation_abs_rep_thms = map deflation_abs_rep iso_infos; |
|
421 |
val deflation_take_thm = |
|
422 |
let |
|
423 |
val i = Free ("i", natT); |
|
424 |
fun mk_goal take_const = mk_deflation (take_const $ i); |
|
425 |
val goal = mk_trp (foldr1 mk_conj (map mk_goal take_consts)); |
|
426 |
val adm_rules = |
|
427 |
@{thms adm_conj adm_deflation cont2cont_fst cont2cont_snd cont_id}; |
|
428 |
val bottom_rules = |
|
429 |
take_0_thms @ @{thms deflation_UU simp_thms}; |
|
430 |
val deflation_rules = |
|
431 |
@{thms conjI deflation_ID} |
|
432 |
@ deflation_abs_rep_thms |
|
433 |
@ DeflMapData.get thy; |
|
434 |
in |
|
435 |
Goal.prove_global thy [] [] goal (fn _ => |
|
436 |
EVERY |
|
437 |
[rtac @{thm nat.induct} 1, |
|
438 |
simp_tac (HOL_basic_ss addsimps bottom_rules) 1, |
|
439 |
simp_tac (HOL_basic_ss addsimps take_Suc_thms) 1, |
|
440 |
REPEAT (resolve_tac deflation_rules 1 ORELSE atac 1)]) |
|
441 |
end; |
|
442 |
fun conjuncts [] thm = [] |
|
443 |
| conjuncts (n::[]) thm = [(n, thm)] |
|
444 |
| conjuncts (n::ns) thm = let |
|
445 |
val thmL = thm RS @{thm conjunct1}; |
|
446 |
val thmR = thm RS @{thm conjunct2}; |
|
447 |
in (n, thmL):: conjuncts ns thmR end; |
|
448 |
val (deflation_take_thms, thy) = |
|
449 |
fold_map (add_qualified_thm "deflation_take") |
|
450 |
(map (apsnd Drule.export_without_context) |
|
451 |
(conjuncts dnames deflation_take_thm)) thy; |
|
452 |
||
453 |
(* prove strictness of take functions *) |
|
454 |
fun prove_take_strict (take_const, dname) thy = |
|
455 |
let |
|
456 |
val goal = mk_trp (mk_strict (take_const $ Free ("i", natT))); |
|
457 |
val tac = rtac @{thm deflation_strict} 1 |
|
458 |
THEN resolve_tac deflation_take_thms 1; |
|
459 |
val take_strict_thm = Goal.prove_global thy [] [] goal (K tac); |
|
460 |
in |
|
461 |
add_qualified_thm "take_strict" (dname, take_strict_thm) thy |
|
462 |
end; |
|
463 |
val (take_strict_thms, thy) = |
|
464 |
fold_map prove_take_strict (take_consts ~~ dnames) thy; |
|
465 |
||
35490 | 466 |
(* prove take/take rules *) |
467 |
fun prove_take_take ((chain_take, deflation_take), dname) thy = |
|
468 |
let |
|
469 |
val take_take_thm = |
|
470 |
@{thm deflation_chain_min} OF [chain_take, deflation_take]; |
|
471 |
in |
|
472 |
add_qualified_thm "take_take" (dname, take_take_thm) thy |
|
473 |
end; |
|
474 |
val (take_take_thms, thy) = |
|
475 |
fold_map prove_take_take |
|
476 |
(chain_take_thms ~~ deflation_take_thms ~~ dnames) thy; |
|
477 |
||
35489 | 478 |
val result = |
479 |
{ |
|
480 |
take_consts = take_consts, |
|
481 |
take_defs = take_defs, |
|
482 |
chain_take_thms = chain_take_thms, |
|
483 |
take_0_thms = take_0_thms, |
|
484 |
take_Suc_thms = take_Suc_thms, |
|
485 |
deflation_take_thms = deflation_take_thms |
|
486 |
}; |
|
487 |
||
488 |
in |
|
489 |
(result, thy) |
|
490 |
end; |
|
491 |
||
35477 | 492 |
(******************************************************************************) |
493 |
(******************************* main function ********************************) |
|
494 |
(******************************************************************************) |
|
33774 | 495 |
|
496 |
fun read_typ thy str sorts = |
|
497 |
let |
|
498 |
val ctxt = ProofContext.init thy |
|
499 |
|> fold (Variable.declare_typ o TFree) sorts; |
|
500 |
val T = Syntax.read_typ ctxt str; |
|
501 |
in (T, Term.add_tfreesT T sorts) end; |
|
502 |
||
503 |
fun cert_typ sign raw_T sorts = |
|
504 |
let |
|
505 |
val T = Type.no_tvars (Sign.certify_typ sign raw_T) |
|
506 |
handle TYPE (msg, _, _) => error msg; |
|
507 |
val sorts' = Term.add_tfreesT T sorts; |
|
508 |
val _ = |
|
509 |
case duplicates (op =) (map fst sorts') of |
|
510 |
[] => () |
|
511 |
| dups => error ("Inconsistent sort constraints for " ^ commas dups) |
|
512 |
in (T, sorts') end; |
|
513 |
||
514 |
fun gen_domain_isomorphism |
|
515 |
(prep_typ: theory -> 'a -> (string * sort) list -> typ * (string * sort) list) |
|
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
516 |
(doms_raw: (string list * binding * mixfix * 'a * (binding * binding) option) list) |
33774 | 517 |
(thy: theory) |
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
518 |
: iso_info list * theory = |
33774 | 519 |
let |
33792 | 520 |
val _ = Theory.requires thy "Representable" "domain isomorphisms"; |
33774 | 521 |
|
522 |
(* this theory is used just for parsing *) |
|
523 |
val tmp_thy = thy |> |
|
524 |
Theory.copy |> |
|
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
525 |
Sign.add_types (map (fn (tvs, tname, mx, _, morphs) => |
33774 | 526 |
(tname, length tvs, mx)) doms_raw); |
527 |
||
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
528 |
fun prep_dom thy (vs, t, mx, typ_raw, morphs) sorts = |
33774 | 529 |
let val (typ, sorts') = prep_typ thy typ_raw sorts |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
530 |
in ((vs, t, mx, typ, morphs), sorts') end; |
33774 | 531 |
|
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
532 |
val (doms : (string list * binding * mixfix * typ * (binding * binding) option) list, |
33774 | 533 |
sorts : (string * sort) list) = |
534 |
fold_map (prep_dom tmp_thy) doms_raw []; |
|
535 |
||
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
536 |
(* domain equations *) |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
537 |
fun mk_dom_eqn (vs, tbind, mx, rhs, morphs) = |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
538 |
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
|
539 |
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
|
540 |
val dom_eqns = map mk_dom_eqn doms; |
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
541 |
|
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
542 |
(* check for valid type parameters *) |
35474 | 543 |
val (tyvars, _, _, _, _) = hd doms; |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
544 |
val new_doms = map (fn (tvs, tname, mx, _, _) => |
33774 | 545 |
let val full_tname = Sign.full_name tmp_thy tname |
546 |
in |
|
547 |
(case duplicates (op =) tvs of |
|
548 |
[] => |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
549 |
if eq_set (op =) (tyvars, tvs) then (full_tname, tvs) |
33774 | 550 |
else error ("Mutually recursive domains must have same type parameters") |
551 |
| dups => error ("Duplicate parameter(s) for domain " ^ quote (Binding.str_of tname) ^ |
|
552 |
" : " ^ commas dups)) |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
553 |
end) doms; |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
554 |
val dom_binds = map (fn (_, tbind, _, _, _) => tbind) doms; |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
555 |
val morphs = map (fn (_, _, _, _, morphs) => morphs) doms; |
33774 | 556 |
|
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
557 |
(* declare deflation combinator constants *) |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
558 |
fun declare_defl_const (vs, tbind, mx, rhs, morphs) thy = |
33774 | 559 |
let |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
560 |
val defl_type = map (K deflT) vs -->> deflT; |
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
561 |
val defl_bind = Binding.suffix_name "_defl" tbind; |
33774 | 562 |
in |
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
563 |
Sign.declare_const ((defl_bind, defl_type), NoSyn) thy |
33774 | 564 |
end; |
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
565 |
val (defl_consts, thy) = fold_map declare_defl_const doms thy; |
33774 | 566 |
|
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
567 |
(* defining equations for type combinators *) |
33790 | 568 |
val defl_tab1 = DeflData.get thy; |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
569 |
val defl_tab2 = |
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
570 |
Symtab.make (map (fst o dest_Type o fst) dom_eqns ~~ defl_consts); |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
571 |
val defl_tab' = Symtab.merge (K true) (defl_tab1, defl_tab2); |
33790 | 572 |
val thy = DeflData.put defl_tab' thy; |
33776 | 573 |
fun mk_defl_spec (lhsT, rhsT) = |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
574 |
mk_eqs (defl_of_typ defl_tab' lhsT, |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
575 |
defl_of_typ defl_tab' rhsT); |
33775
7a1518c42c56
cleaned up; factored out fixed-point definition code
huffman
parents:
33774
diff
changeset
|
576 |
val defl_specs = map mk_defl_spec dom_eqns; |
33774 | 577 |
|
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
578 |
(* register recursive definition of deflation combinators *) |
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
579 |
val defl_binds = map (Binding.suffix_name "_defl") dom_binds; |
33788 | 580 |
val ((defl_apply_thms, defl_unfold_thms), thy) = |
581 |
add_fixdefs (defl_binds ~~ defl_specs) thy; |
|
33774 | 582 |
|
33776 | 583 |
(* define types using deflation combinators *) |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
584 |
fun make_repdef ((vs, tbind, mx, _, _), defl_const) thy = |
33774 | 585 |
let |
586 |
fun tfree a = TFree (a, the (AList.lookup (op =) sorts a)) |
|
587 |
val reps = map (mk_Rep_of o tfree) vs; |
|
35478 | 588 |
val defl = list_ccomb (defl_const, reps); |
33777 | 589 |
val ((_, _, _, {REP, ...}), thy) = |
33774 | 590 |
Repdef.add_repdef false NONE (tbind, vs, mx) defl NONE thy; |
591 |
in |
|
33777 | 592 |
(REP, thy) |
33774 | 593 |
end; |
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
594 |
val (REP_thms, thy) = fold_map make_repdef (doms ~~ defl_consts) thy; |
33791 | 595 |
val thy = RepData.map (fold Thm.add_thm REP_thms) thy; |
33776 | 596 |
|
597 |
(* prove REP equations *) |
|
33782 | 598 |
fun mk_REP_eq_thm (lhsT, rhsT) = |
33776 | 599 |
let |
600 |
val goal = mk_eqs (mk_Rep_of lhsT, mk_Rep_of rhsT); |
|
33791 | 601 |
val REP_simps = RepData.get thy; |
33776 | 602 |
val tac = |
603 |
simp_tac (HOL_basic_ss addsimps REP_simps) 1 |
|
33784
7e434813752f
change naming convention for deflation combinators
huffman
parents:
33782
diff
changeset
|
604 |
THEN resolve_tac defl_unfold_thms 1; |
33776 | 605 |
in |
33777 | 606 |
Goal.prove_global thy [] [] goal (K tac) |
33776 | 607 |
end; |
33782 | 608 |
val REP_eq_thms = map mk_REP_eq_thm dom_eqns; |
609 |
||
610 |
(* register REP equations *) |
|
611 |
val REP_eq_binds = map (Binding.prefix_name "REP_eq_") dom_binds; |
|
612 |
val (_, thy) = thy |> |
|
613 |
(PureThy.add_thms o map Thm.no_attributes) |
|
614 |
(REP_eq_binds ~~ REP_eq_thms); |
|
33776 | 615 |
|
33778 | 616 |
(* define rep/abs functions *) |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
617 |
fun mk_rep_abs ((tbind, morphs), (lhsT, rhsT)) thy = |
33778 | 618 |
let |
619 |
val rep_bind = Binding.suffix_name "_rep" tbind; |
|
620 |
val abs_bind = Binding.suffix_name "_abs" tbind; |
|
35477 | 621 |
val ((rep_const, rep_def), thy) = |
622 |
define_const (rep_bind, coerce_const (lhsT ->> rhsT)) thy; |
|
623 |
val ((abs_const, abs_def), thy) = |
|
624 |
define_const (abs_bind, coerce_const (rhsT ->> lhsT)) thy; |
|
33778 | 625 |
in |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
626 |
(((rep_const, abs_const), (rep_def, abs_def)), thy) |
33778 | 627 |
end; |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
628 |
val ((rep_abs_consts, rep_abs_defs), thy) = thy |
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
629 |
|> fold_map mk_rep_abs (dom_binds ~~ morphs ~~ dom_eqns) |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
630 |
|>> ListPair.unzip; |
33782 | 631 |
|
632 |
(* prove isomorphism and isodefl rules *) |
|
633 |
fun mk_iso_thms ((tbind, REP_eq), (rep_def, abs_def)) thy = |
|
634 |
let |
|
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
34149
diff
changeset
|
635 |
fun make thm = Drule.export_without_context (thm OF [REP_eq, abs_def, rep_def]); |
33782 | 636 |
val rep_iso_thm = make @{thm domain_rep_iso}; |
637 |
val abs_iso_thm = make @{thm domain_abs_iso}; |
|
638 |
val isodefl_thm = make @{thm isodefl_abs_rep}; |
|
33797 | 639 |
val rep_iso_bind = Binding.name "rep_iso"; |
640 |
val abs_iso_bind = Binding.name "abs_iso"; |
|
641 |
val isodefl_bind = Binding.name "isodefl_abs_rep"; |
|
642 |
val (_, thy) = thy |
|
643 |
|> Sign.add_path (Binding.name_of tbind) |
|
644 |
|> (PureThy.add_thms o map Thm.no_attributes) |
|
645 |
[(rep_iso_bind, rep_iso_thm), |
|
646 |
(abs_iso_bind, abs_iso_thm), |
|
647 |
(isodefl_bind, isodefl_thm)] |
|
648 |
||> Sign.parent_path; |
|
33782 | 649 |
in |
35489 | 650 |
(((rep_iso_thm, abs_iso_thm), isodefl_thm), thy) |
33782 | 651 |
end; |
35489 | 652 |
val ((iso_thms, isodefl_abs_rep_thms), thy) = |
35480
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
653 |
thy |
33782 | 654 |
|> fold_map mk_iso_thms (dom_binds ~~ REP_eq_thms ~~ rep_abs_defs) |
35489 | 655 |
|>> ListPair.unzip; |
33778 | 656 |
|
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
657 |
(* collect info about rep/abs *) |
35489 | 658 |
val iso_infos : iso_info list = |
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
659 |
let |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
660 |
fun mk_info (((lhsT, rhsT), (repC, absC)), (rep_iso, abs_iso)) = |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
661 |
{ |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
662 |
repT = rhsT, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
663 |
absT = lhsT, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
664 |
rep_const = repC, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
665 |
abs_const = absC, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
666 |
rep_inverse = rep_iso, |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
667 |
abs_inverse = abs_iso |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
668 |
}; |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
669 |
in |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
670 |
map mk_info (dom_eqns ~~ rep_abs_consts ~~ iso_thms) |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
671 |
end |
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
672 |
|
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
673 |
(* declare map functions *) |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
674 |
fun declare_map_const (tbind, (lhsT, rhsT)) thy = |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
675 |
let |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
676 |
val map_type = mapT lhsT; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
677 |
val map_bind = Binding.suffix_name "_map" tbind; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
678 |
in |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
679 |
Sign.declare_const ((map_bind, map_type), NoSyn) thy |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
680 |
end; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
681 |
val (map_consts, thy) = thy |> |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
682 |
fold_map declare_map_const (dom_binds ~~ dom_eqns); |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
683 |
|
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
684 |
(* defining equations for map functions *) |
33790 | 685 |
val map_tab1 = MapData.get thy; |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
686 |
val map_tab2 = |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
687 |
Symtab.make (map (fst o dest_Type o fst) dom_eqns |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
688 |
~~ map (fst o dest_Const) map_consts); |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
689 |
val map_tab' = Symtab.merge (K true) (map_tab1, map_tab2); |
33790 | 690 |
val thy = MapData.put map_tab' thy; |
33785
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
691 |
fun mk_map_spec ((rep_const, abs_const), (lhsT, rhsT)) = |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
692 |
let |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
693 |
val lhs = map_of_typ map_tab' lhsT; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
694 |
val body = map_of_typ map_tab' rhsT; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
695 |
val rhs = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const)); |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
696 |
in mk_eqs (lhs, rhs) end; |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
697 |
val map_specs = map mk_map_spec (rep_abs_consts ~~ dom_eqns); |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
698 |
|
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
699 |
(* register recursive definition of map functions *) |
2f2d9eb37084
automate definition of map functions; remove unused code
huffman
parents:
33784
diff
changeset
|
700 |
val map_binds = map (Binding.suffix_name "_map") dom_binds; |
33788 | 701 |
val ((map_apply_thms, map_unfold_thms), thy) = |
702 |
add_fixdefs (map_binds ~~ map_specs) thy; |
|
703 |
||
704 |
(* prove isodefl rules for map functions *) |
|
705 |
val isodefl_thm = |
|
706 |
let |
|
707 |
fun unprime a = Library.unprefix "'" a; |
|
35474 | 708 |
fun mk_d T = Free ("d" ^ unprime (fst (dest_TFree T)), deflT); |
709 |
fun mk_f T = Free ("f" ^ unprime (fst (dest_TFree T)), T ->> T); |
|
33788 | 710 |
fun mk_assm T = mk_trp (isodefl_const T $ mk_f T $ mk_d T); |
35474 | 711 |
fun mk_goal ((map_const, defl_const), (T, rhsT)) = |
33788 | 712 |
let |
35474 | 713 |
val (_, Ts) = dest_Type T; |
35478 | 714 |
val map_term = list_ccomb (map_const, map mk_f Ts); |
715 |
val defl_term = list_ccomb (defl_const, map mk_d Ts); |
|
33788 | 716 |
in isodefl_const T $ map_term $ defl_term end; |
717 |
val assms = (map mk_assm o snd o dest_Type o fst o hd) dom_eqns; |
|
718 |
val goals = map mk_goal (map_consts ~~ defl_consts ~~ dom_eqns); |
|
719 |
val goal = mk_trp (foldr1 HOLogic.mk_conj goals); |
|
720 |
val start_thms = |
|
721 |
@{thm split_def} :: defl_apply_thms @ map_apply_thms; |
|
722 |
val adm_rules = |
|
723 |
@{thms adm_conj adm_isodefl cont2cont_fst cont2cont_snd cont_id}; |
|
724 |
val bottom_rules = |
|
725 |
@{thms fst_strict snd_strict isodefl_bottom simp_thms}; |
|
726 |
val isodefl_rules = |
|
33791 | 727 |
@{thms conjI isodefl_ID_REP} |
728 |
@ isodefl_abs_rep_thms |
|
729 |
@ IsodeflData.get thy; |
|
33845 | 730 |
in |
731 |
Goal.prove_global thy [] assms goal (fn {prems, ...} => |
|
732 |
EVERY |
|
33788 | 733 |
[simp_tac (HOL_basic_ss addsimps start_thms) 1, |
33791 | 734 |
(* FIXME: how reliable is unification here? *) |
735 |
(* Maybe I should instantiate the rule. *) |
|
33788 | 736 |
rtac @{thm parallel_fix_ind} 1, |
737 |
REPEAT (resolve_tac adm_rules 1), |
|
738 |
simp_tac (HOL_basic_ss addsimps bottom_rules) 1, |
|
739 |
simp_tac beta_ss 1, |
|
740 |
simp_tac (HOL_basic_ss addsimps @{thms fst_conv snd_conv}) 1, |
|
741 |
REPEAT (etac @{thm conjE} 1), |
|
33845 | 742 |
REPEAT (resolve_tac (isodefl_rules @ prems) 1 ORELSE atac 1)]) |
33788 | 743 |
end; |
33789 | 744 |
val isodefl_binds = map (Binding.prefix_name "isodefl_") dom_binds; |
745 |
fun conjuncts [] thm = [] |
|
746 |
| conjuncts (n::[]) thm = [(n, thm)] |
|
747 |
| conjuncts (n::ns) thm = let |
|
748 |
val thmL = thm RS @{thm conjunct1}; |
|
749 |
val thmR = thm RS @{thm conjunct2}; |
|
750 |
in (n, thmL):: conjuncts ns thmR end; |
|
751 |
val (isodefl_thms, thy) = thy |> |
|
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
34149
diff
changeset
|
752 |
(PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.export_without_context)) |
33789 | 753 |
(conjuncts isodefl_binds isodefl_thm); |
33791 | 754 |
val thy = IsodeflData.map (fold Thm.add_thm isodefl_thms) thy; |
33793 | 755 |
|
756 |
(* prove map_ID theorems *) |
|
757 |
fun prove_map_ID_thm |
|
758 |
(((map_const, (lhsT, _)), REP_thm), isodefl_thm) = |
|
759 |
let |
|
760 |
val Ts = snd (dest_Type lhsT); |
|
35478 | 761 |
val lhs = list_ccomb (map_const, map mk_ID Ts); |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
762 |
val goal = mk_eqs (lhs, mk_ID lhsT); |
33793 | 763 |
val tac = EVERY |
764 |
[rtac @{thm isodefl_REP_imp_ID} 1, |
|
765 |
stac REP_thm 1, |
|
766 |
rtac isodefl_thm 1, |
|
767 |
REPEAT (rtac @{thm isodefl_ID_REP} 1)]; |
|
768 |
in |
|
769 |
Goal.prove_global thy [] [] goal (K tac) |
|
770 |
end; |
|
771 |
val map_ID_binds = map (Binding.suffix_name "_map_ID") dom_binds; |
|
772 |
val map_ID_thms = |
|
773 |
map prove_map_ID_thm |
|
774 |
(map_consts ~~ dom_eqns ~~ REP_thms ~~ isodefl_thms); |
|
775 |
val (_, thy) = thy |> |
|
776 |
(PureThy.add_thms o map Thm.no_attributes) |
|
777 |
(map_ID_binds ~~ map_ID_thms); |
|
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
778 |
val thy = MapIdData.map (fold Thm.add_thm map_ID_thms) thy; |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
779 |
|
35480
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
780 |
(* prove deflation theorems for map functions *) |
35489 | 781 |
val deflation_abs_rep_thms = map deflation_abs_rep iso_infos; |
35480
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
782 |
val deflation_map_thm = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
783 |
let |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
784 |
fun unprime a = Library.unprefix "'" a; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
785 |
fun mk_f T = Free (unprime (fst (dest_TFree T)), T ->> T); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
786 |
fun mk_assm T = mk_trp (mk_deflation (mk_f T)); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
787 |
fun mk_goal (map_const, (lhsT, rhsT)) = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
788 |
let |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
789 |
val (_, Ts) = dest_Type lhsT; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
790 |
val map_term = list_ccomb (map_const, map mk_f Ts); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
791 |
in mk_deflation map_term end; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
792 |
val assms = (map mk_assm o snd o dest_Type o fst o hd) dom_eqns; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
793 |
val goals = map mk_goal (map_consts ~~ dom_eqns); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
794 |
val goal = mk_trp (foldr1 HOLogic.mk_conj goals); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
795 |
val start_thms = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
796 |
@{thm split_def} :: map_apply_thms; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
797 |
val adm_rules = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
798 |
@{thms adm_conj adm_deflation cont2cont_fst cont2cont_snd cont_id}; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
799 |
val bottom_rules = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
800 |
@{thms fst_strict snd_strict deflation_UU simp_thms}; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
801 |
val deflation_rules = |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
802 |
@{thms conjI deflation_ID} |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
803 |
@ deflation_abs_rep_thms |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
804 |
@ DeflMapData.get thy; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
805 |
in |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
806 |
Goal.prove_global thy [] assms goal (fn {prems, ...} => |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
807 |
EVERY |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
808 |
[simp_tac (HOL_basic_ss addsimps start_thms) 1, |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
809 |
rtac @{thm fix_ind} 1, |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
810 |
REPEAT (resolve_tac adm_rules 1), |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
811 |
simp_tac (HOL_basic_ss addsimps bottom_rules) 1, |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
812 |
simp_tac beta_ss 1, |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
813 |
simp_tac (HOL_basic_ss addsimps @{thms fst_conv snd_conv}) 1, |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
814 |
REPEAT (etac @{thm conjE} 1), |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
815 |
REPEAT (resolve_tac (deflation_rules @ prems) 1 ORELSE atac 1)]) |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
816 |
end; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
817 |
val deflation_map_binds = dom_binds |> |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
818 |
map (Binding.prefix_name "deflation_" o Binding.suffix_name "_map"); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
819 |
val (deflation_map_thms, thy) = thy |> |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
820 |
(PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.export_without_context)) |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
821 |
(conjuncts deflation_map_binds deflation_map_thm); |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
822 |
val thy = DeflMapData.map (fold Thm.add_thm deflation_map_thms) thy; |
7a1f285cad25
domain_isomorphism package proves deflation rules for map functions
huffman
parents:
35479
diff
changeset
|
823 |
|
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
824 |
(* define copy combinators *) |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
825 |
val new_dts = |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
826 |
map (apsnd (map (fst o dest_TFree)) o dest_Type o fst) dom_eqns; |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
827 |
val copy_arg_type = mk_tupleT (map (fn (T, _) => T ->> T) dom_eqns); |
33807
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
828 |
val copy_arg = Free ("f", copy_arg_type); |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
829 |
val copy_args = |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
830 |
let fun mk_copy_args [] t = [] |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
831 |
| mk_copy_args (_::[]) t = [t] |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
832 |
| mk_copy_args (_::xs) t = |
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
833 |
mk_fst t :: mk_copy_args xs (mk_snd t); |
33807
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
834 |
in mk_copy_args doms copy_arg end; |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
835 |
fun copy_of_dtyp (T, dt) = |
33971 | 836 |
if Datatype_Aux.is_rec_type dt |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
837 |
then copy_of_dtyp' (T, dt) |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
838 |
else mk_ID T |
33971 | 839 |
and copy_of_dtyp' (T, Datatype_Aux.DtRec i) = nth copy_args i |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
840 |
| copy_of_dtyp' (T, Datatype_Aux.DtTFree a) = mk_ID T |
35474 | 841 |
| copy_of_dtyp' (T, Datatype_Aux.DtType (c, ds)) = |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
842 |
case Symtab.lookup map_tab' c of |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
843 |
SOME f => |
35478 | 844 |
list_ccomb |
35474 | 845 |
(Const (f, mapT T), map copy_of_dtyp (snd (dest_Type T) ~~ ds)) |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
846 |
| NONE => |
35475
979019ab92eb
move common functions into new file holcf_library.ML
huffman
parents:
35474
diff
changeset
|
847 |
(warning ("copy_of_dtyp: unknown type constructor " ^ c); mk_ID T); |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
848 |
fun define_copy ((tbind, (rep_const, abs_const)), (lhsT, rhsT)) thy = |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
849 |
let |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
850 |
val copy_type = copy_arg_type ->> (lhsT ->> lhsT); |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
851 |
val copy_bind = Binding.suffix_name "_copy" tbind; |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
852 |
val (copy_const, thy) = thy |> |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
853 |
Sign.declare_const ((copy_bind, copy_type), NoSyn); |
33971 | 854 |
val dtyp = Datatype_Aux.dtyp_of_typ new_dts rhsT; |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
855 |
val body = copy_of_dtyp (rhsT, dtyp); |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
856 |
val comp = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const)); |
33807
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
857 |
val rhs = big_lambda copy_arg comp; |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
858 |
val eqn = Logic.mk_equals (copy_const, rhs); |
35474 | 859 |
val (copy_def, thy) = |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
860 |
thy |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
861 |
|> Sign.add_path (Binding.name_of tbind) |
35474 | 862 |
|> yield_singleton (PureThy.add_defs false o map Thm.no_attributes) |
863 |
(Binding.name "copy_def", eqn) |
|
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
864 |
||> Sign.parent_path; |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
865 |
in ((copy_const, copy_def), thy) end; |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
866 |
val ((copy_consts, copy_defs), thy) = thy |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
867 |
|> fold_map define_copy (dom_binds ~~ rep_abs_consts ~~ dom_eqns) |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
868 |
|>> ListPair.unzip; |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33801
diff
changeset
|
869 |
|
33807
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
870 |
(* define combined copy combinator *) |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
871 |
val ((c_const, c_def_thms), thy) = |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
872 |
if length doms = 1 |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
873 |
then ((hd copy_consts, []), thy) |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
874 |
else |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
875 |
let |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
876 |
val c_type = copy_arg_type ->> copy_arg_type; |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
877 |
val c_name = space_implode "_" (map Binding.name_of dom_binds); |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
878 |
val c_bind = Binding.name (c_name ^ "_copy"); |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
879 |
val c_body = |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
880 |
mk_tuple (map (mk_capply o rpair copy_arg) copy_consts); |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
881 |
val c_rhs = big_lambda copy_arg c_body; |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
882 |
val (c_const, thy) = |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
883 |
Sign.declare_const ((c_bind, c_type), NoSyn) thy; |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
884 |
val c_eqn = Logic.mk_equals (c_const, c_rhs); |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
885 |
val (c_def_thms, thy) = |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
886 |
thy |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
887 |
|> Sign.add_path c_name |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
888 |
|> (PureThy.add_defs false o map Thm.no_attributes) |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
889 |
[(Binding.name "copy_def", c_eqn)] |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
890 |
||> Sign.parent_path; |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
891 |
in ((c_const, c_def_thms), thy) end; |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
892 |
|
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
893 |
(* fixed-point lemma for combined copy combinator *) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
894 |
val fix_copy_lemma = |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
895 |
let |
35474 | 896 |
fun mk_map_ID (map_const, (T, rhsT)) = |
35478 | 897 |
list_ccomb (map_const, map mk_ID (snd (dest_Type T))); |
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
898 |
val rhs = mk_tuple (map mk_map_ID (map_consts ~~ dom_eqns)); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
899 |
val goal = mk_eqs (mk_fix c_const, rhs); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
900 |
val rules = |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
901 |
[@{thm pair_collapse}, @{thm split_def}] |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
902 |
@ map_apply_thms |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
903 |
@ c_def_thms @ copy_defs |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
904 |
@ MapIdData.get thy; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
905 |
val tac = simp_tac (beta_ss addsimps rules) 1; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
906 |
in |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
907 |
Goal.prove_global thy [] [] goal (K tac) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
908 |
end; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
909 |
|
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
910 |
(* prove reach lemmas *) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
911 |
val reach_thm_projs = |
35474 | 912 |
let fun mk_projs [] t = [] |
913 |
| mk_projs (x::[]) t = [(x, t)] |
|
33809
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
914 |
| mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
915 |
in mk_projs dom_binds (mk_fix c_const) end; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
916 |
fun prove_reach_thm (((bind, t), map_ID_thm), (lhsT, rhsT)) thy = |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
917 |
let |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
918 |
val x = Free ("x", lhsT); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
919 |
val goal = mk_eqs (mk_capply (t, x), x); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
920 |
val rules = |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
921 |
fix_copy_lemma :: map_ID_thm :: @{thms fst_conv snd_conv ID1}; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
922 |
val tac = simp_tac (HOL_basic_ss addsimps rules) 1; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
923 |
val reach_thm = Goal.prove_global thy [] [] goal (K tac); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
924 |
in |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
925 |
thy |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
926 |
|> Sign.add_path (Binding.name_of bind) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
927 |
|> yield_singleton (PureThy.add_thms o map Thm.no_attributes) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
928 |
(Binding.name "reach", reach_thm) |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
929 |
||> Sign.parent_path |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
930 |
end; |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
931 |
val (reach_thms, thy) = thy |> |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
932 |
fold_map prove_reach_thm (reach_thm_projs ~~ map_ID_thms ~~ dom_eqns); |
033831fd9ef3
store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents:
33807
diff
changeset
|
933 |
|
33774 | 934 |
in |
35489 | 935 |
(iso_infos, thy) |
33774 | 936 |
end; |
937 |
||
938 |
val domain_isomorphism = gen_domain_isomorphism cert_typ; |
|
35467
561d8e98d9d3
domain_isomorphism function returns iso_info record
huffman
parents:
35021
diff
changeset
|
939 |
val domain_isomorphism_cmd = snd oo gen_domain_isomorphism read_typ; |
33774 | 940 |
|
941 |
(******************************************************************************) |
|
942 |
(******************************** outer syntax ********************************) |
|
943 |
(******************************************************************************) |
|
944 |
||
945 |
local |
|
946 |
||
947 |
structure P = OuterParse and K = OuterKeyword |
|
948 |
||
34149
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
949 |
val parse_domain_iso : |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
950 |
(string list * binding * mixfix * string * (binding * binding) option) |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
951 |
parser = |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
952 |
(P.type_args -- P.binding -- P.opt_infix -- (P.$$$ "=" |-- P.typ) -- |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
953 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding))) |
a0efb4754cb7
add 'morphisms' option to domain_isomorphism command
huffman
parents:
33971
diff
changeset
|
954 |
>> (fn ((((vs, t), mx), rhs), morphs) => (vs, t, mx, rhs, morphs)); |
33774 | 955 |
|
956 |
val parse_domain_isos = P.and_list1 parse_domain_iso; |
|
957 |
||
958 |
in |
|
959 |
||
960 |
val _ = |
|
961 |
OuterSyntax.command "domain_isomorphism" "define domain isomorphisms (HOLCF)" K.thy_decl |
|
962 |
(parse_domain_isos >> (Toplevel.theory o domain_isomorphism_cmd)); |
|
963 |
||
964 |
end; |
|
965 |
||
966 |
end; |