src/HOLCF/Tools/fixrec.ML
author huffman
Sat Mar 13 10:38:38 2010 -0800 (2010-03-13)
changeset 35770 a57ab2c01369
parent 35769 500c32e5fadc
child 35772 ea0ac5538c53
permissions -rw-r--r--
fixes to allow using fixrec_simp inside a locale, with test in ex/Fixrec_ex.thy
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
wenzelm@30485
     9
  val add_fixrec: bool -> (binding * typ option * mixfix) list
wenzelm@30485
    10
    -> (Attrib.binding * term) list -> local_theory -> local_theory
wenzelm@30485
    11
  val add_fixrec_cmd: bool -> (binding * string option * mixfix) list
huffman@30158
    12
    -> (Attrib.binding * string) list -> local_theory -> local_theory
wenzelm@30485
    13
  val add_fixpat: Thm.binding * term list -> theory -> theory
wenzelm@30485
    14
  val add_fixpat_cmd: Attrib.binding * string list -> theory -> theory
huffman@30131
    15
  val add_matchers: (string * string) list -> theory -> theory
wenzelm@33442
    16
  val fixrec_simp_add: attribute
wenzelm@33442
    17
  val fixrec_simp_del: attribute
huffman@33425
    18
  val fixrec_simp_tac: Proof.context -> int -> tactic
huffman@30131
    19
  val setup: theory -> theory
wenzelm@23152
    20
end;
wenzelm@23152
    21
haftmann@31738
    22
structure Fixrec :> FIXREC =
wenzelm@23152
    23
struct
wenzelm@23152
    24
huffman@35527
    25
open HOLCF_Library;
huffman@35527
    26
huffman@35527
    27
infixr 6 ->>;
huffman@35527
    28
infix -->>;
huffman@35527
    29
infix 9 `;
huffman@35527
    30
huffman@31095
    31
val def_cont_fix_eq = @{thm def_cont_fix_eq};
huffman@31095
    32
val def_cont_fix_ind = @{thm def_cont_fix_ind};
wenzelm@23152
    33
wenzelm@23152
    34
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
wenzelm@23152
    35
fun fixrec_eq_err thy s eq =
wenzelm@26939
    36
  fixrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
wenzelm@23152
    37
huffman@30132
    38
(*************************************************************************)
huffman@30132
    39
(***************************** building types ****************************)
huffman@30132
    40
(*************************************************************************)
huffman@30132
    41
huffman@33401
    42
local
huffman@33401
    43
huffman@35525
    44
fun binder_cfun (Type(@{type_name cfun},[T, U])) = T :: binder_cfun U
huffman@33401
    45
  | binder_cfun (Type(@{type_name "fun"},[T, U])) = T :: binder_cfun U
huffman@33401
    46
  | binder_cfun _   =  [];
huffman@33401
    47
huffman@35525
    48
fun body_cfun (Type(@{type_name cfun},[T, U])) = body_cfun U
huffman@33401
    49
  | body_cfun (Type(@{type_name "fun"},[T, U])) = body_cfun U
huffman@33401
    50
  | body_cfun T   =  T;
huffman@33401
    51
huffman@33401
    52
fun strip_cfun T : typ list * typ =
huffman@33401
    53
  (binder_cfun T, body_cfun T);
huffman@33401
    54
huffman@33401
    55
in
huffman@33401
    56
huffman@35527
    57
fun matcherT (T, U) =
huffman@35527
    58
  body_cfun T ->> (binder_cfun T -->> U) ->> U;
huffman@30912
    59
huffman@33401
    60
end
huffman@30132
    61
huffman@30132
    62
(*************************************************************************)
huffman@30132
    63
(***************************** building terms ****************************)
huffman@30132
    64
(*************************************************************************)
wenzelm@23152
    65
wenzelm@23152
    66
val mk_trp = HOLogic.mk_Trueprop;
wenzelm@23152
    67
wenzelm@23152
    68
(* splits a cterm into the right and lefthand sides of equality *)
wenzelm@23152
    69
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
wenzelm@23152
    70
wenzelm@23152
    71
(* similar to Thm.head_of, but for continuous application *)
huffman@26045
    72
fun chead_of (Const(@{const_name Rep_CFun},_)$f$t) = chead_of f
wenzelm@23152
    73
  | chead_of u = u;
wenzelm@23152
    74
huffman@30132
    75
infix 0 ==;  val (op ==) = Logic.mk_equals;
huffman@30132
    76
infix 1 ===; val (op ===) = HOLogic.mk_eq;
huffman@30132
    77
huffman@30132
    78
fun mk_mplus (t, u) =
huffman@30132
    79
  let val mT = Term.fastype_of t
huffman@30132
    80
  in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end;
huffman@30132
    81
huffman@30132
    82
fun mk_run t =
huffman@30132
    83
  let val mT = Term.fastype_of t
huffman@35527
    84
      val T = dest_matchT mT
huffman@30132
    85
  in Const(@{const_name Fixrec.run}, mT ->> T) ` t end;
