src/HOL/Tools/primrec_package.ML
author berghofe
Wed, 11 Jul 2007 11:44:51 +0200
changeset 23765 997e5fe47532
parent 22728 ecbbdf50df2f
child 24493 d4380e9b287b
permissions -rw-r--r--
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
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$
11539
0f17da240450 tuned headers;
wenzelm
parents: 10525
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen and Norbert Voelker, FernUni Hagen
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
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    10
  val quiet_mode: bool ref
23765
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    11
  val unify_consts: theory -> term list -> term list -> term list * term list
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15574
diff changeset
    12
  val add_primrec: string -> ((bstring * string) * Attrib.src list) list
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    13
    -> theory -> thm list * theory
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
    14
  val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    15
    -> theory -> thm list * theory
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18688
diff changeset
    16
  val add_primrec_i: string -> ((bstring * term) * attribute list) list
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    17
    -> theory -> thm list * theory
19636
50515882049e added add_primrec_unchecked_i;
wenzelm
parents: 19046
diff changeset
    18
  val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    19
    -> theory -> thm list * theory
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    20
  (* FIXME !? *)
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
    21
  val gen_primrec: ((bstring * attribute list) * thm list -> theory -> (bstring * thm list) * theory)
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
    22
    -> ((bstring * attribute list) * term -> theory -> (bstring * thm) * theory)
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
    23
    -> string -> ((bstring * attribute list) * term) list
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
    24
    -> theory -> thm list * theory;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    25
end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    26
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    27
structure PrimrecPackage : PRIMREC_PACKAGE =
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    28
struct
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    29
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    30
open DatatypeAux;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    31
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    32
exception RecError of string;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    33
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    34
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    35
fun primrec_eq_err thy s eq =
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    36
  primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term thy eq));
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    37
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    38
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    39
(* messages *)
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    40
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    41
val quiet_mode = ref false;
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
    42
fun message s = if ! quiet_mode then () else writeln s;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    43
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
    44
