src/HOLCF/Tools/fixrec_package.ML
author huffman
Thu Feb 26 11:17:38 2009 -0800 (2009-02-26)
changeset 30132 243a05a67c41
parent 30131 6be1be402ef0
child 30157 40919ebde2c9
permissions -rw-r--r--
avoid using legacy type inference
wenzelm@23152
     1
(*  Title:      HOLCF/Tools/fixrec_package.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
wenzelm@23152
     7
signature FIXREC_PACKAGE =
wenzelm@23152
     8
sig
wenzelm@23152
     9
  val legacy_infer_term: theory -> term -> term
wenzelm@23152
    10
  val legacy_infer_prop: theory -> term -> term
huffman@30132
    11
wenzelm@28084
    12
  val add_fixrec: bool -> (Attrib.binding * string) list list -> theory -> theory
haftmann@29581
    13
  val add_fixrec_i: bool -> ((binding * attribute list) * term) list list -> theory -> theory
wenzelm@28084
    14
  val add_fixpat: Attrib.binding * string list -> theory -> theory
haftmann@29581
    15
  val add_fixpat_i: (binding * attribute list) * term list -> theory -> theory
huffman@30131
    16
  val add_matchers: (string * string) list -> theory -> theory
huffman@30131
    17
  val setup: theory -> theory
wenzelm@23152
    18
end;
wenzelm@23152
    19
wenzelm@23152
    20
structure FixrecPackage: FIXREC_PACKAGE =
wenzelm@23152
    21
struct
wenzelm@23152
    22
wenzelm@23152
    23
(* legacy type inference *)
huffman@30132
    24
(* used by the domain package *)
wenzelm@23152
    25
fun legacy_infer_term thy t =
wenzelm@24493
    26
  singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
wenzelm@23152
    27
wenzelm@24680
    28
fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
wenzelm@23152
    29
wenzelm@23152
    30
huffman@26045
    31
val fix_eq2 = @{thm fix_eq2};
huffman@26045
    32
val def_fix_ind = @{thm def_fix_ind};
wenzelm@23152
    33
wenzelm@23152
    34
wenzelm@23152
    35
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
wenzelm@23152
    36
fun fixrec_eq_err thy s eq =
wenzelm@26939
    37
  fixrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
wenzelm@23152
    38
huffman@30132
    39
(*************************************************************************)
huffman@30132
    40
(***************************** building types ****************************)
huffman@30132
    41
(*************************************************************************)
huffman@30132
    42
wenzelm@23152
    43
(* ->> is taken from holcf_logic.ML *)
huffman@30132
    44
fun cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
huffman@30132
    45
huffman@30132
    46
infixr 6 ->>; val (op ->>) = cfunT;
huffman@30132
    47
huffman@30132
    48
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U)
huffman@30132
    49
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
huffman@30132
    50
huffman@30132
    51
fun binder_cfun (Type(@{type_name "->"},[T, U])) = T :: binder_cfun U
huffman@30132
    52
  | binder_cfun _   =  [];
huffman@30132
    53
huffman@30132
    54
fun body_cfun (Type(@{type_name "->"},[T, U])) = body_cfun U
huffman@30132
    55
  | body_cfun T   =  T;
wenzelm@23152
    56
huffman@30132
    57
fun strip_cfun T : typ list * typ =
huffman@30132
    58
  (binder_cfun T, body_cfun T);
huffman@30132
    59
huffman@30132
    60
fun maybeT T = Type(@{type_name "maybe"}, [T]);
huffman@30132
    61
huffman@30132
    62
fun dest_maybeT (Type(@{type_name "maybe"}, [T])) = T
huffman@30132
    63
  | dest_maybeT T = raise TYPE ("dest_maybeT", [T], []);
huffman@30132
    64
huffman@30132
    65
fun tupleT [] = @{typ "unit"}
huffman@30132
    66
  | tupleT [T] = T
huffman@30132
    67
  | tupleT (T :: Ts) = HOLogic.mk_prodT (T, tupleT Ts);
huffman@30132
    68
huffman@30132
    69
