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