src/HOL/Tools/old_primrec_package.ML
author haftmann
Thu Dec 06 15:10:09 2007 +0100 (2007-12-06)
changeset 25557 ea6b11021e79
child 25561 d955e1d01e6a
permissions -rw-r--r--
added new primrec package
haftmann@25557
     1
(*  Title:      HOL/Tools/primrec_package.ML
haftmann@25557
     2
    ID:         $Id$
haftmann@25557
     3
    Author:     Stefan Berghofer, TU Muenchen and Norbert Voelker, FernUni Hagen
haftmann@25557
     4
haftmann@25557
     5
Package for defining functions on datatypes by primitive recursion.
haftmann@25557
     6
*)
haftmann@25557
     7
haftmann@25557
     8
signature OLD_PRIMREC_PACKAGE =
haftmann@25557
     9
sig
haftmann@25557
    10
  val quiet_mode: bool ref
haftmann@25557
    11
  val unify_consts: theory -> term list -> term list -> term list * term list
haftmann@25557
    12
  val add_primrec: string -> ((bstring * string) * Attrib.src list) list
haftmann@25557
    13
    -> theory -> thm list * theory
haftmann@25557
    14
  val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list
haftmann@25557
    15
    -> theory -> thm list * theory
haftmann@25557
    16
  val add_primrec_i: string -> ((bstring * term) * attribute list) list
haftmann@25557
    17
    -> theory -> thm list * theory
haftmann@25557
    18
  val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list
haftmann@25557
    19
    -> theory -> thm list * theory
haftmann@25557
    20
  (* FIXME !? *)
haftmann@25557
    21
  val gen_primrec: ((bstring * attribute list) * thm list -> theory -> (bstring * thm list) * theory)
haftmann@25557
    22
    -> ((bstring * attribute list) * term -> theory -> (bstring * thm) * theory)
haftmann@25557
    23
    -> string -> ((bstring * attribute list) * term) list
haftmann@25557
    24
    -> theory -> thm list * theory;
haftmann@25557
    25
end;
haftmann@25557
    26
haftmann@25557
    27
structure OldPrimrecPackage : OLD_PRIMREC_PACKAGE =
haftmann@25557
    28
struct
haftmann@25557
    29
haftmann@25557
    30
open DatatypeAux;
haftmann@25557
    31
haftmann@25557
    32
exception RecError of string;
haftmann@25557
    33
haftmann@25557
    34
fun primrec_err s = error ("Primrec definition error:\n" ^ s);
haftmann@25557
    35
fun primrec_eq_err thy s eq =
haftmann@25557
    36
  primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term thy eq));
haftmann@25557
    37
haftmann@25557
    38
haftmann@25557
    39
(* messages *)
haftmann@25557
    40
haftmann@25557
    41
val quiet_mode = ref false;
haftmann@25557
    42
fun message s = if ! quiet_mode then () else writeln s;
haftmann@25557
    43
haftmann@25557
    44
haftmann@25557
    45
(*the following code ensures that each recursive set always has the
haftmann@25557
    46
  same type in all introduction rules*)
haftmann@25557
    47
fun unify_consts thy cs intr_ts =
haftmann@25557
    48
  (let
haftmann@25557
    49
    val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I);
haftmann@25557
    50
    fun varify (t, (i, ts)) =
haftmann@25557
    51
      let val t' = map_types (Logic.incr_tvar (i + 1)) (snd (Type.varify [] t))
haftmann@25557
    52
      in (maxidx_of_term t', t'::ts) end;
