author | haftmann |
Thu, 06 Apr 2006 16:10:22 +0200 | |
changeset 19345 | 73439b467e75 |
parent 18974 | 593af1a1068b |
child 19764 | 372065f34795 |
permissions | -rw-r--r-- |
16226 | 1 |
(* Title: HOLCF/fixrec_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amber Telfer and Brian Huffman |
|
4 |
||
5 |
Recursive function definition package for HOLCF. |
|
6 |
*) |
|
7 |
||
8 |
signature FIXREC_PACKAGE = |
|
9 |
sig |
|
18728 | 10 |
val add_fixrec: bool -> ((string * Attrib.src list) * string) list list -> theory -> theory |
11 |
val add_fixrec_i: bool -> ((string * attribute list) * term) list list -> theory -> theory |
|
12 |
val add_fixpat: (string * Attrib.src list) * string list -> theory -> theory |
|
13 |
val add_fixpat_i: (string * attribute list) * term list -> theory -> theory |
|
16226 | 14 |
end; |
15 |
||
16 |
structure FixrecPackage: FIXREC_PACKAGE = |
|
17 |
struct |
|
18 |
||
16552 | 19 |
fun fixrec_err s = error ("fixrec definition error:\n" ^ s); |
16628 | 20 |
fun fixrec_eq_err thy s eq = |
21 |
fixrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term thy eq)); |
|
16552 | 22 |
|
16226 | 23 |
(* ->> is taken from holcf_logic.ML *) |
24 |
(* TODO: fix dependencies so we can import HOLCFLogic here *) |
|
25 |
infixr 6 ->>; |
|
26 |
fun S ->> T = Type ("Cfun.->",[S,T]); |
|
27 |
||
28 |
(* extern_name is taken from domain/library.ML *) |
|
29 |
fun extern_name con = case Symbol.explode con of |
|
30 |
("o"::"p"::" "::rest) => implode rest |
|
31 |
| _ => con; |
|
32 |
||
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
33 |
val mk_trp = HOLogic.mk_Trueprop; |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
34 |
|
16226 | 35 |
(* splits a cterm into the right and lefthand sides of equality *) |
16552 | 36 |
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t); |
37 |
(* |
|
16226 | 38 |
fun dest_eqs (Const ("==", _)$lhs$rhs) = (lhs, rhs) |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
39 |
| dest_eqs (Const ("Trueprop", _)$(Const ("op =", _)$lhs$rhs)) = (lhs,rhs) |
16552 | 40 |
| dest_eqs t = fixrec_err (Sign.string_of_term (sign_of (the_context())) t); |
41 |
*) |
|
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
42 |
(* similar to Thm.head_of, but for continuous application *) |
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
43 |
fun chead_of (Const("Cfun.Rep_CFun",_)$f$t) = chead_of f |
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
44 |
| chead_of u = u; |
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
45 |
|
16387 | 46 |
(* these are helpful functions copied from HOLCF/domain/library.ML *) |
47 |
fun %: s = Free(s,dummyT); |
|
48 |
fun %%: s = Const(s,dummyT); |
|
49 |
infix 0 ==; fun S == T = %%:"==" $ S $ T; |
|
50 |
infix 1 ===; fun S === T = %%:"op =" $ S $ T; |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
51 |
infix 9 ` ; fun f ` x = %%:"Cfun.Rep_CFun" $ f $ x; |
16387 | 52 |
|
18974 | 53 |
(* infers the type of a term *) (* FIXME better avoid this low-level stuff *) |
16402 | 54 |
(* similar to Theory.inferT_axm, but allows any type, not just propT *) |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
55 |
fun infer sg t = |
18974 | 56 |
fst (Sign.infer_types (Sign.pp sg) sg (Sign.consts_of sg) (K NONE) (K NONE) [] true |
57 |
([t],dummyT)); |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
58 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
59 |
(* builds the expression (LAM v. rhs) *) |
18974 | 60 |
fun big_lambda v rhs = %%:"Cfun.Abs_CFun"$(Term.lambda v rhs); |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
61 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
62 |
(* builds the expression (LAM v1 v2 .. vn. rhs) *) |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
63 |
fun big_lambdas [] rhs = rhs |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
64 |
| big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
65 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
66 |
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *) |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
67 |
fun lambda_ctuple [] rhs = big_lambda (%:"unit") rhs |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
68 |
| lambda_ctuple (v::[]) rhs = big_lambda v rhs |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
69 |
| lambda_ctuple (v::vs) rhs = |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
70 |
%%:"Cprod.csplit"`(big_lambda v (lambda_ctuple vs rhs)); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
71 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
72 |
(* builds the expression <v1,v2,..,vn> *) |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
73 |
fun mk_ctuple [] = %%:"UU" |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
74 |
| mk_ctuple (t::[]) = t |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
75 |
| mk_ctuple (t::ts) = %%:"Cprod.cpair"`t`(mk_ctuple ts); |
16387 | 76 |
|
77 |
(*************************************************************************) |
|
16402 | 78 |
(************* fixed-point definitions and unfolding theorems ************) |
16387 | 79 |
(*************************************************************************) |
80 |
||
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
81 |
fun add_fixdefs eqs thy = |
16226 | 82 |
let |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
83 |
val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
84 |
val fixpoint = %%:"Fix.fix"`lambda_ctuple lhss (mk_ctuple rhss); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
85 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
86 |
fun one_def (l as Const(n,T)) r = |
16552 | 87 |
let val b = Sign.base_name n in (b, (b^"_def", l == r)) end |
88 |
| one_def _ _ = fixrec_err "fixdefs: lhs not of correct form"; |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
89 |
fun defs [] _ = [] |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
90 |
| defs (l::[]) r = [one_def l r] |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
91 |
| defs (l::ls) r = one_def l (%%:"Cprod.cfst"`r) :: defs ls (%%:"Cprod.csnd"`r); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
92 |
val (names, pre_fixdefs) = ListPair.unzip (defs lhss fixpoint); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
93 |
|
16628 | 94 |
val fixdefs = map (inferT_axm thy) pre_fixdefs; |
18358 | 95 |
val (fixdef_thms, thy') = |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
96 |
PureThy.add_defs_i false (map Thm.no_attributes fixdefs) thy; |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
97 |
val ctuple_fixdef_thm = foldr1 (fn (x,y) => cpair_equalI OF [x,y]) fixdef_thms; |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
98 |
|
17985 | 99 |
val ctuple_unfold = infer thy' (mk_trp (mk_ctuple lhss === mk_ctuple rhss)); |
100 |
val ctuple_unfold_thm = standard (Goal.prove thy' [] [] ctuple_unfold |
|
101 |
(fn _ => EVERY [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1, |
|
102 |
simp_tac (simpset_of thy') 1])); |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
103 |
val ctuple_induct_thm = |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
104 |
(space_implode "_" names ^ "_induct", ctuple_fixdef_thm RS def_fix_ind); |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
105 |
|
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
106 |
fun unfolds [] thm = [] |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
107 |
| unfolds (n::[]) thm = [(n^"_unfold", thm)] |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
108 |
| unfolds (n::ns) thm = let |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
109 |
val thmL = thm RS cpair_eqD1; |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
110 |
val thmR = thm RS cpair_eqD2; |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
111 |
in (n^"_unfold", thmL) :: unfolds ns thmR end; |
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
112 |
val unfold_thms = unfolds names ctuple_unfold_thm; |
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
113 |
val thms = ctuple_induct_thm :: unfold_thms; |
18377 | 114 |
val (_, thy'') = PureThy.add_thms (map Thm.no_attributes thms) thy'; |
16226 | 115 |
in |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
116 |
(thy'', names, fixdef_thms, map snd unfold_thms) |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
117 |
end; |
16226 | 118 |
|
16387 | 119 |
(*************************************************************************) |
120 |
(*********** monadic notation and pattern matching compilation ***********) |
|
121 |
(*************************************************************************) |
|
16226 | 122 |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
123 |
fun add_names (Const(a,_), bs) = Sign.base_name a ins_string bs |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
124 |
| add_names (Free(a,_) , bs) = a ins_string bs |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
125 |
| add_names (f $ u , bs) = add_names (f, add_names(u, bs)) |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
126 |
| add_names (Abs(a,_,t), bs) = add_names (t, a ins_string bs) |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
127 |
| add_names (_ , bs) = bs; |
16226 | 128 |
|
129 |
fun add_terms ts xs = foldr add_names xs ts; |
|
130 |
||
131 |
(* builds a monadic term for matching a constructor pattern *) |
|
16387 | 132 |
fun pre_build pat rhs vs taken = |
133 |
case pat of |
|
134 |
Const("Cfun.Rep_CFun",_)$f$(v as Free(n,T)) => |
|
16226 | 135 |
pre_build f rhs (v::vs) taken |
16387 | 136 |
| Const("Cfun.Rep_CFun",_)$f$x => |
16226 | 137 |
let val (rhs', v, taken') = pre_build x rhs [] taken; |
16387 | 138 |
in pre_build f rhs' (v::vs) taken' end |
139 |
| Const(c,T) => |
|
16226 | 140 |
let |
141 |
val n = variant taken "v"; |
|
16387 | 142 |
fun result_type (Type("Cfun.->",[_,T])) (x::xs) = result_type T xs |
16226 | 143 |
| result_type T _ = T; |
144 |
val v = Free(n, result_type T vs); |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
145 |
val m = "match_"^(extern_name(Sign.base_name c)); |
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
146 |
val k = lambda_ctuple vs rhs; |
16226 | 147 |
in |
16387 | 148 |
(%%:"Fixrec.bind"`(%%:m`v)`k, v, n::taken) |
16552 | 149 |
end |
150 |
| Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n) |
|
151 |
| _ => fixrec_err "pre_build: invalid pattern"; |
|
16226 | 152 |
|
153 |
(* builds a monadic term for matching a function definition pattern *) |
|
154 |
(* returns (name, arity, matcher) *) |
|
16387 | 155 |
fun building pat rhs vs taken = |
156 |
case pat of |
|
157 |
Const("Cfun.Rep_CFun", _)$f$(v as Free(n,T)) => |
|
16226 | 158 |
building f rhs (v::vs) taken |
16387 | 159 |
| Const("Cfun.Rep_CFun", _)$f$x => |
160 |
let val (rhs', v, taken') = pre_build x rhs [] taken; |
|
161 |
in building f rhs' (v::vs) taken' end |
|
16552 | 162 |
| Const(name,_) => (name, length vs, big_lambdas vs rhs) |
163 |
| _ => fixrec_err "function is not declared as constant in theory"; |
|
16226 | 164 |
|
16387 | 165 |
fun match_eq eq = |
16552 | 166 |
let val (lhs,rhs) = dest_eqs eq; |
167 |
in building lhs (%%:"Fixrec.return"`rhs) [] (add_terms [eq] []) end; |
|
16226 | 168 |
|
169 |
(* returns the sum (using +++) of the terms in ms *) |
|
170 |
(* also applies "run" to the result! *) |
|
171 |
fun fatbar arity ms = |
|
172 |
let |
|
173 |
fun unLAM 0 t = t |
|
174 |
| unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t |
|
16552 | 175 |
| unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs"; |
16226 | 176 |
fun reLAM 0 t = t |
16552 | 177 |
| reLAM n t = reLAM (n-1) (%%:"Cfun.Abs_CFun" $ Abs("",dummyT,t)); |
16226 | 178 |
fun mplus (x,y) = %%:"Fixrec.mplus"`x`y; |
179 |
val msum = foldr1 mplus (map (unLAM arity) ms); |
|
180 |
in |
|
181 |
reLAM arity (%%:"Fixrec.run"`msum) |
|
182 |
end; |
|
183 |
||
16387 | 184 |
fun unzip3 [] = ([],[],[]) |
185 |
| unzip3 ((x,y,z)::ts) = |
|
186 |
let val (xs,ys,zs) = unzip3 ts |
|
187 |
in (x::xs, y::ys, z::zs) end; |
|
16226 | 188 |
|
16387 | 189 |
(* this is the pattern-matching compiler function *) |
190 |
fun compile_pats eqs = |
|
191 |
let |
|
192 |
val ((n::names),(a::arities),mats) = unzip3 (map match_eq eqs); |
|
193 |
val cname = if forall (fn x => n=x) names then n |
|
16552 | 194 |
else fixrec_err "all equations in block must define the same function"; |
16387 | 195 |
val arity = if forall (fn x => a=x) arities then a |
16552 | 196 |
else fixrec_err "all equations in block must have the same arity"; |
16387 | 197 |
val rhs = fatbar arity mats; |
198 |
in |
|
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
199 |
mk_trp (%%:cname === rhs) |
16387 | 200 |
end; |
201 |
||
202 |
(*************************************************************************) |
|
203 |
(********************** Proving associated theorems **********************) |
|
204 |
(*************************************************************************) |
|
205 |
||
16552 | 206 |
(* proves a block of pattern matching equations as theorems, using unfold *) |
207 |
fun make_simps thy (unfold_thm, eqns) = |
|
16226 | 208 |
let |
17985 | 209 |
val tacs = [rtac (unfold_thm RS ssubst_lhs) 1, asm_simp_tac (simpset_of thy) 1]; |
210 |
fun prove_term t = standard (Goal.prove thy [] [] t (K (EVERY tacs))); |
|
16552 | 211 |
fun prove_eqn ((name, eqn_t), atts) = ((name, prove_term eqn_t), atts); |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
212 |
in |
16552 | 213 |
map prove_eqn eqns |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
214 |
end; |
16226 | 215 |
|
16387 | 216 |
(*************************************************************************) |
217 |
(************************* Main fixrec function **************************) |
|
218 |
(*************************************************************************) |
|
16226 | 219 |
|
16552 | 220 |
fun gen_add_fixrec prep_prop prep_attrib strict blocks thy = |
16226 | 221 |
let |
16552 | 222 |
val eqns = List.concat blocks; |
223 |
val lengths = map length blocks; |
|
224 |
||
225 |
val ((names, srcss), strings) = apfst split_list (split_list eqns); |
|
226 |
val atts = map (map (prep_attrib thy)) srcss; |
|
227 |
val eqn_ts = map (prep_prop thy) strings; |
|
228 |
val rec_ts = map (fn eq => chead_of (fst (dest_eqs (Logic.strip_imp_concl eq))) |
|
16628 | 229 |
handle TERM _ => fixrec_eq_err thy "not a proper equation" eq) eqn_ts; |
230 |
val (_, eqn_ts') = InductivePackage.unify_consts thy rec_ts eqn_ts; |
|
16552 | 231 |
|
232 |
fun unconcat [] _ = [] |
|
233 |
| unconcat (n::ns) xs = List.take (xs,n) :: unconcat ns (List.drop (xs,n)); |
|
234 |
val pattern_blocks = unconcat lengths (map Logic.strip_imp_concl eqn_ts'); |
|
16628 | 235 |
val compiled_ts = map (infer thy o compile_pats) pattern_blocks; |
16552 | 236 |
val (thy', cnames, fixdef_thms, unfold_thms) = add_fixdefs compiled_ts thy; |
16226 | 237 |
in |
16552 | 238 |
if strict then let (* only prove simp rules if strict = true *) |
239 |
val eqn_blocks = unconcat lengths ((names ~~ eqn_ts') ~~ atts); |
|
240 |
val simps = List.concat (map (make_simps thy') (unfold_thms ~~ eqn_blocks)); |
|
18377 | 241 |
val (simp_thms, thy'') = PureThy.add_thms simps thy'; |
16552 | 242 |
|
243 |
val simp_names = map (fn name => name^"_simps") cnames; |
|
18728 | 244 |
val simp_attribute = rpair [Simplifier.simp_add]; |
16552 | 245 |
val simps' = map simp_attribute (simp_names ~~ unconcat lengths simp_thms); |
246 |
in |
|
18377 | 247 |
(snd o PureThy.add_thmss simps') thy'' |
16552 | 248 |
end |
249 |
else thy' |
|
16226 | 250 |
end; |
251 |
||
18728 | 252 |
val add_fixrec = gen_add_fixrec Sign.read_prop Attrib.attribute; |
16488 | 253 |
val add_fixrec_i = gen_add_fixrec Sign.cert_prop (K I); |
254 |
||
255 |
||
16387 | 256 |
(*************************************************************************) |
257 |
(******************************** Fixpat *********************************) |
|
258 |
(*************************************************************************) |
|
16226 | 259 |
|
16552 | 260 |
fun fix_pat thy t = |
16226 | 261 |
let |
16387 | 262 |
val T = fastype_of t; |
16401
57c35ede00b9
fixrec package now handles mutually-recursive definitions
huffman
parents:
16387
diff
changeset
|
263 |
val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T)); |
16461
e6b431cb8e0c
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents:
16402
diff
changeset
|
264 |
val cname = case chead_of t of Const(c,_) => c | _ => |
16552 | 265 |
fixrec_err "function is not declared as constant in theory"; |
16488 | 266 |
val unfold_thm = PureThy.get_thm thy (Name (cname^"_unfold")); |
17985 | 267 |
val simp = standard (Goal.prove thy [] [] eq |
268 |
(fn _ => EVERY [stac unfold_thm 1, simp_tac (simpset_of thy) 1])); |
|
16552 | 269 |
in simp end; |
16226 | 270 |
|
16552 | 271 |
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy = |
16402 | 272 |
let |
16552 | 273 |
val atts = map (prep_attrib thy) srcs; |
274 |
val ts = map (prep_term thy) strings; |
|
275 |
val simps = map (fix_pat thy) ts; |
|
276 |
in |
|
18377 | 277 |
(snd o PureThy.add_thmss [((name, simps), atts)]) thy |
16552 | 278 |
end; |
16226 | 279 |
|
18728 | 280 |
val add_fixpat = gen_add_fixpat Sign.read_term Attrib.attribute; |
16488 | 281 |
val add_fixpat_i = gen_add_fixpat Sign.cert_term (K I); |
282 |
||
283 |
||
16387 | 284 |
(*************************************************************************) |
285 |
(******************************** Parsers ********************************) |
|
286 |
(*************************************************************************) |
|
16226 | 287 |
|
17057 | 288 |
local structure P = OuterParse and K = OuterKeyword in |
16226 | 289 |
|
16552 | 290 |
val fixrec_eqn = P.opt_thm_name ":" -- P.prop; |
291 |
||
16628 | 292 |
val fixrec_strict = P.opt_keyword "permissive" >> not; |
16552 | 293 |
|
294 |
val fixrec_decl = fixrec_strict -- P.and_list1 (Scan.repeat1 fixrec_eqn); |
|
16226 | 295 |
|
296 |
(* this builds a parser for a new keyword, fixrec, whose functionality |
|
297 |
is defined by add_fixrec *) |
|
298 |
val fixrecP = |
|
16402 | 299 |
OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl |
16552 | 300 |
(fixrec_decl >> (Toplevel.theory o uncurry add_fixrec)); |
16226 | 301 |
|
302 |
(* fixpat parser *) |
|
16552 | 303 |
val fixpat_decl = P.opt_thm_name ":" -- Scan.repeat1 P.prop; |
16226 | 304 |
|
305 |
val fixpatP = |
|
16402 | 306 |
OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl |
16226 | 307 |
(fixpat_decl >> (Toplevel.theory o add_fixpat)); |
308 |
||
16552 | 309 |
val _ = OuterSyntax.add_parsers [fixrecP, fixpatP]; |
16226 | 310 |
|
311 |
end; (* local structure *) |
|
312 |
||
313 |
end; (* struct *) |