src/HOL/HOLCF/Tools/fixrec.ML
author wenzelm
Tue Mar 29 17:47:11 2011 +0200 (2011-03-29)
changeset 42151 4da4fc77664b
parent 41296 6aaf80ea9715
child 42361 23f352990944
permissions -rw-r--r--
tuned headers;
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
fun fixrec_eq_err thy s eq =
huffman@40832
    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@40832
    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@40832
    46
  | body_cfun T   =  T
huffman@33401
    47
huffman@33401
    48
fun strip_cfun T : typ list * typ =
huffman@40832
    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@40832
    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
huffman@40832
    62
val mk_trp = HOLogic.mk_Trueprop
wenzelm@23152
    63
wenzelm@23152
    64
(* splits a cterm into the right and lefthand sides of equality *)
huffman@40832
    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
huffman@40832
    69
  | chead_of u = u
wenzelm@23152
    70
huffman@40832
    71
infix 0 ==  val (op ==) = Logic.mk_equals
huffman@40832
    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@40832
    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@40832
    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@40832
    97
  type T = thm Symtab.table
huffman@40832
    98
  val empty = Symtab.empty
huffman@40832
    99
  val extend = I
huffman@40832
   100
  fun merge data : T = Symtab.merge (K true) data
huffman@40832
   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@40832
   107
  | name_of t = raise TERM ("Fixrec.add_unfold: lhs not a constant", [t])
huffman@33425
   108
huffman@33425
   109
val lhs_name =
huffman@40832
   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@40832
   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@40832
   125
    val thy = ProofContext.theory_of lthy
huffman@40832
   126
    val names = map (Binding.name_of o fst o fst) fixes
huffman@40832
   127
    val all_names = space_implode "_" names
huffman@40832
   128
    val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec)
huffman@40832
   129
    val functional = lambda_tuple lhss (mk_tuple rhss)
huffman@40832
   130
    val fixpoint = mk_fix (mk_cabs functional)
huffman@36996
   131
huffman@31095
   132
    val cont_thm =
huffman@36996
   133
      let
huffman@40832
   134
        val prop = mk_trp (mk_cont functional)
huffman@37096
   135
        fun err _ = error (
huffman@40832
   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@40832
   139
          Syntax.string_of_term lthy prop)
huffman@40832
   140
        val rules = Cont2ContData.get lthy
huffman@40832
   141
        val fast_tac = SOLVED' (REPEAT_ALL_NEW (match_tac rules))
huffman@40832
   142
        val slow_tac = SOLVED' (simp_tac (simpset_of lthy))
huffman@40832
   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@40832
   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
huffman@40832
   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@40832
   154
      | defs (l::ls) r = one_def l (mk_fst r) :: defs ls (mk_snd r)
huffman@40832
   155
    val fixdefs = defs lhss fixpoint
huffman@35772
   156
    val (fixdef_thms : (term * (string * thm)) list, lthy) = lthy
huffman@40832
   157
      |> fold_map Local_Theory.define (map (apfst fst) fixes ~~ fixdefs)
huffman@40832
   158
    fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2]
huffman@40832
   159
    val tuple_fixdef_thm = foldr1 pair_equalI (map (snd o snd) fixdef_thms)
huffman@40832
   160
    val P = Var (("P", 0), map Term.fastype_of lhss ---> HOLogic.boolT)
huffman@40832
   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)]
huffman@40832
   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@40832
   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@40832
   170
          val thmL = thm RS @{thm Pair_eqD1}
huffman@40832
   171
          val thmR = thm RS @{thm Pair_eqD2}
huffman@40832
   172
        in (n, thmL) :: unfolds ns thmR end
huffman@40832
   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@40832
   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@40832
   179
      end
huffman@33425
   180
    fun unfold_note (name, thm) : Attrib.binding * Thm.thm list =
huffman@33425
   181
      let
huffman@40832
   182
        val thm_name = Binding.qualify true name (Binding.name "unfold")
huffman@40832
   183
        val src = Attrib.internal (K add_unfold)
huffman@33425
   184
      in
huffman@33425
   185
        ((thm_name, [src]), [thm])
huffman@40832
   186
      end
huffman@35772
   187
    val (thmss, lthy) = lthy
huffman@40832
   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)
huffman@40832
   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@40832
   199
  type T = string Symtab.table
huffman@40832
   200
  val empty = Symtab.empty
huffman@40832
   201
  val extend = I
huffman@40832
   202
  fun merge data = Symtab.merge (K true) data
huffman@40832
   203
)
huffman@30131
   204
huffman@30131
   205
