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