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