(* associate match functions with pattern constants *)
huffman@40832
   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@40832
   214
      | taken (_         , bs) = bs
huffman@30157
   215
  in
huffman@30157
   216
    taken (t, [])
huffman@40832
   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@40832
   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@40832
   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@40832
   259
                    ^ ML_Syntax.print_term pat)
wenzelm@23152
   260
huffman@30158
   261
fun strip_alls t =
huffman@40832
   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@40832
   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@40832
   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@40832
   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@40832
   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@40832
   283
        | _ => fixrec_err "all equations in block must have the same arity"
huffman@40036
   284
    (* rename so all matchers use same free variables *)
huffman@40832
   285
    fun rename (vs, t) = Term.subst_free (filter_out (op =) (vs ~~ vars)) t
huffman@40832
   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)
huffman@40832
   289
  end
wenzelm@23152
   290
wenzelm@23152
   291
(*************************************************************************)
wenzelm@23152
   292
(********************** Proving associated theorems **********************)
wenzelm@23152
   293
(*************************************************************************)
wenzelm@23152
   294
huffman@40832
   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@40832
   299
    val tab = FixrecUnfoldData.get (Context.Proof ctxt)
huffman@40832
   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@40832
   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@40832
   307
            (dest_Const o head_of o chead_of o fst o HOLogic.dest_eq o concl) t
huffman@40832
   308
        val unfold_thm = the (Symtab.lookup tab c)
huffman@40832
   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@40832
   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@40832
   320
    val ss = Simplifier.simpset_of ctxt
huffman@40832
   321
    val rule = unfold_thm RS @{thm ssubst_lhs}
huffman@40832
   322
    val tac = rtac rule 1 THEN eta_tac 1 THEN asm_simp_tac ss 1
huffman@40832
   323
    fun prove_term t = Goal.prove ctxt [] [] t (K tac)
huffman@40832
   324
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t)
wenzelm@23152
   325
  in
wenzelm@23152
   326
    map prove_eqn eqns
huffman@40832
   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@40832
   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) =
huffman@40832
   345
          fst (prep_spec raw_fixes raw_spec lthy)
huffman@30158
   346
    val chead_of_spec =
huffman@40832
   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@40832
   349
      | name_of t = fixrec_err ("unknown term")
huffman@40832
   350
    val all_names = map (name_of o chead_of_spec) spec
huffman@40832
   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@40832
   355
        (all_names ~~ (spec ~~ skips))
huffman@40832
   356
    val blocks = map block_of_name names
huffman@30158
   357
huffman@40832
   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@40832
   361
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c)
huffman@30131
   362
huffman@40832
   363
    val matches = map (compile_eqs match_name) (map (map (snd o fst)) blocks)
huffman@40832
   364
    val spec' = map (pair Attrib.empty_binding) matches
huffman@35772
   365
    val (lthy, cnames, fixdef_thms, unfold_thms) =
huffman@40832
   366
      add_fixdefs fixes spec' lthy
huffman@40041
   367
huffman@40832
   368
    val blocks' = map (map fst o filter_out snd) blocks
huffman@40041
   369
    val simps : (Attrib.binding * thm) list list =
huffman@40832
   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@40832
   373
       [Attrib.internal (K Simplifier.simp_add)])
huffman@40041
   374
    val simps1 : (Attrib.binding * thm list) list =
huffman@40832
   375
      map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps)
huffman@40041
   376
    val simps2 : (Attrib.binding * thm list) list =
huffman@40832
   377
      map (apsnd (fn thm => [thm])) (flat simps)
huffman@40041
   378
    val (_, lthy) = lthy
huffman@40832
   379
      |> fold_map Local_Theory.note (simps1 @ simps2)
wenzelm@23152
   380
  in
huffman@40041
   381
    lthy
huffman@40832
   382
  end
wenzelm@23152
   383
huffman@30158
   384
in
wenzelm@23152
   385
huffman@40832
   386
val add_fixrec = gen_fixrec Specification.check_spec
huffman@40832
   387
val add_fixrec_cmd = gen_fixrec Specification.read_spec
huffman@30158
   388
huffman@40832
   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@40832
   398
    || Parse_Spec.opt_thm_name ":" >> pair false
huffman@40096
   399
huffman@40096
   400
val spec' : (bool * (Attrib.binding * string)) parser =
huffman@40832
   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@40832
   404
  let val unexpected = Scan.ahead (Parse.name || Parse.$$$ "[" || Parse.$$$ "(")
huffman@40832
   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@40832
   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@40832
   415
    "pattern prover for fixrec constants"
huffman@30131
   416
huffman@40832
   417
end