src/HOLCF/fixrec_package.ML
author aspinall
Wed, 13 Jul 2005 20:07:01 +0200
changeset 16821 ba1f6aba44ed
parent 16628 286e70f0d809
child 17057 0934ac31985f
permissions -rw-r--r--
Update PGIP packet handling, fixing unique session identifier.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/fixrec_package.ML
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     2
    ID:         $Id$
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     3
    Author:     Amber Telfer and Brian Huffman
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     4
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     5
Recursive function definition package for HOLCF.
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     6
*)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     7
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     8
signature FIXREC_PACKAGE =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     9
sig
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    10
  val add_fixrec: bool -> ((string * Attrib.src list) * string) list list
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    11
    -> theory -> theory
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    12
  val add_fixrec_i: bool -> ((string * theory attribute list) * term) list list
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    13
    -> theory -> theory
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    14
  val add_fixpat: (string * Attrib.src list) * string list
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    15
    -> theory -> theory
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    16
  val add_fixpat_i: (string * theory attribute list) * term list
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    17
    -> theory -> theory
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    18
end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    19
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    20
structure FixrecPackage: FIXREC_PACKAGE =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    21
struct
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    22
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    23
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
    24
fun fixrec_eq_err thy s eq =
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
    25
  fixrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term thy eq));
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    26
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    27
(* ->> is taken from holcf_logic.ML *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    28
(* TODO: fix dependencies so we can import HOLCFLogic here *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    29
infixr 6 ->>;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    30
fun S ->> T = Type ("Cfun.->",[S,T]);
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    31
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    32
(* extern_name is taken from domain/library.ML *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    33
fun extern_name con = case Symbol.explode con of 
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    34
		   ("o"::"p"::" "::rest) => implode rest
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    35
		   | _ => con;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    36
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    37
val mk_trp = HOLogic.mk_Trueprop;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    38
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    39
(* splits a cterm into the right and lefthand sides of equality *)
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    40
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    41
(*
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    42
fun dest_eqs (Const ("==", _)$lhs$rhs) = (lhs, rhs)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    43
  | dest_eqs (Const ("Trueprop", _)$(Const ("op =", _)$lhs$rhs)) = (lhs,rhs)
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    44
  | dest_eqs t = fixrec_err (Sign.string_of_term (sign_of (the_context())) t);
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    45
*)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    46
(* similar to Thm.head_of, but for continuous application *)
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    47
fun chead_of (Const("Cfun.Rep_CFun",_)$f$t) = chead_of f
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    48
  | chead_of u = u;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    49
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    50
(* these are helpful functions copied from HOLCF/domain/library.ML *)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    51
fun %: s = Free(s,dummyT);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    52
fun %%: s = Const(s,dummyT);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    53
infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    54
infix 1 ===; fun S === T = %%:"op =" $ S $ T;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    55
infix 9 `  ; fun f ` x = %%:"Cfun.Rep_CFun" $ f $ x;
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    56
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    57
(* infers the type of a term *)
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    58
(* similar to Theory.inferT_axm, but allows any type, not just propT *)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    59
fun infer sg t =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    60
  fst (Sign.infer_types (Sign.pp sg) sg (K NONE) (K NONE) [] true ([t],dummyT));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    61
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    62
(* Similar to Term.lambda, but also allows abstraction over constants *)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    63
fun lambda' (v as Free (x, T)) t = Abs (x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    64
  | lambda' (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    65
  | lambda' (v as Const (x, T)) t = Abs (Sign.base_name x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    66
  | lambda' v t = raise TERM ("lambda'", [v, t]);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    67
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    68
(* builds the expression (LAM v. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    69
fun big_lambda v rhs = %%:"Cfun.Abs_CFun"$(lambda' v rhs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    70
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    71
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    72
fun big_lambdas [] rhs = rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    73
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    74
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    75
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    76
fun lambda_ctuple [] rhs = big_lambda (%:"unit") rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    77
  | lambda_ctuple (v::[]) rhs = big_lambda v rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    78
  | lambda_ctuple (v::vs) rhs =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    79
      %%:"Cprod.csplit"`(big_lambda v (lambda_ctuple vs rhs));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    80
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    81
(* builds the expression <v1,v2,..,vn> *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    82
fun mk_ctuple [] = %%:"UU"
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    83
|   mk_ctuple (t::[]) = t
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    84
|   mk_ctuple (t::ts) = %%:"Cprod.cpair"`t`(mk_ctuple ts);
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    85
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    86
(*************************************************************************)
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    87
(************* fixed-point definitions and unfolding theorems ************)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    88
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    89
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    90
fun add_fixdefs eqs thy =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    91
  let
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    92
    val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    93
    val fixpoint = %%:"Fix.fix"`lambda_ctuple lhss (mk_ctuple rhss);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    94
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    95
    fun one_def (l as Const(n,T)) r =
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    96
          let val b = Sign.base_name n in (b, (b^"_def", l == r)) end
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
    97
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    98
    fun defs [] _ = []
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    99
      | defs (l::[]) r = [one_def l r]
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   100
      | defs (l::ls) r = one_def l (%%:"Cprod.cfst"`r) :: defs ls (%%:"Cprod.csnd"`r);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   101
    val (names, pre_fixdefs) = ListPair.unzip (defs lhss fixpoint);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   102
    
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   103
    val fixdefs = map (inferT_axm thy) pre_fixdefs;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   104
    val (thy', fixdef_thms) =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   105
      PureThy.add_defs_i false (map Thm.no_attributes fixdefs) thy;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   106
    val ctuple_fixdef_thm = foldr1 (fn (x,y) => cpair_equalI OF [x,y]) fixdef_thms;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   107
    
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   108
    fun mk_cterm t = cterm_of thy' (infer thy' t);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   109
    val ctuple_unfold_ct = mk_cterm (mk_trp (mk_ctuple lhss === mk_ctuple rhss));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   110
    val ctuple_unfold_thm = prove_goalw_cterm [] ctuple_unfold_ct
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   111
          (fn _ => [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1,
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   112
                    simp_tac (simpset_of thy') 1]);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   113
    val ctuple_induct_thm =
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   114
          (space_implode "_" names ^ "_induct", ctuple_fixdef_thm RS def_fix_ind);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   115
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   116
    fun unfolds [] thm = []
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   117
      | unfolds (n::[]) thm = [(n^"_unfold", thm)]
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   118
      | unfolds (n::ns) thm = let
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   119
          val thmL = thm RS cpair_eqD1;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   120
          val thmR = thm RS cpair_eqD2;
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   121
        in (n^"_unfold", thmL) :: unfolds ns thmR end;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   122
    val unfold_thms = unfolds names ctuple_unfold_thm;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   123
    val thms = ctuple_induct_thm :: unfold_thms;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   124
    val (thy'', _) = PureThy.add_thms (map Thm.no_attributes thms) thy';
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   125
  in
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   126
    (thy'', names, fixdef_thms, map snd unfold_thms)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   127
  end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   128
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   129
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   130
(*********** monadic notation and pattern matching compilation ***********)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   131
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   132
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   133
fun add_names (Const(a,_), bs) = Sign.base_name a ins_string bs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   134
  | add_names (Free(a,_) , bs) = a ins_string bs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   135
  | add_names (f $ u     , bs) = add_names (f, add_names(u, bs))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   136
  | add_names (Abs(a,_,t), bs) = add_names (t, a ins_string bs)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   137
  | add_names (_         , bs) = bs;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   138
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   139
fun add_terms ts xs = foldr add_names xs ts;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   140
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   141
(* builds a monadic term for matching a constructor pattern *)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   142
fun pre_build pat rhs vs taken =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   143
  case pat of
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   144
    Const("Cfun.Rep_CFun",_)$f$(v as Free(n,T)) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   145
      pre_build f rhs (v::vs) taken
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   146
  | Const("Cfun.Rep_CFun",_)$f$x =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   147
      let val (rhs', v, taken') = pre_build x rhs [] taken;
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   148
      in pre_build f rhs' (v::vs) taken' end
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   149
  | Const(c,T) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   150
      let
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   151
        val n = variant taken "v";
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   152
        fun result_type (Type("Cfun.->",[_,T])) (x::xs) = result_type T xs
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   153
          | result_type T _ = T;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   154
        val v = Free(n, result_type T vs);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   155
        val m = "match_"^(extern_name(Sign.base_name c));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   156
        val k = lambda_ctuple vs rhs;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   157
      in
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   158
        (%%:"Fixrec.bind"`(%%:m`v)`k, v, n::taken)
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   159
      end
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   160
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   161
  | _ => fixrec_err "pre_build: invalid pattern";
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   162
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   163
(* builds a monadic term for matching a function definition pattern *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   164
(* returns (name, arity, matcher) *)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   165
fun building pat rhs vs taken =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   166
  case pat of
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   167
    Const("Cfun.Rep_CFun", _)$f$(v as Free(n,T)) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   168
      building f rhs (v::vs) taken
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   169
  | Const("Cfun.Rep_CFun", _)$f$x =>
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   170
      let val (rhs', v, taken') = pre_build x rhs [] taken;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   171
      in building f rhs' (v::vs) taken' end
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   172
  | Const(name,_) => (name, length vs, big_lambdas vs rhs)
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   173
  | _ => fixrec_err "function is not declared as constant in theory";
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   174
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   175
fun match_eq eq = 
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   176
  let val (lhs,rhs) = dest_eqs eq;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   177
  in building lhs (%%:"Fixrec.return"`rhs) [] (add_terms [eq] []) end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   178
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   179
(* returns the sum (using +++) of the terms in ms *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   180
(* also applies "run" to the result! *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   181
fun fatbar arity ms =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   182
  let
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   183
    fun unLAM 0 t = t
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   184
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   185
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   186
    fun reLAM 0 t = t
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   187
      | reLAM n t = reLAM (n-1) (%%:"Cfun.Abs_CFun" $ Abs("",dummyT,t));
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   188
    fun mplus (x,y) = %%:"Fixrec.mplus"`x`y;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   189
    val msum = foldr1 mplus (map (unLAM arity) ms);
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   190
  in
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   191
    reLAM arity (%%:"Fixrec.run"`msum)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   192
  end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   193
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   194
fun unzip3 [] = ([],[],[])
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   195
  | unzip3 ((x,y,z)::ts) =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   196
      let val (xs,ys,zs) = unzip3 ts
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   197
      in (x::xs, y::ys, z::zs) end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   198
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   199
(* this is the pattern-matching compiler function *)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   200
fun compile_pats eqs = 
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   201
  let
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   202
    val ((n::names),(a::arities),mats) = unzip3 (map match_eq eqs);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   203
    val cname = if forall (fn x => n=x) names then n
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   204
          else fixrec_err "all equations in block must define the same function";
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   205
    val arity = if forall (fn x => a=x) arities then a
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   206
          else fixrec_err "all equations in block must have the same arity";
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   207
    val rhs = fatbar arity mats;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   208
  in
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   209
    mk_trp (%%:cname === rhs)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   210
  end;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   211
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   212
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   213
(********************** Proving associated theorems **********************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   214
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   215
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   216
(* proves a block of pattern matching equations as theorems, using unfold *)
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   217
fun make_simps thy (unfold_thm, eqns) =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   218
  let
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   219
    fun tacsf prems =
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   220
      [rtac (unfold_thm RS ssubst_lhs) 1, simp_tac (simpset_of thy addsimps prems) 1];
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   221
    fun prove_term t = prove_goalw_cterm [] (cterm_of thy t) tacsf;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   222
    fun prove_eqn ((name, eqn_t), atts) = ((name, prove_term eqn_t), atts);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   223
  in
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   224
    map prove_eqn eqns
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   225
  end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   226
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   227
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   228
(************************* Main fixrec function **************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   229
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   230
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   231
fun gen_add_fixrec prep_prop prep_attrib strict blocks thy =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   232
  let
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   233
    val eqns = List.concat blocks;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   234
    val lengths = map length blocks;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   235
    
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   236
    val ((names, srcss), strings) = apfst split_list (split_list eqns);
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   237
    val atts = map (map (prep_attrib thy)) srcss;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   238
    val eqn_ts = map (prep_prop thy) strings;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   239
    val rec_ts = map (fn eq => chead_of (fst (dest_eqs (Logic.strip_imp_concl eq)))
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   240
      handle TERM _ => fixrec_eq_err thy "not a proper equation" eq) eqn_ts;
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   241
    val (_, eqn_ts') = InductivePackage.unify_consts thy rec_ts eqn_ts;
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   242
    
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   243
    fun unconcat [] _ = []
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   244
      | unconcat (n::ns) xs = List.take (xs,n) :: unconcat ns (List.drop (xs,n));
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   245
    val pattern_blocks = unconcat lengths (map Logic.strip_imp_concl eqn_ts');
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   246
    val compiled_ts = map (infer thy o compile_pats) pattern_blocks;
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   247
    val (thy', cnames, fixdef_thms, unfold_thms) = add_fixdefs compiled_ts thy;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   248
  in
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   249
    if strict then let (* only prove simp rules if strict = true *)
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   250
      val eqn_blocks = unconcat lengths ((names ~~ eqn_ts') ~~ atts);
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   251
      val simps = List.concat (map (make_simps thy') (unfold_thms ~~ eqn_blocks));
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   252
      val (thy'', simp_thms) = PureThy.add_thms simps thy';
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   253
      
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   254
      val simp_names = map (fn name => name^"_simps") cnames;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   255
      val simp_attribute = rpair [Simplifier.simp_add_global];
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   256
      val simps' = map simp_attribute (simp_names ~~ unconcat lengths simp_thms);
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   257
    in
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   258
      (#1 o PureThy.add_thmss simps') thy''
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   259
    end
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   260
    else thy'
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   261
  end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   262
16488
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   263
val add_fixrec = gen_add_fixrec Sign.read_prop Attrib.global_attribute;
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   264
val add_fixrec_i = gen_add_fixrec Sign.cert_prop (K I);
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   265
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   266
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   267
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   268
(******************************** Fixpat *********************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   269
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   270
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   271
fun fix_pat thy t = 
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   272
  let
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   273
    val T = fastype_of t;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   274
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   275
    val cname = case chead_of t of Const(c,_) => c | _ =>
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   276
              fixrec_err "function is not declared as constant in theory";
16488
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   277
    val unfold_thm = PureThy.get_thm thy (Name (cname^"_unfold"));
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   278
    val simp = prove_goalw_cterm [] (cterm_of thy eq)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   279
          (fn _ => [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   280
  in simp end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   281
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   282
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   283
  let
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   284
    val atts = map (prep_attrib thy) srcs;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   285
    val ts = map (prep_term thy) strings;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   286
    val simps = map (fix_pat thy) ts;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   287
  in
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   288
    (#1 o PureThy.add_thmss [((name, simps), atts)]) thy
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   289
  end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   290
16488
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   291
val add_fixpat = gen_add_fixpat Sign.read_term Attrib.global_attribute;
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   292
val add_fixpat_i = gen_add_fixpat Sign.cert_term (K I);
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   293
38bc902946b2 added add_fixrec_i, add_fixpat_i;
wenzelm
parents: 16463
diff changeset
   294
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   295
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   296
(******************************** Parsers ********************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   297
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   298
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   299
local structure P = OuterParse and K = OuterSyntax.Keyword in
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   300
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   301
val fixrec_eqn = P.opt_thm_name ":" -- P.prop;
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   302
16628
286e70f0d809 remove uses of sign_of
huffman
parents: 16552
diff changeset
   303
val fixrec_strict = P.opt_keyword "permissive" >> not;
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   304
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   305
val fixrec_decl = fixrec_strict -- P.and_list1 (Scan.repeat1 fixrec_eqn);
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   306
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   307
(* this builds a parser for a new keyword, fixrec, whose functionality 
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   308
is defined by add_fixrec *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   309
val fixrecP =
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   310
  OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   311
    (fixrec_decl >> (Toplevel.theory o uncurry add_fixrec));
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   312
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   313
(* fixpat parser *)
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   314
val fixpat_decl = P.opt_thm_name ":" -- Scan.repeat1 P.prop;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   315
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   316
val fixpatP =
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   317
  OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   318
    (fixpat_decl >> (Toplevel.theory o add_fixpat));
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   319
16552
0774e9bcdb6c New features:
huffman
parents: 16488
diff changeset
   320
val _ = OuterSyntax.add_parsers [fixrecP, fixpatP];
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   321
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   322
end; (* local structure *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   323
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   324
end; (* struct *)