src/HOL/Tools/primrec.ML
author bulwahn
Fri, 03 Dec 2010 08:40:47 +0100
changeset 40905 647142607448
parent 40353 2f44afc0fff3
child 42361 23f352990944
permissions -rw-r--r--
only handle TimeOut exception if used interactively
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31296
diff changeset
     1
(*  Title:      HOL/Tools/primrec.ML
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
     2
    Author:     Norbert Voelker, FernUni Hagen
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
     4
    Author:     Florian Haftmann, TU Muenchen
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     5
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
     6
Primitive recursive functions on datatypes.
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     7
*)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
     8
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31296
diff changeset
     9
signature PRIMREC =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    10
sig
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29276
diff changeset
    11
  val add_primrec: (binding * typ option * mixfix) list ->
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
    12
    (Attrib.binding * term) list -> local_theory -> (term list * thm list) * local_theory
30487
a14ff49d3083 simplified preparation and outer parsing of specification;
wenzelm
parents: 30455
diff changeset
    13
  val add_primrec_cmd: (binding * string option * mixfix) list ->
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
    14
    (Attrib.binding * string) list -> local_theory -> (term list * thm list) * local_theory
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29276
diff changeset
    15
  val add_primrec_global: (binding * typ option * mixfix) list ->
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
    16
    (Attrib.binding * term) list -> theory -> (term list * thm list) * theory
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
    17
  val add_primrec_overloaded: (string * (string * typ) * bool) list ->
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29276
diff changeset
    18
    (binding * typ option * mixfix) list ->
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
    19
    (Attrib.binding * term) list -> theory -> (term list * thm list) * theory
31269
dcbe1f9fe2cd tuned signature of add_primrec_simple
haftmann
parents: 31262
diff changeset
    20
  val add_primrec_simple: ((binding * typ) * mixfix) list -> term list ->
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
    21
    local_theory -> (string * (term list * thm list)) * local_theory
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    22
end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    23
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31296
diff changeset
    24
structure Primrec : PRIMREC =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    25
struct
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    26
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
    27
open Datatype_Aux;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    28
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    29
exception PrimrecError of string * term option;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    30
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    31
fun primrec_error msg = raise PrimrecError (msg, NONE);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    32
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
    33
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    34
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    35
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    36
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
    37
fun process_eqn is_fixed spec rec_fns =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    38
  let
25566
33f740c5e022 tuned further
haftmann
parents: 25562
diff changeset
    39
    val (vs, Ts) = split_list (strip_qnt_vars "all" spec);
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    40
    val body = strip_qnt_body "all" spec;
25566
33f740c5e022 tuned further
haftmann
parents: 25562
diff changeset
    41
    val (vs', _) = Name.variants vs (Name.make_context (fold_aterms
33f740c5e022 tuned further
haftmann
parents: 25562
diff changeset
    42
      (fn Free (v, _) => insert (op =) v | _ => I) body []));
