src/HOL/Tools/old_primrec_package.ML
author wenzelm
Wed, 31 Dec 2008 00:08:13 +0100
changeset 29266 4a478f9d2847
parent 28703 aef727ef30e5
child 29276 94b1ffec9201
permissions -rw-r--r--
use regular Term.add_vars, Term.add_frees etc.; moved old add_term_vars, add_term_frees etc. to structure OldTerm;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28308
d4396a28fb29 fixed headers
haftmann
parents: 27691
diff changeset
     1
(*  Title:      HOL/Tools/old_primrec_package.ML
29266
4a478f9d2847 use regular Term.add_vars, Term.add_frees etc.;
wenzelm
parents: 28703
diff changeset
     2
    Author:     Norbert Voelker, FernUni Hagen
4a478f9d2847 use regular Term.add_vars, Term.add_frees etc.;
wenzelm
parents: 28703
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     4
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     5
Package for defining functions on datatypes by primitive recursion.
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     6
*)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     7
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     8
signature OLD_PRIMREC_PACKAGE =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
     9
sig
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    10
  val unify_consts: theory -> term list -> term list -> term list * term list
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    11
  val add_primrec: string -> ((bstring * string) * Attrib.src list) list
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    12
    -> theory -> thm list * theory
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    13
  val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    14
    -> theory -> thm list * theory
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    15
  val add_primrec_i: string -> ((bstring * term) * attribute list) list
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    16
    -> theory -> thm list * theory
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    17
  val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    18
    -> theory -> thm list * theory
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    19
end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    20
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    21
structure OldPrimrecPackage : OLD_PRIMREC_PACKAGE =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    22
struct
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    23
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    24
open DatatypeAux;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    25
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    26
exception RecError of string;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    27
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    28
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    29
fun primrec_eq_err thy s eq =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26478
diff changeset
    30
  primrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    31
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    32
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    33
(*the following code ensures that each recursive set always has the
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    34
  same type in all introduction rules*)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    35
