dropped old primrec package
authorhaftmann
Tue Sep 28 15:39:59 2010 +0200 (2010-09-28)
changeset 39776cde508d2eac8
parent 39775 e4c85d8c2aba
child 39777 4f8f08362bf7
dropped old primrec package
src/HOL/Inductive.thy
src/HOL/IsaMakefile
src/HOL/Tools/old_primrec.ML
src/HOL/Tools/primrec.ML
     1.1 --- a/src/HOL/Inductive.thy	Tue Sep 28 15:34:47 2010 +0200
     1.2 +++ b/src/HOL/Inductive.thy	Tue Sep 28 15:39:59 2010 +0200
     1.3 @@ -14,7 +14,6 @@
     1.4    "Tools/Datatype/datatype_case.ML"
     1.5    ("Tools/Datatype/datatype_abs_proofs.ML")
     1.6    ("Tools/Datatype/datatype_data.ML")
     1.7 -  ("Tools/old_primrec.ML")
     1.8    ("Tools/primrec.ML")
     1.9    ("Tools/Datatype/datatype_codegen.ML")
    1.10  begin
    1.11 @@ -282,7 +281,6 @@
    1.12  use "Tools/Datatype/datatype_codegen.ML"
    1.13  setup Datatype_Codegen.setup
    1.14  
    1.15 -use "Tools/old_primrec.ML"
    1.16  use "Tools/primrec.ML"
    1.17  
    1.18  text{* Lambda-abstractions with pattern matching: *}
     2.1 --- a/src/HOL/IsaMakefile	Tue Sep 28 15:34:47 2010 +0200
     2.2 +++ b/src/HOL/IsaMakefile	Tue Sep 28 15:39:59 2010 +0200
     2.3 @@ -202,7 +202,6 @@
     2.4    Tools/inductive_set.ML \
     2.5    Tools/lin_arith.ML \
     2.6    Tools/nat_arith.ML \
     2.7 -  Tools/old_primrec.ML \
     2.8    Tools/primrec.ML \
     2.9    Tools/prop_logic.ML \
    2.10    Tools/refute.ML \
     3.1 --- a/src/HOL/Tools/old_primrec.ML	Tue Sep 28 15:34:47 2010 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,326 +0,0 @@
     3.4 -(*  Title:      HOL/Tools/old_primrec.ML
     3.5 -    Author:     Norbert Voelker, FernUni Hagen
     3.6 -    Author:     Stefan Berghofer, TU Muenchen
     3.7 -
     3.8 -Package for defining functions on datatypes by primitive recursion.
     3.9 -*)
    3.10 -
    3.11 -signature OLD_PRIMREC =
    3.12 -sig
    3.13 -  val unify_consts: theory -> term list -> term list -> term list * term list
    3.14 -  val add_primrec: string -> ((bstring * string) * Attrib.src list) list
    3.15 -    -> theory -> thm list * theory
    3.16 -  val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list
    3.17 -    -> theory -> thm list * theory
    3.18 -  val add_primrec_i: string -> ((bstring * term) * attribute list) list
    3.19 -    -> theory -> thm list * theory
    3.20 -  val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list
    3.21 -    -> theory -> thm list * theory
    3.22 -end;
    3.23 -
    3.24 -structure OldPrimrec : OLD_PRIMREC =
    3.25 -struct
    3.26 -
    3.27 -open Datatype_Aux;
    3.28 -
    3.29 -exception RecError of string;
    3.30 -
    3.31 -fun primrec_err s = error ("Primrec definition error:\n" ^ s);
    3.32 -fun primrec_eq_err thy s eq =
    3.33 -  primrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
    3.34 -
    3.35 -
    3.36 -(*the following code ensures that each recursive set always has the
    3.37 -  same type in all introduction rules*)
    3.38 -fun unify_consts thy cs intr_ts =
    3.39 -  (let
    3.40 -    fun varify t (i, ts) =
    3.41 -      let val t' = map_types (Logic.incr_tvar (i + 1)) (snd (Type.varify_global [] t))
    3.42 -      in (maxidx_of_term t', t'::ts) end;
    3.43 -    val (i, cs') = fold_rev varify cs (~1, []);
    3.44 -    val (i', intr_ts') = fold_rev varify intr_ts (i, []);
    3.45 -    val rec_consts = fold Term.add_consts cs' [];
    3.46 -    val intr_consts = fold Term.add_consts intr_ts' [];
    3.47 -    fun unify (cname, cT) =
    3.48 -      let val consts = map snd (filter (fn (c, _) => c = cname) intr_consts)
    3.49 -      in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
    3.50 -    val (env, _) = fold unify rec_consts (Vartab.empty, i');
    3.51 -    val subst = Type.legacy_freeze o map_types (Envir.norm_type env)
    3.52 -
    3.53 -  in (map subst cs', map subst intr_ts')
    3.54 -  end) handle Type.TUNIFY =>
    3.55 -    (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
    3.56 -
    3.57 -
    3.58 -(* preprocessing of equations *)
    3.59 -
    3.60 -fun process_eqn thy eq rec_fns =
    3.61 -  let
    3.62 -    val (lhs, rhs) =
    3.63 -      if null (Term.add_vars eq []) then
    3.64 -        HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
    3.65 -        handle TERM _ => raise RecError "not a proper equation"
    3.66 -      else raise RecError "illegal schematic variable(s)";
    3.67 -
    3.68 -    val (recfun, args) = strip_comb lhs;
    3.69 -    val fnameT = dest_Const recfun handle TERM _ =>
    3.70 -      raise RecError "function is not declared as constant in theory";
    3.71 -
    3.72 -    val (ls', rest)  = take_prefix is_Free args;
    3.73 -    val (middle, rs') = take_suffix is_Free rest;
    3.74 -    val rpos = length ls';
    3.75 -
    3.76 -    val (constr, cargs') = if null middle then raise RecError "constructor missing"
    3.77 -      else strip_comb (hd middle);
    3.78 -    val (cname, T) = dest_Const constr
    3.79 -      handle TERM _ => raise RecError "ill-formed constructor";
    3.80 -    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
    3.81 -      raise RecError "cannot determine datatype associated with function"
    3.82 -
    3.83 -    val (ls, cargs, rs) =
    3.84 -      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
    3.85 -      handle TERM _ => raise RecError "illegal argument in pattern";
    3.86 -    val lfrees = ls @ rs @ cargs;
    3.87 -
    3.88 -    fun check_vars _ [] = ()
    3.89 -      | check_vars s vars = raise RecError (s ^ commas_quote (map fst vars))
    3.90 -  in
    3.91 -    if length middle > 1 then
    3.92 -      raise RecError "more than one non-variable in pattern"
    3.93 -    else
    3.94 -     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
    3.95 -      check_vars "extra variables on rhs: "
    3.96 -        (subtract (op =) lfrees (map dest_Free (OldTerm.term_frees rhs)));
    3.97 -      case AList.lookup (op =) rec_fns fnameT of
    3.98 -        NONE =>
    3.99 -          (fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
   3.100 -      | SOME (_, rpos', eqns) =>
   3.101 -          if AList.defined (op =) eqns cname then
   3.102 -            raise RecError "constructor already occurred as pattern"
   3.103 -          else if rpos <> rpos' then
   3.104 -            raise RecError "position of recursive argument inconsistent"
   3.105 -          else
   3.106 -            AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns))
   3.107 -              rec_fns)
   3.108 -  end
   3.109 -  handle RecError s => primrec_eq_err thy s eq;
   3.110 -
   3.111 -fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) =
   3.112 -  let
   3.113 -    val (_, (tname, _, constrs)) = List.nth (descr, i);
   3.114 -
   3.115 -    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
   3.116 -
   3.117 -    fun subst [] t fs = (t, fs)
   3.118 -      | subst subs (Abs (a, T, t)) fs =
   3.119 -          fs
   3.120 -          |> subst subs t
   3.121 -          |-> (fn t' => pair (Abs (a, T, t')))
   3.122 -      | subst subs (t as (_ $ _)) fs =
   3.123 -          let
   3.124 -            val (f, ts) = strip_comb t;
   3.125 -          in
   3.126 -            if is_Const f andalso member (op =) (map fst rec_eqns) (dest_Const f) then
   3.127 -              let
   3.128 -                val fnameT' as (fname', _) = dest_Const f;
   3.129 -                val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT');
   3.130 -                val ls = take rpos ts;
   3.131 -                val rest = drop rpos ts;
   3.132 -                val (x', rs) = (hd rest, tl rest)
   3.133 -                  handle Empty => raise RecError ("not enough arguments\
   3.134 -                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
   3.135 -                val (x, xs) = strip_comb x'
   3.136 -              in case AList.lookup (op =) subs x
   3.137 -               of NONE =>
   3.138 -                    fs
   3.139 -                    |> fold_map (subst subs) ts
   3.140 -                    |-> (fn ts' => pair (list_comb (f, ts')))
   3.141 -                | SOME (i', y) =>
   3.142 -                    fs
   3.143 -                    |> fold_map (subst subs) (xs @ ls @ rs)
   3.144 -                    ||> process_fun thy descr rec_eqns (i', fnameT')
   3.145 -                    |-> (fn ts' => pair (list_comb (y, ts')))
   3.146 -              end
   3.147 -            else
   3.148 -              fs
   3.149 -              |> fold_map (subst subs) (f :: ts)
   3.150 -              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
   3.151 -          end
   3.152 -      | subst _ t fs = (t, fs);
   3.153 -
   3.154 -    (* translate rec equations into function arguments suitable for rec comb *)
   3.155 -
   3.156 -    fun trans eqns (cname, cargs) (fnameTs', fnss', fns) =
   3.157 -      (case AList.lookup (op =) eqns cname of
   3.158 -          NONE => (warning ("No equation for constructor " ^ quote cname ^
   3.159 -            "\nin definition of function " ^ quote fname);
   3.160 -              (fnameTs', fnss', (Const ("HOL.undefined", dummyT))::fns))
   3.161 -        | SOME (ls, cargs', rs, rhs, eq) =>
   3.162 -            let
   3.163 -              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
   3.164 -              val rargs = map fst recs;
   3.165 -              val subs = map (rpair dummyT o fst)
   3.166 -                (rev (Term.rename_wrt_term rhs rargs));
   3.167 -              val (rhs', (fnameTs'', fnss'')) =
   3.168 -                  (subst (map (fn ((x, y), z) =>
   3.169 -                               (Free x, (body_index y, Free z)))
   3.170 -                          (recs ~~ subs)) rhs (fnameTs', fnss'))
   3.171 -                  handle RecError s => primrec_eq_err thy s eq
   3.172 -            in (fnameTs'', fnss'',
   3.173 -                (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
   3.174 -            end)
   3.175 -
   3.176 -  in (case AList.lookup (op =) fnameTs i of
   3.177 -      NONE =>
   3.178 -        if exists (equal fnameT o snd) fnameTs then
   3.179 -          raise RecError ("inconsistent functions for datatype " ^ quote tname)
   3.180 -        else
   3.181 -          let
   3.182 -            val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT);
   3.183 -            val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs
   3.184 -              ((i, fnameT)::fnameTs, fnss, [])
   3.185 -          in
   3.186 -            (fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
   3.187 -          end
   3.188 -    | SOME fnameT' =>
   3.189 -        if fnameT = fnameT' then (fnameTs, fnss)
   3.190 -        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
   3.191 -  end;
   3.192 -
   3.193 -
   3.194 -(* prepare functions needed for definitions *)
   3.195 -
   3.196 -fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
   3.197 -  case AList.lookup (op =) fns i of
   3.198 -     NONE =>
   3.199 -       let
   3.200 -         val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined",
   3.201 -           replicate (length cargs + length (filter is_rec_type cargs))
   3.202 -             dummyT ---> HOLogic.unitT)) constrs;
   3.203 -         val _ = warning ("No function definition for datatype " ^ quote tname)
   3.204 -       in
   3.205 -         (dummy_fns @ fs, defs)
   3.206 -       end
   3.207 -   | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs);
   3.208 -
   3.209 -
   3.210 -(* make definition *)
   3.211 -
   3.212 -fun make_def thy fs (fname, ls, rec_name, tname) =
   3.213 -  let
   3.214 -    val rhs = fold_rev (fn T => fn t => Abs ("", T, t))
   3.215 -                    ((map snd ls) @ [dummyT])
   3.216 -                    (list_comb (Const (rec_name, dummyT),
   3.217 -                                fs @ map Bound (0 ::(length ls downto 1))))
   3.218 -    val def_name = Long_Name.base_name fname ^ "_" ^ Long_Name.base_name tname ^ "_def";
   3.219 -    val def_prop =
   3.220 -      singleton (Syntax.check_terms (ProofContext.init_global thy))
   3.221 -        (Logic.mk_equals (Const (fname, dummyT), rhs));
   3.222 -  in (def_name, def_prop) end;
   3.223 -
   3.224 -
   3.225 -(* find datatypes which contain all datatypes in tnames' *)
   3.226 -
   3.227 -fun find_dts (dt_info : info Symtab.table) _ [] = []
   3.228 -  | find_dts dt_info tnames' (tname::tnames) =
   3.229 -      (case Symtab.lookup dt_info tname of
   3.230 -          NONE => primrec_err (quote tname ^ " is not a datatype")
   3.231 -        | SOME dt =>
   3.232 -            if subset (op =) (tnames', map (#1 o snd) (#descr dt)) then
   3.233 -              (tname, dt)::(find_dts dt_info tnames' tnames)
   3.234 -            else find_dts dt_info tnames' tnames);
   3.235 -
   3.236 -fun prepare_induct ({descr, induct, ...}: info) rec_eqns =
   3.237 -  let
   3.238 -    fun constrs_of (_, (_, _, cs)) =
   3.239 -      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
   3.240 -    val params_of = these o AList.lookup (op =) (maps constrs_of rec_eqns);
   3.241 -  in
   3.242 -    induct
   3.243 -    |> Rule_Cases.rename_params (map params_of (maps (map #1 o #3 o #2) descr))
   3.244 -    |> Rule_Cases.save induct
   3.245 -  end;
   3.246 -
   3.247 -local
   3.248 -
   3.249 -fun gen_primrec_i note def alt_name eqns_atts thy =
   3.250 -  let
   3.251 -    val (eqns, atts) = split_list eqns_atts;
   3.252 -    val dt_info = Datatype_Data.get_all thy;
   3.253 -    val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ;
   3.254 -    val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
   3.255 -    val dts = find_dts dt_info tnames tnames;
   3.256 -    val main_fns =
   3.257 -      map (fn (tname, {index, ...}) =>
   3.258 -        (index,
   3.259 -          (fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns))
   3.260 -      dts;
   3.261 -    val {descr, rec_names, rec_rewrites, ...} =
   3.262 -      if null dts then
   3.263 -        primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
   3.264 -      else snd (hd dts);
   3.265 -    val (fnameTs, fnss) =
   3.266 -      fold_rev (process_fun thy descr rec_eqns) main_fns ([], []);
   3.267 -    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
   3.268 -    val defs' = map (make_def thy fs) defs;
   3.269 -    val nameTs1 = map snd fnameTs;
   3.270 -    val nameTs2 = map fst rec_eqns;
   3.271 -    val _ = if eq_set (op =) (nameTs1, nameTs2) then ()
   3.272 -            else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^
   3.273 -              "\nare not mutually recursive");
   3.274 -    val primrec_name =
   3.275 -      if alt_name = "" then (space_implode "_" (map (Long_Name.base_name o #1) defs)) else alt_name;
   3.276 -    val (defs_thms', thy') =
   3.277 -      thy
   3.278 -      |> Sign.add_path primrec_name
   3.279 -      |> fold_map def (map (fn (name, t) => ((name, []), t)) defs');
   3.280 -    val rewrites = (map mk_meta_eq rec_rewrites) @ map snd defs_thms';
   3.281 -    val simps = map (fn (_, t) => Goal.prove_global thy' [] [] t
   3.282 -        (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
   3.283 -    val (simps', thy'') =
   3.284 -      thy'
   3.285 -      |> fold_map note ((map fst eqns ~~ atts) ~~ map single simps);
   3.286 -    val simps'' = maps snd simps';
   3.287 -    val lhss = map (Logic.varify_global o fst o Logic.dest_equals o snd) defs';
   3.288 -  in
   3.289 -    thy''
   3.290 -    |> note (("simps",
   3.291 -        [Simplifier.simp_add, Nitpick_Simps.add, Code.add_default_eqn_attribute]), simps'')
   3.292 -    |> snd
   3.293 -    |> Spec_Rules.add_global Spec_Rules.Equational (lhss, simps)
   3.294 -    |> note (("induct", []), [prepare_induct (#2 (hd dts)) rec_eqns])
   3.295 -    |> snd
   3.296 -    |> Sign.parent_path
   3.297 -    |> pair simps''
   3.298 -  end;
   3.299 -
   3.300 -fun gen_primrec note def alt_name eqns thy =
   3.301 -  let
   3.302 -    val ((names, strings), srcss) = apfst split_list (split_list eqns);
   3.303 -    val atts = map (map (Attrib.attribute thy)) srcss;
   3.304 -    val eqn_ts = map (fn s => Syntax.read_prop_global thy s
   3.305 -      handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings;
   3.306 -    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq)))
   3.307 -      handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts;
   3.308 -    val (_, eqn_ts') = unify_consts thy rec_ts eqn_ts
   3.309 -  in
   3.310 -    gen_primrec_i note def alt_name (names ~~ eqn_ts' ~~ atts) thy
   3.311 -  end;
   3.312 -
   3.313 -fun thy_note ((name, atts), thms) =
   3.314 -  Global_Theory.add_thmss [((Binding.name name, thms), atts)] #-> (fn [thms] => pair (name, thms));
   3.315 -fun thy_def false ((name, atts), t) =
   3.316 -      Global_Theory.add_defs false [((Binding.name name, t), atts)] #-> (fn [thm] => pair (name, thm))
   3.317 -  | thy_def true ((name, atts), t) =
   3.318 -      Global_Theory.add_defs_unchecked false [((Binding.name name, t), atts)] #-> (fn [thm] => pair (name, thm));
   3.319 -
   3.320 -in
   3.321 -
   3.322 -val add_primrec = gen_primrec thy_note (thy_def false);
   3.323 -val add_primrec_unchecked = gen_primrec thy_note (thy_def true);
   3.324 -val add_primrec_i = gen_primrec_i thy_note (thy_def false);
   3.325 -val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true);
   3.326 -
   3.327 -end;
   3.328 -
   3.329 -end;
     4.1 --- a/src/HOL/Tools/primrec.ML	Tue Sep 28 15:34:47 2010 +0200
     4.2 +++ b/src/HOL/Tools/primrec.ML	Tue Sep 28 15:39:59 2010 +0200
     4.3 @@ -306,15 +306,6 @@
     4.4  
     4.5  (* outer syntax *)
     4.6  
     4.7 -val opt_unchecked_name =
     4.8 -  Scan.optional (Parse.$$$ "(" |-- Parse.!!!
     4.9 -    (((Parse.$$$ "unchecked" >> K true) -- Scan.optional Parse.name "" ||
    4.10 -      Parse.name >> pair false) --| Parse.$$$ ")")) (false, "");
    4.11 -
    4.12 -val old_primrec_decl =
    4.13 -  opt_unchecked_name --
    4.14 -    Scan.repeat1 ((Parse_Spec.opt_thm_name ":" >> apfst Binding.name_of) -- Parse.prop);
    4.15 -
    4.16  val primrec_decl = Parse.opt_target -- Parse.fixes -- Parse_Spec.where_alt_specs;
    4.17  
    4.18  val _ =