src/HOL/HOLCF/Tools/fixrec.ML
author huffman
Sat Nov 27 16:08:10 2010 -0800 (2010-11-27)
changeset 40774 0437dbc127b3
parent 40327 src/HOLCF/Tools/fixrec.ML@1dfdbd66093a
child 40832 4352ca878c41
permissions -rw-r--r--
moved directory src/HOLCF to src/HOL/HOLCF;
added HOLCF theories to src/HOL/IsaMakefile;
haftmann@31738
     1
(*  Title:      HOLCF/Tools/fixrec.ML
wenzelm@23152
     2
    Author:     Amber Telfer and Brian Huffman
wenzelm@23152
     3
wenzelm@23152
     4
Recursive function definition package for HOLCF.
wenzelm@23152
     5
*)
wenzelm@23152
     6
haftmann@31738
     7
signature FIXREC =
wenzelm@23152
     8
sig
huffman@40041
     9
  val add_fixrec: (binding * typ option * mixfix) list
huffman@40041
    10
    -> (bool * (Attrib.binding * term)) list -> local_theory -> local_theory
huffman@40041
    11
  val add_fixrec_cmd: (binding * string option * mixfix) list
huffman@40041
    12
    -> (bool * (Attrib.binding * string)) list -> local_theory -> local_theory
huffman@30131
    13
  val add_matchers: (string * string) list -> theory -> theory
huffman@33425
    14
  val fixrec_simp_tac: Proof.context -> int -> tactic
huffman@30131
    15
  val setup: theory -> theory
wenzelm@23152
    16
end;
wenzelm@23152
    17
haftmann@31738
    18
structure Fixrec :> FIXREC =
wenzelm@23152
    19
struct
wenzelm@23152
    20
huffman@35527
    21
open HOLCF_Library;
huffman@35527
    22
huffman@35527
    23
infixr 6 ->>;
huffman@35527
    24
infix -->>;
huffman@35527
    25
infix 9 `;
huffman@35527
    26
huffman@31095
    27
val def_cont_fix_eq = @{thm def_cont_fix_eq};
huffman@31095
    28
val def_cont_fix_ind = @{thm def_cont_fix_ind};
wenzelm@23152
    29
wenzelm@23152
    30
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
wenzelm@23152
    31
fun fixrec_eq_err thy s eq =
wenzelm@26939
    32
  fixrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
wenzelm@23152
    33
huffman@30132
    34
(*************************************************************************)
huffman@30132
    35
(***************************** building types ****************************)
huffman@30132
    36
(*************************************************************************)
huffman@30132
    37
huffman@33401
    38
local
huffman@33401
    39
huffman@35525
    40
fun binder_cfun (Type(@{type_name cfun},[T, U])) = T :: binder_cfun U
huffman@33401
    41
  | binder_cfun (Type(@{type_name "fun"},[T, U])) = T :: binder_cfun U
huffman@33401
    42
  | binder_cfun _   =  [];
huffman@33401
    43
huffman@35525
    44
fun body_cfun (Type(@{type_name cfun},[T, U])) = body_cfun U
huffman@33401
    45
  | body_cfun (Type(@{type_name "fun"},[T, U])) = body_cfun U
huffman@33401
    46
  | body_cfun T   =  T;
huffman@33401
    47
huffman@33401
    48
fun strip_cfun T : typ list * typ =
huffman@33401
    49
  (binder_cfun T, body_cfun T);
huffman@33401
    50
huffman@33401
    51
in
huffman@33401
    52
huffman@35527
    53
fun matcherT (T, U) =
huffman@35527
    54
  body_cfun T ->> (binder_cfun T -->> U) ->> U;
huffman@30912
    55
huffman@33401
    56
end
huffman@30132
    57
huffman@30132
    58
(*************************************************************************)
huffman@30132
    59
(***************************** building terms ****************************)
huffman@30132
    60
(*************************************************************************)
wenzelm@23152
    61
wenzelm@23152
    62
val mk_trp = HOLogic.mk_Trueprop;
wenzelm@23152
    63
wenzelm@23152
    64
(* splits a cterm into the right and lefthand sides of equality *)
wenzelm@23152
    65
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
wenzelm@23152
    66
wenzelm@23152
    67
(* similar to Thm.head_of, but for continuous application *)
huffman@40327
    68
fun chead_of (Const(@{const_name Rep_cfun},_)$f$t) = chead_of f
wenzelm@23152
    69
  | chead_of u = u;
wenzelm@23152
    70
huffman@30132
    71
infix 0 ==;  val (op ==) = Logic.mk_equals;
huffman@30132
    72
infix 1 ===; val (op ===) = HOLogic.mk_eq;
huffman@30132
    73
huffman@30132
    74
fun mk_mplus (t, u) =
huffman@30132
    75
  let val mT = Term.fastype_of t
huffman@30132
    76
  in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end;
huffman@30132
    77
huffman@30132
    78
fun mk_run t =
huffman@37097
    79
  let
huffman@37097
    80
    val mT = Term.fastype_of t
huffman@37097
    81
    val T = dest_matchT mT
huffman@37097
    82
    val run = Const(@{const_name Fixrec.run}, mT ->> T)
huffman@37097
    83
  in
huffman@37097
    84
    case t of
huffman@40327
    85
      Const(@{const_name Rep_cfun}, _) $
huffman@37108
    86
        Const(@{const_name Fixrec.succeed}, _) $ u => u
huffman@37097
    87
    | _ => run ` t
