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