src/HOL/Tools/primrec_package.ML
author haftmann
Thu, 06 Dec 2007 16:36:19 +0100
changeset 25559 f14305fb698c
parent 25557 ea6b11021e79
child 25562 f0fc8531c909
permissions -rw-r--r--
authentic primrec
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$
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen; Norbert Voelker, FernUni Hagen;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
     4
                Florian Haftmann, TU Muenchen
5178
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
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    11
  val add_primrec: (string * typ option * mixfix) list ->
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    12
    ((bstring * Attrib.src list) * term) list -> local_theory -> thm list * local_theory
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    13
end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    14
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    15
structure PrimrecPackage : PRIMREC_PACKAGE =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    16
struct
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    17
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    18
open DatatypeAux;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    19
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    20
exception PrimrecError of string * term option;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    21
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    22
fun primrec_error msg = raise PrimrecError (msg, NONE);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    23
fun primrec_error_eqn msg eqn = raise PrimrecError (msg, SOME eqn);
23765
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    24
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    25
fun message s = if ! Toplevel.debug then () else writeln s;
23765
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    26
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    27
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    28
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    29
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
    30
fun process_eqn is_fixed spec rec_fns =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    31
  let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    32
    val vars = strip_qnt_vars "all" spec;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    33
    val body = strip_qnt_body "all" spec;
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
    34
    (*FIXME not necessarily correct*)
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    35
    val eqn = curry subst_bounds (map Free (rev vars)) body;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    36
    val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eqn)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    37
      handle TERM _ => primrec_error "not a proper equation";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    38
    val (recfun, args) = strip_comb lhs;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    39
    val fname = case recfun of Free (v, _) => if is_fixed v then v
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    40
          else primrec_error "illegal head of function equation"
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    41
      | _ => primrec_error "illegal head of function equation";
5178
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
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    47
    val (constr, cargs') = if null middle then primrec_error "constructor missing"
5178
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
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    50
      handle TERM _ => primrec_error "ill-formed constructor";
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    51
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    52
      primrec_error "cannot determine datatype associated with function"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    53
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    54
    val (ls, cargs, rs) =
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    55
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    56
      handle TERM _ => primrec_error "illegal argument in pattern";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    57
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    58
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    59
    fun check_vars _ [] = ()
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    60
      | check_vars s vars = primrec_error (s ^ commas_quote (map fst vars)) eqn;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    61
  in
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    62
    if length middle > 1 then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    63
      primrec_error "more than one non-variable in pattern"
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    64
    else
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18928
diff changeset
    65
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    66
      check_vars "extra variables on rhs: "
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    67
        (map dest_Free (term_frees rhs) |> subtract (op =) lfrees
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
    68
          |> filter_out (is_fixed o fst));
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    69
      case AList.lookup (op =) rec_fns fname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    70
        NONE =>
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    71
          (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eqn))]))::rec_fns
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    72
      | SOME (_, rpos', eqns) =>
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
    73
          if AList.defined (op =) eqns cname then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    74
            primrec_error "constructor already occurred as pattern"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    75
          else if rpos <> rpos' then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    76
            primrec_error "position of recursive argument inconsistent"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    77
          else
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    78
            AList.update (op =)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    79
              (fname, (tname, rpos, (cname, (ls, cargs, rs, rhs, eqn))::eqns))
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 17261
diff changeset
    80
              rec_fns)
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    81
  end handle PrimrecError (msg, NONE) => primrec_error_eqn msg spec;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    82
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    83
fun process_fun descr eqns (i, fname) (fnames, fnss) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    84
  let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    85
    val (_, (tname, _, constrs)) = nth descr i;