huffman@37097
    88
  end;
huffman@30132
    89
huffman@31095
    90
wenzelm@23152
    91
(*************************************************************************)
wenzelm@23152
    92
(************* fixed-point definitions and unfolding theorems ************)
wenzelm@23152
    93
(*************************************************************************)
wenzelm@23152
    94
wenzelm@33519
    95
structure FixrecUnfoldData = Generic_Data
wenzelm@33519
    96
(
huffman@33425
    97
  type T = thm Symtab.table;
huffman@33425
    98
  val empty = Symtab.empty;
huffman@33425
    99
  val extend = I;
wenzelm@33519
   100
  fun merge data : T = Symtab.merge (K true) data;
huffman@33425
   101
);
huffman@33425
   102
huffman@33425
   103
local
huffman@33425
   104
huffman@33425
   105
fun name_of (Const (n, T)) = n
huffman@33425
   106
  | name_of (Free (n, T)) = n
huffman@35770
   107
  | name_of t = raise TERM ("Fixrec.add_unfold: lhs not a constant", [t]);
huffman@33425
   108
huffman@33425
   109
val lhs_name =
huffman@35770
   110
  name_of o head_of o fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
huffman@33425
   111
huffman@33425
   112
in
huffman@33425
   113
wenzelm@33442
   114
val add_unfold : attribute =
huffman@33425
   115
  Thm.declaration_attribute
huffman@33425
   116
    (fn th => FixrecUnfoldData.map (Symtab.insert (K true) (lhs_name th, th)));
huffman@33425
   117
huffman@33425
   118
end
huffman@33425
   119
huffman@30158
   120
fun add_fixdefs
huffman@30158
   121
  (fixes : ((binding * typ) * mixfix) list)
huffman@30158
   122
  (spec : (Attrib.binding * term) list)
huffman@30158
   123
  (lthy : local_theory) =
wenzelm@23152
   124
  let
huffman@31095
   125
    val thy = ProofContext.theory_of lthy;
wenzelm@30223
   126
    val names = map (Binding.name_of o fst o fst) fixes;
huffman@30158
   127
    val all_names = space_implode "_" names;
wenzelm@32149
   128
    val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec);
huffman@31095
   129
    val functional = lambda_tuple lhss (mk_tuple rhss);
huffman@31095
   130
    val fixpoint = mk_fix (mk_cabs functional);
huffman@36996
   131