haftmann@25557
    53
    val (i, cs') = foldr varify (~1, []) cs;
haftmann@25557
    54
    val (i', intr_ts') = foldr varify (i, []) intr_ts;
haftmann@25557
    55
    val rec_consts = fold add_term_consts_2 cs' [];
haftmann@25557
    56
    val intr_consts = fold add_term_consts_2 intr_ts' [];
haftmann@25557
    57
    fun unify (cname, cT) =
haftmann@25557
    58
      let val consts = map snd (filter (fn (c, _) => c = cname) intr_consts)
haftmann@25557
    59
      in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
haftmann@25557
    60
    val (env, _) = fold unify rec_consts (Vartab.empty, i');
haftmann@25557
    61
    val subst = Type.freeze o map_types (Envir.norm_type env)
haftmann@25557
    62
haftmann@25557
    63
  in (map subst cs', map subst intr_ts')
haftmann@25557
    64
  end) handle Type.TUNIFY =>
haftmann@25557
    65
    (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
haftmann@25557
    66
haftmann@25557
    67
haftmann@25557
    68
(* preprocessing of equations *)
haftmann@25557
    69
haftmann@25557
    70
fun process_eqn thy eq rec_fns =
haftmann@25557
    71
  let
haftmann@25557
    72
    val (lhs, rhs) =
haftmann@25557
    73
      if null (term_vars eq) then
haftmann@25557
    74
        HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
haftmann@25557
    75
        handle TERM _ => raise RecError "not a proper equation"
haftmann@25557
    76
      else raise RecError "illegal schematic variable(s)";
haftmann@25557
    77
haftmann@25557
    78
    val (recfun, args) = strip_comb lhs;
haftmann@25557
    79
    val fnameT = dest_Const recfun handle TERM _ =>
haftmann@25557
    80
      raise RecError "function is not declared as constant in theory";
haftmann@25557
    81
haftmann@25557
    82
    val (ls', rest)  = take_prefix is_Free args;
haftmann@25557
    83
    val (middle, rs') = take_suffix is_Free rest;
haftmann@25557
    84
    val rpos = length ls';
haftmann@25557
    85
haftmann@25557
    86
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
haftmann@25557
    87
      else strip_comb (hd middle);
haftmann@25557
    88
    val (cname, T) = dest_Const constr
haftmann@25557
    89
      handle TERM _ => raise RecError "ill-formed constructor";
haftmann@25557
    90
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
haftmann@25557
    91
      raise RecError "cannot determine datatype associated with function"
haftmann@25557
    92
haftmann@25557
    93
    val (ls, cargs, rs) =
haftmann@25557
    94
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
haftmann@25557
    95
      handle TERM _ => raise RecError "illegal argument in pattern";
haftmann@25557
    96
    val lfrees = ls @ rs @ cargs;
haftmann@25557
    97
haftmann@25557
    98
    fun check_vars _ [] = ()
haftmann@25557
    99
      | check_vars s vars = raise RecError (s ^ commas_quote (map fst vars))
haftmann@25557
   100
  in
haftmann@25557
   101
    if length middle > 1 then
haftmann@25557
   102
      raise RecError "more than one non-variable in pattern"
haftmann@25557
   103
    else
haftmann@25557
   104
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
haftmann@25557
   105
      check_vars "extra variables on rhs: "
haftmann@25557
   106
        (map dest_Free (term_frees rhs) \\ lfrees);
haftmann@25557
   107
      case AList.lookup (op =) rec_fns fnameT of
haftmann@25557
   108
        NONE =>
haftmann@25557
   109
          (fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
haftmann@25557
   110
      | SOME (_, rpos', eqns) =>
haftmann@25557
   111
          if AList.defined (op =) eqns cname then
haftmann@25557
   112
            raise RecError "constructor already occurred as pattern"
haftmann@25557
   113
          else if rpos <> rpos' then
haftmann@25557
   114
            raise RecError "position of recursive argument inconsistent"
haftmann@25557
   115
          else
haftmann@25557
   116
            AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns))
haftmann@25557
   117
              rec_fns)
haftmann@25557
   118
  end
haftmann@25557
   119
  handle RecError s => primrec_eq_err thy s eq;
haftmann@25557
   120
haftmann@25557
   121
fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) =
haftmann@25557
   122
  let
haftmann@25557
   123
    val (_, (tname, _, constrs)) = List.nth (descr, i);
haftmann@25557
   124
haftmann@25557
   125
    (* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
haftmann@25557
   126
haftmann@25557
   127
    fun subst [] t fs = (t, fs)
haftmann@25557
   128
      | subst subs (Abs (a, T, t)) fs =
haftmann@25557
   129
          fs
haftmann@25557
   130
          |> subst subs t
haftmann@25557
   131
          |-> (fn t' => pair (Abs (a, T, t')))
haftmann@25557
   132
      | subst subs (t as (_ $ _)) fs =
haftmann@25557
   133
          let
haftmann@25557
   134
            val (f, ts) = strip_comb t;
haftmann@25557
   135
          in
haftmann@25557
   136
            if is_Const f andalso dest_Const f mem map fst rec_eqns then
haftmann@25557
   137
              let
haftmann@25557
   138
                val fnameT' as (fname', _) = dest_Const f;
haftmann@25557
   139
                val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT');
haftmann@25557
   140
                val ls = Library.take (rpos, ts);
haftmann@25557
   141
                val rest = Library.drop (rpos, ts);