23765
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    45
(*the following code ensures that each recursive set always has the
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    46
  same type in all introduction rules*)
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    47
fun unify_consts thy cs intr_ts =
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    48
  (let
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    49
    val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I);
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    50
    fun varify (t, (i, ts)) =
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    51
      let val t' = map_types (Logic.incr_tvar (i + 1)) (snd (Type.varify [] t))
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    52
      in (maxidx_of_term t', t'::ts) end;
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    53
    val (i, cs') = foldr varify (~1, []) cs;
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    54
    val (i', intr_ts') = foldr varify (i, []) intr_ts;
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    55
    val rec_consts = fold add_term_consts_2 cs' [];
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    56
    val intr_consts = fold add_term_consts_2 intr_ts' [];
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    57
    fun unify (cname, cT) =
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    58
      let val consts = map snd (filter (fn (c, _) => c = cname) intr_consts)
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    59
      in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    60
    val (env, _) = fold unify rec_consts (Vartab.empty, i');
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    61
    val subst = Type.freeze o map_types (Envir.norm_type env)
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    62
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    63
  in (map subst cs', map subst intr_ts')
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    64
  end) handle Type.TUNIFY =>
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    65
    (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    66
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
    67
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    68
(* preprocessing of equations *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    69
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    70
fun process_eqn thy eq rec_fns =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    71
  let
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    72
    val (lhs, rhs) =
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    73
      if null (term_vars eq) then
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    74
        HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    75
        handle TERM _ => raise RecError "not a proper equation"
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    76
      else raise RecError "illegal schematic variable(s)";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    77
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    78
    val (recfun, args) = strip_comb lhs;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
    79
    val fnameT = dest_Const recfun handle TERM _ =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    80
      raise RecError "function is not declared as constant in theory";
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    81
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    82
    val (ls', rest)  = take_prefix is_Free args;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    83
    val (middle, rs') = take_suffix is_Free rest;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    84
    val rpos = length ls';
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    85
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    86
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    87
      else strip_comb (hd middle);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    88
    val (cname, T) = dest_Const constr
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    89
      handle TERM _ => raise RecError "ill-formed constructor";
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    90
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    91
      raise RecError "cannot determine datatype associated with function"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    92
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    93
    val (ls, cargs, rs) =
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
    94
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
    95
      handle TERM _ => raise RecError "illegal argument in pattern";
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    96
    val lfrees = ls @ rs @ cargs;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
    97
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    98
    fun check_vars _ [] = ()
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
    99
      | check_vars s vars = raise RecError (s ^ commas_quote (map fst vars))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   100
  in
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   101
    if length middle > 1 then
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   102
      raise RecError "more than one non-variable in pattern"
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
   103
    else
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18928
diff changeset
   104
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
12474
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
   105
      check_vars "extra variables on rhs: "
0404454d97df Improved error messages.
berghofe
parents: 12448
diff changeset
   106
        (map dest_Free (term_frees rhs) \\ lfrees);
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   107
      case AList.lookup (op =) rec_fns fnameT of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   108
        NONE =>
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   109
          (fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   110
      | SOME (_, rpos', eqns) =>
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   111
          if AList.defined (op =) eqns cname then
6037
b0895662fba4 better indentation
paulson
parents: 6032
diff changeset
   112
            raise RecError "constructor already occurred as pattern"
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   113
          else if rpos <> rpos' then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   114
            raise RecError "position of recursive argument inconsistent"
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   115
          else
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 17261
diff changeset
   116
            AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns))
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 17261
diff changeset
   117
              rec_fns)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   118
  end
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   119
  handle RecError s => primrec_eq_err thy s eq;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   120
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   121
fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   122
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   123
    val (_, (tname, _, constrs)) = List.nth (descr, i);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   124
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   125
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   126
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   127
    fun subst [] t fs = (t, fs)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   128
      | subst subs (Abs (a, T, t)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   129
          fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   130
          |> subst subs t
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   131
          |-> (fn t' => pair (Abs (a, T, t')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   132
      | subst subs (t as (_ $ _)) fs =
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   133
          let
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   134
            val (f, ts) = strip_comb t;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   135
          in
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   136
            if is_Const f andalso dest_Const f mem map fst rec_eqns then
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   137
              let
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   138
                val fnameT' as (fname', _) = dest_Const f;
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   139
                val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT');
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   140
                val ls = Library.take (rpos, ts);
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   141
                val rest = Library.drop (rpos, ts);
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   142
                val (x', rs) = (hd rest, tl rest)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   143
                  handle Empty => raise RecError ("not enough arguments\
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   144
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   145
                val (x, xs) = strip_comb x'
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   146
              in case AList.lookup (op =) subs x
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   147
               of NONE =>
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   148
                    fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   149
                    |> fold_map (subst subs) ts
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   150
                    |-> (fn ts' => pair (list_comb (f, ts')))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   151
                | SOME (i', y) =>
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   152
                    fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   153
                    |> fold_map (subst subs) (xs @ ls @ rs)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   154
                    ||> process_fun thy descr rec_eqns (i', fnameT')
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   155
                    |-> (fn ts' => pair (list_comb (y, ts')))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   156
              end
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   157
            else
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   158
              fs
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   159
              |> fold_map (subst subs) (f :: ts)
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   160
              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   161
          end
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   162
      | subst _ t fs = (t, fs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   163
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   164
    (* translate rec equations into function arguments suitable for rec comb *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   165
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   166
    fun trans eqns (cname, cargs) (fnameTs', fnss', fns) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   167
      (case AList.lookup (op =) eqns cname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   168
          NONE => (warning ("No equation for constructor " ^ quote cname ^
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   169
            "\nin definition of function " ^ quote fname);
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22101
diff changeset
   170
              (fnameTs', fnss', (Const ("HOL.undefined", dummyT))::fns))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   171
        | SOME (ls, cargs', rs, rhs, eq) =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   172
            let
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   173
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   174
              val rargs = map fst recs;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   175
              val subs = map (rpair dummyT o fst)
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   176
                (rev (rename_wrt_term rhs rargs));
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   177
              val (rhs', (fnameTs'', fnss'')) =
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   178
                  (subst (map (fn ((x, y), z) =>
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   179
                               (Free x, (body_index y, Free z)))
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   180
                          (recs ~~ subs)) rhs (fnameTs', fnss'))
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   181
                  handle RecError s => primrec_eq_err thy s eq
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   182
            in (fnameTs'', fnss'',
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   183
                (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   184
            end)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   185
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   186
  in (case AList.lookup (op =) fnameTs i of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   187
      NONE =>
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   188
        if exists (equal fnameT o snd) fnameTs then
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   189
          raise RecError ("inconsistent functions for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   190
        else
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   191
          let
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   192
            val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT);
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   193
            val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   194
              ((i, fnameT)::fnameTs, fnss, [])
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   195
          in
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   196
            (fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   197
          end
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   198
    | SOME fnameT' =>
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   199
        if fnameT = fnameT' then (fnameTs, fnss)
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   200
        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   201
  end;
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   202
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   203
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   204
(* prepare functions needed for definitions *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   205
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   206
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   207
  case AList.lookup (op =) fns i of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   208
     NONE =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   209
       let
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22101
diff changeset
   210
         val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   211
           replicate ((length cargs) + (length (List.filter is_rec_type cargs)))
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   212
             dummyT ---> HOLogic.unitT)) constrs;
6427
fd36b2e7d80e tuned messages;
wenzelm
parents: 6425
diff changeset
   213
         val _ = warning ("No function definition for datatype " ^ quote tname)
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   214
       in
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   215
         (dummy_fns @ fs, defs)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   216
       end
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   217
   | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   218
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   219
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   220
(* make definition *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   221
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   222
fun make_def thy fs (fname, ls, rec_name, tname) =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   223
  let
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   224
    val rhs = fold_rev (fn T => fn t => Abs ("", T, t))
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   225
                    ((map snd ls) @ [dummyT])
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   226
                    (list_comb (Const (rec_name, dummyT),
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   227
                                fs @ map Bound (0 ::(length ls downto 1))))
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   228
    val def_name = Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def";
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   229
    val def_prop =
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   230
      singleton (ProofContext.infer_types (ProofContext.init thy))
22728
ecbbdf50df2f simplified ProofContext.infer_types(_pats);
wenzelm
parents: 22692
diff changeset
   231
        (Logic.mk_equals (Const (fname, dummyT), rhs));
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   232
  in (def_name, def_prop) end;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   233
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   234
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   235
(* find datatypes which contain all datatypes in tnames' *)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   236
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   237
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   238
  | find_dts dt_info tnames' (tname::tnames) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17314
diff changeset
   239
      (case Symtab.lookup dt_info tname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   240
          NONE => primrec_err (quote tname ^ " is not a datatype")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   241
        | SOME dt =>
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   242
            if tnames' subset (map (#1 o snd) (#descr dt)) then
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   243
              (tname, dt)::(find_dts dt_info tnames' tnames)
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   244
            else find_dts dt_info tnames' tnames);
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   245
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   246
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   247
  let
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   248
    fun constrs_of (_, (_, _, cs)) =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   249
      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17057
diff changeset
   250
    val params_of = these o AList.lookup (op =) (List.concat (map constrs_of rec_eqns));
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   251
  in
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   252
    induction
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   253
    |> RuleCases.rename_params (map params_of (List.concat (map (map #1 o #3 o #2) descr)))
10525
3e21ab3e5114 RuleCases.save;
wenzelm
parents: 9575
diff changeset
   254
    |> RuleCases.save induction
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   255
  end;
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   256
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   257
local
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   258
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   259
fun gen_primrec_i note def alt_name eqns_atts thy =
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   260
  let
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   261
    val (eqns, atts) = split_list eqns_atts;
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   262
    val dt_info = DatatypePackage.get_datatypes thy;
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   263
    val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ;
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18964
diff changeset
   264
    val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   265
    val dts = find_dts dt_info tnames tnames;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   266
    val main_fns =
18362
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   267
      map (fn (tname, {index, ...}) =>
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   268
        (index,
18362
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   269
          (fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns))
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   270
      dts;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   271
    val {descr, rec_names, rec_rewrites, ...} =
18362
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   272
      if null dts then
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   273
        primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
e8b7e0a22727 removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents: 18358
diff changeset
   274
      else snd (hd dts);
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   275
    val (fnameTs, fnss) =
21064
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   276
      fold_rev (process_fun thy descr rec_eqns) main_fns ([], []);
9684dd7c81b5 fold cleanup
haftmann
parents: 21022
diff changeset
   277
    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   278
    val defs' = map (make_def thy fs) defs;
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   279
    val nameTs1 = map snd fnameTs;
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   280
    val nameTs2 = map fst rec_eqns;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   281
    val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then ()
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   282
            else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   283
              "\nare not mutually recursive");
8432
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   284
    val primrec_name =
daf6b3961ed4 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 7904
diff changeset
   285
      if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   286
    val (defs_thms', thy') =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   287
      thy
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   288
      |> Theory.add_path primrec_name
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   289
      |> fold_map def (map (fn (name, t) => ((name, []), t)) defs');
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   290
    val rewrites = (map mk_meta_eq rec_rewrites) @ map snd defs_thms';
16765
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   291
    val _ = message ("Proving equations for primrec function(s) " ^
b8b1f310877f Some changes to allow mutually recursive, overloaded functions with same name.
berghofe
parents: 16646
diff changeset
   292
      commas_quote (map fst nameTs1) ^ " ...");
20046
9c8909fc5865 Goal.prove_global;
wenzelm
parents: 19688
diff changeset
   293
    val simps = map (fn (_, t) => Goal.prove_global thy' [] [] t
9c8909fc5865 Goal.prove_global;
wenzelm
parents: 19688
diff changeset
   294
        (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   295
    val (simps', thy'') =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   296
      thy'
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   297
      |> fold_map note ((map fst eqns ~~ atts) ~~ map single simps);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   298
    val simps'' = maps snd simps';
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   299
  in
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   300
    thy''
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   301
    |> note (("simps", [Simplifier.simp_add, RecfunCodegen.add NONE]), simps'')
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   302
    |> snd
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   303
    |> note (("induct", []), [prepare_induct (#2 (hd dts)) rec_eqns])
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   304
    |> snd
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   305
    |> Theory.parent_path
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   306
    |> pair simps''
8480
50266d517b0c Added new theory data slot for primrec equations.
berghofe
parents: 8432
diff changeset
   307
  end;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   308
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   309
fun gen_primrec note def alt_name eqns thy =
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   310
  let
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   311
    val ((names, strings), srcss) = apfst split_list (split_list eqns);
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18688
diff changeset
   312
    val atts = map (map (Attrib.attribute thy)) srcss;
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   313
    val eqn_ts = map (fn s => term_of (Thm.read_cterm thy (s, propT))
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18377
diff changeset
   314
      handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings;
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   315
    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq)))
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   316
      handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts;
23765
997e5fe47532 Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents: 22728
diff changeset
   317
    val (_, eqn_ts') = unify_consts thy rec_ts eqn_ts
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   318
  in
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   319
    gen_primrec_i note def alt_name (names ~~ eqn_ts' ~~ atts) thy
7016
df54b5365477 - Now also supports arbitrarily branching datatypes.
berghofe
parents: 6729
diff changeset
   320
  end;
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   321
20841
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   322
fun thy_note ((name, atts), thms) =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   323
  PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   324
fun thy_def false ((name, atts), t) =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   325
      PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm))
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   326
  | thy_def true ((name, atts), t) =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   327
      PureThy.add_defs_unchecked_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   328
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   329
in
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   330
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   331
val add_primrec = gen_primrec thy_note (thy_def false);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   332
val add_primrec_unchecked = gen_primrec thy_note (thy_def true);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   333
val add_primrec_i = gen_primrec_i thy_note (thy_def false);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   334
val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   335
fun gen_primrec note def alt_name specs =
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   336
  gen_primrec_i note def alt_name (map (fn ((name, t), atts) => ((name, atts), t)) specs);
d4f94d2a3414 added gen_primrec
haftmann
parents: 20176
diff changeset
   337
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22480
diff changeset
   338
end;
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   339
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   340
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   341
(* outer syntax *)
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   342
17057
0934ac31985f OuterKeyword;
wenzelm
parents: 16765
diff changeset
   343
local structure P = OuterParse and K = OuterKeyword in
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   344
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   345
val opt_unchecked_name =
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   346
  Scan.optional (P.$$$ "(" |-- P.!!!
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   347
    (((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" ||
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   348
      P.name >> pair false) --| P.$$$ ")")) (false, "");
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   349
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   350
val primrec_decl =
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21064
diff changeset
   351
  opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop);
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   352
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   353
val primrecP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6430
diff changeset
   354
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   355
    (primrec_decl >> (fn ((unchecked, alt_name), eqns) =>
20176
36737fb58614 exported equation transformator
haftmann
parents: 20046
diff changeset
   356
      Toplevel.theory (snd o
19688
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   357
        (if unchecked then add_primrec_unchecked else add_primrec) alt_name
877b763ded7e added syntax for 'unchecked';
wenzelm
parents: 19636
diff changeset
   358
          (map P.triple_swap eqns))));
6359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   359
6fdb0badc6f4 add_primrec(_i): attributes;
wenzelm
parents: 6092
diff changeset
   360
val _ = OuterSyntax.add_parsers [primrecP];
5178
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   361
9337b230ff15 New primrec function definition package
berghofe
parents:
diff changeset
   362
end;
6384
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   363
eed1273c9146 local open OuterParse;
wenzelm
parents: 6359
diff changeset
   364
end;