fun unify_consts thy cs intr_ts =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    36
  (let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    37
    val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    38
    fun varify (t, (i, ts)) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    39
      let val t' = map_types (Logic.incr_tvar (i + 1)) (snd (Type.varify [] t))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    40
      in (maxidx_of_term t', t'::ts) end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    41
    val (i, cs') = foldr varify (~1, []) cs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    42
    val (i', intr_ts') = foldr varify (i, []) intr_ts;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    43
    val rec_consts = fold add_term_consts_2 cs' [];
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    44
    val intr_consts = fold add_term_consts_2 intr_ts' [];
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    45
    fun unify (cname, cT) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    46
      let val consts = map snd (filter (fn (c, _) => c = cname) intr_consts)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    47
      in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    48
    val (env, _) = fold unify rec_consts (Vartab.empty, i');
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    49
    val subst = Type.freeze o map_types (Envir.norm_type env)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    50
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    51
  in (map subst cs', map subst intr_ts')
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    52
  end) handle Type.TUNIFY =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    53
    (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    54
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    55
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    56
(* preprocessing of equations *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    57
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    58
fun process_eqn thy eq rec_fns =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    59
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    60
    val (lhs, rhs) =
29266
4a478f9d2847 use regular Term.add_vars, Term.add_frees etc.;
wenzelm
parents: 28703
diff changeset
    61
      if null (Term.add_vars eq []) then
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    62
        HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    63
        handle TERM _ => raise RecError "not a proper equation"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    64
      else raise RecError "illegal schematic variable(s)";
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    65
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    66
    val (recfun, args) = strip_comb lhs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    67
    val fnameT = dest_Const recfun handle TERM _ =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    68
      raise RecError "function is not declared as constant in theory";
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    69
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    70
    val (ls', rest)  = take_prefix is_Free args;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    71
    val (middle, rs') = take_suffix is_Free rest;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    72
    val rpos = length ls';
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    73
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    74
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    75
      else strip_comb (hd middle);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    76
    val (cname, T) = dest_Const constr
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    77
      handle TERM _ => raise RecError "ill-formed constructor";
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    78
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    79
      raise RecError "cannot determine datatype associated with function"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    80
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    81
    val (ls, cargs, rs) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    82
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    83
      handle TERM _ => raise RecError "illegal argument in pattern";
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    84
    val lfrees = ls @ rs @ cargs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    85
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    86
    fun check_vars _ [] = ()
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    87
      | check_vars s vars = raise RecError (s ^ commas_quote (map fst vars))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    88
  in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    89
    if length middle > 1 then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    90
      raise RecError "more than one non-variable in pattern"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    91
    else
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    92
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    93
      check_vars "extra variables on rhs: "
29266
4a478f9d2847 use regular Term.add_vars, Term.add_frees etc.;
wenzelm
parents: 28703
diff changeset
    94
        (map dest_Free (OldTerm.term_frees rhs) \\ lfrees);
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    95
      case AList.lookup (op =) rec_fns fnameT of
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    96
        NONE =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    97
          (fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    98
      | SOME (_, rpos', eqns) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
    99
          if AList.defined (op =) eqns cname then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   100
            raise RecError "constructor already occurred as pattern"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   101
          else if rpos <> rpos' then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   102
            raise RecError "position of recursive argument inconsistent"
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   103
          else
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   104
            AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   105
              rec_fns)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   106
  end
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   107
  handle RecError s => primrec_eq_err thy s eq;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   108
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   109
fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   110
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   111
    val (_, (tname, _, constrs)) = List.nth (descr, i);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   112
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   113
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   114
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   115
    fun subst [] t fs = (t, fs)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   116
      | subst subs (Abs (a, T, t)) fs =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   117
          fs
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   118
          |> subst subs t
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   119
          |-> (fn t' => pair (Abs (a, T, t')))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   120
      | subst subs (t as (_ $ _)) fs =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   121
          let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   122
            val (f, ts) = strip_comb t;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   123
          in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   124
            if is_Const f andalso dest_Const f mem map fst rec_eqns then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   125
              let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   126
                val fnameT' as (fname', _) = dest_Const f;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   127
                val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT');
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   128
                val ls = Library.take (rpos, ts);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   129
                val rest = Library.drop (rpos, ts);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   130
                val (x', rs) = (hd rest, tl rest)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   131
                  handle Empty => raise RecError ("not enough arguments\
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   132
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   133
                val (x, xs) = strip_comb x'
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   134
              in case AList.lookup (op =) subs x
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   135
               of NONE =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   136
                    fs
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   137
                    |> fold_map (subst subs) ts
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   138
                    |-> (fn ts' => pair (list_comb (f, ts')))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   139
                | SOME (i', y) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   140
                    fs
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   141
                    |> fold_map (subst subs) (xs @ ls @ rs)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   142
                    ||> process_fun thy descr rec_eqns (i', fnameT')
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   143
                    |-> (fn ts' => pair (list_comb (y, ts')))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   144
              end
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   145
            else
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   146
              fs
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   147
              |> fold_map (subst subs) (f :: ts)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   148
              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   149
          end
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   150
      | subst _ t fs = (t, fs);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   151
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   152
    (* translate rec equations into function arguments suitable for rec comb *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   153
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   154
    fun trans eqns (cname, cargs) (fnameTs', fnss', fns) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   155
      (case AList.lookup (op =) eqns cname of
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   156
          NONE => (warning ("No equation for constructor " ^ quote cname ^
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   157
            "\nin definition of function " ^ quote fname);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   158
              (fnameTs', fnss', (Const ("HOL.undefined", dummyT))::fns))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   159
        | SOME (ls, cargs', rs, rhs, eq) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   160
            let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   161
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   162
              val rargs = map fst recs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   163
              val subs = map (rpair dummyT o fst)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   164
                (rev (rename_wrt_term rhs rargs));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   165
              val (rhs', (fnameTs'', fnss'')) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   166
                  (subst (map (fn ((x, y), z) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   167
                               (Free x, (body_index y, Free z)))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   168
                          (recs ~~ subs)) rhs (fnameTs', fnss'))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   169
                  handle RecError s => primrec_eq_err thy s eq
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   170
            in (fnameTs'', fnss'',
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   171
                (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   172
            end)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   173
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   174
  in (case AList.lookup (op =) fnameTs i of
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   175
      NONE =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   176
        if exists (equal fnameT o snd) fnameTs then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   177
          raise RecError ("inconsistent functions for datatype " ^ quote tname)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   178
        else
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   179
          let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   180
            val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   181
            val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   182
              ((i, fnameT)::fnameTs, fnss, [])
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   183
          in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   184
            (fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   185
          end
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   186
    | SOME fnameT' =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   187
        if fnameT = fnameT' then (fnameTs, fnss)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   188
        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   189
  end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   190
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   191
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   192
(* prepare functions needed for definitions *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   193
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   194
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   195
  case AList.lookup (op =) fns i of
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   196
     NONE =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   197
       let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   198
         val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined",
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   199
           replicate ((length cargs) + (length (List.filter is_rec_type cargs)))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   200
             dummyT ---> HOLogic.unitT)) constrs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   201
         val _ = warning ("No function definition for datatype " ^ quote tname)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   202
       in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   203
         (dummy_fns @ fs, defs)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   204
       end
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   205
   | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   206
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   207
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   208
(* make definition *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   209
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   210
fun make_def thy fs (fname, ls, rec_name, tname) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   211
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   212
    val rhs = fold_rev (fn T => fn t => Abs ("", T, t))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   213
                    ((map snd ls) @ [dummyT])
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   214
                    (list_comb (Const (rec_name, dummyT),
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   215
                                fs @ map Bound (0 ::(length ls downto 1))))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   216
    val def_name = Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def";
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   217
    val def_prop =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   218
      singleton (Syntax.check_terms (ProofContext.init thy))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   219
        (Logic.mk_equals (Const (fname, dummyT), rhs));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   220
  in (def_name, def_prop) end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   221
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   222
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   223
(* find datatypes which contain all datatypes in tnames' *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   224
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   225
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   226
  | find_dts dt_info tnames' (tname::tnames) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   227
      (case Symtab.lookup dt_info tname of
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   228
          NONE => primrec_err (quote tname ^ " is not a datatype")
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   229
        | SOME dt =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   230
            if tnames' subset (map (#1 o snd) (#descr dt)) then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   231
              (tname, dt)::(find_dts dt_info tnames' tnames)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   232
            else find_dts dt_info tnames' tnames);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   233
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   234
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   235
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   236
    fun constrs_of (_, (_, _, cs)) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   237
      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   238
    val params_of = these o AList.lookup (op =) (List.concat (map constrs_of rec_eqns));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   239
  in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   240
    induction
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   241
    |> RuleCases.rename_params (map params_of (List.concat (map (map #1 o #3 o #2) descr)))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   242
    |> RuleCases.save induction
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   243
  end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   244
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   245
local
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   246
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   247
fun gen_primrec_i note def alt_name eqns_atts thy =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   248
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   249
    val (eqns, atts) = split_list eqns_atts;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   250
    val dt_info = DatatypePackage.get_datatypes thy;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   251
    val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   252
    val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   253
    val dts = find_dts dt_info tnames tnames;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   254
    val main_fns =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   255
      map (fn (tname, {index, ...}) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   256
        (index,
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   257
          (fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   258
      dts;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   259
    val {descr, rec_names, rec_rewrites, ...} =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   260
      if null dts then
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   261
        primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   262
      else snd (hd dts);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   263
    val (fnameTs, fnss) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   264
      fold_rev (process_fun thy descr rec_eqns) main_fns ([], []);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   265
    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   266
    val defs' = map (make_def thy fs) defs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   267
    val nameTs1 = map snd fnameTs;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   268
    val nameTs2 = map fst rec_eqns;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   269
    val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then ()
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   270
            else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   271
              "\nare not mutually recursive");
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   272
    val primrec_name =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   273
      if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   274
    val (defs_thms', thy') =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   275
      thy
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   276
      |> Sign.add_path primrec_name
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   277
      |> fold_map def (map (fn (name, t) => ((name, []), t)) defs');
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   278
    val rewrites = (map mk_meta_eq rec_rewrites) @ map snd defs_thms';
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   279
    val simps = map (fn (_, t) => Goal.prove_global thy' [] [] t
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   280
        (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   281
    val (simps', thy'') =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   282
      thy'
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   283
      |> fold_map note ((map fst eqns ~~ atts) ~~ map single simps);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   284
    val simps'' = maps snd simps';
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   285
  in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   286
    thy''
28703
aef727ef30e5 cleanup code default attribute
haftmann
parents: 28308
diff changeset
   287
    |> note (("simps", [Simplifier.simp_add, Code.add_default_eqn_attribute]), simps'')
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   288
    |> snd
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   289
    |> note (("induct", []), [prepare_induct (#2 (hd dts)) rec_eqns])
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   290
    |> snd
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   291
    |> Sign.parent_path
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   292
    |> pair simps''
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   293
  end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   294
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   295
fun gen_primrec note def alt_name eqns thy =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   296
  let
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   297
    val ((names, strings), srcss) = apfst split_list (split_list eqns);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   298
    val atts = map (map (Attrib.attribute thy)) srcss;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   299
    val eqn_ts = map (fn s => Syntax.read_prop_global thy s
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   300
      handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   301
    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq)))
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   302
      handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   303
    val (_, eqn_ts') = unify_consts thy rec_ts eqn_ts
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   304
  in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   305
    gen_primrec_i note def alt_name (names ~~ eqn_ts' ~~ atts) thy
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   306
  end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   307
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   308
fun thy_note ((name, atts), thms) =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   309
  PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   310
fun thy_def false ((name, atts), t) =
27691
ce171cbd4b93 PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents: 26939
diff changeset
   311
      PureThy.add_defs false [((name, t), atts)] #-> (fn [thm] => pair (name, thm))
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   312
  | thy_def true ((name, atts), t) =
27691
ce171cbd4b93 PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents: 26939
diff changeset
   313
      PureThy.add_defs_unchecked false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
25557
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   314
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   315
in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   316
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   317
val add_primrec = gen_primrec thy_note (thy_def false);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   318
val add_primrec_unchecked = gen_primrec thy_note (thy_def true);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   319
val add_primrec_i = gen_primrec_i thy_note (thy_def false);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   320
val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   321
fun gen_primrec note def alt_name specs =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   322
  gen_primrec_i note def alt_name (map (fn ((name, t), atts) => ((name, atts), t)) specs);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   323
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   324
end;
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   325
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   326
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   327
(* see primrecr_package.ML (* outer syntax *)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   328
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   329
local structure P = OuterParse and K = OuterKeyword in
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   330
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   331
val opt_unchecked_name =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   332
  Scan.optional (P.$$$ "(" |-- P.!!!
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   333
    (((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" ||
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   334
      P.name >> pair false) --| P.$$$ ")")) (false, "");
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   335
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   336
val primrec_decl =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   337
  opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop);
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   338
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   339
val _ =
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   340
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   341
    (primrec_decl >> (fn ((unchecked, alt_name), eqns) =>
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   342
      Toplevel.theory (snd o
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   343
        (if unchecked then add_primrec_unchecked else add_primrec) alt_name
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   344
          (map P.triple_swap eqns))));
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   345
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   346
end;*)
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   347
ea6b11021e79 added new primrec package
haftmann
parents:
diff changeset
   348
end;