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