huffman@30132
    86
huffman@31095
    87
wenzelm@23152
    88
(*************************************************************************)
wenzelm@23152
    89
(************* fixed-point definitions and unfolding theorems ************)
wenzelm@23152
    90
(*************************************************************************)
wenzelm@23152
    91
wenzelm@33519
    92
structure FixrecUnfoldData = Generic_Data
wenzelm@33519
    93
(
huffman@33425
    94
  type T = thm Symtab.table;
huffman@33425
    95
  val empty = Symtab.empty;
huffman@33425
    96
  val extend = I;
wenzelm@33519
    97
  fun merge data : T = Symtab.merge (K true) data;
huffman@33425
    98
);
huffman@33425
    99
huffman@33425
   100
local
huffman@33425
   101
huffman@33425
   102
fun name_of (Const (n, T)) = n
huffman@33425
   103
  | name_of (Free (n, T)) = n
huffman@35770
   104
  | name_of t = raise TERM ("Fixrec.add_unfold: lhs not a constant", [t]);
huffman@33425
   105
huffman@33425
   106
val lhs_name =
huffman@35770
   107
  name_of o head_of o fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
huffman@33425
   108
huffman@33425
   109
in
huffman@33425
   110
wenzelm@33442
   111
val add_unfold : attribute =
huffman@33425
   112
  Thm.declaration_attribute
huffman@33425
   113
    (fn th => FixrecUnfoldData.map (Symtab.insert (K true) (lhs_name th, th)));
huffman@33425
   114
huffman@33425
   115
end
huffman@33425
   116
huffman@30158
   117
fun add_fixdefs
huffman@30158
   118
  (fixes : ((binding * typ) * mixfix) list)
huffman@30158
   119
  (spec : (Attrib.binding * term) list)
huffman@30158
   120
  (lthy : local_theory) =
wenzelm@23152
   121
  let
huffman@31095
   122
    val thy = ProofContext.theory_of lthy;
wenzelm@30223
   123
    val names = map (Binding.name_of o fst o fst) fixes;
huffman@30158
   124
    val all_names = space_implode "_" names;
wenzelm@32149
   125
    val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec);
huffman@31095
   126
    val functional = lambda_tuple lhss (mk_tuple rhss);
huffman@31095
   127
    val fixpoint = mk_fix (mk_cabs functional);
wenzelm@23152
   128
    
huffman@31095
   129
    val cont_thm =
huffman@31095
   130
      Goal.prove lthy [] [] (mk_trp (mk_cont functional))
wenzelm@32149
   131
        (K (simp_tac (simpset_of lthy) 1));
huffman@31095
   132
huffman@30158
   133
    fun one_def (l as Free(n,_)) r =
wenzelm@30364
   134
          let val b = Long_Name.base_name n
huffman@30158
   135
          in ((Binding.name (b^"_def"), []), r) end
wenzelm@23152
   136
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
wenzelm@23152
   137
    fun defs [] _ = []
wenzelm@23152
   138
      | defs (l::[]) r = [one_def l r]
huffman@31095
   139
      | defs (l::ls) r = one_def l (mk_fst r) :: defs ls (mk_snd r);
huffman@30158
   140
    val fixdefs = defs lhss fixpoint;
huffman@30158
   141
    val (fixdef_thms : (term * (string * thm)) list, lthy') = lthy
wenzelm@33766
   142
      |> fold_map Local_Theory.define (map (apfst fst) fixes ~~ fixdefs);
huffman@31095
   143
    fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2];
