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