author | haftmann |
Wed, 07 Jun 2006 16:55:39 +0200 | |
changeset 19818 | 5c5c1208a3fa |
parent 19688 | 877b763ded7e |
child 20046 | 9c8909fc5865 |
permissions | -rw-r--r-- |
5719 | 1 |
(* Title: HOL/Tools/primrec_package.ML |
5178 | 2 |
ID: $Id$ |
11539 | 3 |
Author: Stefan Berghofer, TU Muenchen and Norbert Voelker, FernUni Hagen |
5178 | 4 |
|
6359 | 5 |
Package for defining functions on datatypes by primitive recursion. |
5178 | 6 |
*) |
7 |
||
8 |
signature PRIMREC_PACKAGE = |
|
9 |
sig |
|
6427 | 10 |
val quiet_mode: bool ref |
15703 | 11 |
val add_primrec: string -> ((bstring * string) * Attrib.src list) list |
6359 | 12 |
-> theory -> theory * thm list |
19688 | 13 |
val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list |
14 |
-> theory -> theory * thm list |
|
18728 | 15 |
val add_primrec_i: string -> ((bstring * term) * attribute list) list |
6359 | 16 |
-> theory -> theory * thm list |
19636 | 17 |
val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list |
18 |
-> theory -> theory * thm list |
|
5178 | 19 |
end; |
20 |
||
21 |
structure PrimrecPackage : PRIMREC_PACKAGE = |
|
22 |
struct |
|
23 |
||
24 |
open DatatypeAux; |
|
25 |
||
26 |
exception RecError of string; |
|
27 |
||
28 |
fun primrec_err s = error ("Primrec definition error:\n" ^ s); |
|
29 |
fun primrec_eq_err sign s eq = |
|
7544
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents:
7152
diff
changeset
|
30 |
primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term sign eq)); |
6427 | 31 |
|
32 |
||
33 |
(* messages *) |
|
34 |
||
35 |
val quiet_mode = ref false; |
|
36 |
fun message s = if ! quiet_mode then () else writeln s; |
|
5178 | 37 |
|
6359 | 38 |
|
5178 | 39 |
(* preprocessing of equations *) |
40 |
||
41 |
fun process_eqn sign (eq, rec_fns) = |
|
42 |
let |
|
6032 | 43 |
val (lhs, rhs) = |
44 |
if null (term_vars eq) then |
|
45 |
HOLogic.dest_eq (HOLogic.dest_Trueprop eq) |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
46 |
handle TERM _ => raise RecError "not a proper equation" |
6032 | 47 |
else raise RecError "illegal schematic variable(s)"; |
5178 | 48 |
|
49 |
val (recfun, args) = strip_comb lhs; |
|
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
50 |
val fnameT = dest_Const recfun handle TERM _ => |
5178 | 51 |
raise RecError "function is not declared as constant in theory"; |
52 |
||
53 |
val (ls', rest) = take_prefix is_Free args; |
|
54 |
val (middle, rs') = take_suffix is_Free rest; |
|
55 |
val rpos = length ls'; |
|
56 |
||
57 |
val (constr, cargs') = if null middle then raise RecError "constructor missing" |
|
58 |
else strip_comb (hd middle); |
|
59 |
val (cname, T) = dest_Const constr |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
60 |
handle TERM _ => raise RecError "ill-formed constructor"; |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
61 |
val (tname, _) = dest_Type (body_type T) handle TYPE _ => |
5178 | 62 |
raise RecError "cannot determine datatype associated with function" |
63 |
||
6037 | 64 |
val (ls, cargs, rs) = (map dest_Free ls', |
65 |
map dest_Free cargs', |
|
66 |
map dest_Free rs') |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
67 |
handle TERM _ => raise RecError "illegal argument in pattern"; |
5178 | 68 |
val lfrees = ls @ rs @ cargs; |
69 |
||
12474 | 70 |
fun check_vars _ [] = () |
71 |
| check_vars s vars = raise RecError (s ^ commas_quote (map fst vars)) |
|
5178 | 72 |
in |
8973 | 73 |
if length middle > 1 then |
5178 | 74 |
raise RecError "more than one non-variable in pattern" |
12474 | 75 |
else |
18964 | 76 |
(check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees); |
12474 | 77 |
check_vars "extra variables on rhs: " |
78 |
(map dest_Free (term_frees rhs) \\ lfrees); |
|
17184 | 79 |
case AList.lookup (op =) rec_fns fnameT of |
15531 | 80 |
NONE => |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
81 |
(fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns |
15531 | 82 |
| SOME (_, rpos', eqns) => |
17184 | 83 |
if AList.defined (op =) eqns cname then |
6037 | 84 |
raise RecError "constructor already occurred as pattern" |
5178 | 85 |
else if rpos <> rpos' then |
86 |
raise RecError "position of recursive argument inconsistent" |
|
87 |
else |
|
17314 | 88 |
AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns)) |
89 |
rec_fns) |
|
5178 | 90 |
end |
91 |
handle RecError s => primrec_eq_err sign s eq; |
|
92 |
||
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
93 |
fun process_fun sign descr rec_eqns ((i, fnameT as (fname, _)), (fnameTs, fnss)) = |
5178 | 94 |
let |
15570 | 95 |
val (_, (tname, _, constrs)) = List.nth (descr, i); |
5178 | 96 |
|
97 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) |
|
98 |
||
99 |
fun subst [] x = x |
|
100 |
| subst subs (fs, Abs (a, T, t)) = |
|
101 |
let val (fs', t') = subst subs (fs, t) |
|
102 |
in (fs', Abs (a, T, t')) end |
|
103 |
| subst subs (fs, t as (_ $ _)) = |
|
104 |
let val (f, ts) = strip_comb t; |
|
105 |
in |
|
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
106 |
if is_Const f andalso dest_Const f mem map fst rec_eqns then |
5178 | 107 |
let |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
108 |
val fnameT' as (fname', _) = dest_Const f; |
17184 | 109 |
val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT'); |
15570 | 110 |
val ls = Library.take (rpos, ts); |
111 |
val rest = Library.drop (rpos, ts); |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
112 |
val (x', rs) = (hd rest, tl rest) |
15570 | 113 |
handle Empty => raise RecError ("not enough arguments\ |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
114 |
\ in recursive application\nof function " ^ quote fname' ^ " on rhs"); |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
115 |
val (x, xs) = strip_comb x' |
5178 | 116 |
in |
17184 | 117 |
(case AList.lookup (op =) subs x of |
15531 | 118 |
NONE => |
5178 | 119 |
let |
120 |
val (fs', ts') = foldl_map (subst subs) (fs, ts) |
|
121 |
in (fs', list_comb (f, ts')) end |
|
15531 | 122 |
| SOME (i', y) => |
5178 | 123 |
let |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
124 |
val (fs', ts') = foldl_map (subst subs) (fs, xs @ ls @ rs); |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
125 |
val fs'' = process_fun sign descr rec_eqns ((i', fnameT'), fs') |
5178 | 126 |
in (fs'', list_comb (y, ts')) |
127 |
end) |
|
128 |
end |
|
129 |
else |
|
130 |
let |
|
131 |
val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts) |
|
132 |
in (fs', list_comb (f', ts')) end |
|
133 |
end |
|
134 |
| subst _ x = x; |
|
135 |
||
136 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
137 |
||
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
138 |
fun trans eqns ((cname, cargs), (fnameTs', fnss', fns)) = |
17184 | 139 |
(case AList.lookup (op =) eqns cname of |
15531 | 140 |
NONE => (warning ("No equation for constructor " ^ quote cname ^ |
6427 | 141 |
"\nin definition of function " ^ quote fname); |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
142 |
(fnameTs', fnss', (Const ("arbitrary", dummyT))::fns)) |
15531 | 143 |
| SOME (ls, cargs', rs, rhs, eq) => |
5178 | 144 |
let |
15570 | 145 |
val recs = List.filter (is_rec_type o snd) (cargs' ~~ cargs); |
5178 | 146 |
val rargs = map fst recs; |
6037 | 147 |
val subs = map (rpair dummyT o fst) |
148 |
(rev (rename_wrt_term rhs rargs)); |
|
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
149 |
val ((fnameTs'', fnss''), rhs') = |
6037 | 150 |
(subst (map (fn ((x, y), z) => |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
12876
diff
changeset
|
151 |
(Free x, (body_index y, Free z))) |
6037 | 152 |
(recs ~~ subs)) |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
153 |
((fnameTs', fnss'), rhs)) |
5178 | 154 |
handle RecError s => primrec_eq_err sign s eq |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
155 |
in (fnameTs'', fnss'', |
6037 | 156 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) |
5178 | 157 |
end) |
158 |
||
17184 | 159 |
in (case AList.lookup (op =) fnameTs i of |
15531 | 160 |
NONE => |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
161 |
if exists (equal fnameT o snd) fnameTs then |
6427 | 162 |
raise RecError ("inconsistent functions for datatype " ^ quote tname) |
5178 | 163 |
else |
164 |
let |
|
17184 | 165 |
val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT); |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
166 |
val (fnameTs', fnss', fns) = foldr (trans eqns) |
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
167 |
((i, fnameT)::fnameTs, fnss, []) constrs |
5178 | 168 |
in |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
169 |
(fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') |
5178 | 170 |
end |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
171 |
| SOME fnameT' => |
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
172 |
if fnameT = fnameT' then (fnameTs, fnss) |
6427 | 173 |
else raise RecError ("inconsistent functions for datatype " ^ quote tname)) |
5178 | 174 |
end; |
175 |
||
6359 | 176 |
|
5178 | 177 |
(* prepare functions needed for definitions *) |
178 |
||
179 |
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) = |
|
17184 | 180 |
case AList.lookup (op =) fns i of |
15531 | 181 |
NONE => |
5178 | 182 |
let |
183 |
val dummy_fns = map (fn (_, cargs) => Const ("arbitrary", |
|
15570 | 184 |
replicate ((length cargs) + (length (List.filter is_rec_type cargs))) |
5178 | 185 |
dummyT ---> HOLogic.unitT)) constrs; |
6427 | 186 |
val _ = warning ("No function definition for datatype " ^ quote tname) |
5178 | 187 |
in |
188 |
(dummy_fns @ fs, defs) |
|
189 |
end |
|
15531 | 190 |
| SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs); |
5178 | 191 |
|
6359 | 192 |
|
5178 | 193 |
(* make definition *) |
194 |
||
195 |
fun make_def sign fs (fname, ls, rec_name, tname) = |
|
196 |
let |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
197 |
val rhs = foldr (fn (T, t) => Abs ("", T, t)) |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
198 |
(list_comb (Const (rec_name, dummyT), |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
199 |
fs @ map Bound (0 ::(length ls downto 1)))) |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
200 |
((map snd ls) @ [dummyT]); |
5178 | 201 |
val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def", |
6037 | 202 |
Logic.mk_equals (Const (fname, dummyT), rhs)) |
6394 | 203 |
in Theory.inferT_axm sign defpair end; |
5178 | 204 |
|
6359 | 205 |
|
5178 | 206 |
(* find datatypes which contain all datatypes in tnames' *) |
207 |
||
208 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = [] |
|
209 |
| find_dts dt_info tnames' (tname::tnames) = |
|
17412 | 210 |
(case Symtab.lookup dt_info tname of |
15531 | 211 |
NONE => primrec_err (quote tname ^ " is not a datatype") |
212 |
| SOME dt => |
|
5178 | 213 |
if tnames' subset (map (#1 o snd) (#descr dt)) then |
214 |
(tname, dt)::(find_dts dt_info tnames' tnames) |
|
215 |
else find_dts dt_info tnames' tnames); |
|
216 |
||
8432 | 217 |
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns = |
218 |
let |
|
219 |
fun constrs_of (_, (_, _, cs)) = |
|
220 |
map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs; |
|
17184 | 221 |
val params_of = these o AList.lookup (op =) (List.concat (map constrs_of rec_eqns)); |
8432 | 222 |
in |
223 |
induction |
|
15570 | 224 |
|> RuleCases.rename_params (map params_of (List.concat (map (map #1 o #3 o #2) descr))) |
10525 | 225 |
|> RuleCases.save induction |
8432 | 226 |
end; |
227 |
||
19688 | 228 |
fun gen_primrec_i unchecked alt_name eqns_atts thy = |
5178 | 229 |
let |
6359 | 230 |
val (eqns, atts) = split_list eqns_atts; |
6394 | 231 |
val sg = Theory.sign_of thy; |
5178 | 232 |
val dt_info = DatatypePackage.get_datatypes thy; |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
233 |
val rec_eqns = foldr (process_eqn sg) [] (map snd eqns); |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18964
diff
changeset
|
234 |
val tnames = distinct (op =) (map (#1 o snd) rec_eqns); |
5178 | 235 |
val dts = find_dts dt_info tnames tnames; |
6037 | 236 |
val main_fns = |
18362
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
237 |
map (fn (tname, {index, ...}) => |
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
238 |
(index, |
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
239 |
(fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns)) |
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
240 |
dts; |
6037 | 241 |
val {descr, rec_names, rec_rewrites, ...} = |
18362
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
242 |
if null dts then |
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
243 |
primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive") |
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
244 |
else snd (hd dts); |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
245 |
val (fnameTs, fnss) = foldr (process_fun sg descr rec_eqns) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
246 |
([], []) main_fns; |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
247 |
val (fs, defs) = foldr (get_fns fnss) ([], []) (descr ~~ rec_names); |
5178 | 248 |
val defs' = map (make_def sg fs) defs; |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
249 |
val nameTs1 = map snd fnameTs; |
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
250 |
val nameTs2 = map fst rec_eqns; |
8432 | 251 |
val primrec_name = |
252 |
if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name; |
|
18358 | 253 |
val (defs_thms', thy') = thy |> Theory.add_path primrec_name |> |
19636 | 254 |
(if eq_set (nameTs1, nameTs2) then |
255 |
((if unchecked then PureThy.add_defs_unchecked_i else PureThy.add_defs_i) false o |
|
256 |
map Thm.no_attributes) defs' |
|
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
257 |
else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^ |
5216 | 258 |
"\nare not mutually recursive")); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
12876
diff
changeset
|
259 |
val rewrites = (map mk_meta_eq rec_rewrites) @ defs_thms'; |
16765
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
260 |
val _ = message ("Proving equations for primrec function(s) " ^ |
b8b1f310877f
Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents:
16646
diff
changeset
|
261 |
commas_quote (map fst nameTs1) ^ " ..."); |
17985 | 262 |
val simps = map (fn (_, t) => standard (Goal.prove thy' [] [] t |
263 |
(fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1]))) eqns; |
|
18377 | 264 |
val (simps', thy'') = PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts) thy'; |
9575
af71f5f4ca6b
Equations that are added to the simpset now have proper names.
berghofe
parents:
9315
diff
changeset
|
265 |
val thy''' = thy'' |
18377 | 266 |
|> (snd o PureThy.add_thmss [(("simps", simps'), |
18728 | 267 |
[Simplifier.simp_add, RecfunCodegen.add NONE])]) |
18377 | 268 |
|> (snd o PureThy.add_thms [(("induct", prepare_induct (#2 (hd dts)) rec_eqns), [])]) |
9575
af71f5f4ca6b
Equations that are added to the simpset now have proper names.
berghofe
parents:
9315
diff
changeset
|
269 |
|> Theory.parent_path |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
270 |
in |
12448
473cb9f9e237
Recursive equations to be used for code generation are now registered
berghofe
parents:
12311
diff
changeset
|
271 |
(thy''', simps') |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
272 |
end; |
6359 | 273 |
|
19688 | 274 |
fun gen_primrec unchecked alt_name eqns thy = |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
275 |
let |
7544
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents:
7152
diff
changeset
|
276 |
val sign = Theory.sign_of thy; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
277 |
val ((names, strings), srcss) = apfst split_list (split_list eqns); |
18728 | 278 |
val atts = map (map (Attrib.attribute thy)) srcss; |
14258
9bd184c007f0
Improved error handling: add_primrec now prints out ill-formed equation
berghofe
parents:
13641
diff
changeset
|
279 |
val eqn_ts = map (fn s => term_of (Thm.read_cterm sign (s, propT)) |
18678 | 280 |
handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
281 |
val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq))) |
7544
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents:
7152
diff
changeset
|
282 |
handle TERM _ => primrec_eq_err sign "not a proper equation" eq) eqn_ts; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
283 |
val (_, eqn_ts') = InductivePackage.unify_consts (sign_of thy) rec_ts eqn_ts |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
284 |
in |
19688 | 285 |
gen_primrec_i unchecked alt_name (names ~~ eqn_ts' ~~ atts) thy |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
286 |
end; |
6359 | 287 |
|
19688 | 288 |
val add_primrec = gen_primrec false; |
289 |
val add_primrec_unchecked = gen_primrec true; |
|
290 |
val add_primrec_i = gen_primrec_i false; |
|
291 |
val add_primrec_unchecked_i = gen_primrec_i true; |
|
292 |
||
5178 | 293 |
|
6359 | 294 |
(* outer syntax *) |
295 |
||
17057 | 296 |
local structure P = OuterParse and K = OuterKeyword in |
6359 | 297 |
|
19688 | 298 |
val opt_unchecked_name = |
299 |
Scan.optional (P.$$$ "(" |-- P.!!! |
|
300 |
(((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" || |
|
301 |
P.name >> pair false) --| P.$$$ ")")) (false, ""); |
|
302 |
||
6359 | 303 |
val primrec_decl = |
19688 | 304 |
opt_unchecked_name -- Scan.repeat1 (P.opt_thm_name ":" -- P.prop); |
6359 | 305 |
|
306 |
val primrecP = |
|
6723 | 307 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
19688 | 308 |
(primrec_decl >> (fn ((unchecked, alt_name), eqns) => |
309 |
Toplevel.theory (#1 o |
|
310 |
(if unchecked then add_primrec_unchecked else add_primrec) alt_name |
|
311 |
(map P.triple_swap eqns)))); |
|
6359 | 312 |
|
313 |
val _ = OuterSyntax.add_parsers [primrecP]; |
|
5178 | 314 |
|
315 |
end; |
|
6384 | 316 |
|
317 |
||
318 |
end; |
|
15705 | 319 |