huffman@31095
   144
    val tuple_fixdef_thm = foldr1 pair_equalI (map (snd o snd) fixdef_thms);
huffman@31095
   145
    val P = Var (("P", 0), map Term.fastype_of lhss ---> HOLogic.boolT);
huffman@31095
   146
    val predicate = lambda_tuple lhss (list_comb (P, lhss));
huffman@31095
   147
    val tuple_induct_thm = (def_cont_fix_ind OF [tuple_fixdef_thm, cont_thm])
huffman@31095
   148
      |> Drule.instantiate' [] [SOME (Thm.cterm_of thy predicate)]
wenzelm@35624
   149
      |> Local_Defs.unfold lthy @{thms split_paired_all split_conv split_strict};
huffman@31095
   150
    val tuple_unfold_thm = (def_cont_fix_eq OF [tuple_fixdef_thm, cont_thm])
wenzelm@35624
   151
      |> Local_Defs.unfold lthy' @{thms split_conv};
wenzelm@23152
   152
    fun unfolds [] thm = []
huffman@33425
   153
      | unfolds (n::[]) thm = [(n, thm)]
wenzelm@23152
   154
      | unfolds (n::ns) thm = let
huffman@31095
   155
          val thmL = thm RS @{thm Pair_eqD1};
huffman@31095
   156
          val thmR = thm RS @{thm Pair_eqD2};
huffman@33425
   157
        in (n, thmL) :: unfolds ns thmR end;
huffman@31095
   158
    val unfold_thms = unfolds names tuple_unfold_thm;
huffman@33425
   159
    val induct_note : Attrib.binding * Thm.thm list =
huffman@33425
   160
      let
huffman@35769
   161
        val thm_name = Binding.qualify true all_names (Binding.name "induct");
huffman@33425
   162
      in
huffman@33425
   163
        ((thm_name, []), [tuple_induct_thm])
huffman@33425
   164
      end;
huffman@33425
   165
    fun unfold_note (name, thm) : Attrib.binding * Thm.thm list =
huffman@33425
   166
      let
huffman@35769
   167
        val thm_name = Binding.qualify true name (Binding.name "unfold");
huffman@33425
   168
        val src = Attrib.internal (K add_unfold);
huffman@33425
   169
      in
huffman@33425
   170
        ((thm_name, [src]), [thm])
huffman@33425
   171
      end;
huffman@30158
   172
    val (thmss, lthy'') = lthy'
wenzelm@33671
   173
      |> fold_map Local_Theory.note (induct_note :: map unfold_note unfold_thms);
wenzelm@23152
   174
  in
huffman@30158
   175
    (lthy'', names, fixdef_thms, map snd unfold_thms)
wenzelm@23152
   176
  end;
wenzelm@23152
   177
wenzelm@23152
   178
(*************************************************************************)
wenzelm@23152
   179
(*********** monadic notation and pattern matching compilation ***********)
wenzelm@23152
   180
(*************************************************************************)
wenzelm@23152
   181
wenzelm@33522
   182
structure FixrecMatchData = Theory_Data
wenzelm@33522
   183
(
huffman@30131
   184
  type T = string Symtab.table;
huffman@30131
   185
  val empty = Symtab.empty;
huffman@30131
   186
  val extend = I;
wenzelm@33522
   187
  fun merge data = Symtab.merge (K true) data;
huffman@30131
   188
);
huffman@30131
   189
huffman@30131
   190
(* associate match functions with pattern constants *)
huffman@30131
   191
fun add_matchers ms = FixrecMatchData.map (fold Symtab.update ms);
huffman@30131
   192
huffman@30157
   193
fun taken_names (t : term) : bstring list =
huffman@30157
   194
  let
