krauss@40353
|
1 |
(* Title: HOL/Tools/Function/partial_function.ML
|
krauss@40353
|
2 |
Author: Alexander Krauss, TU Muenchen
|
krauss@40353
|
3 |
|
krauss@40353
|
4 |
Partial function definitions based on least fixed points in ccpos.
|
krauss@40353
|
5 |
*)
|
krauss@40353
|
6 |
|
krauss@40353
|
7 |
signature PARTIAL_FUNCTION =
|
krauss@40353
|
8 |
sig
|
krauss@40353
|
9 |
val setup: theory -> theory
|
krauss@43921
|
10 |
val init: string -> term -> term -> thm -> thm option -> declaration
|
krauss@40353
|
11 |
|
krauss@40353
|
12 |
val add_partial_function: string -> (binding * typ option * mixfix) list ->
|
krauss@40353
|
13 |
Attrib.binding * term -> local_theory -> local_theory
|
krauss@40353
|
14 |
|
krauss@40353
|
15 |
val add_partial_function_cmd: string -> (binding * string option * mixfix) list ->
|
krauss@40353
|
16 |
Attrib.binding * string -> local_theory -> local_theory
|
krauss@40353
|
17 |
end;
|
krauss@40353
|
18 |
|
krauss@40353
|
19 |
|
krauss@40353
|
20 |
structure Partial_Function: PARTIAL_FUNCTION =
|
krauss@40353
|
21 |
struct
|
krauss@40353
|
22 |
|
krauss@40353
|
23 |
(*** Context Data ***)
|
krauss@40353
|
24 |
|
krauss@43921
|
25 |
datatype setup_data = Setup_Data of
|
krauss@43921
|
26 |
{fixp: term,
|
krauss@43921
|
27 |
mono: term,
|
krauss@43921
|
28 |
fixp_eq: thm,
|
krauss@43921
|
29 |
fixp_induct: thm option};
|
krauss@43921
|
30 |
|
krauss@40353
|
31 |
structure Modes = Generic_Data
|
krauss@40353
|
32 |
(
|
krauss@43921
|
33 |
type T = setup_data Symtab.table;
|
krauss@40353
|
34 |
val empty = Symtab.empty;
|
krauss@40353
|
35 |
val extend = I;
|
wenzelm@41720
|
36 |
fun merge data = Symtab.merge (K true) data;
|
krauss@40353
|
37 |
)
|
krauss@40353
|
38 |
|
krauss@43921
|
39 |
fun init mode fixp mono fixp_eq fixp_induct phi =
|
krauss@40353
|
40 |
let
|
krauss@40353
|
41 |
val term = Morphism.term phi;
|
krauss@43921
|
42 |
val thm = Morphism.thm phi;
|
krauss@43921
|
43 |
val data' = Setup_Data
|
krauss@43921
|
44 |
{fixp=term fixp, mono=term mono, fixp_eq=thm fixp_eq,
|
krauss@43921
|
45 |
fixp_induct=Option.map thm fixp_induct};
|
krauss@40353
|
46 |
in
|
krauss@43790
|
47 |
Modes.map (Symtab.update (mode, data'))
|
krauss@40353
|
48 |
end
|
krauss@40353
|
49 |
|
krauss@40353
|
50 |
val known_modes = Symtab.keys o Modes.get o Context.Proof;
|
krauss@40353
|
51 |
val lookup_mode = Symtab.lookup o Modes.get o Context.Proof;
|
krauss@40353
|
52 |
|
krauss@40353
|
53 |
|
krauss@40353
|
54 |
structure Mono_Rules = Named_Thms
|
krauss@40353
|
55 |
(
|
wenzelm@46165
|
56 |
val name = @{binding partial_function_mono};
|
krauss@40353
|
57 |
val description = "monotonicity rules for partial function definitions";
|
krauss@40353
|
58 |
);
|
krauss@40353
|
59 |
|
krauss@40353
|
60 |
|
krauss@40353
|
61 |
(*** Automated monotonicity proofs ***)
|
krauss@40353
|
62 |
|
krauss@40353
|
63 |
fun strip_cases ctac = ctac #> Seq.map snd;
|
krauss@40353
|
64 |
|
krauss@40353
|
65 |
(*rewrite conclusion with k-th assumtion*)
|
krauss@40353
|
66 |
fun rewrite_with_asm_tac ctxt k =
|
wenzelm@46277
|
67 |
Subgoal.FOCUS (fn {context = ctxt', prems, ...} =>
|
krauss@40353
|
68 |
Local_Defs.unfold_tac ctxt' [nth prems k]) ctxt;
|
krauss@40353
|
69 |
|
krauss@40353
|
70 |
fun dest_case thy t =
|
krauss@40353
|
71 |
case strip_comb t of
|
krauss@40353
|
72 |
(Const (case_comb, _), args) =>
|
krauss@40353
|
73 |
(case Datatype.info_of_case thy case_comb of
|
krauss@40353
|
74 |
NONE => NONE
|
krauss@40353
|
75 |
| SOME {case_rewrites, ...} =>
|
krauss@40353
|
76 |
let
|
krauss@40353
|
77 |
val lhs = prop_of (hd case_rewrites)
|
krauss@40353
|
78 |
|> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst;
|
krauss@40353
|
79 |
val arity = length (snd (strip_comb lhs));
|
krauss@40353
|
80 |
val conv = funpow (length args - arity) Conv.fun_conv
|
krauss@40353
|
81 |
(Conv.rewrs_conv (map mk_meta_eq case_rewrites));
|
krauss@40353
|
82 |
in
|
krauss@40353
|
83 |
SOME (nth args (arity - 1), conv)
|
krauss@40353
|
84 |
end)
|
krauss@40353
|
85 |
| _ => NONE;
|
krauss@40353
|
86 |
|
krauss@40353
|
87 |
(*split on case expressions*)
|
krauss@40353
|
88 |
val split_cases_tac = Subgoal.FOCUS_PARAMS (fn {context=ctxt, ...} =>
|
krauss@40353
|
89 |
SUBGOAL (fn (t, i) => case t of
|
krauss@40353
|
90 |
_ $ (_ $ Abs (_, _, body)) =>
|
wenzelm@43232
|
91 |
(case dest_case (Proof_Context.theory_of ctxt) body of
|
krauss@40353
|
92 |
NONE => no_tac
|
krauss@40353
|
93 |
| SOME (arg, conv) =>
|
krauss@40353
|
94 |
let open Conv in
|
wenzelm@42989
|
95 |
if Term.is_open arg then no_tac
|
krauss@40353
|
96 |
else ((DETERM o strip_cases o Induct.cases_tac ctxt false [[SOME arg]] NONE [])
|
krauss@40353
|
97 |
THEN_ALL_NEW (rewrite_with_asm_tac ctxt 0)
|
krauss@40353
|
98 |
THEN_ALL_NEW etac @{thm thin_rl}
|
krauss@40353
|
99 |
THEN_ALL_NEW (CONVERSION
|
krauss@40353
|
100 |
(params_conv ~1 (fn ctxt' =>
|
krauss@40353
|
101 |
arg_conv (arg_conv (abs_conv (K conv) ctxt'))) ctxt))) i
|
krauss@40353
|
102 |
end)
|
krauss@40353
|
103 |
| _ => no_tac) 1);
|
krauss@40353
|
104 |
|
krauss@40353
|
105 |
(*monotonicity proof: apply rules + split case expressions*)
|
krauss@40353
|
106 |
fun mono_tac ctxt =
|
krauss@40353
|
107 |
K (Local_Defs.unfold_tac ctxt [@{thm curry_def}])
|
krauss@40353
|
108 |
THEN' (TRY o REPEAT_ALL_NEW
|
krauss@40353
|
109 |
(resolve_tac (Mono_Rules.get ctxt)
|
krauss@40353
|
110 |
ORELSE' split_cases_tac ctxt));
|
krauss@40353
|
111 |
|
krauss@40353
|
112 |
|
krauss@40353
|
113 |
(*** Auxiliary functions ***)
|
krauss@40353
|
114 |
|
krauss@40353
|
115 |
(*positional instantiation with computed type substitution.
|
krauss@40353
|
116 |
internal version of attribute "[of s t u]".*)
|
krauss@40353
|
117 |
fun cterm_instantiate' cts thm =
|
krauss@40353
|
118 |
let
|
krauss@40353
|
119 |
val thy = Thm.theory_of_thm thm;
|
krauss@40353
|
120 |
val vs = rev (Term.add_vars (prop_of thm) [])
|
krauss@40353
|
121 |
|> map (Thm.cterm_of thy o Var);
|
krauss@40353
|
122 |
in
|
krauss@40353
|
123 |
cterm_instantiate (zip_options vs cts) thm
|
krauss@40353
|
124 |
end;
|
krauss@40353
|
125 |
|
krauss@40353
|
126 |
(*Returns t $ u, but instantiates the type of t to make the
|
krauss@40353
|
127 |
application type correct*)
|
krauss@40353
|
128 |
fun apply_inst ctxt t u =
|
krauss@40353
|
129 |
let
|
wenzelm@43232
|
130 |
val thy = Proof_Context.theory_of ctxt;
|
krauss@40353
|
131 |
val T = domain_type (fastype_of t);
|
krauss@40353
|
132 |
val T' = fastype_of u;
|
wenzelm@43263
|
133 |
val subst = Sign.typ_match thy (T, T') Vartab.empty
|
krauss@40353
|
134 |
handle Type.TYPE_MATCH => raise TYPE ("apply_inst", [T, T'], [t, u])
|
krauss@40353
|
135 |
in
|
krauss@40353
|
136 |
map_types (Envir.norm_type subst) t $ u
|
krauss@40353
|
137 |
end;
|
krauss@40353
|
138 |
|
krauss@40353
|
139 |
fun head_conv cv ct =
|
krauss@40353
|
140 |
if can Thm.dest_comb ct then Conv.fun_conv (head_conv cv) ct else cv ct;
|
krauss@40353
|
141 |
|
krauss@40353
|
142 |
|
krauss@40353
|
143 |
(*** currying transformation ***)
|
krauss@40353
|
144 |
|
krauss@40353
|
145 |
fun curry_const (A, B, C) =
|
krauss@40353
|
146 |
Const (@{const_name Product_Type.curry},
|
krauss@40353
|
147 |
[HOLogic.mk_prodT (A, B) --> C, A, B] ---> C);
|
krauss@40353
|
148 |
|
krauss@40353
|
149 |
fun mk_curry f =
|
krauss@40353
|
150 |
case fastype_of f of
|
krauss@40353
|
151 |
Type ("fun", [Type (_, [S, T]), U]) =>
|
krauss@40353
|
152 |
curry_const (S, T, U) $ f
|
krauss@40353
|
153 |
| T => raise TYPE ("mk_curry", [T], [f]);
|
krauss@40353
|
154 |
|
krauss@40353
|
155 |
(* iterated versions. Nonstandard left-nested tuples arise naturally
|
krauss@40353
|
156 |
from "split o split o split"*)
|
krauss@40353
|
157 |
fun curry_n arity = funpow (arity - 1) mk_curry;
|
krauss@40353
|
158 |
fun uncurry_n arity = funpow (arity - 1) HOLogic.mk_split;
|
krauss@40353
|
159 |
|
krauss@40353
|
160 |
val curry_uncurry_ss = HOL_basic_ss addsimps
|
krauss@40353
|
161 |
[@{thm Product_Type.curry_split}, @{thm Product_Type.split_curry}]
|
krauss@40353
|
162 |
|
krauss@43924
|
163 |
val split_conv_ss = HOL_basic_ss addsimps
|
krauss@43924
|
164 |
[@{thm Product_Type.split_conv}];
|
krauss@43924
|
165 |
|
krauss@43924
|
166 |
fun mk_curried_induct args ctxt ccurry cuncurry rule =
|
krauss@43924
|
167 |
let
|
krauss@43924
|
168 |
val cert = Thm.cterm_of (Proof_Context.theory_of ctxt)
|
krauss@43924
|
169 |
val ([P], ctxt') = Variable.variant_fixes ["P"] ctxt
|
krauss@43924
|
170 |
|
krauss@43924
|
171 |
val split_paired_all_conv =
|
krauss@43924
|
172 |
Conv.every_conv (replicate (length args - 1) (Conv.rewr_conv @{thm split_paired_all}))
|
krauss@43924
|
173 |
|
krauss@43924
|
174 |
val split_params_conv =
|
krauss@43924
|
175 |
Conv.params_conv ~1 (fn ctxt' =>
|
krauss@43924
|
176 |
Conv.implies_conv split_paired_all_conv Conv.all_conv)
|
krauss@43924
|
177 |
|
krauss@43924
|
178 |
val inst_rule =
|
krauss@43924
|
179 |
cterm_instantiate' [SOME cuncurry, NONE, SOME ccurry] rule
|
krauss@43924
|
180 |
|
krauss@43924
|
181 |
val plain_resultT =
|
krauss@43924
|
182 |
Thm.prop_of inst_rule |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop
|
krauss@43924
|
183 |
|> Term.head_of |> Term.dest_Var |> snd |> range_type |> domain_type
|
krauss@43924
|
184 |
val PT = map (snd o dest_Free) args ---> plain_resultT --> HOLogic.boolT
|
krauss@43924
|
185 |
val x_inst = cert (foldl1 HOLogic.mk_prod args)
|
krauss@43924
|
186 |
val P_inst = cert (uncurry_n (length args) (Free (P, PT)))
|
krauss@43924
|
187 |
|
krauss@43924
|
188 |
val inst_rule' = inst_rule
|
krauss@43924
|
189 |
|> Tactic.rule_by_tactic ctxt
|
krauss@43924
|
190 |
(Simplifier.simp_tac curry_uncurry_ss 4
|
krauss@43924
|
191 |
THEN Simplifier.simp_tac curry_uncurry_ss 3
|
krauss@43924
|
192 |
THEN CONVERSION (split_params_conv ctxt
|
krauss@43924
|
193 |
then_conv (Conv.forall_conv (K split_paired_all_conv) ctxt)) 3)
|
krauss@43924
|
194 |
|> Drule.instantiate' [] [NONE, NONE, SOME P_inst, SOME x_inst]
|
krauss@43924
|
195 |
|> Simplifier.full_simplify split_conv_ss
|
krauss@43924
|
196 |
|> singleton (Variable.export ctxt' ctxt)
|
krauss@43924
|
197 |
in
|
krauss@43924
|
198 |
inst_rule'
|
krauss@43924
|
199 |
end;
|
krauss@43924
|
200 |
|
krauss@40353
|
201 |
|
krauss@40353
|
202 |
(*** partial_function definition ***)
|
krauss@40353
|
203 |
|
krauss@40353
|
204 |
fun gen_add_partial_function prep mode fixes_raw eqn_raw lthy =
|
krauss@40353
|
205 |
let
|
krauss@43921
|
206 |
val setup_data = the (lookup_mode lthy mode)
|
krauss@40353
|
207 |
handle Option.Option => error (cat_lines ["Unknown mode " ^ quote mode ^ ".",
|
krauss@40353
|
208 |
"Known modes are " ^ commas_quote (known_modes lthy) ^ "."]);
|
krauss@43921
|
209 |
val Setup_Data {fixp, mono, fixp_eq, fixp_induct} = setup_data;
|
krauss@40353
|
210 |
|
krauss@40353
|
211 |
val ((fixes, [(eq_abinding, eqn)]), _) = prep fixes_raw [eqn_raw] lthy;
|
krauss@43924
|
212 |
val ((_, plain_eqn), args_ctxt) = Variable.focus eqn lthy;
|
krauss@40353
|
213 |
|
krauss@40353
|
214 |
val ((f_binding, fT), mixfix) = the_single fixes;
|
krauss@40353
|
215 |
val fname = Binding.name_of f_binding;
|
krauss@40353
|
216 |
|
wenzelm@43232
|
217 |
val cert = cterm_of (Proof_Context.theory_of lthy);
|
krauss@40353
|
218 |
val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop plain_eqn);
|
krauss@40353
|
219 |
val (head, args) = strip_comb lhs;
|
krauss@43924
|
220 |
val argnames = map (fst o dest_Free) args;
|
krauss@40353
|
221 |
val F = fold_rev lambda (head :: args) rhs;
|
krauss@40353
|
222 |
|
krauss@40353
|
223 |
val arity = length args;
|
krauss@40353
|
224 |
val (aTs, bTs) = chop arity (binder_types fT);
|
krauss@40353
|
225 |
|
krauss@40353
|
226 |
val tupleT = foldl1 HOLogic.mk_prodT aTs;
|
krauss@40353
|
227 |
val fT_uc = tupleT :: bTs ---> body_type fT;
|
krauss@40353
|
228 |
val f_uc = Var ((fname, 0), fT_uc);
|
krauss@40353
|
229 |
val x_uc = Var (("x", 0), tupleT);
|
krauss@40353
|
230 |
val uncurry = lambda head (uncurry_n arity head);
|
krauss@40353
|
231 |
val curry = lambda f_uc (curry_n arity f_uc);
|
krauss@40353
|
232 |
|
krauss@40353
|
233 |
val F_uc =
|
krauss@40353
|
234 |
lambda f_uc (uncurry_n arity (F $ curry_n arity f_uc));
|
krauss@40353
|
235 |
|
krauss@40353
|
236 |
val mono_goal = apply_inst lthy mono (lambda f_uc (F_uc $ f_uc $ x_uc))
|
krauss@40353
|
237 |
|> HOLogic.mk_Trueprop
|
krauss@40353
|
238 |
|> Logic.all x_uc;
|
krauss@40353
|
239 |
|
krauss@40353
|
240 |
val mono_thm = Goal.prove_internal [] (cert mono_goal)
|
krauss@40353
|
241 |
(K (mono_tac lthy 1))
|
krauss@40353
|
242 |
|> Thm.forall_elim (cert x_uc);
|
krauss@40353
|
243 |
|
krauss@40353
|
244 |
val f_def_rhs = curry_n arity (apply_inst lthy fixp F_uc);
|
krauss@40353
|
245 |
val f_def_binding = Binding.conceal (Binding.name (Thm.def_name fname));
|
krauss@40353
|
246 |
val ((f, (_, f_def)), lthy') = Local_Theory.define
|
krauss@40353
|
247 |
((f_binding, mixfix), ((f_def_binding, []), f_def_rhs)) lthy;
|
krauss@40353
|
248 |
|
krauss@40353
|
249 |
val eqn = HOLogic.mk_eq (list_comb (f, args),
|
krauss@40353
|
250 |
Term.betapplys (F, f :: args))
|
krauss@40353
|
251 |
|> HOLogic.mk_Trueprop;
|
krauss@40353
|
252 |
|
krauss@40353
|
253 |
val unfold =
|
krauss@40353
|
254 |
(cterm_instantiate' (map (SOME o cert) [uncurry, F, curry]) fixp_eq
|
krauss@40353
|
255 |
OF [mono_thm, f_def])
|
krauss@40353
|
256 |
|> Tactic.rule_by_tactic lthy (Simplifier.simp_tac curry_uncurry_ss 1);
|
krauss@40353
|
257 |
|
krauss@43924
|
258 |
val mk_raw_induct =
|
krauss@43924
|
259 |
mk_curried_induct args args_ctxt (cert curry) (cert uncurry)
|
krauss@43924
|
260 |
#> singleton (Variable.export args_ctxt lthy)
|
krauss@45879
|
261 |
#> (fn thm => cterm_instantiate' [SOME (cert F)] thm OF [mono_thm, f_def])
|
krauss@43924
|
262 |
#> Drule.rename_bvars' (map SOME (fname :: argnames @ argnames))
|
krauss@43924
|
263 |
|
krauss@43924
|
264 |
val raw_induct = Option.map mk_raw_induct fixp_induct
|
krauss@40353
|
265 |
val rec_rule = let open Conv in
|
krauss@40353
|
266 |
Goal.prove lthy' (map (fst o dest_Free) args) [] eqn (fn _ =>
|
krauss@40353
|
267 |
CONVERSION ((arg_conv o arg1_conv o head_conv o rewr_conv) (mk_meta_eq unfold)) 1
|
krauss@40353
|
268 |
THEN rtac @{thm refl} 1) end;
|
krauss@40353
|
269 |
in
|
krauss@40353
|
270 |
lthy'
|
krauss@40353
|
271 |
|> Local_Theory.note (eq_abinding, [rec_rule])
|
krauss@40353
|
272 |
|-> (fn (_, rec') =>
|
krauss@40421
|
273 |
Spec_Rules.add Spec_Rules.Equational ([f], rec')
|
krauss@40421
|
274 |
#> Local_Theory.note ((Binding.qualify true fname (Binding.name "simps"), []), rec') #> snd)
|
krauss@43924
|
275 |
|> (case raw_induct of NONE => I | SOME thm =>
|
krauss@43924
|
276 |
Local_Theory.note ((Binding.qualify true fname (Binding.name "raw_induct"), []), [thm]) #> snd)
|
krauss@40353
|
277 |
end;
|
krauss@40353
|
278 |
|
krauss@40353
|
279 |
val add_partial_function = gen_add_partial_function Specification.check_spec;
|
krauss@40353
|
280 |
val add_partial_function_cmd = gen_add_partial_function Specification.read_spec;
|
krauss@40353
|
281 |
|
wenzelm@47823
|
282 |
val mode = @{keyword "("} |-- Parse.xname --| @{keyword ")"};
|
krauss@40353
|
283 |
|
krauss@40353
|
284 |
val _ = Outer_Syntax.local_theory
|
haftmann@40427
|
285 |
"partial_function" "define partial function" Keyword.thy_decl
|
krauss@40353
|
286 |
((mode -- (Parse.fixes -- (Parse.where_ |-- Parse_Spec.spec)))
|
krauss@40353
|
287 |
>> (fn (mode, (fixes, spec)) => add_partial_function_cmd mode fixes spec));
|
krauss@40353
|
288 |
|
krauss@40353
|
289 |
|
krauss@40353
|
290 |
val setup = Mono_Rules.setup;
|
krauss@40353
|
291 |
|
krauss@40353
|
292 |
end
|