fun matchT T = body_cfun T ->> maybeT (tupleT (binder_cfun T));
huffman@30132
    70
huffman@30132
    71
(*************************************************************************)
huffman@30132
    72
(***************************** building terms ****************************)
huffman@30132
    73
(*************************************************************************)
wenzelm@23152
    74
wenzelm@23152
    75
val mk_trp = HOLogic.mk_Trueprop;
wenzelm@23152
    76
wenzelm@23152
    77
(* splits a cterm into the right and lefthand sides of equality *)
wenzelm@23152
    78
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
wenzelm@23152
    79
wenzelm@23152
    80
(* similar to Thm.head_of, but for continuous application *)
huffman@26045
    81
fun chead_of (Const(@{const_name Rep_CFun},_)$f$t) = chead_of f
wenzelm@23152
    82
  | chead_of u = u;
wenzelm@23152
    83
huffman@30132
    84
fun capply_const (S, T) =
huffman@30132
    85
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
huffman@30132
    86
huffman@30132
    87
fun cabs_const (S, T) =
huffman@30132
    88
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
huffman@30132
    89
huffman@30132
    90
fun mk_capply (t, u) =
huffman@30132
    91
  let val (S, T) =
huffman@30132
    92
    case Term.fastype_of t of
huffman@30132
    93
        Type(@{type_name "->"}, [S, T]) => (S, T)
huffman@30132
    94
      | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]);
huffman@30132
    95
  in capply_const (S, T) $ t $ u end;
huffman@30132
    96
huffman@30132
    97
infix 0 ==;  val (op ==) = Logic.mk_equals;
huffman@30132
    98
infix 1 ===; val (op ===) = HOLogic.mk_eq;
huffman@30132
    99
infix 9 `  ; val (op `) = mk_capply;
huffman@30132
   100
huffman@30132
   101
huffman@30132
   102
fun mk_cpair (t, u) =
huffman@30132
   103
  let val T = Term.fastype_of t
huffman@30132
   104
      val U = Term.fastype_of u
huffman@30132
   105
      val cpairT = T ->> U ->> HOLogic.mk_prodT (T, U)
huffman@30132
   106
  in Const(@{const_name cpair}, cpairT) ` t ` u end;
huffman@30132
   107
huffman@30132
   108
fun mk_cfst t =
huffman@30132
   109
  let val T = Term.fastype_of t;
huffman@30132
   110
      val (U, _) = HOLogic.dest_prodT T;
huffman@30132
   111
  in Const(@{const_name cfst}, T ->> U) ` t end;
huffman@30132
   112
huffman@30132
   113
fun mk_csnd t =
huffman@30132
   114
  let val T = Term.fastype_of t;
huffman@30132
   115
      val (_, U) = HOLogic.dest_prodT T;
huffman@30132
   116
  in Const(@{const_name csnd}, T ->> U) ` t end;
huffman@30132
   117
huffman@30132
   118
fun mk_csplit t =
huffman@30132
   119
  let val (S, TU) = dest_cfunT (Term.fastype_of t);
huffman@30132
   120
      val (T, U) = dest_cfunT TU;
huffman@30132
   121
      val csplitT = (S ->> T ->> U) ->> HOLogic.mk_prodT (S, T) ->> U;
huffman@30132
   122
  in Const(@{const_name csplit}, csplitT) ` t end;
wenzelm@23152
   123
wenzelm@23152
   124
(* builds the expression (LAM v. rhs) *)
huffman@30132
   125
fun big_lambda v rhs =
huffman@30132
   126
  cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs;
wenzelm@23152
   127
wenzelm@23152
   128
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
wenzelm@23152
   129
fun big_lambdas [] rhs = rhs
wenzelm@23152
   130
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
wenzelm@23152
   131
wenzelm@23152
   132
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *)
huffman@30132
   133
fun lambda_ctuple [] rhs = big_lambda (Free("unit", HOLogic.unitT)) rhs
wenzelm@23152
   134
  | lambda_ctuple (v::[]) rhs = big_lambda v rhs
wenzelm@23152
   135
  | lambda_ctuple (v::vs) rhs =
huffman@30132
   136
      mk_csplit (big_lambda v (lambda_ctuple vs rhs));
wenzelm@23152
   137
wenzelm@23152
   138
(* builds the expression <v1,v2,..,vn> *)
huffman@30132
   139
fun mk_ctuple [] = @{term "UU::unit"}
wenzelm@23152
   140
|   mk_ctuple (t::[]) = t
huffman@30132
   141
|   mk_ctuple (t::ts) = mk_cpair (t, mk_ctuple ts);
huffman@30132
   142
huffman@30132
   143
fun mk_return t =
huffman@30132
   144
  let val T = Term.fastype_of t
huffman@30132
   145
  in Const(@{const_name Fixrec.return}, T ->> maybeT T) ` t end;
