| author | huffman | 
| Thu, 19 Nov 2009 10:45:34 -0800 | |
| changeset 33789 | c3fbdff7aed0 | 
| parent 33788 | 481bc899febf | 
| child 33790 | b2ff505e30f8 | 
| 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: 
33774diff
changeset | 18 | val beta_ss = | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 19 | HOL_basic_ss | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 20 | addsimps simp_thms | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 21 |     addsimps [@{thm beta_cfun}]
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 22 |     addsimprocs [@{simproc cont_proc}];
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 23 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 24 | val beta_tac = simp_tac beta_ss; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
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 | ||
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 44 | fun mapT (T as Type (_, Ts)) = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 45 | Library.foldr cfunT (map (fn T => T ->> T) Ts, T ->> T); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 46 | |
| 33774 | 47 | (******************************************************************************) | 
| 48 | (******************************* building terms *******************************) | |
| 49 | (******************************************************************************) | |
| 50 | ||
| 51 | (* builds the expression (v1,v2,..,vn) *) | |
| 52 | fun mk_tuple [] = HOLogic.unit | |
| 53 | | mk_tuple (t::[]) = t | |
| 54 | | mk_tuple (t::ts) = HOLogic.mk_prod (t, mk_tuple ts); | |
| 55 | ||
| 56 | (* builds the expression (%(v1,v2,..,vn). rhs) *) | |
| 57 | fun lambda_tuple [] rhs = Term.lambda (Free("unit", HOLogic.unitT)) rhs
 | |
| 58 | | lambda_tuple (v::[]) rhs = Term.lambda v rhs | |
| 59 | | lambda_tuple (v::vs) rhs = | |
| 60 | HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs)); | |
| 61 | ||
| 62 | (* continuous application and abstraction *) | |
| 63 | ||
| 64 | fun capply_const (S, T) = | |
| 65 |   Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
 | |
| 66 | ||
| 67 | fun cabs_const (S, T) = | |
| 68 |   Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
 | |
| 69 | ||
| 70 | fun mk_cabs t = | |
| 71 | let val T = Term.fastype_of t | |
| 72 | in cabs_const (Term.domain_type T, Term.range_type T) $ t end | |
| 73 | ||
| 74 | (* builds the expression (LAM v. rhs) *) | |
| 75 | fun big_lambda v rhs = | |
| 76 | cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs; | |
| 77 | ||
| 78 | (* builds the expression (LAM v1 v2 .. vn. rhs) *) | |
| 79 | fun big_lambdas [] rhs = rhs | |
| 80 | | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs); | |
| 81 | ||
| 82 | fun mk_capply (t, u) = | |
| 83 | let val (S, T) = | |
| 84 | case Term.fastype_of t of | |
| 85 |         Type(@{type_name "->"}, [S, T]) => (S, T)
 | |
| 86 |       | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]);
 | |
| 87 | in capply_const (S, T) $ t $ u end; | |
| 88 | ||
| 89 | (* miscellaneous term constructions *) | |
| 90 | ||
| 91 | val mk_trp = HOLogic.mk_Trueprop; | |
| 92 | ||
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 93 | val mk_fst = HOLogic.mk_fst; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 94 | val mk_snd = HOLogic.mk_snd; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 95 | |
| 33774 | 96 | fun mk_cont t = | 
| 97 | let val T = Term.fastype_of t | |
| 98 |   in Const(@{const_name cont}, T --> HOLogic.boolT) $ t end;
 | |
| 99 | ||
| 100 | fun mk_fix t = | |
| 101 | let val (T, _) = dest_cfunT (Term.fastype_of t) | |
| 102 |   in mk_capply (Const(@{const_name fix}, (T ->> T) ->> T), t) end;
 | |
