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