src/HOL/Tools/primrec_package.ML
author berghofe
Thu, 10 Aug 2000 14:55:21 +0200
changeset 9575 af71f5f4ca6b
parent 9315 f793f05024f6
child 10525 3e21ab3e5114
permissions -rw-r--r--
Equations that are added to the simpset now have proper names.
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
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
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
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    11
  val quiet_mode: bool ref
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    12
  val print_primrecs: theory -> unit
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    13
  val get_primrec: theory -> string -> (string * thm list) list option
6425
9540aa1b5a9a tuned comments;
wenzelm
parents: 6394
diff changeset
    14
  val add_primrec: string -> ((bstring * string) * Args.src list) list
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    15
    -> theory -> theory * thm list
6425
9540aa1b5a9a tuned comments;
wenzelm
parents: 6394
diff changeset
    16
  val add_primrec_i: string -> ((bstring * term) * theory attribute list) list
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    17
    -> theory -> theory * thm list
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    18
  val setup: (theory -> theory) list
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    19
end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    20
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    21
structure PrimrecPackage : PRIMREC_PACKAGE =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    22
struct
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    23
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    24
open DatatypeAux;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    25
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    26
exception RecError of string;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    27
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    28
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    29
fun primrec_eq_err sign s eq =
7544
dee529666dcd Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents: 7152
diff changeset
    30
  primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term sign eq));
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    31
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    32
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    33
(* messages *)
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    34
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    35
val quiet_mode = ref false;
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    36
fun message s = if ! quiet_mode then () else writeln s;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    37
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    38
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    39
(** theory data **)
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    40
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    41
(* data kind 'HOL/primrec' *)
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    42
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    43
structure PrimrecArgs =
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    44
struct
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    45
  val name = "HOL/primrec";
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    46
  type T = (string * thm list) list Symtab.table;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    47
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    48
  val empty = Symtab.empty;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    49
  val copy = I;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    50
  val prep_ext = I;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    51
  val merge: T * T -> T = Symtab.merge (K true);
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    52
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    53
  fun print sg tab =
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    54
    Pretty.writeln (Pretty.strs ("primrecs:" ::
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    55
      map #1 (Sign.cond_extern_table sg Sign.constK tab)));
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    56
end;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    57
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    58
structure PrimrecData = TheoryDataFun(PrimrecArgs);
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    59
val print_primrecs = PrimrecData.print;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    60
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    61
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    62
(* get and put data *)
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    63
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    64
fun get_primrec thy name = Symtab.lookup (PrimrecData.get thy, name);
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    65
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    66
fun put_primrec name info thy =
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    67
  let val tab = PrimrecData.get thy
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    68
  in 
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    69
    PrimrecData.put (case Symtab.lookup (tab, name) of
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    70
       None => Symtab.update_new ((name, [info]), tab)
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    71
     | Some infos => Symtab.update ((name, info::infos), tab)) thy end;
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    72
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
    73
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    74
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    75
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    76
fun process_eqn sign (eq, rec_fns) = 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    77
  let
6032
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    78
    val (lhs, rhs) = 
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    79
	if null (term_vars eq) then
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    80
	    HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    81
	      handle TERM _ => raise RecError "not a proper equation"
6032
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    82
	else raise RecError "illegal schematic variable(s)";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    83
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    84
    val (recfun, args) = strip_comb lhs;
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    85
    val (fname, _) = dest_Const recfun handle TERM _ => 
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    86
      raise RecError "function is not declared as constant in theory";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    87
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    88
    val (ls', rest)  = take_prefix is_Free args;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    89
    val (middle, rs') = take_suffix is_Free rest;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    90
    val rpos = length ls';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    91
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    92
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    93
      else strip_comb (hd middle);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    94
    val (cname, T) = dest_Const constr
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    95
      handle TERM _ => raise RecError "ill-formed constructor";
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    96
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    97
      raise RecError "cannot determine datatype associated with function"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    98
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    99
    val (ls, cargs, rs) = (map dest_Free ls', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   100
			   map dest_Free cargs', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   101
			   map dest_Free rs')
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   102
      handle TERM _ => raise RecError "illegal argument in pattern";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   103
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   104
8973
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   105
    val _ = case duplicates lfrees of
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   106
	       [] => ()
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   107
	     | vars => raise RecError("repeated variable names in pattern: " ^ 
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   108
				      commas_quote (map #1 vars))
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   109
 
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   110
    val _ =  case (map dest_Free (term_frees rhs)) \\ lfrees of
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   111
		[] => ()
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   112
	      | vars => raise RecError 
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   113
		      ("extra variables on rhs: " ^ 
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   114
		       commas_quote (map #1 vars))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   115
  in
8973
ac448cd43452 improved error msgs, listing variable names
paulson
parents: 8480
diff changeset
   116
    if length middle > 1 then 
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   117
      raise RecError "more than one non-variable in pattern"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   118
    else (case assoc (rec_fns, fname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   119
        None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   120
          (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   121
      | Some (_, rpos', eqns) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   122
          if is_some (assoc (eqns, cname)) then
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   123
            raise RecError "constructor already occurred as pattern"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   124
          else if rpos <> rpos' then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   125
            raise RecError "position of recursive argument inconsistent"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   126
          else
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   127
            overwrite (rec_fns, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   128
		       (fname, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   129
			(tname, rpos,
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   130
			 (cname, (ls, cargs, rs, rhs, eq))::eqns))))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   131
  end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   132
  handle RecError s => primrec_eq_err sign s eq;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   133
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   134
fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   135
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   136
    val (_, (tname, _, constrs)) = nth_elem (i, descr);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   137
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   138
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   139
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   140
    fun subst [] x = x
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   141
      | subst subs (fs, Abs (a, T, t)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   142
          let val (fs', t') = subst subs (fs, t)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   143
          in (fs', Abs (a, T, t')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   144
      | subst subs (fs, t as (_ $ _)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   145
          let val (f, ts) = strip_comb t;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   146
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   147
            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
   148
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   149
                val (fname', _) = dest_Const f;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   150
                val (_, rpos, _) = the (assoc (rec_eqns, fname'));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   151
                val ls = take (rpos, ts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   152
                val rest = drop (rpos, ts);
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   153
                val (x', rs) = (hd rest, tl rest)
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   154
                  handle LIST _ => raise RecError ("not enough arguments\
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   155
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   156
                val (x, xs) = strip_comb x'
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   157
              in 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   158
                (case assoc (subs, x) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   159
                    None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   160
                      let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   161
                        val (fs', ts') = foldl_map (subst subs) (fs, ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   162
                      in (fs', list_comb (f, ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   163
                  | Some (i', y) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   164
                      let
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   165
                        val (fs', ts') = foldl_map (subst subs) (fs, xs @ ls @ rs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   166
                        val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   167
                      in (fs'', list_comb (y, ts'))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   168
                      end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   169
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   170
            else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   171
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   172
                val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   173
              in (fs', list_comb (f', ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   174
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   175
      | subst _ x = x;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   176
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   177
    (* translate rec equations into function arguments suitable for rec comb *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   179
    fun trans eqns ((cname, cargs), (fnames', fnss', fns)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   180
      (case assoc (eqns, cname) of
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   181
          None => (warning ("no equation for constructor " ^ quote cname ^
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   182
            "\nin definition of function " ^ quote fname);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   183
              (fnames', fnss', (Const ("arbitrary", dummyT))::fns))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   184
        | Some (ls, cargs', rs, rhs, eq) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   185
            let
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   186
              fun rec_index (DtRec k) = k
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   187
                | rec_index (DtType ("fun", [_, DtRec k])) = k;
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   188
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   189
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   190
              val rargs = map fst recs;
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   191
              val subs = map (rpair dummyT o fst) 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   192
		             (rev (rename_wrt_term rhs rargs));
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   193
              val ((fnames'', fnss''), rhs') = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   194
		  (subst (map (fn ((x, y), z) =>
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   195
			       (Free x, (rec_index y, Free z)))
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   196
			  (recs ~~ subs))
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   197
		   ((fnames', fnss'), rhs))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   198
                  handle RecError s => primrec_eq_err sign s eq
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   199
            in (fnames'', fnss'', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   200
		(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   201
            end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   202
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   203
  in (case assoc (fnames, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   204
      None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   205
        if exists (equal fname o snd) fnames then
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   206
          raise RecError ("inconsistent functions for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   207
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   208
          let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   209
            val (_, _, eqns) = the (assoc (rec_eqns, fname));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   210
            val (fnames', fnss', fns) = foldr (trans eqns)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   211
              (constrs, ((i, fname)::fnames, fnss, []))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   212
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   213
            (fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   214
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   215
    | Some fname' =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   216
        if fname = fname' then (fnames, fnss)
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   217
        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   218
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   219
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   220
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   221
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   222
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   223
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   224
  case assoc (fns, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   225
     None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   226
       let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   227
         val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   228
           replicate ((length cargs) + (length (filter is_rec_type cargs)))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   229
             dummyT ---> HOLogic.unitT)) constrs;
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   230
         val _ = warning ("No function definition for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   231
       in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   232
         (dummy_fns @ fs, defs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   233
       end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   234
   | Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   235
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   236
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   237
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   238
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   239
fun make_def sign fs (fname, ls, rec_name, tname) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   240
  let
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   241
    val rhs = foldr (fn (T, t) => Abs ("", T, t)) 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   242
	            ((map snd ls) @ [dummyT],
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   243
		     list_comb (Const (rec_name, dummyT),
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   244
				fs @ map Bound (0 ::(length ls downto 1))));
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   245
    val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def",
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   246
		   Logic.mk_equals (Const (fname, dummyT), rhs))
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   247
  in Theory.inferT_axm sign defpair end;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   248
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   249
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   250
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   251
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   252
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   253
  | find_dts dt_info tnames' (tname::tnames) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   254
      (case Symtab.lookup (dt_info, tname) of
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   255
          None => primrec_err (quote tname ^ " is not a datatype")
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   256
        | Some dt =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   257
            if tnames' subset (map (#1 o snd) (#descr dt)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   258
              (tname, dt)::(find_dts dt_info tnames' tnames)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   259
            else find_dts dt_info tnames' tnames);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   260
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   261
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   262
  let
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   263
    fun constrs_of (_, (_, _, cs)) =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   264
      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   265
    val params_of = Library.assocs (flat (map constrs_of rec_eqns));
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   266
  in
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   267
    induction
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   268
    |> RuleCases.rename_params (map params_of (flat (map (map #1 o #3 o #2) descr)))
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   269
    |> RuleCases.name (RuleCases.get induction)
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   270
  end;
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   271
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   272
fun add_primrec_i alt_name eqns_atts thy =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   273
  let
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   274
    val (eqns, atts) = split_list eqns_atts;
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   275
    val sg = Theory.sign_of thy;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   276
    val dt_info = DatatypePackage.get_datatypes thy;
5719
2aed60cdb9f2 Changed interface.
berghofe
parents: 5553
diff changeset
   277
    val rec_eqns = foldr (process_eqn sg) (map snd eqns, []);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   278
    val tnames = distinct (map (#1 o snd) rec_eqns);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   279
    val dts = find_dts dt_info tnames tnames;
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   280
    val main_fns = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   281
	map (fn (tname, {index, ...}) =>
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   282
	     (index, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   283
	      fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns))))
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   284
	dts;
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   285
    val {descr, rec_names, rec_rewrites, ...} = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   286
	if null dts then
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   287
	    primrec_err ("datatypes " ^ commas_quote tnames ^ 
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   288
			 "\nare not mutually recursive")
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   289
	else snd (hd dts);
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   290
    val (fnames, fnss) = foldr (process_fun sg descr rec_eqns)
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   291
	                       (main_fns, ([], []));
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   292
    val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], []));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   293
    val defs' = map (make_def sg fs) defs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   294
    val names1 = map snd fnames;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   295
    val names2 = map fst rec_eqns;
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   296
    val primrec_name =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   297
      if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name;
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   298
    val (thy', defs_thms') = thy |> Theory.add_path primrec_name |>
9315
f793f05024f6 adapted PureThy.add_defs_i;
wenzelm
parents: 8973
diff changeset
   299
      (if eq_set (names1, names2) then (PureThy.add_defs_i false o map Thm.no_attributes) defs'
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   300
       else primrec_err ("functions " ^ commas_quote names2 ^
5216
f0a66af5f2cb Equations are now stored in theory.
berghofe
parents: 5178
diff changeset
   301
         "\nare not mutually recursive"));
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   302
    val rewrites = o_def :: (map mk_meta_eq rec_rewrites) @ defs_thms';
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   303
    val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names1 ^ " ...");
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   304
    val simps = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (Theory.sign_of thy') t)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   305
        (fn _ => [rtac refl 1])) eqns;
9575
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   306
    val (thy'', simps') = PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts) thy';
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   307
    val thy''' = thy''
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   308
      |> (#1 o PureThy.add_thmss [(("simps", simps'), [Simplifier.simp_add_global])])
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   309
      |> (#1 o PureThy.add_thms [(("induct", prepare_induct (#2 (hd dts)) rec_eqns), [])])
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   310
      |> Theory.parent_path
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   311
  in
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   312
    (foldl (fn (thy, (fname, _, _, tname)) =>
9575
af71f5f4ca6b Equations that are added to the simpset now have proper names.
berghofe
parents: 9315
diff changeset
   313
       put_primrec fname (tname, simps') thy) (thy''', defs), simps')
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   314
  end;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   315
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   316
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   317
fun add_primrec alt_name eqns thy =
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   318
  let
7544
dee529666dcd Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents: 7152
diff changeset
   319
    val sign = Theory.sign_of thy;
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   320
    val ((names, strings), srcss) = apfst split_list (split_list eqns);
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   321
    val atts = map (map (Attrib.global_attribute thy)) srcss;
7722
8add8fdce3f1 Got rid of readtm.
berghofe
parents: 7544
diff changeset
   322
    val eqn_ts = map (term_of o Thm.read_cterm sign o rpair propT) strings;
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   323
    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq)))
7544
dee529666dcd Fixed bug in add_primrec which caused non-informative error message.
berghofe
parents: 7152
diff changeset
   324
      handle TERM _ => primrec_eq_err sign "not a proper equation" eq) eqn_ts;
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   325
    val (_, eqn_ts') = InductivePackage.unify_consts (sign_of thy) rec_ts eqn_ts
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   326
  in
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   327
    add_primrec_i alt_name (names ~~ eqn_ts' ~~ atts) thy
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   328
  end;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   329
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   330
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   331
(** package setup **)
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   332
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   333
val setup = [PrimrecData.init];
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   334
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   335
(* outer syntax *)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   336
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   337
local structure P = OuterParse and K = OuterSyntax.Keyword in
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   338
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   339
val primrec_decl =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   340
  Scan.optional (P.$$$ "(" |-- P.name --| P.$$$ ")") "" --
7152
44d46a112127 tuned outer syntax;
wenzelm
parents: 7016
diff changeset
   341
    Scan.repeat1 (P.opt_thm_name ":" -- P.prop --| P.marg_comment);
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   342
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   343
val primrecP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   344
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   345
    (primrec_decl >> (fn (alt_name, eqns) =>
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   346
      Toplevel.theory (#1 o add_primrec alt_name (map P.triple_swap eqns))));
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   347
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   348
val _ = OuterSyntax.add_parsers [primrecP];
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   349
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   350
end;
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   351
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   352
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   353
end;