5178
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
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    89
    fun subst [] t fs = (t, fs)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    90
      | subst subs (Abs (a, T, t)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    91
          fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    92
          |> subst subs t
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    93
          |-> (fn t' => pair (Abs (a, T, t')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    94
      | subst subs (t as (_ $ _)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    95
          let
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
    96
            val (f, ts) = strip_comb t;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    97
          in
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    98
            if is_Free f
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    99
              andalso member (fn ((v, _), (w, _)) => v = w) eqns (dest_Free f) then
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   100
              let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   101
                val (fname', _) = dest_Free f;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   102
                val (_, rpos, _) = the (AList.lookup (op =) eqns fname');
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   103
                val (ls, x' :: rs) = chop rpos ts
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   104
                  handle Empty => primrec_error ("not enough arguments\
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   105
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   106
                val (x, xs) = strip_comb x'
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   107
              in case AList.lookup (op =) subs x
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   108
               of NONE =>
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   109
                    fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   110
                    |> fold_map (subst subs) ts
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   111
                    |-> (fn ts' => pair (list_comb (f, ts')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   112
                | SOME (i', y) =>
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   113
                    fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   114
                    |> fold_map (subst subs) (xs @ ls @ rs)
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   115
                    ||> process_fun descr eqns (i', fname')
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   116
                    |-> (fn ts' => pair (list_comb (y, ts')))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   117
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   118
            else
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   119
              fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   120
              |> fold_map (subst subs) (f :: ts)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   121
              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   122
          end
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   123
      | subst _ t fs = (t, fs);
5178
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
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   127
    fun trans eqns (cname, cargs) (fnames', fnss', fns) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   128
      (case AList.lookup (op =) eqns cname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   129
          NONE => (warning ("No equation for constructor " ^ quote cname ^
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   130
            "\nin definition of function " ^ quote fname);
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   131
              (fnames', fnss', (Const ("HOL.undefined", dummyT))::fns))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   132
        | SOME (ls, cargs', rs, rhs, eq) =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   133
            let
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   134
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   135
              val rargs = map fst recs;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   136
              val subs = map (rpair dummyT o fst)
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   137
                (rev (rename_wrt_term rhs rargs));
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   138
              val (rhs', (fnames'', fnss'')) = (subst (map2 (fn (x, y) => fn z =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   139
                (Free x, (body_index y, Free z))) recs subs) rhs (fnames', fnss'))
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   140
                  handle PrimrecError (s, NONE) => primrec_error_eqn s eq
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   141
            in (fnames'', fnss'',
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   142
                (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   143
            end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   144
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   145
  in (case AList.lookup (op =) fnames i of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   146
      NONE =>
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   147
        if exists (fn (_, v) => fname = v) fnames then
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   148
          primrec_error ("inconsistent functions for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   149
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   150
          let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   151
            val (_, _, eqns) = the (AList.lookup (op =) eqns fname);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   152
            val (fnames', fnss', fns) = fold_rev (trans eqns) constrs
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   153
              ((i, fname)::fnames, fnss, [])
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   154
          in
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   155
            (fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   156
          end
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   157
    | SOME fname' =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   158
        if fname = fname' then (fnames, fnss)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   159
        else primrec_error ("inconsistent functions for datatype " ^ quote tname))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   160
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   161
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   162
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   163
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   164
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   165
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   166
  case AList.lookup (op =) fns i of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   167
     NONE =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   168
       let
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22101
diff changeset
   169
         val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   170
           replicate ((length cargs) + (length (List.filter is_rec_type cargs)))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   171
             dummyT ---> HOLogic.unitT)) constrs;
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   172
         val _ = warning ("No function definition for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   173
       in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   174
         (dummy_fns @ fs, defs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   175
       end
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   176
   | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   177
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   178
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   179
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   180
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   181
fun make_def ctxt fixes fs (fname, ls, rec_name, tname) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   182
  let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   183
    val raw_rhs = fold_rev (fn T => fn t => Abs ("", T, t))
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   184
                    ((map snd ls) @ [dummyT])
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   185
                    (list_comb (Const (rec_name, dummyT),
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   186
                                fs @ map Bound (0 ::(length ls downto 1))))
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   187
    val def_name = Thm.def_name (Sign.base_name fname);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   188
    val rhs = singleton (Syntax.check_terms ctxt) raw_rhs;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   189
    val SOME mfx = get_first
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   190
      (fn ((v, _), mfx) => if v = fname then SOME mfx else NONE) fixes;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   191
  in ((fname, mfx), ((def_name, []), rhs)) end;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   192
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   193
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   194
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   195
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   196
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   197
  | find_dts dt_info tnames' (tname::tnames) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17314
diff changeset
   198
      (case Symtab.lookup dt_info tname of
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   199
          NONE => primrec_error (quote tname ^ " is not a datatype")
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   200
        | SOME dt =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   201
            if tnames' subset (map (#1 o snd) (#descr dt)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   202
              (tname, dt)::(find_dts dt_info tnames' tnames)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   203
            else find_dts dt_info tnames' tnames);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   204
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   205
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   206
(* adapted induction rule *)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   207
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   208
fun prepare_induct ({descr, induction, ...}: datatype_info) eqns =
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   209
  let
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   210
    fun constrs_of (_, (_, _, cs)) =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   211
      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   212
    val params_of = these o AList.lookup (op =) (List.concat (map constrs_of eqns));
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   213
  in
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   214
    induction
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   215
    |> RuleCases.rename_params (map params_of (maps (map #1 o #3 o #2) descr))
10525
3e21ab3e5114 RuleCases.save;
wenzelm
parents: 9575
diff changeset
   216
    |> RuleCases.save induction
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   217
  end;
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   218
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   219
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   220
(* primrec definition *)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   221
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   222
local
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   223
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   224
fun prepare_spec prep_spec ctxt raw_fixes raw_spec =
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   225
  let
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   226
    val ((fixes, spec), _) = prep_spec
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   227
      raw_fixes (map (single o apsnd single) raw_spec) ctxt
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   228
  in (fixes, map (apsnd the_single) spec) end;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   229
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   230
fun prove_spec ctxt rec_rewrites defs =
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   231
  let
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   232
    val rewrites = map mk_meta_eq rec_rewrites @ map (snd o snd) defs;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   233
    fun tac _ = EVERY [rewrite_goals_tac rewrites, rtac refl 1];
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   234
    val _ = message "Proving equations for primrec function";
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   235
  in map (fn (name_attr, t) => (name_attr, [Goal.prove ctxt [] [] t tac])) end;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   236
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   237
fun gen_primrec prep_spec raw_fixes raw_spec lthy =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   238
  let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   239
    val (fixes, spec) = prepare_spec prep_spec lthy raw_fixes raw_spec;
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   240
    val eqns = fold_rev (process_eqn (fn v => Variable.is_fixed lthy v
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   241
      orelse exists (fn ((w, _), _) => v = w) fixes) o snd) spec [];
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   242
    val tnames = distinct (op =) (map (#1 o snd) eqns);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   243
    val dts = find_dts (DatatypePackage.get_datatypes
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   244
      (ProofContext.theory_of lthy)) tnames tnames;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   245
    val main_fns = map (fn (tname, {index, ...}) =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   246
      (index, (fst o the o find_first (fn (_, x) => #1 x = tname)) eqns)) dts;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   247
    val {descr, rec_names, rec_rewrites, ...} =
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   248
      if null dts then primrec_error
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   249
        ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
18362
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   250
      else snd (hd dts);
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   251
    val (fnames, fnss) = fold_rev (process_fun descr eqns) main_fns ([], []);
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   252
    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   253
    val nameTs1 = map snd fnames;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   254
    val nameTs2 = map fst eqns;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   255
    val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then ()
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   256
      else primrec_error ("functions " ^ commas_quote nameTs2 ^
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   257
        "\nare not mutually recursive");
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   258
    val qualify = NameSpace.qualified
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   259
      (space_implode "_" (map (Sign.base_name o #1) defs));
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   260
    val simp_atts = map (Attrib.internal o K) [Simplifier.simp_add, RecfunCodegen.add NONE];
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   261
  in
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   262
    lthy
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   263
    |> fold_map (LocalTheory.define Thm.definitionK o make_def lthy fixes fs) defs
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   264
    |-> (fn defs => `(fn ctxt => prove_spec ctxt rec_rewrites defs spec))
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   265
    |-> (fn simps => fold_map (LocalTheory.note Thm.theoremK) simps)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   266
    |-> (fn simps' => LocalTheory.note Thm.theoremK
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   267
          ((qualify "simps", simp_atts), maps snd simps'))
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   268
    ||>> LocalTheory.note Thm.theoremK
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   269
          ((qualify "induct", []), [prepare_induct (#2 (hd dts)) eqns])
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   270
    |>> (snd o fst)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   271
  end handle PrimrecError (msg, some_eqn) =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   272
    error ("Primrec definition error:\n" ^ msg ^ (case some_eqn
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   273
     of SOME eqn => "\nin\n" ^ quote (Syntax.string_of_term lthy eqn)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   274
      | NONE => ""));
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   275
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   276
in
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   277
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   278
val add_primrec = gen_primrec Specification.check_specification;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   279
val add_primrec_cmd = gen_primrec Specification.read_specification;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   280
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   281
end;
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   282
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   283
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   284
(* outer syntax *)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   285
17057
0934ac31985f OuterKeyword;
wenzelm
parents: 16765
diff changeset
   286
local structure P = OuterParse and K = OuterKeyword in
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   287
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   288
val opt_unchecked_name =
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   289
  Scan.optional (P.$$$ "(" |-- P.!!!
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   290
    (((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" ||
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   291
      P.name >> pair false) --| P.$$$ ")")) (false, "");
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   292
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   293
val old_primrec_decl =
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21064
diff changeset
   294
  opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop);
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   295
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   296
fun pipe_error t = P.!!! (Scan.fail_with (K
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   297
  (cat_lines ["Equations must be separated by " ^ quote "|", quote t])));
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   298
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   299
val statement = SpecParse.opt_thm_name ":" -- P.prop --| Scan.ahead
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   300
  ((P.term :-- pipe_error) || Scan.succeed ("",""));
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   301
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   302
val statements = P.enum1 "|" statement;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   303
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   304
val primrec_decl = P.opt_target -- P.fixes --| P.$$$ "where" -- statements;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   305
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   306
val _ =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   307
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   308
    ((primrec_decl >> (fn ((opt_target, raw_fixes), raw_spec) =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   309
      Toplevel.local_theory opt_target (add_primrec_cmd raw_fixes raw_spec #> snd)))
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   310
    || (old_primrec_decl >> (fn ((unchecked, alt_name), eqns) =>
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   311
      Toplevel.theory (snd o
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   312
        (if unchecked then OldPrimrecPackage.add_primrec_unchecked else OldPrimrecPackage.add_primrec) alt_name
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   313
          (map P.triple_swap eqns)))));
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   314
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   315
end;
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   316
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   317
end;