haftmann@25557
   142
                val (x', rs) = (hd rest, tl rest)
haftmann@25557
   143
                  handle Empty => raise RecError ("not enough arguments\
haftmann@25557
   144
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
haftmann@25557
   145
                val (x, xs) = strip_comb x'
haftmann@25557
   146
              in case AList.lookup (op =) subs x
haftmann@25557
   147
               of NONE =>
haftmann@25557
   148
                    fs
haftmann@25557
   149
                    |> fold_map (subst subs) ts
haftmann@25557
   150
                    |-> (fn ts' => pair (list_comb (f, ts')))
haftmann@25557
   151
                | SOME (i', y) =>
haftmann@25557
   152
                    fs
haftmann@25557
   153
                    |> fold_map (subst subs) (xs @ ls @ rs)
haftmann@25557
   154
                    ||> process_fun thy descr rec_eqns (i', fnameT')
haftmann@25557
   155
                    |-> (fn ts' => pair (list_comb (y, ts')))
haftmann@25557
   156
              end
haftmann@25557
   157
            else
haftmann@25557
   158
              fs
haftmann@25557
   159
              |> fold_map (subst subs) (f :: ts)
haftmann@25557
   160
              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
haftmann@25557
   161
          end
haftmann@25557
   162
      | subst _ t fs = (t, fs);
haftmann@25557
   163
haftmann@25557
   164
    (* translate rec equations into function arguments suitable for rec comb *)
haftmann@25557
   165
haftmann@25557
   166
    fun trans eqns (cname, cargs) (fnameTs', fnss', fns) =
