src/HOLCF/Tools/fixrec.ML
author huffman
Wed May 19 13:07:15 2010 -0700 (2010-05-19)
changeset 36996 63fadc0a33db
parent 36960 01594f816e3a
child 37079 0cd15d8c90a0
permissions -rw-r--r--
more informative error message for fixrec when continuity proof fails
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);
huffman@36996
   128
huffman@31095
   129
    val cont_thm =
huffman@36996
   130
      let
huffman@36996
   131
        val prop = mk_trp (mk_cont functional);
huffman@36996
   132
        fun err () = error (
huffman@36996
   133
          "Continuity proof failed; please check that cont2cont rules\n" ^
huffman@36996
   134
          "are configured for all non-HOLCF constants.\n" ^
huffman@36996
   135
          "The error occurred for the goal statement:\n" ^
huffman@36996
   136
          Syntax.string_of_term lthy prop);
huffman@36996
   137
        fun check th = case Thm.nprems_of th of 0 => all_tac th | n => err ();
huffman@36996
   138
        val tac = simp_tac (simpset_of lthy) 1 THEN check;
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
wenzelm@33519
   307
structure FixrecSimpData = Generic_Data
wenzelm@33519
   308
(
huffman@33425
   309
  type T = simpset;
huffman@33425
   310
  val empty =
huffman@33425
   311
    HOL_basic_ss
huffman@33425
   312
      addsimps simp_thms
huffman@33425
   313
      addsimps [@{thm beta_cfun}]
huffman@33425
   314
      addsimprocs [@{simproc cont_proc}];
huffman@33425
   315
  val extend = I;
wenzelm@33519
   316
  val merge = merge_ss;
huffman@33425
   317
);
huffman@33425
   318
huffman@33425
   319
fun fixrec_simp_tac ctxt =
huffman@33425
   320
  let
huffman@33425
   321
    val tab = FixrecUnfoldData.get (Context.Proof ctxt);
huffman@35780
   322
    val ss = Simplifier.context ctxt (FixrecSimpData.get (Context.Proof ctxt));
huffman@33425
   323
    fun concl t =
huffman@33425
   324
      if Logic.is_all t then concl (snd (Logic.dest_all t))
huffman@33425
   325
      else HOLogic.dest_Trueprop (Logic.strip_imp_concl t);
huffman@33425
   326
    fun tac (t, i) =
huffman@33430
   327
      let
huffman@35903
   328
        val (c, T) =
huffman@35903
   329
            (dest_Const o head_of o chead_of o fst o HOLogic.dest_eq o concl) t;
huffman@33430
   330
        val unfold_thm = the (Symtab.lookup tab c);
huffman@33430
   331
        val rule = unfold_thm RS @{thm ssubst_lhs};
huffman@33430
   332
      in
huffman@33430
   333
        CHANGED (rtac rule i THEN asm_simp_tac ss i)
huffman@33430
   334
      end
huffman@33425
   335
  in
huffman@33505
   336
    SUBGOAL (fn ti => the_default no_tac (try tac ti))
huffman@33425
   337
  end;
huffman@33425
   338
wenzelm@33442
   339
val fixrec_simp_add : attribute =
huffman@33425
   340
  Thm.declaration_attribute
huffman@33425
   341
    (fn th => FixrecSimpData.map (fn ss => ss addsimps [th]));
huffman@33425
   342
wenzelm@33442
   343
val fixrec_simp_del : attribute =
huffman@33425
   344
  Thm.declaration_attribute
huffman@33425
   345
    (fn th => FixrecSimpData.map (fn ss => ss delsimps [th]));
huffman@33425
   346
wenzelm@23152
   347
(* proves a block of pattern matching equations as theorems, using unfold *)
wenzelm@32149
   348
fun make_simps ctxt (unfold_thm, eqns : (Attrib.binding * term) list) =
wenzelm@23152
   349
  let
huffman@36628
   350
    val ss = Simplifier.context ctxt (FixrecSimpData.get (Context.Proof ctxt));