wenzelm@30364
   195
    fun taken (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
huffman@30157
   196
      | taken (Free(a,_) , bs) = insert (op =) a bs
huffman@30157
   197
      | taken (f $ u     , bs) = taken (f, taken (u, bs))
huffman@30157
   198
      | taken (Abs(a,_,t), bs) = taken (t, insert (op =) a bs)
huffman@30157
   199
      | taken (_         , bs) = bs;
huffman@30157
   200
  in
huffman@30157
   201
    taken (t, [])
huffman@30157
   202
  end;
wenzelm@23152
   203
wenzelm@23152
   204
(* builds a monadic term for matching a constructor pattern *)
huffman@30131
   205
fun pre_build match_name pat rhs vs taken =
wenzelm@23152
   206
  case pat of
huffman@26045
   207
    Const(@{const_name Rep_CFun},_)$f$(v as Free(n,T)) =>
huffman@30131
   208
      pre_build match_name f rhs (v::vs) taken
huffman@26045
   209
  | Const(@{const_name Rep_CFun},_)$f$x =>
huffman@30131
   210
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
huffman@30131
   211
      in pre_build match_name f rhs' (v::vs) taken' end
huffman@33401
   212
  | f$(v as Free(n,T)) =>
huffman@33401
   213
      pre_build match_name f rhs (v::vs) taken
huffman@33401
   214
  | f$x =>
huffman@33401
   215
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
huffman@33401
   216
      in pre_build match_name f rhs' (v::vs) taken' end
wenzelm@23152
   217
  | Const(c,T) =>
wenzelm@23152
   218
      let
wenzelm@23152
   219
        val n = Name.variant taken "v";
huffman@35525
   220
        fun result_type (Type(@{type_name cfun},[_,T])) (x::xs) = result_type T xs
huffman@33401
   221
          | result_type (Type (@{type_name "fun"},[_,T])) (x::xs) = result_type T xs
wenzelm@23152
   222
          | result_type T _ = T;
wenzelm@23152
   223
        val v = Free(n, result_type T vs);
huffman@35527
   224
        val m = Const(match_name c, matcherT (T, fastype_of rhs));
huffman@30912
   225
        val k = big_lambdas vs rhs;
wenzelm@23152
   226
      in
huffman@30912
   227
        (m`v`k, v, n::taken)
wenzelm@23152
   228
      end
wenzelm@23152
   229
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
wenzelm@23152
   230
  | _ => fixrec_err "pre_build: invalid pattern";
wenzelm@23152
   231
wenzelm@23152
   232
(* builds a monadic term for matching a function definition pattern *)
wenzelm@23152
   233
(* returns (name, arity, matcher) *)
huffman@30131
   234
fun building match_name pat rhs vs taken =
wenzelm@23152
   235
  case pat of
huffman@26045
   236
    Const(@{const_name Rep_CFun}, _)$f$(v as Free(n,T)) =>
huffman@30131
   237
      building match_name f rhs (v::vs) taken
huffman@26045
   238
  | Const(@{const_name Rep_CFun}, _)$f$x =>
huffman@30131
   239
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
huffman@30131
   240
      in building match_name f rhs' (v::vs) taken' end
huffman@30158
   241
  | Free(_,_) => ((pat, length vs), big_lambdas vs rhs)
huffman@30158
   242
  | Const(_,_) => ((pat, length vs), big_lambdas vs rhs)
huffman@30158
   243
  | _ => fixrec_err ("function is not declared as constant in theory: "
huffman@30158
   244
                    ^ ML_Syntax.print_term pat);
wenzelm@23152
   245
huffman@30158
   246
fun strip_alls t =
huffman@30158
   247
  if Logic.is_all t then strip_alls (snd (Logic.dest_all t)) else t;
huffman@30158
   248
huffman@30158
   249
fun match_eq match_name eq =
huffman@30158
   250
  let
huffman@30158
   251
    val (lhs,rhs) = dest_eqs (Logic.strip_imp_concl (strip_alls eq));
huffman@30131
   252
  in
huffman@30157
   253
    building match_name lhs (mk_return rhs) [] (taken_names eq)
huffman@30131
   254
  end;
wenzelm@23152
   255
wenzelm@23152
   256
(* returns the sum (using +++) of the terms in ms *)
wenzelm@23152
   257
(* also applies "run" to the result! *)
wenzelm@23152
   258
fun fatbar arity ms =
wenzelm@23152
   259
  let
huffman@30132
   260
    fun LAM_Ts 0 t = ([], Term.fastype_of t)
huffman@30132
   261
      | LAM_Ts n (_ $ Abs(_,T,t)) =
huffman@30132
   262
          let val (Ts, U) = LAM_Ts (n-1) t in (T::Ts, U) end
huffman@30132
   263
      | LAM_Ts _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
wenzelm@23152
   264
    fun unLAM 0 t = t
wenzelm@23152
   265
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
wenzelm@23152
   266
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
huffman@30132
   267
    fun reLAM ([], U) t = t
huffman@30132
   268
      | reLAM (T::Ts, U) t = reLAM (Ts, T ->> U) (cabs_const(T,U)$Abs("",T,t));
huffman@30132
   269
    val msum = foldr1 mk_mplus (map (unLAM arity) ms);
huffman@30132
   270
    val (Ts, U) = LAM_Ts arity (hd ms)
wenzelm@23152
   271
  in
huffman@35527
   272
    reLAM (rev Ts, dest_matchT U) (mk_run msum)
wenzelm@23152
   273
  end;
wenzelm@23152
   274
wenzelm@23152
   275
(* this is the pattern-matching compiler function *)
huffman@30158
   276
fun compile_pats match_name eqs =
wenzelm@23152
   277
  let
huffman@30158
   278
    val (((n::names),(a::arities)),mats) =
huffman@30158
   279
      apfst ListPair.unzip (ListPair.unzip (map (match_eq match_name) eqs));
wenzelm@23152
   280
    val cname = if forall (fn x => n=x) names then n
wenzelm@23152
   281
          else fixrec_err "all equations in block must define the same function";
wenzelm@23152
   282
    val arity = if forall (fn x => a=x) arities then a
wenzelm@23152
   283
          else fixrec_err "all equations in block must have the same arity";
wenzelm@23152
   284
    val rhs = fatbar arity mats;
wenzelm@23152
   285
  in
huffman@30132
   286
    mk_trp (cname === rhs)
wenzelm@23152
   287
  end;
wenzelm@23152
   288
wenzelm@23152
   289
(*************************************************************************)
wenzelm@23152
   290
(********************** Proving associated theorems **********************)
wenzelm@23152
   291
(*************************************************************************)
wenzelm@23152
   292
wenzelm@33519
   293
structure FixrecSimpData = Generic_Data
wenzelm@33519
   294
(
huffman@33425
   295
  type T = simpset;
huffman@33425
   296
  val empty =
huffman@33425
   297
    HOL_basic_ss
huffman@33425
   298
      addsimps simp_thms
huffman@33425
   299
      addsimps [@{thm beta_cfun}]
huffman@33425
   300
      addsimprocs [@{simproc cont_proc}];
huffman@33425
   301
  val extend = I;
wenzelm@33519
   302
  val merge = merge_ss;
huffman@33425
   303
);
huffman@33425
   304
huffman@33425
   305
fun fixrec_simp_tac ctxt =
huffman@33425
   306
  let
huffman@33425
   307
    val tab = FixrecUnfoldData.get (Context.Proof ctxt);
huffman@33425
   308
    val ss = FixrecSimpData.get (Context.Proof ctxt);
huffman@33425
   309
    fun concl t =
huffman@33425
   310
      if Logic.is_all t then concl (snd (Logic.dest_all t))
huffman@33425
   311
      else HOLogic.dest_Trueprop (Logic.strip_imp_concl t);
huffman@33425
   312
    fun tac (t, i) =
huffman@33430
   313
      let
huffman@35770
   314
        val Const (c, T) = head_of (chead_of (fst (HOLogic.dest_eq (concl t))));
huffman@33430
   315
        val unfold_thm = the (Symtab.lookup tab c);
huffman@33430
   316
        val rule = unfold_thm RS @{thm ssubst_lhs};
huffman@33430
   317
      in
huffman@33430
   318
        CHANGED (rtac rule i THEN asm_simp_tac ss i)
huffman@33430
   319
      end
huffman@33425
   320
  in
huffman@33505
   321
    SUBGOAL (fn ti => the_default no_tac (try tac ti))
huffman@33425
   322
  end;
huffman@33425
   323
wenzelm@33442
   324
val fixrec_simp_add : attribute =
huffman@33425
   325
  Thm.declaration_attribute
huffman@33425
   326
    (fn th => FixrecSimpData.map (fn ss => ss addsimps [th]));
huffman@33425
   327
wenzelm@33442
   328
val fixrec_simp_del : attribute =
huffman@33425
   329
  Thm.declaration_attribute
huffman@33425
   330
    (fn th => FixrecSimpData.map (fn ss => ss delsimps [th]));
huffman@33425
   331
wenzelm@23152
   332
(* proves a block of pattern matching equations as theorems, using unfold *)
wenzelm@32149
   333
fun make_simps ctxt (unfold_thm, eqns : (Attrib.binding * term) list) =
wenzelm@23152
   334
  let
huffman@30158
   335
    val tacs =
huffman@30158
   336
      [rtac (unfold_thm RS @{thm ssubst_lhs}) 1,
wenzelm@32149
   337
       asm_simp_tac (simpset_of ctxt) 1];
wenzelm@32149
   338
    fun prove_term t = Goal.prove ctxt [] [] t (K (EVERY tacs));
huffman@30158
   339
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t);
wenzelm@23152
   340
  in