| 103 | ||
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 104 | fun ID_const T = Const (@{const_name ID}, cfunT (T, T));
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 105 | |
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 106 | fun cfcomp_const (T, U, V) = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 107 |   Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 108 | |
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 109 | fun mk_cfcomp (f, g) = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 110 | let | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 111 | val (U, V) = dest_cfunT (Term.fastype_of f); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 112 | val (T, U') = dest_cfunT (Term.fastype_of g); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 113 | in | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 114 | if U = U' | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 115 | then mk_capply (mk_capply (cfcomp_const (T, U, V), f), g) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 116 |     else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 117 | end; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 118 | |
| 33774 | 119 | fun mk_Rep_of T = | 
| 120 |   Const (@{const_name Rep_of}, Term.itselfT T --> deflT) $ Logic.mk_type T;
 | |
| 121 | ||
| 33778 | 122 | fun coerce_const T = Const (@{const_name coerce}, T);
 | 
| 123 | ||
| 33788 | 124 | fun isodefl_const T = | 
| 125 |   Const (@{const_name isodefl}, (T ->> T) --> deflT --> HOLogic.boolT);
 | |
| 126 | ||
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 127 | (* splits a cterm into the right and lefthand sides of equality *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 128 | fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 129 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 130 | fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u)); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 131 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 132 | (******************************************************************************) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 133 | (*************** fixed-point definitions and unfolding theorems ***************) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 134 | (******************************************************************************) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 135 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 136 | fun add_fixdefs | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 137 | (spec : (binding * term) list) | 
| 33788 | 138 | (thy : theory) : (thm list * thm list) * theory = | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 139 | let | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 140 | val binds = map fst spec; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 141 | val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 142 | val functional = lambda_tuple lhss (mk_tuple rhss); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 143 | val fixpoint = mk_fix (mk_cabs functional); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 144 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 145 | (* project components of fixpoint *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 146 | fun mk_projs (x::[]) t = [(x, t)] | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 147 | | 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: 
33774diff
changeset | 148 | val projs = mk_projs lhss fixpoint; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 149 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 150 | (* convert parameters to lambda abstractions *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 151 | fun mk_eqn (lhs, rhs) = | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 152 | case lhs of | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 153 |           Const (@{const_name Rep_CFun}, _) $ f $ (x as Free _) =>
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 154 | mk_eqn (f, big_lambda x rhs) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 155 | | Const _ => Logic.mk_equals (lhs, rhs) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 156 |         | _ => raise TERM ("lhs not of correct form", [lhs, rhs]);
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 157 | val eqns = map mk_eqn projs; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 158 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 159 | (* register constant definitions *) | 
| 33777 | 160 | val (fixdef_thms, thy) = | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 161 | (PureThy.add_defs false o map Thm.no_attributes) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 162 | (map (Binding.suffix_name "_def") binds ~~ eqns) thy; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 163 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 164 | (* prove applied version of definitions *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 165 | fun prove_proj (lhs, rhs) = | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 166 | let | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 167 | val tac = rewrite_goals_tac fixdef_thms THEN beta_tac 1; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 168 | val goal = Logic.mk_equals (lhs, rhs); | 
| 33777 | 169 | in Goal.prove_global thy [] [] goal (K tac) end; | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 170 | val proj_thms = map prove_proj projs; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 171 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 172 | (* mk_tuple lhss == fixpoint *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 173 |     fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2];
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 174 | val tuple_fixdef_thm = foldr1 pair_equalI proj_thms; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 175 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 176 | val cont_thm = | 
| 33777 | 177 | Goal.prove_global thy [] [] (mk_trp (mk_cont functional)) | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 178 | (K (beta_tac 1)); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 179 | val tuple_unfold_thm = | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 180 |       (@{thm def_cont_fix_eq} OF [tuple_fixdef_thm, cont_thm])
 | 
| 33777 | 181 |       |> LocalDefs.unfold (ProofContext.init thy) @{thms split_conv};
 | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 182 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 183 | fun mk_unfold_thms [] thm = [] | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 184 | | mk_unfold_thms (n::[]) thm = [(n, thm)] | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 185 | | mk_unfold_thms (n::ns) thm = let | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 186 |           val thmL = thm RS @{thm Pair_eqD1};
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 187 |           val thmR = thm RS @{thm Pair_eqD2};
 | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 188 | in (n, thmL) :: mk_unfold_thms ns thmR end; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 189 | val unfold_binds = map (Binding.suffix_name "_unfold") binds; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 190 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 191 | (* register unfold theorems *) | 
| 33777 | 192 | val (unfold_thms, thy) = | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 193 | (PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.standard)) | 
| 33777 | 194 | (mk_unfold_thms unfold_binds tuple_unfold_thm) thy; | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 195 | in | 
| 33788 | 196 | ((proj_thms, unfold_thms), thy) | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 197 | end; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 198 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 199 | |
| 33774 | 200 | (******************************************************************************) | 
| 201 | ||
| 202 | (* FIXME: use theory data for this *) | |
| 203 | val defl_tab : term Symtab.table = | |
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 204 |     Symtab.make [(@{type_name "->"}, @{term "cfun_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 205 |                  (@{type_name "++"}, @{term "ssum_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 206 |                  (@{type_name "**"}, @{term "sprod_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 207 |                  (@{type_name "*"}, @{term "cprod_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 208 |                  (@{type_name "u"}, @{term "u_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 209 |                  (@{type_name "upper_pd"}, @{term "upper_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 210 |                  (@{type_name "lower_pd"}, @{term "lower_defl"}),
 | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 211 |                  (@{type_name "convex_pd"}, @{term "convex_defl"})];
 | 
| 33774 | 212 | |
| 213 | fun defl_of_typ | |
| 214 | (tab : term Symtab.table) | |
| 215 | (T : typ) : term = | |
| 216 | let | |
| 217 | fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts | |
| 218 | | is_closed_typ _ = false; | |
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 219 | fun defl_of (TFree (a, _)) = Free (Library.unprefix "'" a, deflT) | 
| 33774 | 220 |       | defl_of (TVar _) = error ("defl_of_typ: TVar")
 | 
| 221 | | defl_of (T as Type (c, Ts)) = | |
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 222 | case Symtab.lookup tab c of | 
| 33774 | 223 | SOME t => Library.foldl mk_capply (t, map defl_of Ts) | 
| 224 | | NONE => if is_closed_typ T | |
| 225 | then mk_Rep_of T | |
| 226 |                   else error ("defl_of_typ: type variable under unsupported type constructor " ^ c);
 | |
| 227 | in defl_of T end; | |
| 228 | ||
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 229 | (* FIXME: use theory data for this *) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 230 | val map_tab : string Symtab.table = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 231 |     Symtab.make [(@{type_name "->"}, @{const_name "cfun_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 232 |                  (@{type_name "++"}, @{const_name "ssum_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 233 |                  (@{type_name "**"}, @{const_name "sprod_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 234 |                  (@{type_name "*"}, @{const_name "cprod_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 235 |                  (@{type_name "u"}, @{const_name "u_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 236 |                  (@{type_name "upper_pd"}, @{const_name "upper_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 237 |                  (@{type_name "lower_pd"}, @{const_name "lower_map"}),
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 238 |                  (@{type_name "convex_pd"}, @{const_name "convex_map"})];
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 239 | |
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 240 | fun map_of_typ | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 241 | (tab : string Symtab.table) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 242 | (T : typ) : term = | 
| 33774 | 243 | let | 
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 244 | fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 245 | | is_closed_typ _ = false; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 246 | fun map_of (T as TFree (a, _)) = Free (Library.unprefix "'" a, T ->> T) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 247 |       | map_of (T as TVar _) = error ("map_of_typ: TVar")
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 248 | | map_of (T as Type (c, Ts)) = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 249 | case Symtab.lookup tab c of | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 250 | SOME t => Library.foldl mk_capply (Const (t, mapT T), map map_of Ts) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 251 | | NONE => if is_closed_typ T | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 252 | then ID_const T | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 253 |                   else error ("map_of_typ: type variable under unsupported type constructor " ^ c);
 | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 254 | in map_of T end; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 255 | |
| 33774 | 256 | |
| 257 | (******************************************************************************) | |
| 258 | (* prepare datatype specifications *) | |
| 259 | ||
| 260 | fun read_typ thy str sorts = | |
| 261 | let | |
| 262 | val ctxt = ProofContext.init thy | |
| 263 | |> fold (Variable.declare_typ o TFree) sorts; | |
| 264 | val T = Syntax.read_typ ctxt str; | |
| 265 | in (T, Term.add_tfreesT T sorts) end; | |
| 266 | ||
| 267 | fun cert_typ sign raw_T sorts = | |
| 268 | let | |
| 269 | val T = Type.no_tvars (Sign.certify_typ sign raw_T) | |
| 270 | handle TYPE (msg, _, _) => error msg; | |
| 271 | val sorts' = Term.add_tfreesT T sorts; | |
| 272 | val _ = | |
| 273 | case duplicates (op =) (map fst sorts') of | |
| 274 | [] => () | |
| 275 |       | dups => error ("Inconsistent sort constraints for " ^ commas dups)
 | |
| 276 | in (T, sorts') end; | |
| 277 | ||
| 278 | fun gen_domain_isomorphism | |
| 279 | (prep_typ: theory -> 'a -> (string * sort) list -> typ * (string * sort) list) | |
| 280 | (doms_raw: (string list * binding * mixfix * 'a) list) | |
| 281 | (thy: theory) | |
| 282 | : theory = | |
| 283 | let | |
| 284 | val _ = Theory.requires thy "Domain" "domain definitions"; | |
| 285 | ||
| 286 | (* this theory is used just for parsing *) | |
| 287 | val tmp_thy = thy |> | |
| 288 | Theory.copy |> | |
| 289 | Sign.add_types (map (fn (tvs, tname, mx, _) => | |
| 290 | (tname, length tvs, mx)) doms_raw); | |
| 291 | ||
| 292 | fun prep_dom thy (vs, t, mx, typ_raw) sorts = | |
| 293 | let val (typ, sorts') = prep_typ thy typ_raw sorts | |
| 294 | in ((vs, t, mx, typ), sorts') end; | |
| 295 | ||
| 296 | val (doms : (string list * binding * mixfix * typ) list, | |
| 297 | sorts : (string * sort) list) = | |
| 298 | fold_map (prep_dom tmp_thy) doms_raw []; | |
| 299 | ||
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 300 | (* domain equations *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 301 | fun mk_dom_eqn (vs, tbind, mx, rhs) = | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 302 | let fun arg v = TFree (v, the (AList.lookup (op =) sorts v)); | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 303 | in (Type (Sign.full_name tmp_thy tbind, map arg vs), rhs) end; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 304 | val dom_eqns = map mk_dom_eqn doms; | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 305 | |
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 306 | (* check for valid type parameters *) | 
| 33774 | 307 | val (tyvars, _, _, _)::_ = doms; | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 308 | val new_doms = map (fn (tvs, tname, mx, _) => | 
| 33774 | 309 | let val full_tname = Sign.full_name tmp_thy tname | 
| 310 | in | |
| 311 | (case duplicates (op =) tvs of | |
| 312 | [] => | |
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 313 | if eq_set (op =) (tyvars, tvs) then (full_tname, tvs) | 
| 33774 | 314 |             else error ("Mutually recursive domains must have same type parameters")
 | 
| 315 |         | dups => error ("Duplicate parameter(s) for domain " ^ quote (Binding.str_of tname) ^
 | |
| 316 | " : " ^ commas dups)) | |
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 317 | end) doms; | 
| 33782 | 318 | val dom_binds = map (fn (_, tbind, _, _) => tbind) doms; | 
| 33774 | 319 | |
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 320 | (* declare deflation combinator constants *) | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 321 | fun declare_defl_const (vs, tbind, mx, rhs) thy = | 
| 33774 | 322 | let | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 323 | val defl_type = Library.foldr cfunT (map (K deflT) vs, deflT); | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 324 | val defl_bind = Binding.suffix_name "_defl" tbind; | 
| 33774 | 325 | in | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 326 | Sign.declare_const ((defl_bind, defl_type), NoSyn) thy | 
| 33774 | 327 | end; | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 328 | val (defl_consts, thy) = fold_map declare_defl_const doms thy; | 
| 33774 | 329 | |
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 330 | (* defining equations for type combinators *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 331 | val defl_tab1 = defl_tab; (* FIXME: use theory data *) | 
| 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 332 | val defl_tab2 = | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 333 | 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: 
33774diff
changeset | 334 | val defl_tab' = Symtab.merge (K true) (defl_tab1, defl_tab2); | 
| 33776 | 335 | fun mk_defl_spec (lhsT, rhsT) = | 
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 336 | mk_eqs (defl_of_typ defl_tab' lhsT, | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 337 | defl_of_typ defl_tab' rhsT); | 
| 33775 
7a1518c42c56
cleaned up; factored out fixed-point definition code
 huffman parents: 
33774diff
changeset | 338 | val defl_specs = map mk_defl_spec dom_eqns; | 
| 33774 | 339 | |
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 340 | (* register recursive definition of deflation combinators *) | 
| 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 341 | val defl_binds = map (Binding.suffix_name "_defl") dom_binds; | 
| 33788 | 342 | val ((defl_apply_thms, defl_unfold_thms), thy) = | 
| 343 | add_fixdefs (defl_binds ~~ defl_specs) thy; | |
| 33774 | 344 | |
| 33776 | 345 | (* define types using deflation combinators *) | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 346 | fun make_repdef ((vs, tbind, mx, _), defl_const) thy = | 
| 33774 | 347 | let | 
| 348 | fun tfree a = TFree (a, the (AList.lookup (op =) sorts a)) | |
| 349 | val reps = map (mk_Rep_of o tfree) vs; | |
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 350 | val defl = Library.foldl mk_capply (defl_const, reps); | 
| 33777 | 351 |         val ((_, _, _, {REP, ...}), thy) =
 | 
| 33774 | 352 | Repdef.add_repdef false NONE (tbind, vs, mx) defl NONE thy; | 
| 353 | in | |
| 33777 | 354 | (REP, thy) | 
| 33774 | 355 | end; | 
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 356 | val (REP_thms, thy) = fold_map make_repdef (doms ~~ defl_consts) thy; | 
| 33774 | 357 | |
| 33776 | 358 | (* FIXME: use theory data for this *) | 
| 359 | val REP_simps = REP_thms @ | |
| 360 |       @{thms REP_cfun REP_ssum REP_sprod REP_cprod REP_up
 | |
| 361 | REP_upper REP_lower REP_convex}; | |
| 362 | ||
| 363 | (* prove REP equations *) | |
| 33782 | 364 | fun mk_REP_eq_thm (lhsT, rhsT) = | 
| 33776 | 365 | let | 
| 366 | val goal = mk_eqs (mk_Rep_of lhsT, mk_Rep_of rhsT); | |
| 367 | val tac = | |
| 368 | simp_tac (HOL_basic_ss addsimps REP_simps) 1 | |
| 33784 
7e434813752f
change naming convention for deflation combinators
 huffman parents: 
33782diff
changeset | 369 | THEN resolve_tac defl_unfold_thms 1; | 
| 33776 | 370 | in | 
| 33777 | 371 | Goal.prove_global thy [] [] goal (K tac) | 
| 33776 | 372 | end; | 
| 33782 | 373 | val REP_eq_thms = map mk_REP_eq_thm dom_eqns; | 
| 374 | ||
| 375 | (* register REP equations *) | |
| 376 | val REP_eq_binds = map (Binding.prefix_name "REP_eq_") dom_binds; | |
| 377 | val (_, thy) = thy |> | |
| 378 | (PureThy.add_thms o map Thm.no_attributes) | |
| 379 | (REP_eq_binds ~~ REP_eq_thms); | |
| 33776 | 380 | |
| 33778 | 381 | (* define rep/abs functions *) | 
| 33782 | 382 | fun mk_rep_abs (tbind, (lhsT, rhsT)) thy = | 
| 33778 | 383 | let | 
| 384 | val rep_type = cfunT (lhsT, rhsT); | |
| 33782 | 385 | val abs_type = cfunT (rhsT, lhsT); | 
| 33778 | 386 | val rep_bind = Binding.suffix_name "_rep" tbind; | 
| 387 | val abs_bind = Binding.suffix_name "_abs" tbind; | |
| 388 | val (rep_const, thy) = thy |> | |
| 389 | Sign.declare_const ((rep_bind, rep_type), NoSyn); | |
| 390 | val (abs_const, thy) = thy |> | |
| 391 | Sign.declare_const ((abs_bind, abs_type), NoSyn); | |
| 392 | val rep_eqn = Logic.mk_equals (rep_const, coerce_const rep_type); | |
| 393 | val abs_eqn = Logic.mk_equals (abs_const, coerce_const abs_type); | |
| 394 | val ([rep_def, abs_def], thy) = thy |> | |
| 395 | (PureThy.add_defs false o map Thm.no_attributes) | |
| 396 | [(Binding.suffix_name "_rep_def" tbind, rep_eqn), | |
| 397 | (Binding.suffix_name "_abs_def" tbind, abs_eqn)]; | |
| 398 | in | |
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 399 | (((rep_const, abs_const), (rep_def, abs_def)), thy) | 
| 33778 | 400 | end; | 
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 401 | val ((rep_abs_consts, rep_abs_defs), thy) = thy | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 402 | |> fold_map mk_rep_abs (dom_binds ~~ dom_eqns) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 403 | |>> ListPair.unzip; | 
| 33782 | 404 | |
| 405 | (* prove isomorphism and isodefl rules *) | |
| 406 | fun mk_iso_thms ((tbind, REP_eq), (rep_def, abs_def)) thy = | |
| 407 | let | |
| 408 | fun make thm = Drule.standard (thm OF [REP_eq, abs_def, rep_def]); | |
| 409 |         val rep_iso_thm = make @{thm domain_rep_iso};
 | |
| 410 |         val abs_iso_thm = make @{thm domain_abs_iso};
 | |
| 411 |         val isodefl_thm = make @{thm isodefl_abs_rep};
 | |
| 412 | val rep_iso_bind = Binding.suffix_name "_rep_iso" tbind; | |
| 413 | val abs_iso_bind = Binding.suffix_name "_abs_iso" tbind; | |
| 414 | val isodefl_bind = Binding.prefix_name "isodefl_abs_rep_" tbind; | |
| 415 | val (_, thy) = thy |> | |
| 416 | (PureThy.add_thms o map Thm.no_attributes) | |
| 417 | [(rep_iso_bind, rep_iso_thm), | |
| 418 | (abs_iso_bind, abs_iso_thm), | |
| 419 | (isodefl_bind, isodefl_thm)]; | |
| 420 | in | |
| 421 | (((rep_iso_thm, abs_iso_thm), isodefl_thm), thy) | |
| 422 | end; | |
| 423 | val ((iso_thms, isodefl_abs_rep_thms), thy) = thy | |
| 424 | |> fold_map mk_iso_thms (dom_binds ~~ REP_eq_thms ~~ rep_abs_defs) | |
| 425 | |>> ListPair.unzip; | |
| 33778 | 426 | |
| 33785 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 427 | (* declare map functions *) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 428 | fun declare_map_const (tbind, (lhsT, rhsT)) thy = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 429 | let | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 430 | val map_type = mapT lhsT; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 431 | val map_bind = Binding.suffix_name "_map" tbind; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 432 | in | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 433 | Sign.declare_const ((map_bind, map_type), NoSyn) thy | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 434 | end; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 435 | val (map_consts, thy) = thy |> | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 436 | fold_map declare_map_const (dom_binds ~~ dom_eqns); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 437 | |
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 438 | (* defining equations for map functions *) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 439 | val map_tab1 = map_tab; (* FIXME: use theory data *) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 440 | val map_tab2 = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 441 | Symtab.make (map (fst o dest_Type o fst) dom_eqns | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 442 | ~~ map (fst o dest_Const) map_consts); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 443 | val map_tab' = Symtab.merge (K true) (map_tab1, map_tab2); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 444 | fun mk_map_spec ((rep_const, abs_const), (lhsT, rhsT)) = | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 445 | let | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 446 | val lhs = map_of_typ map_tab' lhsT; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 447 | val body = map_of_typ map_tab' rhsT; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 448 | val rhs = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const)); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 449 | in mk_eqs (lhs, rhs) end; | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 450 | val map_specs = map mk_map_spec (rep_abs_consts ~~ dom_eqns); | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 451 | |
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 452 | (* register recursive definition of map functions *) | 
| 
2f2d9eb37084
automate definition of map functions; remove unused code
 huffman parents: 
33784diff
changeset | 453 | val map_binds = map (Binding.suffix_name "_map") dom_binds; | 
| 33788 | 454 | val ((map_apply_thms, map_unfold_thms), thy) = | 
| 455 | add_fixdefs (map_binds ~~ map_specs) thy; | |
| 456 | ||
| 457 | (* prove isodefl rules for map functions *) | |
| 458 | val isodefl_thm = | |
| 459 | let | |
| 460 | fun unprime a = Library.unprefix "'" a; | |
| 461 |         fun mk_d (TFree (a, _)) = Free ("d" ^ unprime a, deflT);
 | |
| 462 |         fun mk_f (T as TFree (a, _)) = Free ("f" ^ unprime a, T ->> T);
 | |
| 463 | fun mk_assm T = mk_trp (isodefl_const T $ mk_f T $ mk_d T); | |
| 464 | fun mk_goal ((map_const, defl_const), (T as Type (c, Ts), rhsT)) = | |
| 465 | let | |
| 466 | val map_term = Library.foldl mk_capply (map_const, map mk_f Ts); | |
| 467 | val defl_term = Library.foldl mk_capply (defl_const, map mk_d Ts); | |
| 468 | in isodefl_const T $ map_term $ defl_term end; | |
| 469 | val assms = (map mk_assm o snd o dest_Type o fst o hd) dom_eqns; | |
| 470 | val goals = map mk_goal (map_consts ~~ defl_consts ~~ dom_eqns); | |
| 471 | val goal = mk_trp (foldr1 HOLogic.mk_conj goals); | |
| 472 | val start_thms = | |
| 473 |           @{thm split_def} :: defl_apply_thms @ map_apply_thms;
 | |
| 474 | val adm_rules = | |
| 475 |           @{thms adm_conj adm_isodefl cont2cont_fst cont2cont_snd cont_id};
 | |
| 476 | val bottom_rules = | |
| 477 |           @{thms fst_strict snd_strict isodefl_bottom simp_thms};
 | |
| 478 | (* FIXME: use theory data for this *) | |
| 479 | val isodefl_rules = | |
| 480 |           @{thms conjI isodefl_ID_REP} @ isodefl_abs_rep_thms @
 | |
| 481 |           @{thms isodefl_cfun isodefl_ssum isodefl_sprod isodefl_cprod
 | |
| 482 | isodefl_u isodefl_upper isodefl_lower isodefl_convex}; | |
| 483 |         fun tacf {prems, ...} = EVERY
 | |
| 484 | [simp_tac (HOL_basic_ss addsimps start_thms) 1, | |
| 485 |            rtac @{thm parallel_fix_ind} 1,
 | |
| 486 | REPEAT (resolve_tac adm_rules 1), | |
| 487 | simp_tac (HOL_basic_ss addsimps bottom_rules) 1, | |
| 488 | simp_tac beta_ss 1, | |
| 489 |            simp_tac (HOL_basic_ss addsimps @{thms fst_conv snd_conv}) 1,
 | |
| 490 |            REPEAT (etac @{thm conjE} 1),
 | |
| 491 | REPEAT (resolve_tac (isodefl_rules @ prems) 1 ORELSE atac 1)]; | |
| 492 | in | |
| 493 | Goal.prove_global thy [] assms goal tacf | |
| 494 | end; | |
| 33789 | 495 | val isodefl_binds = map (Binding.prefix_name "isodefl_") dom_binds; | 
| 496 | fun conjuncts [] thm = [] | |
| 497 | | conjuncts (n::[]) thm = [(n, thm)] | |
| 498 | | conjuncts (n::ns) thm = let | |
| 499 |           val thmL = thm RS @{thm conjunct1};
 | |
| 500 |           val thmR = thm RS @{thm conjunct2};
 | |
| 501 | in (n, thmL):: conjuncts ns thmR end; | |
| 502 | val (isodefl_thms, thy) = thy |> | |
| 503 | (PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.standard)) | |
| 504 | (conjuncts isodefl_binds isodefl_thm); | |
| 33774 | 505 | in | 
| 33777 | 506 | thy | 
| 33774 | 507 | end; | 
| 508 | ||
| 509 | val domain_isomorphism = gen_domain_isomorphism cert_typ; | |
| 510 | val domain_isomorphism_cmd = gen_domain_isomorphism read_typ; | |
| 511 | ||
| 512 | (******************************************************************************) | |
| 513 | (******************************** outer syntax ********************************) | |
| 514 | (******************************************************************************) | |
| 515 | ||
| 516 | local | |
| 517 | ||
| 518 | structure P = OuterParse and K = OuterKeyword | |
| 519 | ||
| 520 | val parse_domain_iso : (string list * binding * mixfix * string) parser = | |
| 521 | (P.type_args -- P.binding -- P.opt_infix -- (P.$$$ "=" |-- P.typ)) | |
| 522 | >> (fn (((vs, t), mx), rhs) => (vs, t, mx, rhs)); | |
| 523 | ||
| 524 | val parse_domain_isos = P.and_list1 parse_domain_iso; | |
| 525 | ||
| 526 | in | |
| 527 | ||
| 528 | val _ = | |
| 529 | OuterSyntax.command "domain_isomorphism" "define domain isomorphisms (HOLCF)" K.thy_decl | |
| 530 | (parse_domain_isos >> (Toplevel.theory o domain_isomorphism_cmd)); | |
| 531 | ||
| 532 | end; | |
| 533 | ||
| 534 | end; |