huffman@36628
   351
    val rule = unfold_thm RS @{thm ssubst_lhs};
huffman@36628
   352
    val tac = rtac rule 1 THEN asm_simp_tac ss 1;
huffman@36628
   353
    fun prove_term t = Goal.prove ctxt [] [] t (K tac);
huffman@30158
   354
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t);
wenzelm@23152
   355
  in
wenzelm@23152
   356
    map prove_eqn eqns
wenzelm@23152
   357
  end;
wenzelm@23152
   358
wenzelm@23152
   359
(*************************************************************************)
wenzelm@23152
   360
(************************* Main fixrec function **************************)
wenzelm@23152
   361
(*************************************************************************)
wenzelm@23152
   362
huffman@30158
   363
local
haftmann@31738
   364
(* code adapted from HOL/Tools/primrec.ML *)
huffman@30158
   365
huffman@30158
   366
fun gen_fixrec
wenzelm@30485
   367
  prep_spec
huffman@30158
   368
  (strict : bool)
huffman@30158
   369
  raw_fixes
huffman@30158
   370
  raw_spec
huffman@30158
   371
  (lthy : local_theory) =
wenzelm@23152
   372
  let
huffman@30158
   373
    val (fixes : ((binding * typ) * mixfix) list,
huffman@30158
   374
         spec : (Attrib.binding * term) list) =
wenzelm@30485
   375
          fst (prep_spec raw_fixes raw_spec lthy);
huffman@30158
   376
    val chead_of_spec =
huffman@30158
   377
      chead_of o fst o dest_eqs o Logic.strip_imp_concl o strip_alls o snd;
huffman@30158
   378
    fun name_of (Free (n, _)) = n
huffman@30158
   379
      | name_of t = fixrec_err ("unknown term");
huffman@30158
   380
    val all_names = map (name_of o chead_of_spec) spec;
huffman@30158
   381
    val names = distinct (op =) all_names;
huffman@30158
   382
    fun block_of_name n =
huffman@30158
   383
      map_filter
huffman@30158
   384
        (fn (m,eq) => if m = n then SOME eq else NONE)
huffman@30158
   385
        (all_names ~~ spec);
huffman@30158
   386
    val blocks = map block_of_name names;
huffman@30158
   387
huffman@30158
   388
    val matcher_tab = FixrecMatchData.get (ProofContext.theory_of lthy);
huffman@30131
   389
    fun match_name c =
huffman@30158
   390
      case Symtab.lookup matcher_tab c of SOME m => m
huffman@30158
   391
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
huffman@30131
   392
huffman@30158
   393
    val matches = map (compile_pats match_name) (map (map snd) blocks);
huffman@30158
   394
    val spec' = map (pair Attrib.empty_binding) matches;
huffman@35772
   395
    val (lthy, cnames, fixdef_thms, unfold_thms) =
huffman@30158
   396
      add_fixdefs fixes spec' lthy;
wenzelm@23152
   397
  in
wenzelm@23152
   398
    if strict then let (* only prove simp rules if strict = true *)
huffman@30158
   399
      val simps : (Attrib.binding * thm) list list =
huffman@35772
   400
        map (make_simps lthy) (unfold_thms ~~ blocks);
huffman@30158
   401
      fun mk_bind n : Attrib.binding =
huffman@35769
   402
       (Binding.qualify true n (Binding.name "simps"),
huffman@30158
   403
         [Attrib.internal (K Simplifier.simp_add)]);
huffman@30158
   404
      val simps1 : (Attrib.binding * thm list) list =
huffman@30158
   405
        map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps);
huffman@30158
   406
      val simps2 : (Attrib.binding * thm list) list =
wenzelm@32952
   407
        map (apsnd (fn thm => [thm])) (flat simps);
huffman@35772
   408
      val (_, lthy) = lthy
wenzelm@33671
   409
        |> fold_map Local_Theory.note (simps1 @ simps2);
wenzelm@23152
   410
    in
huffman@35772
   411
      lthy
wenzelm@23152
   412
    end