wenzelm@23152
   341
    map prove_eqn eqns
wenzelm@23152
   342
  end;
wenzelm@23152
   343
wenzelm@23152
   344
(*************************************************************************)
wenzelm@23152
   345
(************************* Main fixrec function **************************)
wenzelm@23152
   346
(*************************************************************************)
wenzelm@23152
   347
huffman@30158
   348
local
haftmann@31738
   349
(* code adapted from HOL/Tools/primrec.ML *)
huffman@30158
   350
huffman@30158
   351
fun gen_fixrec
wenzelm@30485
   352
  prep_spec
huffman@30158
   353
  (strict : bool)
huffman@30158
   354
  raw_fixes
huffman@30158
   355
  raw_spec
huffman@30158
   356
  (lthy : local_theory) =
wenzelm@23152
   357
  let
huffman@30158
   358
    val (fixes : ((binding * typ) * mixfix) list,
huffman@30158
   359
         spec : (Attrib.binding * term) list) =
wenzelm@30485
   360
          fst (prep_spec raw_fixes raw_spec lthy);
huffman@30158
   361
    val chead_of_spec =
huffman@30158
   362
      chead_of o fst o dest_eqs o Logic.strip_imp_concl o strip_alls o snd;
huffman@30158
   363
    fun name_of (Free (n, _)) = n
