1 (* Title: HOLCF/Tools/repdef.ML |
|
2 Author: Brian Huffman |
|
3 |
|
4 Defining representable domains using algebraic deflations. |
|
5 *) |
|
6 |
|
7 signature DOMAINDEF = |
|
8 sig |
|
9 type rep_info = |
|
10 { |
|
11 emb_def : thm, |
|
12 prj_def : thm, |
|
13 defl_def : thm, |
|
14 liftemb_def : thm, |
|
15 liftprj_def : thm, |
|
16 liftdefl_def : thm, |
|
17 DEFL : thm |
|
18 } |
|
19 |
|
20 val add_domaindef: bool -> binding option -> binding * (string * sort) list * mixfix -> |
|
21 term -> (binding * binding) option -> theory -> |
|
22 (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory |
|
23 |
|
24 val domaindef_cmd: (bool * binding) * (binding * (string * string option) list * mixfix) * string |
|
25 * (binding * binding) option -> theory -> theory |
|
26 end; |
|
27 |
|
28 structure Domaindef :> DOMAINDEF = |
|
29 struct |
|
30 |
|
31 open HOLCF_Library; |
|
32 |
|
33 infixr 6 ->>; |
|
34 infix -->>; |
|
35 |
|
36 (** type definitions **) |
|
37 |
|
38 type rep_info = |
|
39 { |
|
40 emb_def : thm, |
|
41 prj_def : thm, |
|
42 defl_def : thm, |
|
43 liftemb_def : thm, |
|
44 liftprj_def : thm, |
|
45 liftdefl_def : thm, |
|
46 DEFL : thm |
|
47 }; |
|
48 |
|
49 (* building types and terms *) |
|
50 |
|
51 val udomT = @{typ udom}; |
|
52 val deflT = @{typ defl}; |
|
53 fun emb_const T = Const (@{const_name emb}, T ->> udomT); |
|
54 fun prj_const T = Const (@{const_name prj}, udomT ->> T); |
|
55 fun defl_const T = Const (@{const_name defl}, Term.itselfT T --> deflT); |
|
56 fun liftemb_const T = Const (@{const_name liftemb}, mk_upT T ->> udomT); |
|
57 fun liftprj_const T = Const (@{const_name liftprj}, udomT ->> mk_upT T); |
|
58 fun liftdefl_const T = Const (@{const_name liftdefl}, Term.itselfT T --> deflT); |
|
59 |
|
60 fun mk_u_map t = |
|
61 let |
|
62 val (T, U) = dest_cfunT (fastype_of t); |
|
63 val u_map_type = (T ->> U) ->> (mk_upT T ->> mk_upT U); |
|
64 val u_map_const = Const (@{const_name u_map}, u_map_type); |
|
65 in |
|
66 mk_capply (u_map_const, t) |
|
67 end; |
|
68 |
|
69 fun mk_cast (t, x) = |
|
70 capply_const (udomT, udomT) |
|
71 $ (capply_const (deflT, udomT ->> udomT) $ @{const cast} $ t) |
|
72 $ x; |
|
73 |
|
74 (* manipulating theorems *) |
|
75 |
|
76 (* proving class instances *) |
|
77 |
|
78 fun declare_type_name a = |
|
79 Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
|
80 |
|
81 fun gen_add_domaindef |
|
82 (prep_term: Proof.context -> 'a -> term) |
|
83 (def: bool) |
|
84 (name: binding) |
|
85 (typ as (tname, raw_args, mx) : binding * (string * sort) list * mixfix) |
|
86 (raw_defl: 'a) |
|
87 (opt_morphs: (binding * binding) option) |
|
88 (thy: theory) |
|
89 : (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory = |
|
90 let |
|
91 val _ = Theory.requires thy "Domain" "domaindefs"; |
|
92 |
|
93 (*rhs*) |
|
94 val tmp_ctxt = |
|
95 ProofContext.init_global thy |
|
96 |> fold (Variable.declare_typ o TFree) raw_args; |
|
97 val defl = prep_term tmp_ctxt raw_defl; |
|
98 val tmp_ctxt = tmp_ctxt |> Variable.declare_constraints defl; |
|
99 |
|
100 val deflT = Term.fastype_of defl; |
|
101 val _ = if deflT = @{typ "defl"} then () |
|
102 else error ("Not type defl: " ^ quote (Syntax.string_of_typ tmp_ctxt deflT)); |
|
103 |
|
104 (*lhs*) |
|
105 val lhs_tfrees = map (ProofContext.check_tfree tmp_ctxt) raw_args; |
|
106 val lhs_sorts = map snd lhs_tfrees; |
|
107 val full_tname = Sign.full_name thy tname; |
|
108 val newT = Type (full_tname, map TFree lhs_tfrees); |
|
109 |
|
110 (*morphisms*) |
|
111 val morphs = opt_morphs |
|
112 |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name); |
|
113 |
|
114 (*set*) |
|
115 val set = @{const defl_set} $ defl; |
|
116 |
|
117 (*pcpodef*) |
|
118 val tac1 = rtac @{thm defl_set_bottom} 1; |
|
119 val tac2 = rtac @{thm adm_defl_set} 1; |
|
120 val ((info, cpo_info, pcpo_info), thy) = thy |
|
121 |> Cpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2); |
|
122 |
|
123 (*definitions*) |
|
124 val Rep_const = Const (#Rep_name (#1 info), newT --> udomT); |
|
125 val Abs_const = Const (#Abs_name (#1 info), udomT --> newT); |
|
126 val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const); |
|
127 val prj_eqn = Logic.mk_equals (prj_const newT, cabs_const (udomT, newT) $ |
|
128 Abs ("x", udomT, Abs_const $ mk_cast (defl, Bound 0))); |
|
129 val defl_eqn = Logic.mk_equals (defl_const newT, |
|
130 Abs ("x", Term.itselfT newT, defl)); |
|
131 val liftemb_eqn = |
|
132 Logic.mk_equals (liftemb_const newT, |
|
133 mk_cfcomp (@{term "udom_emb u_approx"}, mk_u_map (emb_const newT))); |
|
134 val liftprj_eqn = |
|
135 Logic.mk_equals (liftprj_const newT, |
|
136 mk_cfcomp (mk_u_map (prj_const newT), @{term "udom_prj u_approx"})); |
|
137 val liftdefl_eqn = |
|
138 Logic.mk_equals (liftdefl_const newT, |
|
139 Abs ("t", Term.itselfT newT, |
|
140 mk_capply (@{const u_defl}, defl_const newT $ Logic.mk_type newT))); |
|
141 |
|
142 val name_def = Binding.suffix_name "_def" name; |
|
143 val emb_bind = (Binding.prefix_name "emb_" name_def, []); |
|
144 val prj_bind = (Binding.prefix_name "prj_" name_def, []); |
|
145 val defl_bind = (Binding.prefix_name "defl_" name_def, []); |
|
146 val liftemb_bind = (Binding.prefix_name "liftemb_" name_def, []); |
|
147 val liftprj_bind = (Binding.prefix_name "liftprj_" name_def, []); |
|
148 val liftdefl_bind = (Binding.prefix_name "liftdefl_" name_def, []); |
|
149 |
|
150 (*instantiate class rep*) |
|
151 val lthy = thy |
|
152 |> Class.instantiation ([full_tname], lhs_tfrees, @{sort liftdomain}); |
|
153 val ((_, (_, emb_ldef)), lthy) = |
|
154 Specification.definition (NONE, (emb_bind, emb_eqn)) lthy; |
|
155 val ((_, (_, prj_ldef)), lthy) = |
|
156 Specification.definition (NONE, (prj_bind, prj_eqn)) lthy; |
|
157 val ((_, (_, defl_ldef)), lthy) = |
|
158 Specification.definition (NONE, (defl_bind, defl_eqn)) lthy; |
|
159 val ((_, (_, liftemb_ldef)), lthy) = |
|
160 Specification.definition (NONE, (liftemb_bind, liftemb_eqn)) lthy; |
|
161 val ((_, (_, liftprj_ldef)), lthy) = |
|
162 Specification.definition (NONE, (liftprj_bind, liftprj_eqn)) lthy; |
|
163 val ((_, (_, liftdefl_ldef)), lthy) = |
|
164 Specification.definition (NONE, (liftdefl_bind, liftdefl_eqn)) lthy; |
|
165 val ctxt_thy = ProofContext.init_global (ProofContext.theory_of lthy); |
|
166 val emb_def = singleton (ProofContext.export lthy ctxt_thy) emb_ldef; |
|
167 val prj_def = singleton (ProofContext.export lthy ctxt_thy) prj_ldef; |
|
168 val defl_def = singleton (ProofContext.export lthy ctxt_thy) defl_ldef; |
|
169 val liftemb_def = singleton (ProofContext.export lthy ctxt_thy) liftemb_ldef; |
|
170 val liftprj_def = singleton (ProofContext.export lthy ctxt_thy) liftprj_ldef; |
|
171 val liftdefl_def = singleton (ProofContext.export lthy ctxt_thy) liftdefl_ldef; |
|
172 val type_definition_thm = |
|
173 MetaSimplifier.rewrite_rule |
|
174 (the_list (#set_def (#2 info))) |
|
175 (#type_definition (#2 info)); |
|
176 val typedef_thms = |
|
177 [type_definition_thm, #below_def cpo_info, emb_def, prj_def, defl_def, |
|
178 liftemb_def, liftprj_def, liftdefl_def]; |
|
179 val thy = lthy |
|
180 |> Class.prove_instantiation_instance |
|
181 (K (Tactic.rtac (@{thm typedef_liftdomain_class} OF typedef_thms) 1)) |
|
182 |> Local_Theory.exit_global; |
|
183 |
|
184 (*other theorems*) |
|
185 val defl_thm' = Thm.transfer thy defl_def; |
|
186 val (DEFL_thm, thy) = thy |
|
187 |> Sign.add_path (Binding.name_of name) |
|
188 |> Global_Theory.add_thm |
|
189 ((Binding.prefix_name "DEFL_" name, |
|
190 Drule.zero_var_indexes (@{thm typedef_DEFL} OF [defl_thm'])), []) |
|
191 ||> Sign.restore_naming thy; |
|
192 |
|
193 val rep_info = |
|
194 { emb_def = emb_def, prj_def = prj_def, defl_def = defl_def, |
|
195 liftemb_def = liftemb_def, liftprj_def = liftprj_def, |
|
196 liftdefl_def = liftdefl_def, DEFL = DEFL_thm }; |
|
197 in |
|
198 ((info, cpo_info, pcpo_info, rep_info), thy) |
|
199 end |
|
200 handle ERROR msg => |
|
201 cat_error msg ("The error(s) above occurred in domaindef " ^ quote (Binding.str_of name)); |
|
202 |
|
203 fun add_domaindef def opt_name typ defl opt_morphs thy = |
|
204 let |
|
205 val name = the_default (#1 typ) opt_name; |
|
206 in |
|
207 gen_add_domaindef Syntax.check_term def name typ defl opt_morphs thy |
|
208 end; |
|
209 |
|
210 fun domaindef_cmd ((def, name), (b, raw_args, mx), A, morphs) thy = |
|
211 let |
|
212 val ctxt = ProofContext.init_global thy; |
|
213 val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args; |
|
214 in snd (gen_add_domaindef Syntax.read_term def name (b, args, mx) A morphs thy) end; |
|
215 |
|
216 |
|
217 (** outer syntax **) |
|
218 |
|
219 val domaindef_decl = |
|
220 Scan.optional (Parse.$$$ "(" |-- |
|
221 ((Parse.$$$ "open" >> K false) -- Scan.option Parse.binding || |
|
222 Parse.binding >> (fn s => (true, SOME s))) |
|
223 --| Parse.$$$ ")") (true, NONE) -- |
|
224 (Parse.type_args_constrained -- Parse.binding) -- |
|
225 Parse.opt_mixfix -- (Parse.$$$ "=" |-- Parse.term) -- |
|
226 Scan.option (Parse.$$$ "morphisms" |-- Parse.!!! (Parse.binding -- Parse.binding)); |
|
227 |
|
228 fun mk_domaindef ((((((def, opt_name), (args, t)), mx), A), morphs)) = |
|
229 domaindef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs); |
|
230 |
|
231 val _ = |
|
232 Outer_Syntax.command "domaindef" "HOLCF definition of domains from deflations" Keyword.thy_decl |
|
233 (domaindef_decl >> |
|
234 (Toplevel.print oo (Toplevel.theory o mk_domaindef))); |
|
235 |
|
236 end; |
|