huffman@31095
   132
    val cont_thm =
huffman@36996
   133
      let
huffman@36996
   134
        val prop = mk_trp (mk_cont functional);
huffman@37096
   135
        fun err _ = error (
huffman@36996
   136
          "Continuity proof failed; please check that cont2cont rules\n" ^
huffman@37097
   137
          "or simp rules are configured for all non-HOLCF constants.\n" ^
huffman@36996
   138
          "The error occurred for the goal statement:\n" ^
huffman@36996
   139
          Syntax.string_of_term lthy prop);
huffman@37084
   140
        val rules = Cont2ContData.get lthy;
huffman@37084
   141
        val fast_tac = SOLVED' (REPEAT_ALL_NEW (match_tac rules));
huffman@37096
   142
        val slow_tac = SOLVED' (simp_tac (simpset_of lthy));
huffman@37096
   143
        val tac = fast_tac 1 ORELSE slow_tac 1 ORELSE err;
huffman@36996
   144
      in
huffman@36996
   145
        Goal.prove lthy [] [] prop (K tac)
huffman@36996
   146
      end;
huffman@31095
   147
huffman@30158
   148
    fun one_def (l as Free(n,_)) r =
wenzelm@30364
   149
          let val b = Long_Name.base_name n
huffman@30158
   150
          in ((Binding.name (b^"_def"), []), r) end
wenzelm@23152
   151
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
wenzelm@23152
   152
    fun defs [] _ = []
wenzelm@23152
   153
      | defs (l::[]) r = [one_def l r]
huffman@31095
   154
      | defs (l::ls) r = one_def l (mk_fst r) :: defs ls (mk_snd r);
huffman@30158
   155
    val fixdefs = defs lhss fixpoint;
huffman@35772
   156
    val (fixdef_thms : (term * (string * thm)) list, lthy) = lthy
wenzelm@33766
   157
      |> fold_map Local_Theory.define (map (apfst fst) fixes ~~ fixdefs);
huffman@31095
   158
    fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2];
huffman@31095
   159
    val tuple_fixdef_thm = foldr1 pair_equalI (map (snd o snd) fixdef_thms);
huffman@31095
   160
    val P = Var (("P", 0), map Term.fastype_of lhss ---> HOLogic.boolT);
huffman@31095
   161
    val predicate = lambda_tuple lhss (list_comb (P, lhss));
huffman@31095
   162
    val tuple_induct_thm = (def_cont_fix_ind OF [tuple_fixdef_thm, cont_thm])
huffman@31095
   163
      |> Drule.instantiate' [] [SOME (Thm.cterm_of thy predicate)]
wenzelm@35624
   164
      |> Local_Defs.unfold lthy @{thms split_paired_all split_conv split_strict};
huffman@31095
   165
    val tuple_unfold_thm = (def_cont_fix_eq OF [tuple_fixdef_thm, cont_thm])
huffman@35772
   166
      |> Local_Defs.unfold lthy @{thms split_conv};
wenzelm@23152
   167
    fun unfolds [] thm = []
huffman@33425
   168
      | unfolds (n::[]) thm = [(n, thm)]
wenzelm@23152
   169
      | unfolds (n::ns) thm = let
huffman@31095
   170
          val thmL = thm RS @{thm Pair_eqD1};
huffman@31095
   171
          val thmR = thm RS @{thm Pair_eqD2};
huffman@33425
   172
        in (n, thmL) :: unfolds ns thmR end;
huffman@31095
   173
    val unfold_thms = unfolds names tuple_unfold_thm;
huffman@33425
   174
    val induct_note : Attrib.binding * Thm.thm list =
huffman@33425
   175
      let
huffman@35769
   176
        val thm_name = Binding.qualify true all_names (Binding.name "induct");
huffman@33425
   177
      in
huffman@33425
   178
        ((thm_name, []), [tuple_induct_thm])
huffman@33425
   179
      end;
huffman@33425
   180
    fun unfold_note (name, thm) : Attrib.binding * Thm.thm list =
