| author | wenzelm | 
| Tue, 05 Oct 1999 18:26:34 +0200 | |
| changeset 7742 | 01386eb4eab0 | 
| parent 7722 | 8add8fdce3f1 | 
| child 7904 | 2b551893583e | 
| 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 | 
| 6425 | 12 | val add_primrec: string -> ((bstring * string) * Args.src list) list | 
| 6359 | 13 | -> theory -> theory * thm list | 
| 6425 | 14 | val add_primrec_i: string -> ((bstring * term) * theory attribute list) list | 
| 6359 | 15 | -> theory -> theory * thm list | 
| 5178 | 16 | end; | 
| 17 | ||
| 18 | structure PrimrecPackage : PRIMREC_PACKAGE = | |
| 19 | struct | |
| 20 | ||
| 21 | open DatatypeAux; | |
| 22 | ||
| 23 | exception RecError of string; | |
| 24 | ||
| 25 | fun primrec_err s = error ("Primrec definition error:\n" ^ s);
 | |
| 26 | fun primrec_eq_err sign s eq = | |
| 7544 
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
 berghofe parents: 
7152diff
changeset | 27 | primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term sign eq)); | 
| 6427 | 28 | |
| 29 | ||
| 30 | (* messages *) | |
| 31 | ||
| 32 | val quiet_mode = ref false; | |
| 33 | fun message s = if ! quiet_mode then () else writeln s; | |
| 5178 | 34 | |
| 6359 | 35 | |
| 5178 | 36 | (* preprocessing of equations *) | 
| 37 | ||
| 38 | fun process_eqn sign (eq, rec_fns) = | |
| 39 | let | |
| 6032 | 40 | val (lhs, rhs) = | 
| 41 | if null (term_vars eq) then | |
| 42 | HOLogic.dest_eq (HOLogic.dest_Trueprop eq) | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 43 | handle TERM _ => raise RecError "not a proper equation" | 
| 6032 | 44 | else raise RecError "illegal schematic variable(s)"; | 
| 5178 | 45 | |
| 46 | val (recfun, args) = strip_comb lhs; | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 47 | val (fname, _) = dest_Const recfun handle TERM _ => | 
| 5178 | 48 | raise RecError "function is not declared as constant in theory"; | 
| 49 | ||
| 50 | val (ls', rest) = take_prefix is_Free args; | |
| 51 | val (middle, rs') = take_suffix is_Free rest; | |
| 52 | val rpos = length ls'; | |
| 53 | ||
| 54 | val (constr, cargs') = if null middle then raise RecError "constructor missing" | |
| 55 | else strip_comb (hd middle); | |
| 56 | val (cname, T) = dest_Const constr | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 57 | handle TERM _ => raise RecError "ill-formed constructor"; | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 58 | val (tname, _) = dest_Type (body_type T) handle TYPE _ => | 
| 5178 | 59 | raise RecError "cannot determine datatype associated with function" | 
| 60 | ||
| 6037 | 61 | val (ls, cargs, rs) = (map dest_Free ls', | 
| 62 | map dest_Free cargs', | |
| 63 | map dest_Free rs') | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 64 | handle TERM _ => raise RecError "illegal argument in pattern"; | 
| 5178 | 65 | val lfrees = ls @ rs @ cargs; | 
| 66 | ||
| 67 | in | |
| 68 | if not (null (duplicates lfrees)) then | |
| 69 | raise RecError "repeated variable name in pattern" | |
| 70 | else if not ((map dest_Free (term_frees rhs)) subset lfrees) then | |
| 71 | raise RecError "extra variables on rhs" | |
| 72 | else if length middle > 1 then | |
| 73 | raise RecError "more than one non-variable in pattern" | |
| 74 | else (case assoc (rec_fns, fname) of | |
| 75 | None => | |
| 76 | (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns | |
| 77 | | Some (_, rpos', eqns) => | |
| 78 | if is_some (assoc (eqns, cname)) then | |
| 6037 | 79 | raise RecError "constructor already occurred as pattern" | 
| 5178 | 80 | else if rpos <> rpos' then | 
| 81 | raise RecError "position of recursive argument inconsistent" | |
| 82 | else | |
| 6037 | 83 | overwrite (rec_fns, | 
| 84 | (fname, | |
| 85 | (tname, rpos, | |
| 86 | (cname, (ls, cargs, rs, rhs, eq))::eqns)))) | |
| 5178 | 87 | end | 
| 88 | handle RecError s => primrec_eq_err sign s eq; | |
| 89 | ||
| 90 | fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) = | |
| 91 | let | |
| 92 | val (_, (tname, _, constrs)) = nth_elem (i, descr); | |
| 93 | ||
| 94 | (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) | |
| 95 | ||
| 96 | fun subst [] x = x | |
| 97 | | subst subs (fs, Abs (a, T, t)) = | |
| 98 | let val (fs', t') = subst subs (fs, t) | |
| 99 | in (fs', Abs (a, T, t')) end | |
| 100 | | subst subs (fs, t as (_ $ _)) = | |
| 101 | let val (f, ts) = strip_comb t; | |
| 102 | in | |
| 103 | if is_Const f andalso (fst (dest_Const f)) mem (map fst rec_eqns) then | |
| 104 | let | |
| 105 | val (fname', _) = dest_Const f; | |
| 106 | val (_, rpos, _) = the (assoc (rec_eqns, fname')); | |
| 107 | val ls = take (rpos, ts); | |
| 108 | val rest = drop (rpos, ts); | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 109 | val (x', rs) = (hd rest, tl rest) | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 110 |                   handle LIST _ => raise RecError ("not enough arguments\
 | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 111 | \ in recursive application\nof function " ^ quote fname' ^ " on rhs"); | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 112 | val (x, xs) = strip_comb x' | 
| 5178 | 113 | in | 
| 114 | (case assoc (subs, x) of | |
| 115 | None => | |
| 116 | let | |
| 117 | val (fs', ts') = foldl_map (subst subs) (fs, ts) | |
| 118 | in (fs', list_comb (f, ts')) end | |
| 119 | | Some (i', y) => | |
| 120 | let | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 121 | val (fs', ts') = foldl_map (subst subs) (fs, xs @ ls @ rs); | 
| 5178 | 122 | val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs') | 
| 123 | in (fs'', list_comb (y, ts')) | |
| 124 | end) | |
| 125 | end | |
| 126 | else | |
| 127 | let | |
| 128 | val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts) | |
| 129 | in (fs', list_comb (f', ts')) end | |
| 130 | end | |
| 131 | | subst _ x = x; | |
| 132 | ||
| 133 | (* translate rec equations into function arguments suitable for rec comb *) | |
| 134 | ||
| 135 | fun trans eqns ((cname, cargs), (fnames', fnss', fns)) = | |
| 136 | (case assoc (eqns, cname) of | |
| 6427 | 137 |           None => (warning ("no equation for constructor " ^ quote cname ^
 | 
| 138 | "\nin definition of function " ^ quote fname); | |
| 5178 | 139 |               (fnames', fnss', (Const ("arbitrary", dummyT))::fns))
 | 
| 140 | | Some (ls, cargs', rs, rhs, eq) => | |
| 141 | let | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 142 | fun rec_index (DtRec k) = k | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 143 |                 | rec_index (DtType ("fun", [_, DtRec k])) = k;
 | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 144 | |
| 5178 | 145 | val recs = filter (is_rec_type o snd) (cargs' ~~ cargs); | 
| 146 | val rargs = map fst recs; | |
| 6037 | 147 | val subs = map (rpair dummyT o fst) | 
| 148 | (rev (rename_wrt_term rhs rargs)); | |
| 149 | val ((fnames'', fnss''), rhs') = | |
| 150 | (subst (map (fn ((x, y), z) => | |
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 151 | (Free x, (rec_index y, Free z))) | 
| 6037 | 152 | (recs ~~ subs)) | 
| 153 | ((fnames', fnss'), rhs)) | |
| 5178 | 154 | handle RecError s => primrec_eq_err sign s eq | 
| 6037 | 155 | in (fnames'', fnss'', | 
| 156 | (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) | |
| 5178 | 157 | end) | 
| 158 | ||
| 159 | in (case assoc (fnames, i) of | |
| 160 | None => | |
| 161 | if exists (equal fname o snd) fnames then | |
| 6427 | 162 |           raise RecError ("inconsistent functions for datatype " ^ quote tname)
 | 
| 5178 | 163 | else | 
| 164 | let | |
| 165 | val (_, _, eqns) = the (assoc (rec_eqns, fname)); | |
| 166 | val (fnames', fnss', fns) = foldr (trans eqns) | |
| 167 | (constrs, ((i, fname)::fnames, fnss, [])) | |
| 168 | in | |
| 169 | (fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') | |
| 170 | end | |
| 171 | | Some fname' => | |
| 172 | if fname = fname' then (fnames, 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)) = | |
| 180 | case assoc (fns, i) of | |
| 181 | None => | |
| 182 | let | |
| 183 |          val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
 | |
| 184 | replicate ((length cargs) + (length (filter is_rec_type cargs))) | |
| 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 | |
| 190 | | Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs); | |
| 191 | ||
| 6359 | 192 | |
| 5178 | 193 | (* make definition *) | 
| 194 | ||
| 195 | fun make_def sign fs (fname, ls, rec_name, tname) = | |
| 196 | let | |
| 6037 | 197 |     val rhs = foldr (fn (T, t) => Abs ("", T, t)) 
 | 
| 198 | ((map snd ls) @ [dummyT], | |
| 199 | list_comb (Const (rec_name, dummyT), | |
| 200 | fs @ map Bound (0 ::(length ls downto 1)))); | |
| 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) = | |
| 210 | (case Symtab.lookup (dt_info, tname) of | |
| 6427 | 211 | None => primrec_err (quote tname ^ " is not a datatype") | 
| 5178 | 212 | | Some dt => | 
| 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 | ||
| 6359 | 217 | fun add_primrec_i alt_name eqns_atts thy = | 
| 5178 | 218 | let | 
| 6359 | 219 | val (eqns, atts) = split_list eqns_atts; | 
| 6394 | 220 | val sg = Theory.sign_of thy; | 
| 5178 | 221 | val dt_info = DatatypePackage.get_datatypes thy; | 
| 5719 | 222 | val rec_eqns = foldr (process_eqn sg) (map snd eqns, []); | 
| 5178 | 223 | val tnames = distinct (map (#1 o snd) rec_eqns); | 
| 224 | val dts = find_dts dt_info tnames tnames; | |
| 6037 | 225 | val main_fns = | 
| 226 | 	map (fn (tname, {index, ...}) =>
 | |
| 227 | (index, | |
| 228 | fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns)))) | |
| 229 | dts; | |
| 230 |     val {descr, rec_names, rec_rewrites, ...} = 
 | |
| 231 | if null dts then | |
| 6359 | 232 | 	    primrec_err ("datatypes " ^ commas_quote tnames ^ 
 | 
| 6037 | 233 | "\nare not mutually recursive") | 
| 234 | else snd (hd dts); | |
| 235 | val (fnames, fnss) = foldr (process_fun sg descr rec_eqns) | |
| 236 | (main_fns, ([], [])); | |
| 5178 | 237 | val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], [])); | 
| 238 | val defs' = map (make_def sg fs) defs; | |
| 239 | val names1 = map snd fnames; | |
| 240 | val names2 = map fst rec_eqns; | |
| 5216 | 241 | val thy' = thy |> | 
| 5719 | 242 | Theory.add_path (if alt_name = "" then (space_implode "_" | 
| 243 | (map (Sign.base_name o #1) defs)) else alt_name) |> | |
| 5216 | 244 | (if eq_set (names1, names2) then Theory.add_defs_i defs' | 
| 6359 | 245 |        else primrec_err ("functions " ^ commas_quote names2 ^
 | 
| 5216 | 246 | "\nare not mutually recursive")); | 
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 247 | val rewrites = o_def :: (map mk_meta_eq rec_rewrites) @ (map (get_axiom thy' o fst) defs'); | 
| 6427 | 248 |     val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names1 ^ " ...");
 | 
| 6394 | 249 | val char_thms = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (Theory.sign_of thy') t) | 
| 5178 | 250 | (fn _ => [rtac refl 1])) eqns; | 
| 6092 | 251 | val simps = char_thms; | 
| 6359 | 252 | val thy'' = | 
| 253 | thy' | |
| 254 |       |> PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])]
 | |
| 255 | |> PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts) | |
| 256 | |> Theory.parent_path; | |
| 257 | in (thy'', char_thms) end; | |
| 258 | ||
| 259 | ||
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 260 | fun add_primrec alt_name eqns thy = | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 261 | let | 
| 7544 
dee529666dcd
Fixed bug in add_primrec which caused non-informative error message.
 berghofe parents: 
7152diff
changeset | 262 | val sign = Theory.sign_of thy; | 
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 263 | val ((names, strings), srcss) = apfst split_list (split_list eqns); | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 264 | val atts = map (map (Attrib.global_attribute thy)) srcss; | 
| 7722 | 265 | 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: 
6729diff
changeset | 266 | 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: 
7152diff
changeset | 267 | handle TERM _ => primrec_eq_err sign "not a proper equation" eq) eqn_ts; | 
| 7016 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 268 | val (_, eqn_ts') = InductivePackage.unify_consts (sign_of thy) rec_ts eqn_ts | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 269 | in | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 270 | add_primrec_i alt_name (names ~~ eqn_ts' ~~ atts) thy | 
| 
df54b5365477
- Now also supports arbitrarily branching datatypes.
 berghofe parents: 
6729diff
changeset | 271 | end; | 
| 6359 | 272 | |
| 5178 | 273 | |
| 6359 | 274 | (* outer syntax *) | 
| 275 | ||
| 6723 | 276 | local structure P = OuterParse and K = OuterSyntax.Keyword in | 
| 6359 | 277 | |
| 278 | val primrec_decl = | |
| 6723 | 279 |   Scan.optional (P.$$$ "(" |-- P.name --| P.$$$ ")") "" --
 | 
| 7152 | 280 | Scan.repeat1 (P.opt_thm_name ":" -- P.prop --| P.marg_comment); | 
| 6359 | 281 | |
| 282 | val primrecP = | |
| 6723 | 283 | OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl | 
| 6359 | 284 | (primrec_decl >> (fn (alt_name, eqns) => | 
| 6723 | 285 | Toplevel.theory (#1 o add_primrec alt_name (map P.triple_swap eqns)))); | 
| 6359 | 286 | |
| 287 | val _ = OuterSyntax.add_parsers [primrecP]; | |
| 5178 | 288 | |
| 289 | end; | |
| 6384 | 290 | |
| 291 | ||
| 292 | end; |