haftmann@25557
   167
      (case AList.lookup (op =) eqns cname of
haftmann@25557
   168
          NONE => (warning ("No equation for constructor " ^ quote cname ^
haftmann@25557
   169
            "\nin definition of function " ^ quote fname);
haftmann@25557
   170
              (fnameTs', fnss', (Const ("HOL.undefined", dummyT))::fns))
haftmann@25557
   171
        | SOME (ls, cargs', rs, rhs, eq) =>
haftmann@25557
   172
            let
haftmann@25557
   173
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
haftmann@25557
   174
              val rargs = map fst recs;
haftmann@25557
   175
              val subs = map (rpair dummyT o fst)
haftmann@25557
   176
                (rev (rename_wrt_term rhs rargs));
haftmann@25557
   177
              val (rhs', (fnameTs'', fnss'')) =
haftmann@25557
   178
                  (subst (map (fn ((x, y), z) =>
haftmann@25557
   179
                               (Free x, (body_index y, Free z)))
haftmann@25557
   180
                          (recs ~~ subs)) rhs (fnameTs', fnss'))
haftmann@25557
   181
                  handle RecError s => primrec_eq_err thy s eq
haftmann@25557
   182
            in (fnameTs'', fnss'',
haftmann@25557
   183
                (list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
haftmann@25557
   184
            end)
haftmann@25557
   185
haftmann@25557
   186
  in (case AList.lookup (op =) fnameTs i of
haftmann@25557
   187
      NONE =>
haftmann@25557
   188
        if exists (equal fnameT o snd) fnameTs then
haftmann@25557
   189
          raise RecError ("inconsistent functions for datatype " ^ quote tname)
haftmann@25557
   190
        else
haftmann@25557
   191
          let
haftmann@25557
   192
            val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT);
haftmann@25557
   193
            val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs
haftmann@25557
   194
              ((i, fnameT)::fnameTs, fnss, [])
haftmann@25557
   195
          in
haftmann@25557
   196
            (fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
haftmann@25557
   197
          end
haftmann@25557
   198
    | SOME fnameT' =>
haftmann@25557
   199
        if fnameT = fnameT' then (fnameTs, fnss)
haftmann@25557
   200
        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
haftmann@25557
   201
  end;
haftmann@25557
   202
haftmann@25557
   203
haftmann@25557
   204
(* prepare functions needed for definitions *)
haftmann@25557
   205
haftmann@25557
   206
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
haftmann@25557
   207
  case AList.lookup (op =) fns i of
haftmann@25557
   208
     NONE =>
haftmann@25557
   209
       let
haftmann@25557
   210
         val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined",
haftmann@25557
   211
           replicate ((length cargs) + (length (List.filter is_rec_type cargs)))
haftmann@25557
   212
             dummyT ---> HOLogic.unitT)) constrs;
haftmann@25557
   213
         val _ = warning ("No function definition for datatype " ^ quote tname)
haftmann@25557
   214
       in
haftmann@25557
   215
         (dummy_fns @ fs, defs)
haftmann@25557
   216
       end
haftmann@25557
   217
   | SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs);
haftmann@25557
   218
haftmann@25557
   219
haftmann@25557
   220
(* make definition *)
haftmann@25557
   221
haftmann@25557
   222
fun make_def thy fs (fname, ls, rec_name, tname) =
haftmann@25557
   223
  let
haftmann@25557
   224
    val rhs = fold_rev (fn T => fn t => Abs ("", T, t))
haftmann@25557
   225
                    ((map snd ls) @ [dummyT])
haftmann@25557
   226
                    (list_comb (Const (rec_name, dummyT),
haftmann@25557
   227
                                fs @ map Bound (0 ::(length ls downto 1))))
haftmann@25557
   228
    val def_name = Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def";
haftmann@25557
   229
    val def_prop =
haftmann@25557
   230
      singleton (Syntax.check_terms (ProofContext.init thy))
haftmann@25557
   231
        (Logic.mk_equals (Const (fname, dummyT), rhs));
haftmann@25557
   232
  in (def_name, def_prop) end;
haftmann@25557
   233
haftmann@25557
   234
haftmann@25557
   235
(* find datatypes which contain all datatypes in tnames' *)
haftmann@25557
   236
haftmann@25557
   237
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
haftmann@25557
   238
  | find_dts dt_info tnames' (tname::tnames) =
haftmann@25557
   239
      (case Symtab.lookup dt_info tname of
haftmann@25557
   240
          NONE => primrec_err (quote tname ^ " is not a datatype")
haftmann@25557
   241
        | SOME dt =>
haftmann@25557
   242
            if tnames' subset (map (#1 o snd) (#descr dt)) then
haftmann@25557
   243
              (tname, dt)::(find_dts dt_info tnames' tnames)
haftmann@25557
   244
            else find_dts dt_info tnames' tnames);
haftmann@25557
   245
haftmann@25557
   246
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns =
haftmann@25557
   247
  let
haftmann@25557
   248
    fun constrs_of (_, (_, _, cs)) =
haftmann@25557
   249
      map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs;
haftmann@25557
   250
    val params_of = these o AList.lookup (op =) (List.concat (map constrs_of rec_eqns));
haftmann@25557
   251
  in
haftmann@25557
   252
    induction
haftmann@25557
   253
    |> RuleCases.rename_params (map params_of (List.concat (map (map #1 o #3 o #2) descr)))
haftmann@25557
   254
    |> RuleCases.save induction
haftmann@25557
   255
  end;
haftmann@25557
   256
haftmann@25557
   257
local
haftmann@25557
   258
haftmann@25557
   259
fun gen_primrec_i note def alt_name eqns_atts thy =
haftmann@25557
   260
  let
haftmann@25557
   261
    val (eqns, atts) = split_list eqns_atts;
haftmann@25557
   262
    val dt_info = DatatypePackage.get_datatypes thy;
haftmann@25557
   263
    val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ;
haftmann@25557
   264
    val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
haftmann@25557
   265
    val dts = find_dts dt_info tnames tnames;
haftmann@25557
   266
    val main_fns =
haftmann@25557
   267
      map (fn (tname, {index, ...}) =>
haftmann@25557
   268
        (index,
haftmann@25557
   269
          (fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns))
haftmann@25557
   270
      dts;
haftmann@25557
   271
    val {descr, rec_names, rec_rewrites, ...} =
haftmann@25557
   272
      if null dts then
haftmann@25557
   273
        primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
haftmann@25557
   274
      else snd (hd dts);
haftmann@25557
   275
    val (fnameTs, fnss) =
haftmann@25557
   276
      fold_rev (process_fun thy descr rec_eqns) main_fns ([], []);
haftmann@25557
   277
    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
haftmann@25557
   278
    val defs' = map (make_def thy fs) defs;
haftmann@25557
   279
    val nameTs1 = map snd fnameTs;
haftmann@25557
   280
    val nameTs2 = map fst rec_eqns;
haftmann@25557
   281
    val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then ()
haftmann@25557
   282
            else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^
haftmann@25557
   283
              "\nare not mutually recursive");
haftmann@25557
   284
    val primrec_name =
haftmann@25557
   285
      if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name;
haftmann@25557
   286
    val (defs_thms', thy') =
haftmann@25557
   287
      thy
haftmann@25557
   288
      |> Sign.add_path primrec_name
haftmann@25557
   289
      |> fold_map def (map (fn (name, t) => ((name, []), t)) defs');
haftmann@25557
   290
    val rewrites = (map mk_meta_eq rec_rewrites) @ map snd defs_thms';
haftmann@25557
   291
    val _ = message ("Proving equations for primrec function(s) " ^
haftmann@25557
   292
      commas_quote (map fst nameTs1) ^ " ...");
haftmann@25557
   293
    val simps = map (fn (_, t) => Goal.prove_global thy' [] [] t
haftmann@25557
   294
        (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
haftmann@25557
   295
    val (simps', thy'') =
haftmann@25557
   296
      thy'
haftmann@25557
   297
      |> fold_map note ((map fst eqns ~~ atts) ~~ map single simps);
haftmann@25557
   298
    val simps'' = maps snd simps';
haftmann@25557
   299
  in
haftmann@25557
   300
    thy''
haftmann@25557
   301
    |> note (("simps", [Simplifier.simp_add, RecfunCodegen.add_default]), simps'')
haftmann@25557
   302
    |> snd
haftmann@25557
   303
    |> note (("induct", []), [prepare_induct (#2 (hd dts)) rec_eqns])
haftmann@25557
   304
    |> snd
haftmann@25557
   305
    |> Sign.parent_path
haftmann@25557
   306
    |> pair simps''
haftmann@25557
   307
  end;
haftmann@25557
   308
haftmann@25557
   309
fun gen_primrec note def alt_name eqns thy =
haftmann@25557
   310
  let
haftmann@25557
   311
    val ((names, strings), srcss) = apfst split_list (split_list eqns);
haftmann@25557
   312
    val atts = map (map (Attrib.attribute thy)) srcss;
haftmann@25557
   313
    val eqn_ts = map (fn s => Syntax.read_prop_global thy s
haftmann@25557
   314
      handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings;
haftmann@25557
   315
    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq)))
haftmann@25557
   316
      handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts;
haftmann@25557
   317
    val (_, eqn_ts') = unify_consts thy rec_ts eqn_ts
haftmann@25557
   318
  in
haftmann@25557
   319
    gen_primrec_i note def alt_name (names ~~ eqn_ts' ~~ atts) thy
haftmann@25557
   320
  end;
haftmann@25557
   321
haftmann@25557
   322
fun thy_note ((name, atts), thms) =
haftmann@25557
   323
  PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
haftmann@25557
   324
fun thy_def false ((name, atts), t) =
haftmann@25557
   325
      PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm))
haftmann@25557
   326
  | thy_def true ((name, atts), t) =
haftmann@25557
   327
      PureThy.add_defs_unchecked_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
haftmann@25557
   328
haftmann@25557
   329
in
haftmann@25557
   330
haftmann@25557
   331
val add_primrec = gen_primrec thy_note (thy_def false);
haftmann@25557
   332
val add_primrec_unchecked = gen_primrec thy_note (thy_def true);
haftmann@25557
   333
val add_primrec_i = gen_primrec_i thy_note (thy_def false);
haftmann@25557
   334
val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true);
haftmann@25557
   335
fun gen_primrec note def alt_name specs =
haftmann@25557
   336
  gen_primrec_i note def alt_name (map (fn ((name, t), atts) => ((name, atts), t)) specs);
haftmann@25557
   337
haftmann@25557
   338
end;
haftmann@25557
   339
haftmann@25557
   340
haftmann@25557
   341
(* see primrecr_package.ML (* outer syntax *)
haftmann@25557
   342
haftmann@25557
   343
local structure P = OuterParse and K = OuterKeyword in
haftmann@25557
   344
haftmann@25557
   345
val opt_unchecked_name =
haftmann@25557
   346
  Scan.optional (P.$$$ "(" |-- P.!!!
haftmann@25557
   347
    (((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" ||
haftmann@25557
   348
      P.name >> pair false) --| P.$$$ ")")) (false, "");
haftmann@25557
   349
haftmann@25557
   350
val primrec_decl =
haftmann@25557
   351
  opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop);
haftmann@25557
   352
haftmann@25557
   353
val _ =
haftmann@25557
   354
  OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl
haftmann@25557
   355
    (primrec_decl >> (fn ((unchecked, alt_name), eqns) =>
haftmann@25557
   356
      Toplevel.theory (snd o
haftmann@25557
   357
        (if unchecked then add_primrec_unchecked else add_primrec) alt_name
haftmann@25557
   358
          (map P.triple_swap eqns))));
haftmann@25557
   359
haftmann@25557
   360
end;*)
haftmann@25557
   361
haftmann@25557
   362
end;