src/HOL/Tools/primrec_package.ML
author berghofe
Fri, 24 Jul 1998 12:50:34 +0200
changeset 5178 9337b230ff15
child 5216 f0a66af5f2cb
permissions -rw-r--r--
New primrec function definition package
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Tools/datatype_package.ML
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     2
    ID:         $Id$
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     4
    Copyright   1998  TU Muenchen
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     5
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     6
Package for defining functions on datatypes
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     7
by primitive recursion
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     8
*)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     9
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    10
signature PRIMREC_PACKAGE =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    11
sig
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    12
  val add_primrec_i : term list -> theory -> theory * thm list
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    13
  val add_primrec : string list -> theory -> theory * thm list
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    14
end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    15
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    16
structure PrimrecPackage : PRIMREC_PACKAGE =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    17
struct
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    18
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    19
open DatatypeAux;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    20
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    21
exception RecError of string;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    22
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    23
(* FIXME: move? *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    24
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    25
fun dest_eq (Const ("Trueprop", _) $ (Const ("op =", _) $ lhs $ rhs)) = (lhs, rhs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    26
  | dest_eq t = raise TERM ("dest_eq", [t])
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    27
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    28
fun dest_Type (Type x) = x
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    29
  | dest_Type T = raise TYPE ("dest_Type", [T], []);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    30
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    31
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    32
fun primrec_eq_err sign s eq =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    33
  primrec_err (s ^ "\nin equation\n" ^ Sign.string_of_term sign eq);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    34
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    35
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    36
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    37
fun process_eqn sign (eq, rec_fns) = 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    38
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    39
    val (lhs, rhs) = if null (term_vars eq) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    40
        dest_eq eq handle _ => raise RecError "not a proper equation"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    41
      else raise RecError "illegal schematic variable(s)";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    42
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    43
    val (recfun, args) = strip_comb lhs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    44
    val (fname, _) = dest_Const recfun handle _ => 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    45
      raise RecError "function is not declared as constant in theory";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    46
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    47
    val (ls', rest)  = take_prefix is_Free args;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    48
    val (middle, rs') = take_suffix is_Free rest;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    49
    val rpos = length ls';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    50
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    51
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    52
      else strip_comb (hd middle);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    53
    val (cname, T) = dest_Const constr
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    54
      handle _ => raise RecError "ill-formed constructor";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    55
    val (tname, _) = dest_Type (body_type T) handle _ =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    56
      raise RecError "cannot determine datatype associated with function"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    57
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    58
    val (ls, cargs, rs) = (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    59
      handle _ => raise RecError "illegal argument in pattern";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    60
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    61
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    62
  in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    63
    if not (null (duplicates lfrees)) then 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    64
      raise RecError "repeated variable name in pattern" 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    65
    else if not ((map dest_Free (term_frees rhs)) subset lfrees) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    66
      raise RecError "extra variables on rhs"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    67
    else if length middle > 1 then 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    68
      raise RecError "more than one non-variable in pattern"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    69
    else (case assoc (rec_fns, fname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    70
        None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    71
          (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    72
      | Some (_, rpos', eqns) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    73
          if is_some (assoc (eqns, cname)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    74
            raise RecError "constructor already occured as pattern"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    75
          else if rpos <> rpos' then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    76
            raise RecError "position of recursive argument inconsistent"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    77
          else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    78
            overwrite (rec_fns, (fname, (tname, rpos,
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    79
              (cname, (ls, cargs, rs, rhs, eq))::eqns))))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    80
  end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    81
  handle RecError s => primrec_eq_err sign s eq;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    82
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    83
fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    84
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    85
    val (_, (tname, _, constrs)) = nth_elem (i, descr);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    86
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    87
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    88
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    89
    fun subst [] x = x
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    90
      | subst subs (fs, Abs (a, T, t)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    91
          let val (fs', t') = subst subs (fs, t)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    92
          in (fs', Abs (a, T, t')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    93
      | subst subs (fs, t as (_ $ _)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    94
          let val (f, ts) = strip_comb t;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    95
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    96
            if is_Const f andalso (fst (dest_Const f)) mem (map fst rec_eqns) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    97
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    98
                val (fname', _) = dest_Const f;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    99
                val (_, rpos, _) = the (assoc (rec_eqns, fname'));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   100
                val ls = take (rpos, ts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   101
                val rest = drop (rpos, ts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   102
                val (x, rs) = (hd rest, tl rest)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   103
                  handle _ => raise RecError ("not enough arguments\
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   104
                   \ in recursive application\nof function " ^ fname' ^ " on rhs")
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   105
              in 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   106
                (case assoc (subs, x) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   107
                    None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   108
                      let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   109
                        val (fs', ts') = foldl_map (subst subs) (fs, ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   110
                      in (fs', list_comb (f, ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   111
                  | Some (i', y) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   112
                      let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   113
                        val (fs', ts') = foldl_map (subst subs) (fs, ls @ rs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   114
                        val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   115
                      in (fs'', list_comb (y, ts'))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   116
                      end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   117
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   118
            else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   119
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   120
                val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   121
              in (fs', list_comb (f', ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   122
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   123
      | subst _ x = x;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   124
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   125
    (* translate rec equations into function arguments suitable for rec comb *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   126
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   127
    fun trans eqns ((cname, cargs), (fnames', fnss', fns)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   128
      (case assoc (eqns, cname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   129
          None => (warning ("no equation for constructor " ^ cname ^
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   130
            "\nin definition of function " ^ fname);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   131
              (fnames', fnss', (Const ("arbitrary", dummyT))::fns))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   132
        | Some (ls, cargs', rs, rhs, eq) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   133
            let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   134
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   135
              val rargs = map fst recs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   136
              val subs = map (rpair dummyT o fst) (rev (rename_wrt_term rhs rargs));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   137
              val ((fnames'', fnss''), rhs') = (subst (map (fn ((x, y), z) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   138
                (Free x, (dest_DtRec y, Free z))) (recs ~~ subs)) ((fnames', fnss'), rhs))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   139
                  handle RecError s => primrec_eq_err sign s eq
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   140
            in (fnames'', fnss'', (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   141
            end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   142
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   143
  in (case assoc (fnames, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   144
      None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   145
        if exists (equal fname o snd) fnames then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   146
          raise RecError ("inconsistent functions for datatype " ^ tname)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   147
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   148
          let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   149
            val (_, _, eqns) = the (assoc (rec_eqns, fname));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   150
            val (fnames', fnss', fns) = foldr (trans eqns)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   151
              (constrs, ((i, fname)::fnames, fnss, []))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   152
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   153
            (fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   154
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   155
    | Some fname' =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   156
        if fname = fname' then (fnames, fnss)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   157
        else raise RecError ("inconsistent functions for datatype " ^ tname))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   158
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   159
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   160
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   161
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   162
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   163
  case assoc (fns, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   164
     None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   165
       let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   166
         val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   167
           replicate ((length cargs) + (length (filter is_rec_type cargs)))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   168
             dummyT ---> HOLogic.unitT)) constrs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   169
         val _ = warning ("no function definition for datatype " ^ tname)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   170
       in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   171
         (dummy_fns @ fs, defs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   172
       end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   173
   | Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   174
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   175
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   176
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   177
fun make_def sign fs (fname, ls, rec_name, tname) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   178
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   179
    val rhs = foldr (fn (T, t) => Abs ("", T, t)) ((map snd ls) @ [dummyT],
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   180
      list_comb (Const (rec_name, dummyT),
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   181
        fs @ map Bound (0 ::(length ls downto 1))));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   182
    val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def",
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   183
      Logic.mk_equals (Const (fname, dummyT), rhs))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   184
  in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   185
    inferT_axm sign defpair
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   186
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   187
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   188
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   189
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   190
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   191
  | find_dts dt_info tnames' (tname::tnames) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   192
      (case Symtab.lookup (dt_info, tname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   193
          None => primrec_err (tname ^ " is not a datatype")
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   194
        | Some dt =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   195
            if tnames' subset (map (#1 o snd) (#descr dt)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   196
              (tname, dt)::(find_dts dt_info tnames' tnames)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   197
            else find_dts dt_info tnames' tnames);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   198
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   199
fun add_primrec_i eqns thy =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   200
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   201
    val sg = sign_of thy;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   202
    val dt_info = DatatypePackage.get_datatypes thy;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   203
    val rec_eqns = foldr (process_eqn sg) (eqns, []);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   204
    val tnames = distinct (map (#1 o snd) rec_eqns);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   205
    val dts = find_dts dt_info tnames tnames;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   206
    val main_fns = map (fn (tname, {index, ...}) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   207
      (index, fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns)))) dts;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   208
    val {descr, rec_names, rec_rewrites, ...} = if null dts then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   209
        primrec_err ("datatypes " ^ commas tnames ^ "\nare not mutually recursive")
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   210
      else snd (hd dts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   211
    val (fnames, fnss) = foldr (process_fun sg descr rec_eqns) (main_fns, ([], []));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   212
    val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], []));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   213
    val defs' = map (make_def sg fs) defs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   214
    val names1 = map snd fnames;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   215
    val names2 = map fst rec_eqns;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   216
    val thy' = if eq_set (names1, names2) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   217
        Theory.add_defs_i defs' thy
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   218
      else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   219
        primrec_err ("functions " ^ commas names2 ^ "\nare not mutually recursive");
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   220
    val rewrites = (map mk_meta_eq rec_rewrites) @ (map (get_axiom thy' o fst) defs');
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   221
    val _ = writeln ("Proving equations for primrec function(s)\n" ^
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   222
      commas names1 ^ " ...");
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   223
    val char_thms = map (fn t => prove_goalw_cterm rewrites (cterm_of (sign_of thy') t)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   224
        (fn _ => [rtac refl 1])) eqns;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   225
    val simpref = simpset_ref_of thy';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   226
    val _ = simpref := !simpref addsimps char_thms
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   227
  in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   228
    (thy', char_thms)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   229
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   230
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   231
fun add_primrec eqns thy =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   232
  add_primrec_i (map (readtm (sign_of thy) propT) eqns) thy;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   233
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   234
end;