33f740c5e022 tuned further
haftmann
parents: 25562
diff changeset
    43
    val eqn = curry subst_bounds (map2 (curry Free) vs' Ts |> rev) body;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    44
    val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eqn)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    45
      handle TERM _ => primrec_error "not a proper equation";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    46
    val (recfun, args) = strip_comb lhs;
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    47
    val fname =
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    48
      (case recfun of
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    49
        Free (v, _) =>
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    50
          if is_fixed v then v
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    51
          else primrec_error "illegal head of function equation"
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    52
      | _ => primrec_error "illegal head of function equation");
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    53
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    54
    val (ls', rest)  = take_prefix is_Free args;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    55
    val (middle, rs') = take_suffix is_Free rest;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    56
    val rpos = length ls';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    57
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    58
    val (constr, cargs') =
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    59
      if null middle then primrec_error "constructor missing"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    60
      else strip_comb (hd middle);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    61
    val (cname, T) = dest_Const constr
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    62
      handle TERM _ => primrec_error "ill-formed constructor";
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    63
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    64
      primrec_error "cannot determine datatype associated with function"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    65
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    66
    val (ls, cargs, rs) =
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    67
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    68
      handle TERM _ => primrec_error "illegal argument in pattern";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    69
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    70
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    71
    fun check_vars _ [] = ()
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    72
      | check_vars s vars = primrec_error (s ^ commas_quote (map fst vars)) eqn;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    73
  in
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    74
    if length middle > 1 then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    75
      primrec_error "more than one non-variable in pattern"
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    76
    else
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18928
diff changeset
    77
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    78
      check_vars "extra variables on rhs: "
39814
63a1eb22d7d3 eliminated ancient OldTerm.term_frees;
wenzelm
parents: 39813
diff changeset
    79
        (Term.add_frees rhs [] |> subtract (op =) lfrees
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
    80
          |> filter_out (is_fixed o fst));
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    81
      (case AList.lookup (op =) rec_fns fname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    82
        NONE =>
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    83
          (fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eqn))])) :: rec_fns
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    84
      | SOME (_, rpos', eqns) =>
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
    85
          if AList.defined (op =) eqns cname then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    86
            primrec_error "constructor already occurred as pattern"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    87
          else if rpos <> rpos' then
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    88
            primrec_error "position of recursive argument inconsistent"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    89
          else
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    90
            AList.update (op =)
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    91
              (fname, (tname, rpos, (cname, (ls, cargs, rs, rhs, eqn)) :: eqns))
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
    92
              rec_fns))
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    93
  end handle PrimrecError (msg, NONE) => primrec_error_eqn msg spec;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    94
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    95
fun process_fun descr eqns (i, fname) (fnames, fnss) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    96
  let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
    97
    val (_, (tname, _, constrs)) = nth descr i;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    98
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    99
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   100
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   101
    fun subst [] t fs = (t, fs)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   102
      | subst subs (Abs (a, T, t)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   103
          fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   104
          |> subst subs t
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   105
          |-> (fn t' => pair (Abs (a, T, t')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   106
      | subst subs (t as (_ $ _)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   107
          let
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   108
            val (f, ts) = strip_comb t;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   109
          in
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   110
            if is_Free f
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   111
              andalso member (fn ((v, _), (w, _)) => v = w) eqns (dest_Free f) then
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   112
              let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   113
                val (fname', _) = dest_Free f;
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   114
                val (_, rpos, _) = the (AList.lookup (op =) eqns fname');
27301
bf7d82193a2e fixed bind error for malformed primrec specifications
haftmann
parents: 26679
diff changeset
   115
                val (ls, rs) = chop rpos ts
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   116
                val (x', rs') =
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   117
                  (case rs of
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   118
                    x' :: rs => (x', rs)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   119
                  | [] => primrec_error ("not enough arguments in recursive application\n" ^
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   120
                      "of function " ^ quote fname' ^ " on rhs"));
27301
bf7d82193a2e fixed bind error for malformed primrec specifications
haftmann
parents: 26679
diff changeset
   121
                val (x, xs) = strip_comb x';
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   122
              in
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   123
                (case AList.lookup (op =) subs x of
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   124
                  NONE =>
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   125
                    fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   126
                    |> fold_map (subst subs) ts
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   127
                    |-> (fn ts' => pair (list_comb (f, ts')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   128
                | SOME (i', y) =>
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   129
                    fs
27301
bf7d82193a2e fixed bind error for malformed primrec specifications
haftmann
parents: 26679
diff changeset
   130
                    |> fold_map (subst subs) (xs @ ls @ rs')
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   131
                    ||> process_fun descr eqns (i', fname')
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   132
                    |-> (fn ts' => pair (list_comb (y, ts'))))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   133
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   134
            else
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   135
              fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   136
              |> fold_map (subst subs) (f :: ts)
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   137
              |-> (fn f' :: ts' => pair (list_comb (f', ts')))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   138
          end
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   139
      | subst _ t fs = (t, fs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   140
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   141
    (* translate rec equations into function arguments suitable for rec comb *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   142
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   143
    fun trans eqns (cname, cargs) (fnames', fnss', fns) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   144
      (case AList.lookup (op =) eqns cname of
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   145
        NONE => (warning ("No equation for constructor " ^ quote cname ^
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   146
          "\nin definition of function " ^ quote fname);
39813
d466bd29c887 more antiquotations;
wenzelm
parents: 39812
diff changeset
   147
            (fnames', fnss', (Const (@{const_name undefined}, dummyT)) :: fns))
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   148
      | SOME (ls, cargs', rs, rhs, eq) =>
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   149
          let
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   150
            val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   151
            val rargs = map fst recs;
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   152
            val subs = map (rpair dummyT o fst)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   153
              (rev (Term.rename_wrt_term rhs rargs));
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   154
            val (rhs', (fnames'', fnss'')) = subst (map2 (fn (x, y) => fn z =>
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   155
              (Free x, (body_index y, Free z))) recs subs) rhs (fnames', fnss')
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   156
                handle PrimrecError (s, NONE) => primrec_error_eqn s eq
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   157
          in (fnames'', fnss'',
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   158
              (list_abs_free (cargs' @ subs @ ls @ rs, rhs')) :: fns)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   159
          end)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   160
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   161
  in
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   162
    (case AList.lookup (op =) fnames i of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   163
      NONE =>
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   164
        if exists (fn (_, v) => fname = v) fnames then
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   165
          primrec_error ("inconsistent functions for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   166
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   167
          let
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   168
            val (_, _, eqns) = the (AList.lookup (op =) eqns fname);
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   169
            val (fnames', fnss', fns) = fold_rev (trans eqns) constrs
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   170
              ((i, fname) :: fnames, fnss, [])
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   171
          in
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   172
            (fnames', (i, (fname, #1 (snd (hd eqns)), fns)) :: fnss')
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   173
          end
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   174
    | SOME fname' =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   175
        if fname = fname' then (fnames, fnss)
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   176
        else primrec_error ("inconsistent functions for datatype " ^ quote tname))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   177
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   178
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   179
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   180
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   181
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   182
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   183
  (case AList.lookup (op =) fns i of
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   184
    NONE =>
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   185
      let
39813
d466bd29c887 more antiquotations;
wenzelm
parents: 39812
diff changeset
   186
        val dummy_fns = map (fn (_, cargs) => Const (@{const_name undefined},
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   187
          replicate (length cargs + length (filter is_rec_type cargs))
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   188
            dummyT ---> HOLogic.unitT)) constrs;
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   189
        val _ = warning ("No function definition for datatype " ^ quote tname)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   190
      in
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   191
        (dummy_fns @ fs, defs)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   192
      end
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   193
  | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs));
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   194
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   195
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   196
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   197
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   198
fun make_def ctxt fixes fs (fname, ls, rec_name, tname) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   199
  let
30451
11e5e8bb28f9 corrected type inference of primitive definitions
haftmann
parents: 30364
diff changeset
   200
    val SOME (var, varT) = get_first (fn ((b, T), mx) =>
11e5e8bb28f9 corrected type inference of primitive definitions
haftmann
parents: 30364
diff changeset
   201
      if Binding.name_of b = fname then SOME ((b, mx), T) else NONE) fixes;
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   202
    val def_name = Thm.def_name (Long_Name.base_name fname);
30451
11e5e8bb28f9 corrected type inference of primitive definitions
haftmann
parents: 30364
diff changeset
   203
    val raw_rhs = fold_rev (fn T => fn t => Abs ("", T, t)) (map snd ls @ [dummyT])
11e5e8bb28f9 corrected type inference of primitive definitions
haftmann
parents: 30364
diff changeset
   204
      (list_comb (Const (rec_name, dummyT), fs @ map Bound (0 :: (length ls downto 1))))
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 38388
diff changeset
   205
    val rhs = singleton (Syntax.check_terms ctxt) (Type.constraint varT raw_rhs);
33755
6dc1b67f2127 concealing internal definitions of primrec specifications
bulwahn
parents: 33726
diff changeset
   206
  in (var, ((Binding.conceal (Binding.name def_name), []), rhs)) end;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   207
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   208
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   209
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   210
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31723
diff changeset
   211
fun find_dts (dt_info : info Symtab.table) _ [] = []
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   212
  | find_dts dt_info tnames' (tname :: tnames) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17314
diff changeset
   213
      (case Symtab.lookup dt_info tname of
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   214
        NONE => primrec_error (quote tname ^ " is not a datatype")
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   215
      | SOME dt =>
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   216
          if subset (op =) (tnames', map (#1 o snd) (#descr dt)) then
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   217
            (tname, dt) :: (find_dts dt_info tnames' tnames)
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   218
          else find_dts dt_info tnames' tnames);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   219
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   220
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   221
(* distill primitive definition(s) from primrec specification *)
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   222
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   223
fun distill lthy fixes eqs = 
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   224
  let
25559
f14305fb698c authentic primrec
haftmann
parents: 25557
diff changeset
   225
    val eqns = fold_rev (process_eqn (fn v => Variable.is_fixed lthy v
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   226
      orelse exists (fn ((w, _), _) => v = Binding.name_of w) fixes)) eqs [];
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   227
    val tnames = distinct (op =) (map (#1 o snd) eqns);
33963
977b94b64905 renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33766
diff changeset
   228
    val dts = find_dts (Datatype_Data.get_all (ProofContext.theory_of lthy)) tnames tnames;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   229
    val main_fns = map (fn (tname, {index, ...}) =>
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   230
      (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
   231
    val {descr, rec_names, rec_rewrites, ...} =
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   232
      if null dts then primrec_error
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   233
        ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
18362
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   234
      else snd (hd dts);
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   235
    val (fnames, fnss) = fold_rev (process_fun descr eqns) main_fns ([], []);
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   236
    val (fs, raw_defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   237
    val defs = map (make_def lthy fixes fs) raw_defs;
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   238
    val names = map snd fnames;
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   239
    val names_eqns = map fst eqns;
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   240
    val _ =
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   241
      if eq_set (op =) (names, names_eqns) then ()
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   242
      else primrec_error ("functions " ^ commas_quote names_eqns ^
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   243
        "\nare not mutually recursive");
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   244
    val rec_rewrites' = map mk_meta_eq rec_rewrites;
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   245
    val prefix = space_implode "_" (map (Long_Name.base_name o #1) raw_defs);
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   246
    fun prove lthy defs =
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   247
      let
31786
a5ad48ae17e5 corrected handling of free variables in arguments
haftmann
parents: 31784
diff changeset
   248
        val frees = (fold o Term.fold_aterms) (fn Free (x, _) =>
a5ad48ae17e5 corrected handling of free variables in arguments
haftmann
parents: 31784
diff changeset
   249
          if Variable.is_fixed lthy x then I else insert (op =) x | _ => I) eqs [];
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   250
        val rewrites = rec_rewrites' @ map (snd o snd) defs;
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   251
        fun tac _ = EVERY [rewrite_goals_tac rewrites, rtac refl 1];
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   252
      in map (fn eq => Goal.prove lthy frees [] eq tac) eqs end;
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   253
  in ((prefix, (fs, defs)), prove) end
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   254
  handle PrimrecError (msg, some_eqn) =>
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   255
    error ("Primrec definition error:\n" ^ msg ^
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   256
      (case some_eqn of
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   257
        SOME eqn => "\nin\n" ^ quote (Syntax.string_of_term lthy eqn)
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   258
      | NONE => ""));
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   259
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   260
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   261
(* primrec definition *)
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   262
31269
dcbe1f9fe2cd tuned signature of add_primrec_simple
haftmann
parents: 31262
diff changeset
   263
fun add_primrec_simple fixes ts lthy =
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   264
  let
31269
dcbe1f9fe2cd tuned signature of add_primrec_simple
haftmann
parents: 31262
diff changeset
   265
    val ((prefix, (fs, defs)), prove) = distill lthy fixes ts;
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   266
  in
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   267
    lthy
33766
c679f05600cd adapted Local_Theory.define -- eliminated odd thm kind;
wenzelm
parents: 33755
diff changeset
   268
    |> fold_map Local_Theory.define defs
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   269
    |-> (fn defs => `(fn lthy => (prefix, (map fst defs, prove lthy defs))))
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   270
  end;
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   271
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   272
local
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   273
33726
0878aecbf119 eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents: 33671
diff changeset
   274
fun gen_primrec prep_spec raw_fixes raw_spec lthy =
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   275
  let
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   276
    val (fixes, spec) = fst (prep_spec raw_fixes raw_spec lthy);
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   277
    fun attr_bindings prefix = map (fn ((b, attrs), _) =>
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   278
      (Binding.qualify false prefix b, Code.add_default_eqn_attrib :: attrs)) spec;
33552
506f80a9afe8 removed unused Quickcheck_RecFun_Simps;
wenzelm
parents: 33317
diff changeset
   279
    fun simp_attr_binding prefix =
506f80a9afe8 removed unused Quickcheck_RecFun_Simps;
wenzelm
parents: 33317
diff changeset
   280
      (Binding.qualify true prefix (Binding.name "simps"),
506f80a9afe8 removed unused Quickcheck_RecFun_Simps;
wenzelm
parents: 33317
diff changeset
   281
        map (Attrib.internal o K) [Simplifier.simp_add, Nitpick_Simps.add]);
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   282
  in
25557
ea6b11021e79 added new primrec package
haftmann
parents: 24867
diff changeset
   283
    lthy
31269
dcbe1f9fe2cd tuned signature of add_primrec_simple
haftmann
parents: 31262
diff changeset
   284
    |> add_primrec_simple fixes (map snd spec)
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35166
diff changeset
   285
    |-> (fn (prefix, (ts, simps)) =>
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35166
diff changeset
   286
      Spec_Rules.add Spec_Rules.Equational (ts, simps)
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35166
diff changeset
   287
      #> fold_map Local_Theory.note (attr_bindings prefix ~~ map single simps)
34952
bd7e347eb768 added registration of equational theorems from prim_rec and rec_def to Spec_Rules
bulwahn
parents: 33968
diff changeset
   288
      #-> (fn simps' => Local_Theory.note (simp_attr_binding prefix, maps snd simps')
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35166
diff changeset
   289
      #>> (fn (_, simps'') => (ts, simps''))))
31262
580510315dda add_primrec_simple
haftmann
parents: 31177
diff changeset
   290
  end;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   291
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   292
in
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   293
33726
0878aecbf119 eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents: 33671
diff changeset
   294
val add_primrec = gen_primrec Specification.check_spec;
0878aecbf119 eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents: 33671
diff changeset
   295
val add_primrec_cmd = gen_primrec Specification.read_spec;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   296
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   297
end;
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   298
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   299
fun add_primrec_global fixes specs thy =
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   300
  let
38388
94d5624dd1f7 Named_Target.theory_init
haftmann
parents: 38350
diff changeset
   301
    val lthy = Named_Target.theory_init thy;
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   302
    val ((ts, simps), lthy') = add_primrec fixes specs lthy;
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   303
    val simps' = ProofContext.export lthy' lthy simps;
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   304
  in ((ts, simps'), Local_Theory.exit_global lthy') end;
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   305
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   306
fun add_primrec_overloaded ops fixes specs thy =
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   307
  let
38342
09d4a04d5c2e moved overloading target formally to overloading.ML
haftmann
parents: 37145
diff changeset
   308
    val lthy = Overloading.overloading ops thy;
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   309
    val ((ts, simps), lthy') = add_primrec fixes specs lthy;
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   310
    val simps' = ProofContext.export lthy' lthy simps;
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34952
diff changeset
   311
  in ((ts, simps'), Local_Theory.exit_global lthy') end;
26679
447f4872b7ee Added add_primrec_global and add_primrec_overloaded functions (thanks to Markus).
berghofe
parents: 26556
diff changeset
   312
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   313
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   314
(* outer syntax *)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   315
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   316
val _ =
39812
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   317
  Outer_Syntax.local_theory "primrec" "define primitive recursive functions on datatypes"
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   318
    Keyword.thy_decl
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   319
    (Parse.fixes -- Parse_Spec.where_alt_specs
cdee5ca9ba9e simplified outer syntax setup;
wenzelm
parents: 39776
diff changeset
   320
      >> (fn (fixes, specs) => add_primrec_cmd fixes specs #> snd));
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   321
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   322
end;