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