huffman@33425
   181
      let
huffman@35769
   182
        val thm_name = Binding.qualify true name (Binding.name "unfold");
huffman@33425
   183
        val src = Attrib.internal (K add_unfold);
huffman@33425
   184
      in
huffman@33425
   185
        ((thm_name, [src]), [thm])
huffman@33425
   186
      end;
huffman@35772
   187
    val (thmss, lthy) = lthy
wenzelm@33671
   188
      |> fold_map Local_Theory.note (induct_note :: map unfold_note unfold_thms);
wenzelm@23152
   189
  in
huffman@35772
   190
    (lthy, names, fixdef_thms, map snd unfold_thms)
wenzelm@23152
   191
  end;
wenzelm@23152
   192
wenzelm@23152
   193
(*************************************************************************)
wenzelm@23152
   194
(*********** monadic notation and pattern matching compilation ***********)
wenzelm@23152
   195
(*************************************************************************)
wenzelm@23152
   196
wenzelm@33522
   197
structure FixrecMatchData = Theory_Data
wenzelm@33522
   198
(
huffman@30131
   199
  type T = string Symtab.table;
huffman@30131
   200
  val empty = Symtab.empty;
huffman@30131
   201
  val extend = I;
wenzelm@33522
   202
  fun merge data = Symtab.merge (K true) data;
huffman@30131
   203
);
huffman@30131
   204
huffman@30131
   205
(* associate match functions with pattern constants *)
huffman@30131
   206
fun add_matchers ms = FixrecMatchData.map (fold Symtab.update ms);
huffman@30131
   207
huffman@30157
   208
fun taken_names (t : term) : bstring list =
huffman@30157
   209
  let