huffman@30158
   364
      | name_of t = fixrec_err ("unknown term");
huffman@30158
   365
    val all_names = map (name_of o chead_of_spec) spec;
huffman@30158
   366
    val names = distinct (op =) all_names;
huffman@30158
   367
    fun block_of_name n =
huffman@30158
   368
      map_filter
huffman@30158
   369
        (fn (m,eq) => if m = n then SOME eq else NONE)
huffman@30158
   370
        (all_names ~~ spec);
huffman@30158
   371
    val blocks = map block_of_name names;
huffman@30158
   372
huffman@30158
   373
    val matcher_tab = FixrecMatchData.get (ProofContext.theory_of lthy);
huffman@30131
   374
    fun match_name c =
huffman@30158
   375
      case Symtab.lookup matcher_tab c of SOME m => m
huffman@30158
   376
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
huffman@30131
   377
huffman@30158
   378
    val matches = map (compile_pats match_name) (map (map snd) blocks);
huffman@30158
   379
    val spec' = map (pair Attrib.empty_binding) matches;
huffman@30158
   380
    val (lthy', cnames, fixdef_thms, unfold_thms) =
huffman@30158
   381
      add_fixdefs fixes spec' lthy;
wenzelm@23152
   382
  in
wenzelm@23152
   383
    if strict then let (* only prove simp rules if strict = true *)
huffman@30158
   384
      val simps : (Attrib.binding * thm) list list =
huffman@30158
   385
        map (make_simps lthy') (unfold_thms ~~ blocks);
huffman@30158
   386
      fun mk_bind n : Attrib.binding =
huffman@35769
   387
       (Binding.qualify true n (Binding.name "simps"),
huffman@30158
   388
         [Attrib.internal (K Simplifier.simp_add)]);
huffman@30158
   389
      val simps1 : (Attrib.binding * thm list) list =
huffman@30158
   390
        map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps);
huffman@30158
   391
      val simps2 : (Attrib.binding * thm list) list =
wenzelm@32952
   392
        map (apsnd (fn thm => [thm])) (flat simps);
huffman@30158
   393
      val (_, lthy'') = lthy'
wenzelm@33671
   394
        |> fold_map Local_Theory.note (simps1 @ simps2);
wenzelm@23152
   395
    in
huffman@30158
   396
      lthy''
wenzelm@23152
   397
    end
huffman@30158
   398
    else lthy'
wenzelm@23152
   399
  end;
wenzelm@23152
   400
huffman@30158
   401
in
wenzelm@23152
   402
wenzelm@33726
   403
val add_fixrec = gen_fixrec Specification.check_spec;
wenzelm@33726
   404
val add_fixrec_cmd = gen_fixrec Specification.read_spec;
huffman@30158
   405
huffman@30158
   406
end; (* local *)
wenzelm@23152
   407
wenzelm@23152
   408
(*************************************************************************)
wenzelm@23152
   409
(******************************** Fixpat *********************************)
wenzelm@23152
   410
(*************************************************************************)
wenzelm@23152
   411
wenzelm@23152
   412
fun fix_pat thy t = 
wenzelm@23152
   413
  let
wenzelm@23152
   414
    val T = fastype_of t;
wenzelm@23152
   415
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
wenzelm@23152
   416
    val cname = case chead_of t of Const(c,_) => c | _ =>
wenzelm@23152
   417
              fixrec_err "function is not declared as constant in theory";
huffman@35769
   418
    val unfold_thm = PureThy.get_thm thy (cname^".unfold");
wenzelm@23152
   419
    val simp = Goal.prove_global thy [] [] eq
wenzelm@32149
   420
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (global_simpset_of thy) 1]);
wenzelm@23152
   421
  in simp end;
