author | wenzelm |
Tue, 14 Oct 2008 16:01:36 +0200 | |
changeset 28592 | 824f8390aaa2 |
parent 28521 | 5b426c051e36 |
child 28703 | aef727ef30e5 |
permissions | -rw-r--r-- |
5719 | 1 |
(* Title: HOL/Tools/primrec_package.ML |
5178 | 2 |
ID: $Id$ |
25557 | 3 |
Author: Stefan Berghofer, TU Muenchen; Norbert Voelker, FernUni Hagen; |
4 |
Florian Haftmann, TU Muenchen |
|
5178 | 5 |
|
6359 | 6 |
Package for defining functions on datatypes by primitive recursion. |
5178 | 7 |
*) |
8 |
||
9 |
signature PRIMREC_PACKAGE = |
|
10 |
sig |
|
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
11 |
val add_primrec: (Name.binding * typ option * mixfix) list -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
12 |
(Attrib.binding * term) list -> local_theory -> thm list * local_theory |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
13 |
val add_primrec_global: (Name.binding * typ option * mixfix) list -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
14 |
(Attrib.binding * term) list -> theory -> thm list * theory |
26679
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
15 |
val add_primrec_overloaded: (string * (string * typ) * bool) list -> |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
16 |
(Name.binding * typ option * mixfix) list -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
17 |
(Attrib.binding * term) list -> theory -> thm list * theory |
5178 | 18 |
end; |
19 |
||
20 |
structure PrimrecPackage : PRIMREC_PACKAGE = |
|
21 |
struct |
|
22 |
||
23 |
open DatatypeAux; |
|
24 |
||
25557 | 25 |
exception PrimrecError of string * term option; |
6359 | 26 |
|
25557 | 27 |
fun primrec_error msg = raise PrimrecError (msg, NONE); |
28 |
fun primrec_error_eqn msg eqn = raise PrimrecError (msg, SOME eqn); |
|
23765
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
29 |
|
25557 | 30 |
fun message s = if ! Toplevel.debug then () else writeln s; |
23765
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
31 |
|
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
32 |
|
5178 | 33 |
(* preprocessing of equations *) |
34 |
||
25559 | 35 |
fun process_eqn is_fixed spec rec_fns = |
5178 | 36 |
let |
25566 | 37 |
val (vs, Ts) = split_list (strip_qnt_vars "all" spec); |
25557 | 38 |
val body = strip_qnt_body "all" spec; |
25566 | 39 |
val (vs', _) = Name.variants vs (Name.make_context (fold_aterms |
40 |
(fn Free (v, _) => insert (op =) v | _ => I) body [])); |
|
41 |
val eqn = curry subst_bounds (map2 (curry Free) vs' Ts |> rev) body; |
|
25557 | 42 |
val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eqn) |
43 |
handle TERM _ => primrec_error "not a proper equation"; |
|
5178 | 44 |
val (recfun, args) = strip_comb lhs; |
25557 | 45 |
val fname = case recfun of Free (v, _) => if is_fixed v then v |
46 |
else primrec_error "illegal head of function equation" |
|
47 |
| _ => primrec_error "illegal head of function equation"; |
|
5178 | 48 |
|
49 |
val (ls', rest) = take_prefix is_Free args; |
|
50 |
val (middle, rs') = take_suffix is_Free rest; |
|
51 |
val rpos = length ls'; |
|
52 |
||
25557 | 53 |
val (constr, cargs') = if null middle then primrec_error "constructor missing" |
5178 | 54 |
else strip_comb (hd middle); |
55 |
val (cname, T) = dest_Const constr |
|
25557 | 56 |
handle TERM _ => primrec_error "ill-formed constructor"; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
57 |
val (tname, _) = dest_Type (body_type T) handle TYPE _ => |
25557 | 58 |
primrec_error "cannot determine datatype associated with function" |
5178 | 59 |
|
20176 | 60 |
val (ls, cargs, rs) = |
61 |
(map dest_Free ls', map dest_Free cargs', map dest_Free rs') |
|
25557 | 62 |
handle TERM _ => primrec_error "illegal argument in pattern"; |
5178 | 63 |
val lfrees = ls @ rs @ cargs; |
64 |
||
12474 | 65 |
fun check_vars _ [] = () |
25557 | 66 |
| check_vars s vars = primrec_error (s ^ commas_quote (map fst vars)) eqn; |
5178 | 67 |
in |
22692 | 68 |
if length middle > 1 then |
25557 | 69 |
primrec_error "more than one non-variable in pattern" |
12474 | 70 |
else |
18964 | 71 |
(check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees); |
12474 | 72 |
check_vars "extra variables on rhs: " |
25557 | 73 |
(map dest_Free (term_frees rhs) |> subtract (op =) lfrees |
25559 | 74 |
|> filter_out (is_fixed o fst)); |
25557 | 75 |
case AList.lookup (op =) rec_fns fname of |
15531 | 76 |
NONE => |
25557 | 77 |
(fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eqn))]))::rec_fns |
15531 | 78 |
| SOME (_, rpos', eqns) => |
17184 | 79 |
if AList.defined (op =) eqns cname then |
25557 | 80 |
primrec_error "constructor already occurred as pattern" |
5178 | 81 |
else if rpos <> rpos' then |
25557 | 82 |
primrec_error "position of recursive argument inconsistent" |
5178 | 83 |
else |
25557 | 84 |
AList.update (op =) |
85 |
(fname, (tname, rpos, (cname, (ls, cargs, rs, rhs, eqn))::eqns)) |
|
17314 | 86 |
rec_fns) |
25557 | 87 |
end handle PrimrecError (msg, NONE) => primrec_error_eqn msg spec; |
5178 | 88 |
|
25557 | 89 |
fun process_fun descr eqns (i, fname) (fnames, fnss) = |
5178 | 90 |
let |
25557 | 91 |
val (_, (tname, _, constrs)) = nth descr i; |
5178 | 92 |
|
93 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) |
|
94 |
||
21064 | 95 |
fun subst [] t fs = (t, fs) |
96 |
| subst subs (Abs (a, T, t)) fs = |
|
97 |
fs |
|
98 |
|> subst subs t |
|
99 |
|-> (fn t' => pair (Abs (a, T, t'))) |
|
100 |
| subst subs (t as (_ $ _)) fs = |
|
101 |
let |
|
102 |
val (f, ts) = strip_comb t; |
|
5178 | 103 |
in |
25557 | 104 |
if is_Free f |
105 |
andalso member (fn ((v, _), (w, _)) => v = w) eqns (dest_Free f) then |
|
5178 | 106 |
let |
25557 | 107 |
val (fname', _) = dest_Free f; |
108 |
val (_, rpos, _) = the (AList.lookup (op =) eqns fname'); |
|
27301
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
109 |
val (ls, rs) = chop rpos ts |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
110 |
val (x', rs') = case rs |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
111 |
of x' :: rs => (x', rs) |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
112 |
| [] => primrec_error ("not enough arguments in recursive application\n" |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
113 |
^ "of function " ^ quote fname' ^ " on rhs"); |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
114 |
val (x, xs) = strip_comb x'; |
21064 | 115 |
in case AList.lookup (op =) subs x |
116 |
of NONE => |
|
117 |
fs |
|
118 |
|> fold_map (subst subs) ts |
|
119 |
|-> (fn ts' => pair (list_comb (f, ts'))) |
|
120 |
| SOME (i', y) => |
|
121 |
fs |
|
27301
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
122 |
|> fold_map (subst subs) (xs @ ls @ rs') |
25557 | 123 |
||> process_fun descr eqns (i', fname') |
21064 | 124 |
|-> (fn ts' => pair (list_comb (y, ts'))) |
5178 | 125 |
end |
126 |
else |
|
21064 | 127 |
fs |
128 |
|> fold_map (subst subs) (f :: ts) |
|
129 |
|-> (fn (f'::ts') => pair (list_comb (f', ts'))) |
|
5178 | 130 |
end |
21064 | 131 |
| subst _ t fs = (t, fs); |
5178 | 132 |
|
133 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
134 |
||
25557 | 135 |
fun trans eqns (cname, cargs) (fnames', fnss', fns) = |
17184 | 136 |
(case AList.lookup (op =) eqns cname of |
15531 | 137 |
NONE => (warning ("No equation for constructor " ^ quote cname ^ |
6427 | 138 |
"\nin definition of function " ^ quote fname); |
25557 | 139 |
(fnames', fnss', (Const ("HOL.undefined", dummyT))::fns)) |
15531 | 140 |
| SOME (ls, cargs', rs, rhs, eq) => |
5178 | 141 |
let |
21064 | 142 |
val recs = filter (is_rec_type o snd) (cargs' ~~ cargs); |
5178 | 143 |
val rargs = map fst recs; |
22692 | 144 |
val subs = map (rpair dummyT o fst) |
20176 | 145 |
(rev (rename_wrt_term rhs rargs)); |
27301
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
146 |
val (rhs', (fnames'', fnss'')) = subst (map2 (fn (x, y) => fn z => |
bf7d82193a2e
fixed bind error for malformed primrec specifications
haftmann
parents:
26679
diff
changeset
|
147 |
(Free x, (body_index y, Free z))) recs subs) rhs (fnames', fnss') |
25557 | 148 |
handle PrimrecError (s, NONE) => primrec_error_eqn s eq |
149 |
in (fnames'', fnss'', |
|
20176 | 150 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) |
5178 | 151 |
end) |
152 |
||
25557 | 153 |
in (case AList.lookup (op =) fnames i of |
15531 | 154 |
NONE => |
25557 | 155 |
if exists (fn (_, v) => fname = v) fnames then |
156 |
primrec_error ("inconsistent functions for datatype " ^ quote tname) |
|
5178 | 157 |
else |
158 |
let |
|
25557 | 159 |
val (_, _, eqns) = the (AList.lookup (op =) eqns fname); |
160 |
val (fnames', fnss', fns) = fold_rev (trans eqns) constrs |
|
161 |
((i, fname)::fnames, fnss, []) |
|
5178 | 162 |
in |
25557 | 163 |
(fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') |
5178 | 164 |
end |
25557 | 165 |
| SOME fname' => |
166 |
if fname = fname' then (fnames, fnss) |
|
167 |
else primrec_error ("inconsistent functions for datatype " ^ quote tname)) |
|
5178 | 168 |
end; |
169 |
||
6359 | 170 |
|
5178 | 171 |
(* prepare functions needed for definitions *) |
172 |
||
21064 | 173 |
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) = |
17184 | 174 |
case AList.lookup (op =) fns i of |
15531 | 175 |
NONE => |
5178 | 176 |
let |
22480 | 177 |
val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined", |
15570 | 178 |
replicate ((length cargs) + (length (List.filter is_rec_type cargs))) |
5178 | 179 |
dummyT ---> HOLogic.unitT)) constrs; |
6427 | 180 |
val _ = warning ("No function definition for datatype " ^ quote tname) |
5178 | 181 |
in |
182 |
(dummy_fns @ fs, defs) |
|
183 |
end |
|
21064 | 184 |
| SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs); |
5178 | 185 |
|
6359 | 186 |
|
5178 | 187 |
(* make definition *) |
188 |
||
25557 | 189 |
fun make_def ctxt fixes fs (fname, ls, rec_name, tname) = |
5178 | 190 |
let |
25557 | 191 |
val raw_rhs = fold_rev (fn T => fn t => Abs ("", T, t)) |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
192 |
(map snd ls @ [dummyT]) |
20176 | 193 |
(list_comb (Const (rec_name, dummyT), |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
194 |
fs @ map Bound (0 :: (length ls downto 1)))) |
25557 | 195 |
val def_name = Thm.def_name (Sign.base_name fname); |
196 |
val rhs = singleton (Syntax.check_terms ctxt) raw_rhs; |
|
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
197 |
val SOME var = get_first (fn ((b, _), mx) => |
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
198 |
if Name.name_of b = fname then SOME (b, mx) else NONE) fixes; |
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
199 |
in (var, ((Name.binding def_name, []), rhs)) end; |
5178 | 200 |
|
6359 | 201 |
|
5178 | 202 |
(* find datatypes which contain all datatypes in tnames' *) |
203 |
||
204 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = [] |
|
205 |
| find_dts dt_info tnames' (tname::tnames) = |
|
17412 | 206 |
(case Symtab.lookup dt_info tname of |
25557 | 207 |
NONE => primrec_error (quote tname ^ " is not a datatype") |
15531 | 208 |
| SOME dt => |
5178 | 209 |
if tnames' subset (map (#1 o snd) (#descr dt)) then |
210 |
(tname, dt)::(find_dts dt_info tnames' tnames) |
|
211 |
else find_dts dt_info tnames' tnames); |
|
212 |
||
25557 | 213 |
|
214 |
(* primrec definition *) |
|
215 |
||
20841 | 216 |
local |
217 |
||
25557 | 218 |
fun prepare_spec prep_spec ctxt raw_fixes raw_spec = |
219 |
let |
|
25559 | 220 |
val ((fixes, spec), _) = prep_spec |
221 |
raw_fixes (map (single o apsnd single) raw_spec) ctxt |
|
222 |
in (fixes, map (apsnd the_single) spec) end; |
|
25557 | 223 |
|
25566 | 224 |
fun prove_spec ctxt names rec_rewrites defs = |
25557 | 225 |
let |
226 |
val rewrites = map mk_meta_eq rec_rewrites @ map (snd o snd) defs; |
|
227 |
fun tac _ = EVERY [rewrite_goals_tac rewrites, rtac refl 1]; |
|
25566 | 228 |
val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names); |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
229 |
in map (fn (a, t) => (a, [Goal.prove ctxt [] [] t tac])) end; |
25557 | 230 |
|
26129 | 231 |
fun gen_primrec set_group prep_spec raw_fixes raw_spec lthy = |
5178 | 232 |
let |
25557 | 233 |
val (fixes, spec) = prepare_spec prep_spec lthy raw_fixes raw_spec; |
25559 | 234 |
val eqns = fold_rev (process_eqn (fn v => Variable.is_fixed lthy v |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
235 |
orelse exists (fn ((w, _), _) => v = Name.name_of w) fixes) o snd) spec []; |
25557 | 236 |
val tnames = distinct (op =) (map (#1 o snd) eqns); |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
237 |
val dts = find_dts (DatatypePackage.get_datatypes (ProofContext.theory_of lthy)) tnames tnames; |
25557 | 238 |
val main_fns = map (fn (tname, {index, ...}) => |
239 |
(index, (fst o the o find_first (fn (_, x) => #1 x = tname)) eqns)) dts; |
|
22692 | 240 |
val {descr, rec_names, rec_rewrites, ...} = |
25557 | 241 |
if null dts then primrec_error |
242 |
("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive") |
|
18362
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
243 |
else snd (hd dts); |
25557 | 244 |
val (fnames, fnss) = fold_rev (process_fun descr eqns) main_fns ([], []); |
21064 | 245 |
val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []); |
25566 | 246 |
val names1 = map snd fnames; |
247 |
val names2 = map fst eqns; |
|
248 |
val _ = if gen_eq_set (op =) (names1, names2) then () |
|
249 |
else primrec_error ("functions " ^ commas_quote names2 ^ |
|
25557 | 250 |
"\nare not mutually recursive"); |
28107 | 251 |
val qualify = Name.qualified |
25557 | 252 |
(space_implode "_" (map (Sign.base_name o #1) defs)); |
28107 | 253 |
val spec' = (map o apfst o apfst) qualify spec; |
25570 | 254 |
val simp_atts = map (Attrib.internal o K) |
28521 | 255 |
[Simplifier.simp_add, RecfunCodegen.add_default]; |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
256 |
in |
25557 | 257 |
lthy |
26129 | 258 |
|> set_group ? LocalTheory.set_group (serial_string ()) |
25557 | 259 |
|> fold_map (LocalTheory.define Thm.definitionK o make_def lthy fixes fs) defs |
26556 | 260 |
|-> (fn defs => `(fn ctxt => prove_spec ctxt names1 rec_rewrites defs spec')) |
25557 | 261 |
|-> (fn simps => fold_map (LocalTheory.note Thm.theoremK) simps) |
262 |
|-> (fn simps' => LocalTheory.note Thm.theoremK |
|
28107 | 263 |
((qualify (Name.binding "simps"), simp_atts), maps snd simps')) |
25604 | 264 |
|>> snd |
25557 | 265 |
end handle PrimrecError (msg, some_eqn) => |
266 |
error ("Primrec definition error:\n" ^ msg ^ (case some_eqn |
|
267 |
of SOME eqn => "\nin\n" ^ quote (Syntax.string_of_term lthy eqn) |
|
268 |
| NONE => "")); |
|
20841 | 269 |
|
270 |
in |
|
271 |
||
26129 | 272 |
val add_primrec = gen_primrec false Specification.check_specification; |
273 |
val add_primrec_cmd = gen_primrec true Specification.read_specification; |
|
20841 | 274 |
|
22692 | 275 |
end; |
19688 | 276 |
|
26679
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
277 |
fun add_primrec_global fixes specs thy = |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
278 |
let |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
279 |
val lthy = TheoryTarget.init NONE thy; |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
280 |
val (simps, lthy') = add_primrec fixes specs lthy; |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
281 |
val simps' = ProofContext.export lthy' lthy simps; |
28394 | 282 |
in (simps', LocalTheory.exit_global lthy') end; |
26679
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
283 |
|
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
284 |
fun add_primrec_overloaded ops fixes specs thy = |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
285 |
let |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
286 |
val lthy = TheoryTarget.overloading ops thy; |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
287 |
val (simps, lthy') = add_primrec fixes specs lthy; |
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
288 |
val simps' = ProofContext.export lthy' lthy simps; |
28394 | 289 |
in (simps', LocalTheory.exit_global lthy') end; |
26679
447f4872b7ee
Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents:
26556
diff
changeset
|
290 |
|
5178 | 291 |
|
6359 | 292 |
(* outer syntax *) |
293 |
||
17057 | 294 |
local structure P = OuterParse and K = OuterKeyword in |
6359 | 295 |
|
19688 | 296 |
val opt_unchecked_name = |
297 |
Scan.optional (P.$$$ "(" |-- P.!!! |
|
298 |
(((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" || |
|
299 |
P.name >> pair false) --| P.$$$ ")")) (false, ""); |
|
300 |
||
25557 | 301 |
val old_primrec_decl = |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27301
diff
changeset
|
302 |
opt_unchecked_name -- Scan.repeat1 ((SpecParse.opt_thm_name ":" >> apfst Name.name_of) -- P.prop); |
6359 | 303 |
|
25557 | 304 |
fun pipe_error t = P.!!! (Scan.fail_with (K |
305 |
(cat_lines ["Equations must be separated by " ^ quote "|", quote t]))); |
|
306 |
||
307 |
val statement = SpecParse.opt_thm_name ":" -- P.prop --| Scan.ahead |
|
308 |
((P.term :-- pipe_error) || Scan.succeed ("","")); |
|
309 |
||
310 |
val statements = P.enum1 "|" statement; |
|
311 |
||
312 |
val primrec_decl = P.opt_target -- P.fixes --| P.$$$ "where" -- statements; |
|
313 |
||
24867 | 314 |
val _ = |
6723 | 315 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
25557 | 316 |
((primrec_decl >> (fn ((opt_target, raw_fixes), raw_spec) => |
317 |
Toplevel.local_theory opt_target (add_primrec_cmd raw_fixes raw_spec #> snd))) |
|
318 |
|| (old_primrec_decl >> (fn ((unchecked, alt_name), eqns) => |
|
20176 | 319 |
Toplevel.theory (snd o |
25557 | 320 |
(if unchecked then OldPrimrecPackage.add_primrec_unchecked else OldPrimrecPackage.add_primrec) alt_name |
321 |
(map P.triple_swap eqns))))); |
|
6359 | 322 |
|
5178 | 323 |
end; |
6384 | 324 |
|
325 |
end; |