wenzelm@30364
   210
    fun taken (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
huffman@30157
   211
      | taken (Free(a,_) , bs) = insert (op =) a bs
huffman@30157
   212
      | taken (f $ u     , bs) = taken (f, taken (u, bs))
huffman@30157
   213
      | taken (Abs(a,_,t), bs) = taken (t, insert (op =) a bs)
huffman@30157
   214
      | taken (_         , bs) = bs;
huffman@30157
   215
  in
huffman@30157
   216
    taken (t, [])
huffman@30157
   217
  end;
wenzelm@23152
   218
huffman@39805
   219
(* builds a monadic term for matching a pattern *)
huffman@39805
   220
(* returns (rhs, free variable, used varnames) *)
huffman@39805
   221
fun compile_pat match_name pat rhs taken =
huffman@39805
   222
  let
huffman@39805
   223
    fun comp_pat p rhs taken =
huffman@39805
   224
      if is_Free p then (rhs, p, taken)
huffman@39805
   225
      else comp_con (fastype_of p) p rhs [] taken
huffman@39805
   226
    (* compiles a monadic term for a constructor pattern *)
huffman@39805
   227
    and comp_con T p rhs vs taken =
huffman@39805
   228
      case p of
huffman@40327
   229
        Const(@{const_name Rep_cfun},_) $ f $ x =>
huffman@39805
   230
          let val (rhs', v, taken') = comp_pat x rhs taken
huffman@39805
   231
          in comp_con T f rhs' (v::vs) taken' end
huffman@39805
   232
      | f $ x =>
huffman@39805
   233
          let val (rhs', v, taken') = comp_pat x rhs taken
huffman@39805
   234
          in comp_con T f rhs' (v::vs) taken' end
huffman@39805
   235
      | Const (c, cT) =>
huffman@39805
   236
          let
huffman@39805
   237
            val n = Name.variant taken "v"
huffman@39805
   238
            val v = Free(n, T)
huffman@39805
   239
            val m = Const(match_name c, matcherT (cT, fastype_of rhs))
huffman@39805
   240
            val k = big_lambdas vs rhs
huffman@39805
   241
          in
huffman@39805
   242
            (m`v`k, v, n::taken)
huffman@39805
   243
          end
huffman@39805
   244
      | _ => raise TERM ("fixrec: invalid pattern ", [p])
huffman@39805
   245
  in
huffman@39805
   246
    comp_pat pat rhs taken
huffman@39805
   247
  end;
wenzelm@23152
   248
wenzelm@23152
   249
(* builds a monadic term for matching a function definition pattern *)
huffman@40036
   250
(* returns (constant, (vars, matcher)) *)
huffman@39804
   251
fun compile_lhs match_name pat rhs vs taken =
wenzelm@23152
   252
  case pat of
huffman@40327
   253
    Const(@{const_name Rep_cfun}, _) $ f $ x =>
huffman@39805
   254
      let val (rhs', v, taken') = compile_pat match_name x rhs taken;
huffman@39804
   255
      in compile_lhs match_name f rhs' (v::vs) taken' end
huffman@40036
   256
  | Free(_,_) => (pat, (vs, rhs))
huffman@40036
   257
  | Const(_,_) => (pat, (vs, rhs))
huffman@40036
   258
  | _ => fixrec_err ("invalid function pattern: "
huffman@30158
   259
                    ^ ML_Syntax.print_term pat);
wenzelm@23152
   260
huffman@30158
   261
fun strip_alls t =
huffman@30158
   262
  if Logic.is_all t then strip_alls (snd (Logic.dest_all t)) else t;
huffman@30158
   263
huffman@39804
   264
fun compile_eq match_name eq =
huffman@30158
   265
  let
huffman@30158
   266
    val (lhs,rhs) = dest_eqs (Logic.strip_imp_concl (strip_alls eq));
huffman@30131
   267
  in
huffman@39804
   268
    compile_lhs match_name lhs (mk_succeed rhs) [] (taken_names eq)
huffman@30131
   269
  end;
wenzelm@23152
   270
wenzelm@23152
   271
(* this is the pattern-matching compiler function *)
huffman@39804
   272
fun compile_eqs match_name eqs =
wenzelm@23152
   273
  let
huffman@40036
   274
    val (consts, matchers) =
huffman@40036
   275
      ListPair.unzip (map (compile_eq match_name) eqs);
huffman@40036
   276
    val const =
huffman@40036
   277
        case distinct (op =) consts of
huffman@35903
   278
          [n] => n
huffman@35903
   279
        | _ => fixrec_err "all equations in block must define the same function";
huffman@40036
   280
    val vars =
huffman@40036
   281
        case distinct (op = o pairself length) (map fst matchers) of
huffman@40036
   282
          [vars] => vars
huffman@35903
   283
        | _ => fixrec_err "all equations in block must have the same arity";
huffman@40036
   284
    (* rename so all matchers use same free variables *)
huffman@40036
   285
    fun rename (vs, t) = Term.subst_free (filter_out (op =) (vs ~~ vars)) t;
huffman@40036
   286
    val rhs = big_lambdas vars (mk_run (foldr1 mk_mplus (map rename matchers)));
wenzelm@23152
   287
  in
huffman@40036
   288
    mk_trp (const === rhs)
wenzelm@23152
   289
  end;
wenzelm@23152
   290
wenzelm@23152
   291
(*************************************************************************)
wenzelm@23152
   292
(********************** Proving associated theorems **********************)
wenzelm@23152
   293
(*************************************************************************)
wenzelm@23152
   294
huffman@37081
   295
fun eta_tac i = CONVERSION Thm.eta_conversion i;
huffman@33425
   296
huffman@33425
   297
fun fixrec_simp_tac ctxt =
huffman@33425
   298
  let
huffman@33425
   299
    val tab = FixrecUnfoldData.get (Context.Proof ctxt);
huffman@37080
   300
    val ss = Simplifier.simpset_of ctxt;
huffman@33425
   301
    fun concl t =
huffman@33425
   302
      if Logic.is_all t then concl (snd (Logic.dest_all t))
huffman@33425
   303
      else HOLogic.dest_Trueprop (Logic.strip_imp_concl t);
huffman@33425
   304
    fun tac (t, i) =
huffman@33430
   305
      let
huffman@35903
   306
        val (c, T) =
huffman@35903
   307
            (dest_Const o head_of o chead_of o fst o HOLogic.dest_eq o concl) t;
huffman@33430
   308
        val unfold_thm = the (Symtab.lookup tab c);
huffman@33430
   309
        val rule = unfold_thm RS @{thm ssubst_lhs};
huffman@33430
   310
      in
huffman@37081
   311
        CHANGED (rtac rule i THEN eta_tac i THEN asm_simp_tac ss i)
huffman@33430
   312
      end
huffman@33425
   313
  in
huffman@33505
   314
    SUBGOAL (fn ti => the_default no_tac (try tac ti))
huffman@33425
   315
  end;
huffman@33425
   316
wenzelm@23152
   317
(* proves a block of pattern matching equations as theorems, using unfold *)
wenzelm@32149
   318
fun make_simps ctxt (unfold_thm, eqns : (Attrib.binding * term) list) =
wenzelm@23152
   319
  let
huffman@37080
   320
    val ss = Simplifier.simpset_of ctxt;
huffman@36628
   321
    val rule = unfold_thm RS @{thm ssubst_lhs};
huffman@37081
   322
    val tac = rtac rule 1 THEN eta_tac 1 THEN asm_simp_tac ss 1;
huffman@36628
   323
    fun prove_term t = Goal.prove ctxt [] [] t (K tac);
huffman@30158
   324
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t);
wenzelm@23152
   325
  in