huffman@30132
   146
huffman@30132
   147
fun mk_bind (t, u) =
huffman@30132
   148
  let val (T, mU) = dest_cfunT (Term.fastype_of u);
huffman@30132
   149
      val bindT = maybeT T ->> (T ->> mU) ->> mU;
huffman@30132
   150
  in Const(@{const_name Fixrec.bind}, bindT) ` t ` u end;
huffman@30132
   151
huffman@30132
   152
fun mk_mplus (t, u) =
huffman@30132
   153
  let val mT = Term.fastype_of t
huffman@30132
   154
  in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end;
huffman@30132
   155
huffman@30132
   156
fun mk_run t =
huffman@30132
   157
  let val mT = Term.fastype_of t
huffman@30132
   158
      val T = dest_maybeT mT
huffman@30132
   159
  in Const(@{const_name Fixrec.run}, mT ->> T) ` t end;
huffman@30132
   160
huffman@30132
   161
fun mk_fix t =
huffman@30132
   162
  let val (T, _) = dest_cfunT (Term.fastype_of t)
huffman@30132
   163
  in Const(@{const_name fix}, (T ->> T) ->> T) ` t end;
wenzelm@23152
   164
wenzelm@23152
   165
(*************************************************************************)
wenzelm@23152
   166
(************* fixed-point definitions and unfolding theorems ************)
wenzelm@23152
   167
(*************************************************************************)
wenzelm@23152
   168
wenzelm@23152
   169
fun add_fixdefs eqs thy =
wenzelm@23152
   170
  let
wenzelm@23152
   171
    val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
huffman@30132
   172
    val fixpoint = mk_fix (lambda_ctuple lhss (mk_ctuple rhss));
wenzelm@23152
   173
    
wenzelm@23152
   174
    fun one_def (l as Const(n,T)) r =
wenzelm@23152
   175
          let val b = Sign.base_name n in (b, (b^"_def", l == r)) end
wenzelm@23152
   176
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
wenzelm@23152
   177
    fun defs [] _ = []
wenzelm@23152
   178
      | defs (l::[]) r = [one_def l r]
huffman@30132
   179
      | defs (l::ls) r = one_def l (mk_cfst r) :: defs ls (mk_csnd r);
huffman@30132
   180
    val (names, fixdefs) = ListPair.unzip (defs lhss fixpoint);
wenzelm@23152
   181
    
