src/HOL/Tools/primrec_package.ML
author wenzelm
Wed, 14 Apr 1999 14:42:53 +0200
changeset 6425 9540aa1b5a9a
parent 6394 3d9fd50fcc43
child 6427 fd36b2e7d80e
permissions -rw-r--r--
tuned comments; tuned types;
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
6425
9540aa1b5a9a tuned comments;
wenzelm
parents: 6394
diff changeset
    11
  val add_primrec: string -> ((bstring * string) * Args.src list) list
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    12
    -> theory -> theory * thm list
6425
9540aa1b5a9a tuned comments;
wenzelm
parents: 6394
diff changeset
    13
  val add_primrec_i: string -> ((bstring * term) * theory attribute list) list
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
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
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    25
fun primrec_eq_err sign s eq =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    26
  primrec_err (s ^ "\nin equation\n" ^ Sign.string_of_term sign eq);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    27
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    28
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    29
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    30
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    31
fun process_eqn sign (eq, rec_fns) = 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    32
  let
6032
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    33
    val (lhs, rhs) = 
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    34
	if null (term_vars eq) then
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    35
	    HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    36
	      handle _ => raise RecError "not a proper equation"
c4e62bab69bd moved dest_eq to hologic.ML and tidied
paulson
parents: 5891
diff changeset
    37
	else raise RecError "illegal schematic variable(s)";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    38
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    39
    val (recfun, args) = strip_comb lhs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    40
    val (fname, _) = dest_Const recfun handle _ => 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    41
      raise RecError "function is not declared as constant in theory";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    42
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    43
    val (ls', rest)  = take_prefix is_Free args;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    44
    val (middle, rs') = take_suffix is_Free rest;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    45
    val rpos = length ls';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    46
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    47
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    48
      else strip_comb (hd middle);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    49
    val (cname, T) = dest_Const constr
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    50
      handle _ => raise RecError "ill-formed constructor";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    51
    val (tname, _) = dest_Type (body_type T) handle _ =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    52
      raise RecError "cannot determine datatype associated with function"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    53
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    54
    val (ls, cargs, rs) = (map dest_Free ls', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    55
			   map dest_Free cargs', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    56
			   map dest_Free rs')
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    57
      handle _ => raise RecError "illegal argument in pattern";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    58
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    59
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    60
  in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    61
    if not (null (duplicates lfrees)) then 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    62
      raise RecError "repeated variable name in pattern" 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    63
    else if not ((map dest_Free (term_frees rhs)) subset lfrees) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    64
      raise RecError "extra variables on rhs"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    65
    else if length middle > 1 then 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    66
      raise RecError "more than one non-variable in pattern"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    67
    else (case assoc (rec_fns, fname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    68
        None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    69
          (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    70
      | Some (_, rpos', eqns) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    71
          if is_some (assoc (eqns, cname)) then
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    72
            raise RecError "constructor already occurred as pattern"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    73
          else if rpos <> rpos' then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    74
            raise RecError "position of recursive argument inconsistent"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    75
          else
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    76
            overwrite (rec_fns, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    77
		       (fname, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    78
			(tname, rpos,
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
    79
			 (cname, (ls, cargs, rs, rhs, eq))::eqns))))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    80
  end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    81
  handle RecError s => primrec_eq_err sign s eq;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    82
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    83
fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    84
  let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    85
    val (_, (tname, _, constrs)) = nth_elem (i, descr);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    86
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    87
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    88
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    89
    fun subst [] x = x
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    90
      | subst subs (fs, Abs (a, T, t)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    91
          let val (fs', t') = subst subs (fs, t)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    92
          in (fs', Abs (a, T, t')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    93
      | subst subs (fs, t as (_ $ _)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    94
          let val (f, ts) = strip_comb t;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    95
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    96
            if is_Const f andalso (fst (dest_Const f)) mem (map fst rec_eqns) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    97
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    98
                val (fname', _) = dest_Const f;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    99
                val (_, rpos, _) = the (assoc (rec_eqns, fname'));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   100
                val ls = take (rpos, ts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   101
                val rest = drop (rpos, ts);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   102
                val (x, rs) = (hd rest, tl rest)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   103
                  handle _ => raise RecError ("not enough arguments\
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   104
                   \ in recursive application\nof function " ^ fname' ^ " on rhs")
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   105
              in 
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   106
                (case assoc (subs, x) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   107
                    None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   108
                      let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   109
                        val (fs', ts') = foldl_map (subst subs) (fs, ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   110
                      in (fs', list_comb (f, ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   111
                  | Some (i', y) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   112
                      let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   113
                        val (fs', ts') = foldl_map (subst subs) (fs, ls @ rs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   114
                        val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   115
                      in (fs'', list_comb (y, ts'))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   116
                      end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   117
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   118
            else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   119
              let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   120
                val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   121
              in (fs', list_comb (f', ts')) end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   122
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   123
      | subst _ x = x;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   124
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   125
    (* translate rec equations into function arguments suitable for rec comb *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   126
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   127
    fun trans eqns ((cname, cargs), (fnames', fnss', fns)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   128
      (case assoc (eqns, cname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   129
          None => (warning ("no equation for constructor " ^ cname ^
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   130
            "\nin definition of function " ^ fname);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   131
              (fnames', fnss', (Const ("arbitrary", dummyT))::fns))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   132
        | Some (ls, cargs', rs, rhs, eq) =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   133
            let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   134
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   135
              val rargs = map fst recs;
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   136
              val subs = map (rpair dummyT o fst) 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   137
		             (rev (rename_wrt_term rhs rargs));
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   138
              val ((fnames'', fnss''), rhs') = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   139
		  (subst (map (fn ((x, y), z) =>
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   140
			       (Free x, (dest_DtRec y, Free z)))
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   141
			  (recs ~~ subs))
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   142
		   ((fnames', fnss'), rhs))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   143
                  handle RecError s => primrec_eq_err sign s eq
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   144
            in (fnames'', fnss'', 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   145
		(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   146
            end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   147
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   148
  in (case assoc (fnames, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   149
      None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   150
        if exists (equal fname o snd) fnames then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   151
          raise RecError ("inconsistent functions for datatype " ^ tname)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   152
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   153
          let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   154
            val (_, _, eqns) = the (assoc (rec_eqns, fname));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   155
            val (fnames', fnss', fns) = foldr (trans eqns)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   156
              (constrs, ((i, fname)::fnames, fnss, []))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   157
          in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   158
            (fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   159
          end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   160
    | Some fname' =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   161
        if fname = fname' then (fnames, fnss)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   162
        else raise RecError ("inconsistent functions for datatype " ^ tname))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   163
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   164
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   165
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   166
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   167
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   168
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   169
  case assoc (fns, i) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   170
     None =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   171
       let
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   172
         val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   173
           replicate ((length cargs) + (length (filter is_rec_type cargs)))
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   174
             dummyT ---> HOLogic.unitT)) constrs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   175
         val _ = warning ("no function definition for datatype " ^ tname)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   176
       in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   177
         (dummy_fns @ fs, defs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   178
       end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   179
   | Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   180
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   181
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   182
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   183
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   184
fun make_def sign fs (fname, ls, rec_name, tname) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   185
  let
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   186
    val rhs = foldr (fn (T, t) => Abs ("", T, t)) 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   187
	            ((map snd ls) @ [dummyT],
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   188
		     list_comb (Const (rec_name, dummyT),
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   189
				fs @ map Bound (0 ::(length ls downto 1))));
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   190
    val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def",
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   191
		   Logic.mk_equals (Const (fname, dummyT), rhs))
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   192
  in Theory.inferT_axm sign defpair end;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   193
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   194
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   195
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   196
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   197
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   198
  | find_dts dt_info tnames' (tname::tnames) =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   199
      (case Symtab.lookup (dt_info, tname) of
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   200
          None => primrec_err (tname ^ " is not a datatype")
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   201
        | Some dt =>
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   202
            if tnames' subset (map (#1 o snd) (#descr dt)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   203
              (tname, dt)::(find_dts dt_info tnames' tnames)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   204
            else find_dts dt_info tnames' tnames);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   205
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   206
fun add_primrec_i alt_name eqns_atts thy =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   207
  let
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   208
    val (eqns, atts) = split_list eqns_atts;
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   209
    val sg = Theory.sign_of thy;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   210
    val dt_info = DatatypePackage.get_datatypes thy;
5719
2aed60cdb9f2 Changed interface.
berghofe
parents: 5553
diff changeset
   211
    val rec_eqns = foldr (process_eqn sg) (map snd eqns, []);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   212
    val tnames = distinct (map (#1 o snd) rec_eqns);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   213
    val dts = find_dts dt_info tnames tnames;
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   214
    val main_fns = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   215
	map (fn (tname, {index, ...}) =>
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   216
	     (index, 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   217
	      fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns))))
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   218
	dts;
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   219
    val {descr, rec_names, rec_rewrites, ...} = 
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   220
	if null dts then
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   221
	    primrec_err ("datatypes " ^ commas_quote tnames ^ 
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   222
			 "\nare not mutually recursive")
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   223
	else snd (hd dts);
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   224
    val (fnames, fnss) = foldr (process_fun sg descr rec_eqns)
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   225
	                       (main_fns, ([], []));
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   226
    val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], []));
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   227
    val defs' = map (make_def sg fs) defs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   228
    val names1 = map snd fnames;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   229
    val names2 = map fst rec_eqns;
5216
f0a66af5f2cb Equations are now stored in theory.
berghofe
parents: 5178
diff changeset
   230
    val thy' = thy |>
5719
2aed60cdb9f2 Changed interface.
berghofe
parents: 5553
diff changeset
   231
      Theory.add_path (if alt_name = "" then (space_implode "_"
2aed60cdb9f2 Changed interface.
berghofe
parents: 5553
diff changeset
   232
        (map (Sign.base_name o #1) defs)) else alt_name) |>
5216
f0a66af5f2cb Equations are now stored in theory.
berghofe
parents: 5178
diff changeset
   233
      (if eq_set (names1, names2) then Theory.add_defs_i defs'
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   234
       else primrec_err ("functions " ^ commas_quote names2 ^
5216
f0a66af5f2cb Equations are now stored in theory.
berghofe
parents: 5178
diff changeset
   235
         "\nare not mutually recursive"));
5553
ae42b36a50c2 renamed mk_meta_eq to mk_eq
oheimb
parents: 5303
diff changeset
   236
    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
   237
    val _ = writeln ("Proving equations for primrec function(s)\n" ^
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   238
      commas_quote names1 ^ " ...");
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   239
    val char_thms = 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
   240
        (fn _ => [rtac refl 1])) eqns;
6092
d9db67970c73 eliminated tthm type and Attribute structure;
wenzelm
parents: 6037
diff changeset
   241
    val simps = char_thms;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   242
    val thy'' =
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   243
      thy'
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   244
      |> PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])]
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   245
      |> PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   246
      |> Theory.parent_path;
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   247
  in (thy'', char_thms) end;
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   248
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   249
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   250
fun read_eqn thy ((name, s), srcs) =
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6384
diff changeset
   251
  ((name, readtm (Theory.sign_of thy) propT s), map (Attrib.global_attribute thy) srcs);
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   252
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   253
fun add_primrec alt_name eqns thy = add_primrec_i alt_name (map (read_eqn thy) eqns) thy;
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   254
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   255
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   256
(* outer syntax *)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   257
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   258
local open OuterParse in
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   259
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   260
val primrec_decl =
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   261
  Scan.optional ($$$ "(" |-- name --| $$$ ")") "" --
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   262
    Scan.repeat1 (opt_thm_name ":" -- term);
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   263
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   264
val primrecP =
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   265
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes"
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   266
    (primrec_decl >> (fn (alt_name, eqns) =>
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   267
      Toplevel.theory (#1 o add_primrec alt_name (map (fn ((x, y), z) => ((x, z), y)) eqns))));
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   268
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   269
val _ = OuterSyntax.add_parsers [primrecP];
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   270
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   271
end;
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   272
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   273
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   274
end;