author | wenzelm |
Tue, 03 Mar 2009 14:07:43 +0100 | |
changeset 30211 | 556d1810cdad |
parent 30158 | 83c50c62cf23 |
child 30223 | 24d975352879 |
permissions | -rw-r--r-- |
23152 | 1 |
(* Title: HOLCF/Tools/fixrec_package.ML |
2 |
Author: Amber Telfer and Brian Huffman |
|
3 |
||
4 |
Recursive function definition package for HOLCF. |
|
5 |
*) |
|
6 |
||
7 |
signature FIXREC_PACKAGE = |
|
8 |
sig |
|
9 |
val legacy_infer_term: theory -> term -> term |
|
10 |
val legacy_infer_prop: theory -> term -> term |
|
30132 | 11 |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
12 |
val add_fixrec: bool -> (binding * string option * mixfix) list |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
13 |
-> (Attrib.binding * string) list -> local_theory -> local_theory |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
14 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
15 |
val add_fixrec_i: bool -> (binding * typ option * mixfix) list |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
16 |
-> (Attrib.binding * term) list -> local_theory -> local_theory |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
17 |
|
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
18 |
val add_fixpat: Attrib.binding * string list -> theory -> theory |
30211 | 19 |
val add_fixpat_i: Thm.binding * term list -> theory -> theory |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
20 |
val add_matchers: (string * string) list -> theory -> theory |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
21 |
val setup: theory -> theory |
23152 | 22 |
end; |
23 |
||
24 |
structure FixrecPackage: FIXREC_PACKAGE = |
|
25 |
struct |
|
26 |
||
27 |
(* legacy type inference *) |
|
30132 | 28 |
(* used by the domain package *) |
23152 | 29 |
fun legacy_infer_term thy t = |
24493
d4380e9b287b
replaced ProofContext.infer_types by general Syntax.check_terms;
wenzelm
parents:
23779
diff
changeset
|
30 |
singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t); |
23152 | 31 |
|
24680 | 32 |
fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t); |
23152 | 33 |
|
34 |
||
26045 | 35 |
val fix_eq2 = @{thm fix_eq2}; |
36 |
val def_fix_ind = @{thm def_fix_ind}; |
|
23152 | 37 |
|
38 |
||
39 |
fun fixrec_err s = error ("fixrec definition error:\n" ^ s); |
|
40 |
fun fixrec_eq_err thy s eq = |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26343
diff
changeset
|
41 |
fixrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq)); |
23152 | 42 |
|
30132 | 43 |
(*************************************************************************) |
44 |
(***************************** building types ****************************) |
|
45 |
(*************************************************************************) |
|
46 |
||
23152 | 47 |
(* ->> is taken from holcf_logic.ML *) |
30132 | 48 |
fun cfunT (T, U) = Type(@{type_name "->"}, [T, U]); |
49 |
||
50 |
infixr 6 ->>; val (op ->>) = cfunT; |
|
51 |
||
52 |
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U) |
|
53 |
| dest_cfunT T = raise TYPE ("dest_cfunT", [T], []); |
|
54 |
||
55 |
fun binder_cfun (Type(@{type_name "->"},[T, U])) = T :: binder_cfun U |
|
56 |
| binder_cfun _ = []; |
|
57 |
||
58 |
fun body_cfun (Type(@{type_name "->"},[T, U])) = body_cfun U |
|
59 |
| body_cfun T = T; |
|
23152 | 60 |
|
30132 | 61 |
fun strip_cfun T : typ list * typ = |
62 |
(binder_cfun T, body_cfun T); |
|
63 |
||
64 |
fun maybeT T = Type(@{type_name "maybe"}, [T]); |
|
65 |
||
66 |
fun dest_maybeT (Type(@{type_name "maybe"}, [T])) = T |
|
67 |
| dest_maybeT T = raise TYPE ("dest_maybeT", [T], []); |
|
68 |
||
69 |
fun tupleT [] = @{typ "unit"} |
|
70 |
| tupleT [T] = T |
|
71 |
| tupleT (T :: Ts) = HOLogic.mk_prodT (T, tupleT Ts); |
|
72 |
||
73 |
fun matchT T = body_cfun T ->> maybeT (tupleT (binder_cfun T)); |
|
74 |
||
75 |
(*************************************************************************) |
|
76 |
(***************************** building terms ****************************) |
|
77 |
(*************************************************************************) |
|
23152 | 78 |
|
79 |
val mk_trp = HOLogic.mk_Trueprop; |
|
80 |
||
81 |
(* splits a cterm into the right and lefthand sides of equality *) |
|
82 |
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t); |
|
83 |
||
84 |
(* similar to Thm.head_of, but for continuous application *) |
|
26045 | 85 |
fun chead_of (Const(@{const_name Rep_CFun},_)$f$t) = chead_of f |
23152 | 86 |
| chead_of u = u; |
87 |
||
30132 | 88 |
fun capply_const (S, T) = |
89 |
Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T)); |
|
90 |
||
91 |
fun cabs_const (S, T) = |
|
92 |
Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T)); |
|
93 |
||
94 |
fun mk_capply (t, u) = |
|
95 |
let val (S, T) = |
|
96 |
case Term.fastype_of t of |
|
97 |
Type(@{type_name "->"}, [S, T]) => (S, T) |
|
98 |
| _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]); |
|
99 |
in capply_const (S, T) $ t $ u end; |
|
100 |
||
101 |
infix 0 ==; val (op ==) = Logic.mk_equals; |
|
102 |
infix 1 ===; val (op ===) = HOLogic.mk_eq; |
|
103 |
infix 9 ` ; val (op `) = mk_capply; |
|
104 |
||
105 |
||
106 |
fun mk_cpair (t, u) = |
|
107 |
let val T = Term.fastype_of t |
|
108 |
val U = Term.fastype_of u |
|
109 |
val cpairT = T ->> U ->> HOLogic.mk_prodT (T, U) |
|
110 |
in Const(@{const_name cpair}, cpairT) ` t ` u end; |
|
111 |
||
112 |
fun mk_cfst t = |
|
113 |
let val T = Term.fastype_of t; |
|
114 |
val (U, _) = HOLogic.dest_prodT T; |
|
115 |
in Const(@{const_name cfst}, T ->> U) ` t end; |
|
116 |
||
117 |
fun mk_csnd t = |
|
118 |
let val T = Term.fastype_of t; |
|
119 |
val (_, U) = HOLogic.dest_prodT T; |
|
120 |
in Const(@{const_name csnd}, T ->> U) ` t end; |
|
121 |
||
122 |
fun mk_csplit t = |
|
123 |
let val (S, TU) = dest_cfunT (Term.fastype_of t); |
|
124 |
val (T, U) = dest_cfunT TU; |
|
125 |
val csplitT = (S ->> T ->> U) ->> HOLogic.mk_prodT (S, T) ->> U; |
|
126 |
in Const(@{const_name csplit}, csplitT) ` t end; |
|
23152 | 127 |
|
128 |
(* builds the expression (LAM v. rhs) *) |
|
30132 | 129 |
fun big_lambda v rhs = |
130 |
cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs; |
|
23152 | 131 |
|
132 |
(* builds the expression (LAM v1 v2 .. vn. rhs) *) |
|
133 |
fun big_lambdas [] rhs = rhs |
|
134 |
| big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs); |
|
135 |
||
136 |
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *) |
|
30132 | 137 |
fun lambda_ctuple [] rhs = big_lambda (Free("unit", HOLogic.unitT)) rhs |
23152 | 138 |
| lambda_ctuple (v::[]) rhs = big_lambda v rhs |
139 |
| lambda_ctuple (v::vs) rhs = |
|
30132 | 140 |
mk_csplit (big_lambda v (lambda_ctuple vs rhs)); |
23152 | 141 |
|
142 |
(* builds the expression <v1,v2,..,vn> *) |
|
30132 | 143 |
fun mk_ctuple [] = @{term "UU::unit"} |
23152 | 144 |
| mk_ctuple (t::[]) = t |
30132 | 145 |
| mk_ctuple (t::ts) = mk_cpair (t, mk_ctuple ts); |
146 |
||
147 |
fun mk_return t = |
|
148 |
let val T = Term.fastype_of t |
|
149 |
in Const(@{const_name Fixrec.return}, T ->> maybeT T) ` t end; |
|
150 |
||
151 |
fun mk_bind (t, u) = |
|
152 |
let val (T, mU) = dest_cfunT (Term.fastype_of u); |
|
153 |
val bindT = maybeT T ->> (T ->> mU) ->> mU; |
|
154 |
in Const(@{const_name Fixrec.bind}, bindT) ` t ` u end; |
|
155 |
||
156 |
fun mk_mplus (t, u) = |
|
157 |
let val mT = Term.fastype_of t |
|
158 |
in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end; |
|
159 |
||
160 |
fun mk_run t = |
|
161 |
let val mT = Term.fastype_of t |
|
162 |
val T = dest_maybeT mT |
|
163 |
in Const(@{const_name Fixrec.run}, mT ->> T) ` t end; |
|
164 |
||
165 |
fun mk_fix t = |
|
166 |
let val (T, _) = dest_cfunT (Term.fastype_of t) |
|
167 |
in Const(@{const_name fix}, (T ->> T) ->> T) ` t end; |
|
23152 | 168 |
|
169 |
(*************************************************************************) |
|
170 |
(************* fixed-point definitions and unfolding theorems ************) |
|
171 |
(*************************************************************************) |
|
172 |
||
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
173 |
fun add_fixdefs |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
174 |
(fixes : ((binding * typ) * mixfix) list) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
175 |
(spec : (Attrib.binding * term) list) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
176 |
(lthy : local_theory) = |
23152 | 177 |
let |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
178 |
val names = map (Binding.base_name o fst o fst) fixes; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
179 |
val all_names = space_implode "_" names; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
180 |
val (lhss,rhss) = ListPair.unzip (map (dest_eqs o snd) spec); |
30132 | 181 |
val fixpoint = mk_fix (lambda_ctuple lhss (mk_ctuple rhss)); |
23152 | 182 |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
183 |
fun one_def (l as Free(n,_)) r = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
184 |
let val b = Sign.base_name n |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
185 |
in ((Binding.name (b^"_def"), []), r) end |
23152 | 186 |
| one_def _ _ = fixrec_err "fixdefs: lhs not of correct form"; |
187 |
fun defs [] _ = [] |
|
188 |
| defs (l::[]) r = [one_def l r] |
|
30132 | 189 |
| defs (l::ls) r = one_def l (mk_cfst r) :: defs ls (mk_csnd r); |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
190 |
val fixdefs = defs lhss fixpoint; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
191 |
val define_all = fold_map (LocalTheory.define Thm.definitionK); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
192 |
val (fixdef_thms : (term * (string * thm)) list, lthy') = lthy |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
193 |
|> define_all (map (apfst fst) fixes ~~ fixdefs); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
194 |
fun cpair_equalI (thm1, thm2) = @{thm cpair_equalI} OF [thm1, thm2]; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
195 |
val ctuple_fixdef_thm = foldr1 cpair_equalI (map (snd o snd) fixdef_thms); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
196 |
val ctuple_induct_thm = ctuple_fixdef_thm RS def_fix_ind; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
197 |
val ctuple_unfold_thm = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
198 |
Goal.prove lthy' [] [] (mk_trp (mk_ctuple lhss === mk_ctuple rhss)) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
199 |
(fn _ => EVERY [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1, |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
200 |
simp_tac (local_simpset_of lthy') 1]); |
23152 | 201 |
fun unfolds [] thm = [] |
202 |
| unfolds (n::[]) thm = [(n^"_unfold", thm)] |
|
203 |
| unfolds (n::ns) thm = let |
|
25132 | 204 |
val thmL = thm RS @{thm cpair_eqD1}; |
205 |
val thmR = thm RS @{thm cpair_eqD2}; |
|
23152 | 206 |
in (n^"_unfold", thmL) :: unfolds ns thmR end; |
207 |
val unfold_thms = unfolds names ctuple_unfold_thm; |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
208 |
fun mk_note (n, thm) = ((Binding.name n, []), [thm]); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
209 |
val (thmss, lthy'') = lthy' |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
210 |
|> fold_map (LocalTheory.note Thm.theoremK o mk_note) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
211 |
((all_names ^ "_induct", ctuple_induct_thm) :: unfold_thms); |
23152 | 212 |
in |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
213 |
(lthy'', names, fixdef_thms, map snd unfold_thms) |
23152 | 214 |
end; |
215 |
||
216 |
(*************************************************************************) |
|
217 |
(*********** monadic notation and pattern matching compilation ***********) |
|
218 |
(*************************************************************************) |
|
219 |
||
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
220 |
structure FixrecMatchData = TheoryDataFun ( |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
221 |
type T = string Symtab.table; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
222 |
val empty = Symtab.empty; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
223 |
val copy = I; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
224 |
val extend = I; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
225 |
fun merge _ tabs : T = Symtab.merge (K true) tabs; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
226 |
); |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
227 |
|
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
228 |
(* associate match functions with pattern constants *) |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
229 |
fun add_matchers ms = FixrecMatchData.map (fold Symtab.update ms); |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
230 |
|
30157 | 231 |
fun taken_names (t : term) : bstring list = |
232 |
let |
|
233 |
fun taken (Const(a,_), bs) = insert (op =) (Sign.base_name a) bs |
|
234 |
| taken (Free(a,_) , bs) = insert (op =) a bs |
|
235 |
| taken (f $ u , bs) = taken (f, taken (u, bs)) |
|
236 |
| taken (Abs(a,_,t), bs) = taken (t, insert (op =) a bs) |
|
237 |
| taken (_ , bs) = bs; |
|
238 |
in |
|
239 |
taken (t, []) |
|
240 |
end; |
|
23152 | 241 |
|
242 |
(* builds a monadic term for matching a constructor pattern *) |
|
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
243 |
fun pre_build match_name pat rhs vs taken = |
23152 | 244 |
case pat of |
26045 | 245 |
Const(@{const_name Rep_CFun},_)$f$(v as Free(n,T)) => |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
246 |
pre_build match_name f rhs (v::vs) taken |
26045 | 247 |
| Const(@{const_name Rep_CFun},_)$f$x => |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
248 |
let val (rhs', v, taken') = pre_build match_name x rhs [] taken; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
249 |
in pre_build match_name f rhs' (v::vs) taken' end |
23152 | 250 |
| Const(c,T) => |
251 |
let |
|
252 |
val n = Name.variant taken "v"; |
|
26045 | 253 |
fun result_type (Type(@{type_name "->"},[_,T])) (x::xs) = result_type T xs |
23152 | 254 |
| result_type T _ = T; |
255 |
val v = Free(n, result_type T vs); |
|
30132 | 256 |
val m = Const(match_name c, matchT T); |
23152 | 257 |
val k = lambda_ctuple vs rhs; |
258 |
in |
|
30132 | 259 |
(mk_bind (m`v, k), v, n::taken) |
23152 | 260 |
end |
261 |
| Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n) |
|
262 |
| _ => fixrec_err "pre_build: invalid pattern"; |
|
263 |
||
264 |
(* builds a monadic term for matching a function definition pattern *) |
|
265 |
(* returns (name, arity, matcher) *) |
|
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
266 |
fun building match_name pat rhs vs taken = |
23152 | 267 |
case pat of |
26045 | 268 |
Const(@{const_name Rep_CFun}, _)$f$(v as Free(n,T)) => |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
269 |
building match_name f rhs (v::vs) taken |
26045 | 270 |
| Const(@{const_name Rep_CFun}, _)$f$x => |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
271 |
let val (rhs', v, taken') = pre_build match_name x rhs [] taken; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
272 |
in building match_name f rhs' (v::vs) taken' end |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
273 |
| Free(_,_) => ((pat, length vs), big_lambdas vs rhs) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
274 |
| Const(_,_) => ((pat, length vs), big_lambdas vs rhs) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
275 |
| _ => fixrec_err ("function is not declared as constant in theory: " |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
276 |
^ ML_Syntax.print_term pat); |
23152 | 277 |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
278 |
fun strip_alls t = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
279 |
if Logic.is_all t then strip_alls (snd (Logic.dest_all t)) else t; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
280 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
281 |
fun match_eq match_name eq = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
282 |
let |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
283 |
val (lhs,rhs) = dest_eqs (Logic.strip_imp_concl (strip_alls eq)); |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
284 |
in |
30157 | 285 |
building match_name lhs (mk_return rhs) [] (taken_names eq) |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
286 |
end; |
23152 | 287 |
|
288 |
(* returns the sum (using +++) of the terms in ms *) |
|
289 |
(* also applies "run" to the result! *) |
|
290 |
fun fatbar arity ms = |
|
291 |
let |
|
30132 | 292 |
fun LAM_Ts 0 t = ([], Term.fastype_of t) |
293 |
| LAM_Ts n (_ $ Abs(_,T,t)) = |
|
294 |
let val (Ts, U) = LAM_Ts (n-1) t in (T::Ts, U) end |
|
295 |
| LAM_Ts _ _ = fixrec_err "fatbar: internal error, not enough LAMs"; |
|
23152 | 296 |
fun unLAM 0 t = t |
297 |
| unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t |
|
298 |
| unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs"; |
|
30132 | 299 |
fun reLAM ([], U) t = t |
300 |
| reLAM (T::Ts, U) t = reLAM (Ts, T ->> U) (cabs_const(T,U)$Abs("",T,t)); |
|
301 |
val msum = foldr1 mk_mplus (map (unLAM arity) ms); |
|
302 |
val (Ts, U) = LAM_Ts arity (hd ms) |
|
23152 | 303 |
in |
30132 | 304 |
reLAM (rev Ts, dest_maybeT U) (mk_run msum) |
23152 | 305 |
end; |
306 |
||
307 |
(* this is the pattern-matching compiler function *) |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
308 |
fun compile_pats match_name eqs = |
23152 | 309 |
let |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
310 |
val (((n::names),(a::arities)),mats) = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
311 |
apfst ListPair.unzip (ListPair.unzip (map (match_eq match_name) eqs)); |
23152 | 312 |
val cname = if forall (fn x => n=x) names then n |
313 |
else fixrec_err "all equations in block must define the same function"; |
|
314 |
val arity = if forall (fn x => a=x) arities then a |
|
315 |
else fixrec_err "all equations in block must have the same arity"; |
|
316 |
val rhs = fatbar arity mats; |
|
317 |
in |
|
30132 | 318 |
mk_trp (cname === rhs) |
23152 | 319 |
end; |
320 |
||
321 |
(*************************************************************************) |
|
322 |
(********************** Proving associated theorems **********************) |
|
323 |
(*************************************************************************) |
|
324 |
||
325 |
(* proves a block of pattern matching equations as theorems, using unfold *) |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
326 |
fun make_simps lthy (unfold_thm, eqns : (Attrib.binding * term) list) = |
23152 | 327 |
let |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
328 |
val tacs = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
329 |
[rtac (unfold_thm RS @{thm ssubst_lhs}) 1, |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
330 |
asm_simp_tac (local_simpset_of lthy) 1]; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
331 |
fun prove_term t = Goal.prove lthy [] [] t (K (EVERY tacs)); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
332 |
fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t); |
23152 | 333 |
in |
334 |
map prove_eqn eqns |
|
335 |
end; |
|
336 |
||
337 |
(*************************************************************************) |
|
338 |
(************************* Main fixrec function **************************) |
|
339 |
(*************************************************************************) |
|
340 |
||
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
341 |
local |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
342 |
(* code adapted from HOL/Tools/primrec_package.ML *) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
343 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
344 |
fun prepare_spec prep_spec ctxt raw_fixes raw_spec = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
345 |
let |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
346 |
val ((fixes, spec), _) = prep_spec |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
347 |
raw_fixes (map (single o apsnd single) raw_spec) ctxt |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
348 |
in (fixes, map (apsnd the_single) spec) end; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
349 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
350 |
fun gen_fixrec |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
351 |
(set_group : bool) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
352 |
(prep_spec : (binding * 'a option * mixfix) list -> |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
353 |
(Attrib.binding * 'b list) list list -> |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
354 |
Proof.context -> |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
355 |
(((binding * typ) * mixfix) list * (Attrib.binding * term list) list) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
356 |
* Proof.context |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
357 |
) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
358 |
(strict : bool) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
359 |
raw_fixes |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
360 |
raw_spec |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
361 |
(lthy : local_theory) = |
23152 | 362 |
let |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
363 |
val (fixes : ((binding * typ) * mixfix) list, |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
364 |
spec : (Attrib.binding * term) list) = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
365 |
prepare_spec prep_spec lthy raw_fixes raw_spec; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
366 |
val chead_of_spec = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
367 |
chead_of o fst o dest_eqs o Logic.strip_imp_concl o strip_alls o snd; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
368 |
fun name_of (Free (n, _)) = n |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
369 |
| name_of t = fixrec_err ("unknown term"); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
370 |
val all_names = map (name_of o chead_of_spec) spec; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
371 |
val names = distinct (op =) all_names; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
372 |
fun block_of_name n = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
373 |
map_filter |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
374 |
(fn (m,eq) => if m = n then SOME eq else NONE) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
375 |
(all_names ~~ spec); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
376 |
val blocks = map block_of_name names; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
377 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
378 |
val matcher_tab = FixrecMatchData.get (ProofContext.theory_of lthy); |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
379 |
fun match_name c = |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
380 |
case Symtab.lookup matcher_tab c of SOME m => m |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
381 |
| NONE => fixrec_err ("unknown pattern constructor: " ^ c); |
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
382 |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
383 |
val matches = map (compile_pats match_name) (map (map snd) blocks); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
384 |
val spec' = map (pair Attrib.empty_binding) matches; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
385 |
val (lthy', cnames, fixdef_thms, unfold_thms) = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
386 |
add_fixdefs fixes spec' lthy; |
23152 | 387 |
in |
388 |
if strict then let (* only prove simp rules if strict = true *) |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
389 |
val simps : (Attrib.binding * thm) list list = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
390 |
map (make_simps lthy') (unfold_thms ~~ blocks); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
391 |
fun mk_bind n : Attrib.binding = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
392 |
(Binding.name (n ^ "_simps"), |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
393 |
[Attrib.internal (K Simplifier.simp_add)]); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
394 |
val simps1 : (Attrib.binding * thm list) list = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
395 |
map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
396 |
val simps2 : (Attrib.binding * thm list) list = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
397 |
map (apsnd (fn thm => [thm])) (List.concat simps); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
398 |
val (_, lthy'') = lthy' |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
399 |
|> fold_map (LocalTheory.note Thm.theoremK) (simps1 @ simps2); |
23152 | 400 |
in |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
401 |
lthy'' |
23152 | 402 |
end |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
403 |
else lthy' |
23152 | 404 |
end; |
405 |
||
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
406 |
in |
23152 | 407 |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
408 |
val add_fixrec_i = gen_fixrec false Specification.check_specification; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
409 |
val add_fixrec = gen_fixrec true Specification.read_specification; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
410 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
411 |
end; (* local *) |
23152 | 412 |
|
413 |
(*************************************************************************) |
|
414 |
(******************************** Fixpat *********************************) |
|
415 |
(*************************************************************************) |
|
416 |
||
417 |
fun fix_pat thy t = |
|
418 |
let |
|
419 |
val T = fastype_of t; |
|
420 |
val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T)); |
|
421 |
val cname = case chead_of t of Const(c,_) => c | _ => |
|
422 |
fixrec_err "function is not declared as constant in theory"; |
|
26343
0dd2eab7b296
simplified get_thm(s): back to plain name argument;
wenzelm
parents:
26336
diff
changeset
|
423 |
val unfold_thm = PureThy.get_thm thy (cname^"_unfold"); |
23152 | 424 |
val simp = Goal.prove_global thy [] [] eq |
425 |
(fn _ => EVERY [stac unfold_thm 1, simp_tac (simpset_of thy) 1]); |
|
426 |
in simp end; |
|
427 |
||
428 |
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy = |
|
429 |
let |
|
430 |
val atts = map (prep_attrib thy) srcs; |
|
431 |
val ts = map (prep_term thy) strings; |
|
432 |
val simps = map (fix_pat thy) ts; |
|
433 |
in |
|
29585 | 434 |
(snd o PureThy.add_thmss [((name, simps), atts)]) thy |
23152 | 435 |
end; |
436 |
||
24707 | 437 |
val add_fixpat = gen_add_fixpat Syntax.read_term_global Attrib.attribute; |
23152 | 438 |
val add_fixpat_i = gen_add_fixpat Sign.cert_term (K I); |
439 |
||
440 |
||
441 |
(*************************************************************************) |
|
442 |
(******************************** Parsers ********************************) |
|
443 |
(*************************************************************************) |
|
444 |
||
445 |
local structure P = OuterParse and K = OuterKeyword in |
|
446 |
||
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
447 |
(* bool parser *) |
23152 | 448 |
val fixrec_strict = P.opt_keyword "permissive" >> not; |
449 |
||
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
450 |
fun pipe_error t = P.!!! (Scan.fail_with (K |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
451 |
(cat_lines ["Equations must be separated by " ^ quote "|", quote t]))); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
452 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
453 |
(* (Attrib.binding * string) parser *) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
454 |
val statement = SpecParse.opt_thm_name ":" -- P.prop --| Scan.ahead |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
455 |
((P.term :-- pipe_error) || Scan.succeed ("","")); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
456 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
457 |
(* ((Attrib.binding * string) list) parser *) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
458 |
val statements = P.enum1 "|" statement; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
459 |
|
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
460 |
(* (((xstring option * bool) * (Binding.binding * string option * Mixfix.mixfix) list) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
461 |
* (Attrib.binding * string) list) parser *) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
462 |
val fixrec_decl = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
463 |
P.opt_target -- fixrec_strict -- P.fixes --| P.$$$ "where" -- statements; |
23152 | 464 |
|
465 |
(* this builds a parser for a new keyword, fixrec, whose functionality |
|
466 |
is defined by add_fixrec *) |
|
24867 | 467 |
val _ = |
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
468 |
let |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
469 |
val desc = "define recursive functions (HOLCF)"; |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
470 |
fun fixrec (((opt_target, strict), raw_fixes), raw_spec) = |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
471 |
Toplevel.local_theory opt_target (add_fixrec strict raw_fixes raw_spec); |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
472 |
in |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
473 |
OuterSyntax.command "fixrec" desc K.thy_decl (fixrec_decl >> fixrec) |
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
474 |
end; |
23152 | 475 |
|
476 |
(* fixpat parser *) |
|
477 |
val fixpat_decl = SpecParse.opt_thm_name ":" -- Scan.repeat1 P.prop; |
|
478 |
||
24867 | 479 |
val _ = |
23152 | 480 |
OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl |
481 |
(fixpat_decl >> (Toplevel.theory o add_fixpat)); |
|
30158
83c50c62cf23
fixrec package uses new-style syntax and local-theory interface
huffman
parents:
30157
diff
changeset
|
482 |
|
26045 | 483 |
end; (* local structure *) |
23152 | 484 |
|
30131
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
485 |
val setup = FixrecMatchData.init; |
6be1be402ef0
use TheoryData to keep track of pattern match combinators
huffman
parents:
29585
diff
changeset
|
486 |
|
24867 | 487 |
end; |