wenzelm@23152
   182
    val (fixdef_thms, thy') =
haftmann@29585
   183
      PureThy.add_defs false (map (Thm.no_attributes o apfst Binding.name) fixdefs) thy;
wenzelm@25132
   184
    val ctuple_fixdef_thm = foldr1 (fn (x,y) => @{thm cpair_equalI} OF [x,y]) fixdef_thms;
wenzelm@23152
   185
    
huffman@30132
   186
    val ctuple_unfold = mk_trp (mk_ctuple lhss === mk_ctuple rhss);
wenzelm@23152
   187
    val ctuple_unfold_thm = Goal.prove_global thy' [] [] ctuple_unfold
wenzelm@23152
   188
          (fn _ => EVERY [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1,
wenzelm@23152
   189
                    simp_tac (simpset_of thy') 1]);
wenzelm@23152
   190
    val ctuple_induct_thm =
wenzelm@23152
   191
          (space_implode "_" names ^ "_induct", ctuple_fixdef_thm RS def_fix_ind);
wenzelm@23152
   192
    
wenzelm@23152
   193
    fun unfolds [] thm = []
wenzelm@23152
   194
      | unfolds (n::[]) thm = [(n^"_unfold", thm)]
wenzelm@23152
   195
      | unfolds (n::ns) thm = let
wenzelm@25132
   196
          val thmL = thm RS @{thm cpair_eqD1};
wenzelm@25132
   197
          val thmR = thm RS @{thm cpair_eqD2};
wenzelm@23152
   198
        in (n^"_unfold", thmL) :: unfolds ns thmR end;
wenzelm@23152
   199
    val unfold_thms = unfolds names ctuple_unfold_thm;
wenzelm@23152
   200
    val thms = ctuple_induct_thm :: unfold_thms;
haftmann@29585
   201
    val (_, thy'') = PureThy.add_thms (map (Thm.no_attributes o apfst Binding.name) thms) thy';
wenzelm@23152
   202
  in
wenzelm@23152
   203
    (thy'', names, fixdef_thms, map snd unfold_thms)
wenzelm@23152
   204
  end;
wenzelm@23152
   205
wenzelm@23152
   206
(*************************************************************************)
wenzelm@23152
   207
(*********** monadic notation and pattern matching compilation ***********)
wenzelm@23152
   208
(*************************************************************************)
wenzelm@23152
   209
huffman@30131
   210
structure FixrecMatchData = TheoryDataFun (
huffman@30131
   211
  type T = string Symtab.table;
huffman@30131
   212
  val empty = Symtab.empty;
huffman@30131
   213
  val copy = I;
huffman@30131
   214
  val extend = I;
huffman@30131
   215
  fun merge _ tabs : T = Symtab.merge (K true) tabs;
huffman@30131
   216
);
huffman@30131
   217
huffman@30131
   218
(* associate match functions with pattern constants *)
huffman@30131
   219
fun add_matchers ms = FixrecMatchData.map (fold Symtab.update ms);
huffman@30131
   220
wenzelm@23152
   221
fun add_names (Const(a,_), bs) = insert (op =) (Sign.base_name a) bs
wenzelm@23152
   222
  | add_names (Free(a,_) , bs) = insert (op =) a bs
wenzelm@23152
   223
  | add_names (f $ u     , bs) = add_names (f, add_names(u, bs))
wenzelm@23152
   224
  | add_names (Abs(a,_,t), bs) = add_names (t, insert (op =) a bs)
wenzelm@23152
   225
  | add_names (_         , bs) = bs;
wenzelm@23152
   226
wenzelm@23152
   227
fun add_terms ts xs = foldr add_names xs ts;
wenzelm@23152
   228
wenzelm@23152
   229
(* builds a monadic term for matching a constructor pattern *)
huffman@30131
   230
fun pre_build match_name pat rhs vs taken =
wenzelm@23152
   231
  case pat of
huffman@26045
   232
    Const(@{const_name Rep_CFun},_)$f$(v as Free(n,T)) =>
huffman@30131
   233
      pre_build match_name f rhs (v::vs) taken
huffman@26045
   234
  | Const(@{const_name Rep_CFun},_)$f$x =>
huffman@30131
   235
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
huffman@30131
   236
      in pre_build match_name f rhs' (v::vs) taken' end
wenzelm@23152
   237
  | Const(c,T) =>
wenzelm@23152
   238
      let
wenzelm@23152
   239
        val n = Name.variant taken "v";
huffman@26045
   240
        fun result_type (Type(@{type_name "->"},[_,T])) (x::xs) = result_type T xs
wenzelm@23152
   241
          | result_type T _ = T;
wenzelm@23152
   242
        val v = Free(n, result_type T vs);
huffman@30132
   243
        val m = Const(match_name c, matchT T);
wenzelm@23152
   244
        val k = lambda_ctuple vs rhs;
wenzelm@23152
   245
      in
huffman@30132
   246
        (mk_bind (m`v, k), v, n::taken)
wenzelm@23152
   247
      end
wenzelm@23152
   248
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
wenzelm@23152
   249
  | _ => fixrec_err "pre_build: invalid pattern";
wenzelm@23152
   250
wenzelm@23152
   251
(* builds a monadic term for matching a function definition pattern *)
wenzelm@23152
   252
(* returns (name, arity, matcher) *)
huffman@30131
   253
fun building match_name pat rhs vs taken =
wenzelm@23152
   254
  case pat of
huffman@26045
   255
    Const(@{const_name Rep_CFun}, _)$f$(v as Free(n,T)) =>
huffman@30131
   256
      building match_name f rhs (v::vs) taken
huffman@26045
   257
  | Const(@{const_name Rep_CFun}, _)$f$x =>
huffman@30131
   258
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
huffman@30131
   259
      in building match_name f rhs' (v::vs) taken' end
huffman@30132
   260
  | Const(name,_) => (pat, length vs, big_lambdas vs rhs)
wenzelm@23152
   261
  | _ => fixrec_err "function is not declared as constant in theory";
wenzelm@23152
   262
huffman@30131
   263
fun match_eq match_name eq = 
wenzelm@23152
   264
  let val (lhs,rhs) = dest_eqs eq;
huffman@30131
   265
  in
huffman@30132
   266
    building match_name lhs (mk_return rhs) []
huffman@30131
   267
      (add_terms [eq] [])
huffman@30131
   268
  end;
wenzelm@23152
   269
wenzelm@23152
   270
(* returns the sum (using +++) of the terms in ms *)
wenzelm@23152
   271
(* also applies "run" to the result! *)
wenzelm@23152
   272
fun fatbar arity ms =
wenzelm@23152
   273
  let
huffman@30132
   274
    fun LAM_Ts 0 t = ([], Term.fastype_of t)
huffman@30132
   275
      | LAM_Ts n (_ $ Abs(_,T,t)) =
huffman@30132
   276
          let val (Ts, U) = LAM_Ts (n-1) t in (T::Ts, U) end
huffman@30132
   277
      | LAM_Ts _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
wenzelm@23152
   278
    fun unLAM 0 t = t
wenzelm@23152
   279
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
wenzelm@23152
   280
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
huffman@30132
   281
    fun reLAM ([], U) t = t
huffman@30132
   282
      | reLAM (T::Ts, U) t = reLAM (Ts, T ->> U) (cabs_const(T,U)$Abs("",T,t));
huffman@30132
   283
    val msum = foldr1 mk_mplus (map (unLAM arity) ms);
huffman@30132
   284
    val (Ts, U) = LAM_Ts arity (hd ms)
wenzelm@23152
   285
  in
huffman@30132
   286
    reLAM (rev Ts, dest_maybeT U) (mk_run msum)
wenzelm@23152
   287
  end;
wenzelm@23152
   288
wenzelm@23152
   289
fun unzip3 [] = ([],[],[])
wenzelm@23152
   290
  | unzip3 ((x,y,z)::ts) =
wenzelm@23152
   291
      let val (xs,ys,zs) = unzip3 ts
wenzelm@23152
   292
      in (x::xs, y::ys, z::zs) end;
wenzelm@23152
   293
wenzelm@23152
   294
(* this is the pattern-matching compiler function *)
huffman@30131
   295
fun compile_pats match_name eqs = 
wenzelm@23152
   296
  let
huffman@30131
   297
    val ((n::names),(a::arities),mats) = unzip3 (map (match_eq match_name) eqs);
wenzelm@23152
   298
    val cname = if forall (fn x => n=x) names then n
wenzelm@23152
   299
          else fixrec_err "all equations in block must define the same function";
wenzelm@23152
   300
    val arity = if forall (fn x => a=x) arities then a
wenzelm@23152
   301
          else fixrec_err "all equations in block must have the same arity";
wenzelm@23152
   302
    val rhs = fatbar arity mats;
wenzelm@23152
   303
  in
huffman@30132
   304
    mk_trp (cname === rhs)
wenzelm@23152
   305
  end;
wenzelm@23152
   306
wenzelm@23152
   307
(*************************************************************************)
wenzelm@23152
   308
(********************** Proving associated theorems **********************)
wenzelm@23152
   309
(*************************************************************************)
wenzelm@23152
   310
wenzelm@23152
   311
(* proves a block of pattern matching equations as theorems, using unfold *)
wenzelm@23152
   312
fun make_simps thy (unfold_thm, eqns) =
wenzelm@23152
   313
  let
wenzelm@25132
   314
    val tacs = [rtac (unfold_thm RS @{thm ssubst_lhs}) 1, asm_simp_tac (simpset_of thy) 1];
wenzelm@23152
   315
    fun prove_term t = Goal.prove_global thy [] [] t (K (EVERY tacs));
wenzelm@23152
   316
    fun prove_eqn ((name, eqn_t), atts) = ((name, prove_term eqn_t), atts);
wenzelm@23152
   317
  in
wenzelm@23152
   318
    map prove_eqn eqns
wenzelm@23152
   319
  end;
wenzelm@23152
   320
wenzelm@23152
   321
(*************************************************************************)
wenzelm@23152
   322
(************************* Main fixrec function **************************)
wenzelm@23152
   323
(*************************************************************************)
wenzelm@23152
   324
wenzelm@23152
   325
fun gen_add_fixrec prep_prop prep_attrib strict blocks thy =
wenzelm@23152
   326
  let
wenzelm@23152
   327
    val eqns = List.concat blocks;
wenzelm@23152
   328
    val lengths = map length blocks;
wenzelm@23152
   329
    
wenzelm@28083
   330
    val ((bindings, srcss), strings) = apfst split_list (split_list eqns);
haftmann@29006
   331
    val names = map Binding.base_name bindings;
wenzelm@23152
   332
    val atts = map (map (prep_attrib thy)) srcss;
wenzelm@23152
   333
    val eqn_ts = map (prep_prop thy) strings;
wenzelm@23152
   334
    val rec_ts = map (fn eq => chead_of (fst (dest_eqs (Logic.strip_imp_concl eq)))
wenzelm@23152
   335
      handle TERM _ => fixrec_eq_err thy "not a proper equation" eq) eqn_ts;
haftmann@25557
   336
    val (_, eqn_ts') = OldPrimrecPackage.unify_consts thy rec_ts eqn_ts;
wenzelm@23152
   337
    
wenzelm@23152
   338
    fun unconcat [] _ = []
wenzelm@23152
   339
      | unconcat (n::ns) xs = List.take (xs,n) :: unconcat ns (List.drop (xs,n));
huffman@30131
   340
    val matcher_tab = FixrecMatchData.get thy;
huffman@30131
   341
    fun match_name c =
huffman@30131
   342
          case Symtab.lookup matcher_tab c of SOME m => m
huffman@30131
   343
            | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
huffman@30131
   344
wenzelm@23152
   345
    val pattern_blocks = unconcat lengths (map Logic.strip_imp_concl eqn_ts');
huffman@30131
   346
    val compiled_ts =
huffman@30132
   347
          map (compile_pats match_name) pattern_blocks;
wenzelm@23152
   348
    val (thy', cnames, fixdef_thms, unfold_thms) = add_fixdefs compiled_ts thy;
wenzelm@23152
   349
  in
wenzelm@23152
   350
    if strict then let (* only prove simp rules if strict = true *)
wenzelm@23152
   351
      val eqn_blocks = unconcat lengths ((names ~~ eqn_ts') ~~ atts);
haftmann@29585
   352
      val simps = maps (make_simps thy') (unfold_thms ~~ eqn_blocks);
haftmann@29585
   353
      val (simp_thms, thy'') = PureThy.add_thms ((map o apfst o apfst) Binding.name simps) thy';
wenzelm@23152
   354
      
wenzelm@23152
   355
      val simp_names = map (fn name => name^"_simps") cnames;
wenzelm@23152
   356
      val simp_attribute = rpair [Simplifier.simp_add];
wenzelm@23152
   357
      val simps' = map simp_attribute (simp_names ~~ unconcat lengths simp_thms);
wenzelm@23152
   358
    in
haftmann@29585
   359
      (snd o PureThy.add_thmss ((map o apfst o apfst) Binding.name simps')) thy''
wenzelm@23152
   360
    end
wenzelm@23152
   361
    else thy'
wenzelm@23152
   362
  end;
wenzelm@23152
   363
wenzelm@24707
   364
val add_fixrec = gen_add_fixrec Syntax.read_prop_global Attrib.attribute;
wenzelm@23152
   365
val add_fixrec_i = gen_add_fixrec Sign.cert_prop (K I);
wenzelm@23152
   366
wenzelm@23152
   367
wenzelm@23152
   368
(*************************************************************************)
wenzelm@23152
   369
(******************************** Fixpat *********************************)
wenzelm@23152
   370
(*************************************************************************)
wenzelm@23152
   371
wenzelm@23152
   372
fun fix_pat thy t = 
wenzelm@23152
   373
  let
wenzelm@23152
   374
    val T = fastype_of t;
wenzelm@23152
   375
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
wenzelm@23152
   376
    val cname = case chead_of t of Const(c,_) => c | _ =>
wenzelm@23152
   377
              fixrec_err "function is not declared as constant in theory";
wenzelm@26343
   378
    val unfold_thm = PureThy.get_thm thy (cname^"_unfold");
wenzelm@23152
   379
    val simp = Goal.prove_global thy [] [] eq
wenzelm@23152
   380
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
wenzelm@23152
   381
  in simp end;
wenzelm@23152
   382
wenzelm@23152
   383
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
wenzelm@23152
   384
  let
wenzelm@23152
   385
    val atts = map (prep_attrib thy) srcs;
wenzelm@23152
   386
    val ts = map (prep_term thy) strings;
wenzelm@23152
   387
    val simps = map (fix_pat thy) ts;
wenzelm@23152
   388
  in
haftmann@29585
   389
    (snd o PureThy.add_thmss [((name, simps), atts)]) thy
wenzelm@23152
   390
  end;
wenzelm@23152
   391
wenzelm@24707
   392
val add_fixpat = gen_add_fixpat Syntax.read_term_global Attrib.attribute;
wenzelm@23152
   393
val add_fixpat_i = gen_add_fixpat Sign.cert_term (K I);
wenzelm@23152
   394
wenzelm@23152
   395
wenzelm@23152
   396
(*************************************************************************)
wenzelm@23152
   397
(******************************** Parsers ********************************)
wenzelm@23152
   398
(*************************************************************************)
wenzelm@23152
   399
wenzelm@23152
   400
local structure P = OuterParse and K = OuterKeyword in
wenzelm@23152
   401
wenzelm@23152
   402
val fixrec_eqn = SpecParse.opt_thm_name ":" -- P.prop;
wenzelm@23152
   403
wenzelm@23152
   404
val fixrec_strict = P.opt_keyword "permissive" >> not;
wenzelm@23152
   405
wenzelm@23152
   406
val fixrec_decl = fixrec_strict -- P.and_list1 (Scan.repeat1 fixrec_eqn);
wenzelm@23152
   407
wenzelm@23152
   408
(* this builds a parser for a new keyword, fixrec, whose functionality 
wenzelm@23152
   409
is defined by add_fixrec *)
wenzelm@24867
   410
val _ =
wenzelm@23152
   411
  OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl
wenzelm@23152
   412
    (fixrec_decl >> (Toplevel.theory o uncurry add_fixrec));
wenzelm@23152
   413
wenzelm@23152
   414
(* fixpat parser *)
wenzelm@23152
   415
val fixpat_decl = SpecParse.opt_thm_name ":" -- Scan.repeat1 P.prop;
wenzelm@23152
   416
wenzelm@24867
   417
val _ =
wenzelm@23152
   418
  OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
wenzelm@23152
   419
    (fixpat_decl >> (Toplevel.theory o add_fixpat));
wenzelm@23152
   420
huffman@26045
   421
end; (* local structure *)
wenzelm@23152
   422
huffman@30131
   423
val setup = FixrecMatchData.init;
huffman@30131
   424
wenzelm@24867
   425
end;