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