author | paulson |
Fri, 21 Apr 2000 11:28:18 +0200 | |
changeset 8756 | b03a0b219139 |
parent 8480 | 50266d517b0c |
child 8973 | ac448cd43452 |
permissions | -rw-r--r-- |
5719 | 1 |
(* Title: HOL/Tools/primrec_package.ML |
5178 | 2 |
ID: $Id$ |
5719 | 3 |
Author: Stefan Berghofer and Norbert Voelker |
5178 | 4 |
Copyright 1998 TU Muenchen |
5 |
||
6359 | 6 |
Package for defining functions on datatypes by primitive recursion. |
5178 | 7 |
*) |
8 |
||
9 |
signature PRIMREC_PACKAGE = |
|
10 |
sig |
|
6427 | 11 |
val quiet_mode: bool ref |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
12 |
val print_primrecs: theory -> unit |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
13 |
val get_primrec: theory -> string -> (string * thm list) list option |
6425 | 14 |
val add_primrec: string -> ((bstring * string) * Args.src list) list |
6359 | 15 |
-> theory -> theory * thm list |
6425 | 16 |
val add_primrec_i: string -> ((bstring * term) * theory attribute list) list |
6359 | 17 |
-> theory -> theory * thm list |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
18 |
val setup: (theory -> theory) 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 |
|
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
39 |
(** theory data **) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
40 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
41 |
(* data kind 'HOL/primrec' *) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
42 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
43 |
structure PrimrecArgs = |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
44 |
struct |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
45 |
val name = "HOL/primrec"; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
46 |
type T = (string * thm list) list Symtab.table; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
47 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
48 |
val empty = Symtab.empty; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
49 |
val copy = I; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
50 |
val prep_ext = I; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
51 |
val merge: T * T -> T = Symtab.merge (K true); |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
52 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
53 |
fun print sg tab = |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
54 |
Pretty.writeln (Pretty.strs ("primrecs:" :: |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
55 |
map #1 (Sign.cond_extern_table sg Sign.constK tab))); |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
56 |
end; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
57 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
58 |
structure PrimrecData = TheoryDataFun(PrimrecArgs); |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
59 |
val print_primrecs = PrimrecData.print; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
60 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
61 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
62 |
(* get and put data *) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
63 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
64 |
fun get_primrec thy name = Symtab.lookup (PrimrecData.get thy, name); |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
65 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
66 |
fun put_primrec name info thy = |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
67 |
let val tab = PrimrecData.get thy |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
68 |
in |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
69 |
PrimrecData.put (case Symtab.lookup (tab, name) of |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
70 |
None => Symtab.update_new ((name, [info]), tab) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
71 |
| Some infos => Symtab.update ((name, info::infos), tab)) thy end; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
72 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
73 |
|
5178 | 74 |
(* preprocessing of equations *) |
75 |
||
76 |
fun process_eqn sign (eq, rec_fns) = |
|
77 |
let |
|
6032 | 78 |
val (lhs, rhs) = |
79 |
if null (term_vars eq) then |
|
80 |
HOLogic.dest_eq (HOLogic.dest_Trueprop eq) |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
81 |
handle TERM _ => raise RecError "not a proper equation" |
6032 | 82 |
else raise RecError "illegal schematic variable(s)"; |
5178 | 83 |
|
84 |
val (recfun, args) = strip_comb lhs; |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
85 |
val (fname, _) = dest_Const recfun handle TERM _ => |
5178 | 86 |
raise RecError "function is not declared as constant in theory"; |
87 |
||
88 |
val (ls', rest) = take_prefix is_Free args; |
|
89 |
val (middle, rs') = take_suffix is_Free rest; |
|
90 |
val rpos = length ls'; |
|
91 |
||
92 |
val (constr, cargs') = if null middle then raise RecError "constructor missing" |
|
93 |
else strip_comb (hd middle); |
|
94 |
val (cname, T) = dest_Const constr |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
95 |
handle TERM _ => raise RecError "ill-formed constructor"; |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
96 |
val (tname, _) = dest_Type (body_type T) handle TYPE _ => |
5178 | 97 |
raise RecError "cannot determine datatype associated with function" |
98 |
||
6037 | 99 |
val (ls, cargs, rs) = (map dest_Free ls', |
100 |
map dest_Free cargs', |
|
101 |
map dest_Free rs') |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
102 |
handle TERM _ => raise RecError "illegal argument in pattern"; |
5178 | 103 |
val lfrees = ls @ rs @ cargs; |
104 |
||
105 |
in |
|
106 |
if not (null (duplicates lfrees)) then |
|
107 |
raise RecError "repeated variable name in pattern" |
|
108 |
else if not ((map dest_Free (term_frees rhs)) subset lfrees) then |
|
109 |
raise RecError "extra variables on rhs" |
|
110 |
else if length middle > 1 then |
|
111 |
raise RecError "more than one non-variable in pattern" |
|
112 |
else (case assoc (rec_fns, fname) of |
|
113 |
None => |
|
114 |
(fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns |
|
115 |
| Some (_, rpos', eqns) => |
|
116 |
if is_some (assoc (eqns, cname)) then |
|
6037 | 117 |
raise RecError "constructor already occurred as pattern" |
5178 | 118 |
else if rpos <> rpos' then |
119 |
raise RecError "position of recursive argument inconsistent" |
|
120 |
else |
|
6037 | 121 |
overwrite (rec_fns, |
122 |
(fname, |
|
123 |
(tname, rpos, |
|
124 |
(cname, (ls, cargs, rs, rhs, eq))::eqns)))) |
|
5178 | 125 |
end |
126 |
handle RecError s => primrec_eq_err sign s eq; |
|
127 |
||
128 |
fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) = |
|
129 |
let |
|
130 |
val (_, (tname, _, constrs)) = nth_elem (i, descr); |
|
131 |
||
132 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) |
|
133 |
||
134 |
fun subst [] x = x |
|
135 |
| subst subs (fs, Abs (a, T, t)) = |
|
136 |
let val (fs', t') = subst subs (fs, t) |
|
137 |
in (fs', Abs (a, T, t')) end |
|
138 |
| subst subs (fs, t as (_ $ _)) = |
|
139 |
let val (f, ts) = strip_comb t; |
|
140 |
in |
|
141 |
if is_Const f andalso (fst (dest_Const f)) mem (map fst rec_eqns) then |
|
142 |
let |
|
143 |
val (fname', _) = dest_Const f; |
|
144 |
val (_, rpos, _) = the (assoc (rec_eqns, fname')); |
|
145 |
val ls = take (rpos, ts); |
|
146 |
val rest = drop (rpos, ts); |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
147 |
val (x', rs) = (hd rest, tl rest) |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
148 |
handle LIST _ => raise RecError ("not enough arguments\ |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
149 |
\ in recursive application\nof function " ^ quote fname' ^ " on rhs"); |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
150 |
val (x, xs) = strip_comb x' |
5178 | 151 |
in |
152 |
(case assoc (subs, x) of |
|
153 |
None => |
|
154 |
let |
|
155 |
val (fs', ts') = foldl_map (subst subs) (fs, ts) |
|
156 |
in (fs', list_comb (f, ts')) end |
|
157 |
| Some (i', y) => |
|
158 |
let |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
159 |
val (fs', ts') = foldl_map (subst subs) (fs, xs @ ls @ rs); |
5178 | 160 |
val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs') |
161 |
in (fs'', list_comb (y, ts')) |
|
162 |
end) |
|
163 |
end |
|
164 |
else |
|
165 |
let |
|
166 |
val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts) |
|
167 |
in (fs', list_comb (f', ts')) end |
|
168 |
end |
|
169 |
| subst _ x = x; |
|
170 |
||
171 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
172 |
||
173 |
fun trans eqns ((cname, cargs), (fnames', fnss', fns)) = |
|
174 |
(case assoc (eqns, cname) of |
|
6427 | 175 |
None => (warning ("no equation for constructor " ^ quote cname ^ |
176 |
"\nin definition of function " ^ quote fname); |
|
5178 | 177 |
(fnames', fnss', (Const ("arbitrary", dummyT))::fns)) |
178 |
| Some (ls, cargs', rs, rhs, eq) => |
|
179 |
let |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
180 |
fun rec_index (DtRec k) = k |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
181 |
| rec_index (DtType ("fun", [_, DtRec k])) = k; |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
182 |
|
5178 | 183 |
val recs = filter (is_rec_type o snd) (cargs' ~~ cargs); |
184 |
val rargs = map fst recs; |
|
6037 | 185 |
val subs = map (rpair dummyT o fst) |
186 |
(rev (rename_wrt_term rhs rargs)); |
|
187 |
val ((fnames'', fnss''), rhs') = |
|
188 |
(subst (map (fn ((x, y), z) => |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
189 |
(Free x, (rec_index y, Free z))) |
6037 | 190 |
(recs ~~ subs)) |
191 |
((fnames', fnss'), rhs)) |
|
5178 | 192 |
handle RecError s => primrec_eq_err sign s eq |
6037 | 193 |
in (fnames'', fnss'', |
194 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) |
|
5178 | 195 |
end) |
196 |
||
197 |
in (case assoc (fnames, i) of |
|
198 |
None => |
|
199 |
if exists (equal fname o snd) fnames then |
|
6427 | 200 |
raise RecError ("inconsistent functions for datatype " ^ quote tname) |
5178 | 201 |
else |
202 |
let |
|
203 |
val (_, _, eqns) = the (assoc (rec_eqns, fname)); |
|
204 |
val (fnames', fnss', fns) = foldr (trans eqns) |
|
205 |
(constrs, ((i, fname)::fnames, fnss, [])) |
|
206 |
in |
|
207 |
(fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') |
|
208 |
end |
|
209 |
| Some fname' => |
|
210 |
if fname = fname' then (fnames, fnss) |
|
6427 | 211 |
else raise RecError ("inconsistent functions for datatype " ^ quote tname)) |
5178 | 212 |
end; |
213 |
||
6359 | 214 |
|
5178 | 215 |
(* prepare functions needed for definitions *) |
216 |
||
217 |
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) = |
|
218 |
case assoc (fns, i) of |
|
219 |
None => |
|
220 |
let |
|
221 |
val dummy_fns = map (fn (_, cargs) => Const ("arbitrary", |
|
222 |
replicate ((length cargs) + (length (filter is_rec_type cargs))) |
|
223 |
dummyT ---> HOLogic.unitT)) constrs; |
|
6427 | 224 |
val _ = warning ("No function definition for datatype " ^ quote tname) |
5178 | 225 |
in |
226 |
(dummy_fns @ fs, defs) |
|
227 |
end |
|
228 |
| Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs); |
|
229 |
||
6359 | 230 |
|
5178 | 231 |
(* make definition *) |
232 |
||
233 |
fun make_def sign fs (fname, ls, rec_name, tname) = |
|
234 |
let |
|
6037 | 235 |
val rhs = foldr (fn (T, t) => Abs ("", T, t)) |
236 |
((map snd ls) @ [dummyT], |
|
237 |
list_comb (Const (rec_name, dummyT), |
|
238 |
fs @ map Bound (0 ::(length ls downto 1)))); |
|
5178 | 239 |
val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def", |
6037 | 240 |
Logic.mk_equals (Const (fname, dummyT), rhs)) |
6394 | 241 |
in Theory.inferT_axm sign defpair end; |
5178 | 242 |
|
6359 | 243 |
|
5178 | 244 |
(* find datatypes which contain all datatypes in tnames' *) |
245 |
||
246 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = [] |
|
247 |
| find_dts dt_info tnames' (tname::tnames) = |
|
248 |
(case Symtab.lookup (dt_info, tname) of |
|
6427 | 249 |
None => primrec_err (quote tname ^ " is not a datatype") |
5178 | 250 |
| Some dt => |
251 |
if tnames' subset (map (#1 o snd) (#descr dt)) then |
|
252 |
(tname, dt)::(find_dts dt_info tnames' tnames) |
|
253 |
else find_dts dt_info tnames' tnames); |
|
254 |
||
8432 | 255 |
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns = |
256 |
let |
|
257 |
fun constrs_of (_, (_, _, cs)) = |
|
258 |
map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs; |
|
259 |
val params_of = Library.assocs (flat (map constrs_of rec_eqns)); |
|
260 |
in |
|
261 |
induction |
|
262 |
|> RuleCases.rename_params (map params_of (flat (map (map #1 o #3 o #2) descr))) |
|
263 |
|> RuleCases.name (RuleCases.get induction) |
|
264 |
end; |
|
265 |
||
6359 | 266 |
fun add_primrec_i alt_name eqns_atts thy = |
5178 | 267 |
let |
6359 | 268 |
val (eqns, atts) = split_list eqns_atts; |
6394 | 269 |
val sg = Theory.sign_of thy; |
5178 | 270 |
val dt_info = DatatypePackage.get_datatypes thy; |
5719 | 271 |
val rec_eqns = foldr (process_eqn sg) (map snd eqns, []); |
5178 | 272 |
val tnames = distinct (map (#1 o snd) rec_eqns); |
273 |
val dts = find_dts dt_info tnames tnames; |
|
6037 | 274 |
val main_fns = |
275 |
map (fn (tname, {index, ...}) => |
|
276 |
(index, |
|
277 |
fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns)))) |
|
278 |
dts; |
|
279 |
val {descr, rec_names, rec_rewrites, ...} = |
|
280 |
if null dts then |
|
6359 | 281 |
primrec_err ("datatypes " ^ commas_quote tnames ^ |
6037 | 282 |
"\nare not mutually recursive") |
283 |
else snd (hd dts); |
|
284 |
val (fnames, fnss) = foldr (process_fun sg descr rec_eqns) |
|
285 |
(main_fns, ([], [])); |
|
5178 | 286 |
val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], [])); |
287 |
val defs' = map (make_def sg fs) defs; |
|
288 |
val names1 = map snd fnames; |
|
289 |
val names2 = map fst rec_eqns; |
|
8432 | 290 |
val primrec_name = |
291 |
if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name; |
|
292 |
val (thy', defs_thms') = thy |> Theory.add_path primrec_name |> |
|
7904 | 293 |
(if eq_set (names1, names2) then (PureThy.add_defs_i o map Thm.no_attributes) defs' |
6359 | 294 |
else primrec_err ("functions " ^ commas_quote names2 ^ |
5216 | 295 |
"\nare not mutually recursive")); |
8432 | 296 |
val rewrites = o_def :: (map mk_meta_eq rec_rewrites) @ defs_thms'; |
6427 | 297 |
val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names1 ^ " ..."); |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
298 |
val simps = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (Theory.sign_of thy') t) |
5178 | 299 |
(fn _ => [rtac refl 1])) eqns; |
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
300 |
val (thy'', [simps']) = thy' |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
301 |
|> PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])] |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
302 |
|>> (#1 o PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts)) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
303 |
|>> (#1 o PureThy.add_thms [(("induct", prepare_induct (#2 (hd dts)) rec_eqns), [])]) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
304 |
|>> Theory.parent_path |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
305 |
in |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
306 |
(foldl (fn (thy, (fname, _, _, tname)) => |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
307 |
put_primrec fname (tname, simps') thy) (thy'', defs), simps') |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
308 |
end; |
6359 | 309 |
|
310 |
||
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
311 |
fun add_primrec alt_name eqns thy = |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
312 |
let |
7544
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents:
7152
diff
changeset
|
313 |
val sign = Theory.sign_of thy; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
314 |
val ((names, strings), srcss) = apfst split_list (split_list eqns); |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
315 |
val atts = map (map (Attrib.global_attribute thy)) srcss; |
7722 | 316 |
val eqn_ts = map (term_of o Thm.read_cterm sign o rpair propT) strings; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
in |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
321 |
add_primrec_i alt_name (names ~~ eqn_ts' ~~ atts) thy |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
322 |
end; |
6359 | 323 |
|
5178 | 324 |
|
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
325 |
(** package setup **) |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
326 |
|
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
327 |
val setup = [PrimrecData.init]; |
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
328 |
|
6359 | 329 |
(* outer syntax *) |
330 |
||
6723 | 331 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6359 | 332 |
|
333 |
val primrec_decl = |
|
6723 | 334 |
Scan.optional (P.$$$ "(" |-- P.name --| P.$$$ ")") "" -- |
7152 | 335 |
Scan.repeat1 (P.opt_thm_name ":" -- P.prop --| P.marg_comment); |
6359 | 336 |
|
337 |
val primrecP = |
|
6723 | 338 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
6359 | 339 |
(primrec_decl >> (fn (alt_name, eqns) => |
6723 | 340 |
Toplevel.theory (#1 o add_primrec alt_name (map P.triple_swap eqns)))); |
6359 | 341 |
|
342 |
val _ = OuterSyntax.add_parsers [primrecP]; |
|
5178 | 343 |
|
344 |
end; |
|
6384 | 345 |
|
346 |
||
347 |
end; |