wenzelm@23152
   326
    map prove_eqn eqns
wenzelm@23152
   327
  end;
wenzelm@23152
   328
wenzelm@23152
   329
(*************************************************************************)
wenzelm@23152
   330
(************************* Main fixrec function **************************)
wenzelm@23152
   331
(*************************************************************************)
wenzelm@23152
   332
huffman@30158
   333
local
haftmann@31738
   334
(* code adapted from HOL/Tools/primrec.ML *)
huffman@30158
   335
huffman@30158
   336
fun gen_fixrec
wenzelm@30485
   337
  prep_spec
huffman@40041
   338
  (raw_fixes : (binding * 'a option * mixfix) list)
huffman@40041
   339
  (raw_spec' : (bool * (Attrib.binding * 'b)) list)
huffman@30158
   340
  (lthy : local_theory) =
wenzelm@23152
   341
  let
huffman@40041
   342
    val (skips, raw_spec) = ListPair.unzip raw_spec';
huffman@30158
   343
    val (fixes : ((binding * typ) * mixfix) list,
huffman@30158
   344
         spec : (Attrib.binding * term) list) =
wenzelm@30485
   345
          fst (prep_spec raw_fixes raw_spec lthy);
huffman@30158
   346
    val chead_of_spec =
huffman@30158
   347
      chead_of o fst o dest_eqs o Logic.strip_imp_concl o strip_alls o snd;
huffman@30158
   348
    fun name_of (Free (n, _)) = n
huffman@30158
   349
      | name_of t = fixrec_err ("unknown term");
huffman@30158
   350
    val all_names = map (name_of o chead_of_spec) spec;
huffman@30158
   351
    val names = distinct (op =) all_names;
huffman@30158
   352
    fun block_of_name n =
huffman@30158
   353
      map_filter
huffman@30158
   354
        (fn (m,eq) => if m = n then SOME eq else NONE)
huffman@40041
   355
        (all_names ~~ (spec ~~ skips));
huffman@30158
   356
    val blocks = map block_of_name names;
huffman@30158
   357
huffman@30158
   358
    val matcher_tab = FixrecMatchData.get (ProofContext.theory_of lthy);
huffman@30131
   359
    fun match_name c =
huffman@30158
   360
      case Symtab.lookup matcher_tab c of SOME m => m
huffman@30158
   361
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
huffman@30131
   362