huffman@35772
   413
    else lthy
wenzelm@23152
   414
  end;
wenzelm@23152
   415
huffman@30158
   416
in
wenzelm@23152
   417
wenzelm@33726
   418
val add_fixrec = gen_fixrec Specification.check_spec;
wenzelm@33726
   419
val add_fixrec_cmd = gen_fixrec Specification.read_spec;
huffman@30158
   420
huffman@30158
   421
end; (* local *)
wenzelm@23152
   422
wenzelm@23152
   423
(*************************************************************************)
wenzelm@23152
   424
(******************************** Fixpat *********************************)
wenzelm@23152
   425
(*************************************************************************)
wenzelm@23152
   426
wenzelm@23152
   427
fun fix_pat thy t = 
wenzelm@23152
   428
  let
wenzelm@23152
   429
    val T = fastype_of t;
wenzelm@23152
   430
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
wenzelm@23152
   431
    val cname = case chead_of t of Const(c,_) => c | _ =>
wenzelm@23152
   432
              fixrec_err "function is not declared as constant in theory";
huffman@35769
   433
    val unfold_thm = PureThy.get_thm thy (cname^".unfold");
wenzelm@23152
   434
    val simp = Goal.prove_global thy [] [] eq
wenzelm@32149
   435
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (global_simpset_of thy) 1]);
wenzelm@23152
   436
  in simp end;
wenzelm@23152
   437
wenzelm@23152
   438
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
wenzelm@23152
   439
  let
wenzelm@36865
   440
    val _ = legacy_feature "Old 'fixpat' command -- use \"fixrec_simp\" method instead";
wenzelm@23152
   441
    val atts = map (prep_attrib thy) srcs;
wenzelm@23152
   442
    val ts = map (prep_term thy) strings;
wenzelm@23152
   443
    val simps = map (fix_pat thy) ts;
wenzelm@23152
   444
  in
haftmann@29585
   445
    (snd o PureThy.add_thmss [((name, simps), atts)]) thy
wenzelm@23152
   446
  end;
wenzelm@23152
   447
wenzelm@30485
   448
val add_fixpat = gen_add_fixpat Sign.cert_term (K I);
wenzelm@30485
   449
val add_fixpat_cmd = gen_add_fixpat Syntax.read_term_global Attrib.attribute;
wenzelm@23152
   450
wenzelm@23152
   451
wenzelm@23152
   452
(*************************************************************************)
wenzelm@23152
   453
(******************************** Parsers ********************************)
wenzelm@23152
   454
(*************************************************************************)
wenzelm@23152
   455
wenzelm@36960
   456
val _ =
wenzelm@36960
   457
  Outer_Syntax.local_theory "fixrec" "define recursive functions (HOLCF)" Keyword.thy_decl
wenzelm@36960
   458
    ((Parse.opt_keyword "permissive" >> not) -- Parse.fixes -- Parse_Spec.where_alt_specs
wenzelm@36960
   459
      >> (fn ((strict, fixes), specs) => add_fixrec_cmd strict fixes specs));
wenzelm@23152
   460
wenzelm@36960
   461
val _ =
wenzelm@36960
   462
  Outer_Syntax.command "fixpat" "define rewrites for fixrec functions" Keyword.thy_decl
wenzelm@36960
   463
    (Parse_Spec.specs >> (Toplevel.theory o add_fixpat_cmd));
wenzelm@23152
   464
huffman@33425
   465
val setup =
wenzelm@33522
   466
  Attrib.setup @{binding fixrec_simp}
huffman@33427
   467
                     (Attrib.add_del fixrec_simp_add fixrec_simp_del)
huffman@33425
   468
                     "declaration of fixrec simp rule"
huffman@33425
   469
  #> Method.setup @{binding fixrec_simp}
huffman@33425
   470
                     (Scan.succeed (SIMPLE_METHOD' o fixrec_simp_tac))
huffman@33425
   471
                     "pattern prover for fixrec constants";
huffman@30131
   472
wenzelm@24867
   473
end;