wenzelm@23152
   422
wenzelm@23152
   423
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
wenzelm@23152
   424
  let
wenzelm@23152
   425
    val atts = map (prep_attrib thy) srcs;
wenzelm@23152
   426
    val ts = map (prep_term thy) strings;
wenzelm@23152
   427
    val simps = map (fix_pat thy) ts;
wenzelm@23152
   428
  in
haftmann@29585
   429
    (snd o PureThy.add_thmss [((name, simps), atts)]) thy
wenzelm@23152
   430
  end;
wenzelm@23152
   431
wenzelm@30485
   432
val add_fixpat = gen_add_fixpat Sign.cert_term (K I);
wenzelm@30485
   433
val add_fixpat_cmd = gen_add_fixpat Syntax.read_term_global Attrib.attribute;
wenzelm@23152
   434
wenzelm@23152
   435
wenzelm@23152
   436
(*************************************************************************)
wenzelm@23152
   437
(******************************** Parsers ********************************)
wenzelm@23152
   438
(*************************************************************************)
wenzelm@23152
   439
wenzelm@23152
   440
local structure P = OuterParse and K = OuterKeyword in
wenzelm@23152
   441
wenzelm@30485
   442
val _ = OuterSyntax.local_theory "fixrec" "define recursive functions (HOLCF)" K.thy_decl
wenzelm@30485
   443
  ((P.opt_keyword "permissive" >> not) -- P.fixes -- SpecParse.where_alt_specs
wenzelm@30485
   444
    >> (fn ((strict, fixes), specs) => add_fixrec_cmd strict fixes specs));
wenzelm@23152
   445
wenzelm@30485
   446
val _ = OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
wenzelm@30485
   447
  (SpecParse.specs >> (Toplevel.theory o add_fixpat_cmd));
huffman@33425
   448
wenzelm@30485
   449
end;
wenzelm@23152
   450
huffman@33425
   451
val setup =
wenzelm@33522
   452
  Attrib.setup @{binding fixrec_simp}
huffman@33427
   453
                     (Attrib.add_del fixrec_simp_add fixrec_simp_del)
huffman@33425
   454
                     "declaration of fixrec simp rule"
huffman@33425
   455
  #> Method.setup @{binding fixrec_simp}
huffman@33425
   456
                     (Scan.succeed (SIMPLE_METHOD' o fixrec_simp_tac))
huffman@33425
   457
                     "pattern prover for fixrec constants";
huffman@30131
   458
wenzelm@24867
   459
end;