huffman@40041
   363
    val matches = map (compile_eqs match_name) (map (map (snd o fst)) blocks);
huffman@30158
   364
    val spec' = map (pair Attrib.empty_binding) matches;
huffman@35772
   365
    val (lthy, cnames, fixdef_thms, unfold_thms) =
huffman@30158
   366
      add_fixdefs fixes spec' lthy;
huffman@40041
   367
huffman@40041
   368
    val blocks' = map (map fst o filter_out snd) blocks;
huffman@40041
   369
    val simps : (Attrib.binding * thm) list list =
huffman@40041
   370
      map (make_simps lthy) (unfold_thms ~~ blocks');
huffman@40041
   371
    fun mk_bind n : Attrib.binding =
huffman@40041
   372
     (Binding.qualify true n (Binding.name "simps"),
huffman@40041
   373
       [Attrib.internal (K Simplifier.simp_add)]);
huffman@40041
   374
    val simps1 : (Attrib.binding * thm list) list =
huffman@40041
   375
      map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps);
huffman@40041
   376
    val simps2 : (Attrib.binding * thm list) list =
huffman@40041
   377
      map (apsnd (fn thm => [thm])) (flat simps);
huffman@40041
   378
    val (_, lthy) = lthy
huffman@40041
   379
      |> fold_map Local_Theory.note (simps1 @ simps2);
wenzelm@23152
   380
  in
huffman@40041
   381
    lthy
wenzelm@23152
   382
  end;
wenzelm@23152
   383
huffman@30158
   384
in
wenzelm@23152
   385
wenzelm@33726
   386
val add_fixrec = gen_fixrec Specification.check_spec;
wenzelm@33726
   387
val add_fixrec_cmd = gen_fixrec Specification.read_spec;
huffman@30158
   388
huffman@30158
   389
end; (* local *)
wenzelm@23152
   390
wenzelm@23152
   391
wenzelm@23152
   392
(*************************************************************************)
wenzelm@23152
   393
(******************************** Parsers ********************************)
wenzelm@23152
   394
(*************************************************************************)
wenzelm@23152
   395
huffman@40096
   396
val opt_thm_name' : (bool * Attrib.binding) parser =
huffman@40096
   397
  Parse.$$$ "(" -- Parse.$$$ "unchecked" -- Parse.$$$ ")" >> K (true, Attrib.empty_binding)
huffman@40096
   398
    || Parse_Spec.opt_thm_name ":" >> pair false;
huffman@40096
   399
huffman@40096
   400
val spec' : (bool * (Attrib.binding * string)) parser =
huffman@40096
   401
  opt_thm_name' -- Parse.prop >> (fn ((a, b), c) => (a, (b, c)));
huffman@40096
   402
huffman@40041
   403
val alt_specs' : (bool * (Attrib.binding * string)) list parser =
huffman@40096
   404
  let val unexpected = Scan.ahead (Parse.name || Parse.$$$ "[" || Parse.$$$ "(");
huffman@40096
   405
  in Parse.enum1 "|" (spec' --| Scan.option (unexpected -- Parse.!!! (Parse.$$$ "|"))) end;
huffman@40041
   406
wenzelm@36960
   407
val _ =
wenzelm@36960
   408
  Outer_Syntax.local_theory "fixrec" "define recursive functions (HOLCF)" Keyword.thy_decl
huffman@40041
   409
    (Parse.fixes -- (Parse.where_ |-- Parse.!!! alt_specs')
huffman@40041
   410
      >> (fn (fixes, specs) => add_fixrec_cmd fixes specs));
wenzelm@23152
   411
huffman@33425
   412
val setup =
huffman@37080
   413
  Method.setup @{binding fixrec_simp}
huffman@37080
   414
    (Scan.succeed (SIMPLE_METHOD' o fixrec_simp_tac))
huffman@37080
   415
    "pattern prover for fixrec constants";
huffman@30131
   416
wenzelm@24867
   417
end;