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