datatype Algebra (f:t@ype -> t@ype,a:t@ype) = Algebra of (f a)
datatype Fix (f:t@ype -> t@ype) = Iso of @{ invIso = f (Fix f) }
fun cata {f:t@ype -> t@ype}{a:t@ype} (alg:Algebra (f,a)): (Fix f -> a) = lam (f) => alg (map (cata alg) (f.invIso))
typedef Algebra (f:t@ype -> t@ype,a:t@ype) = f a -> a
(* ****** ****** *)
//
infixr (->) ->>
//
typedef
->> (a:type, b:type) = a -<cloref1> b
//
(* ****** ****** *)
sortdef
ftype = type -> type
typedef
Algebra (f:ftype, a:type) = f(a) ->> a
(* ****** ****** *)
//
extern
fun Map{f:ftype}{a,b:type} (a ->> b): f a ->> f b
//
(* ****** ****** *)
(*
datatype
Fix (f:type -> type) = Iso of @{ invIso = f (Fix f) }
*)
abstype
Fix(f:type -> type)
extern fun Fix_unfold{f:ftype} : Fix(f) -> f (Fix(f))
(* ****** ****** *)
//
fun
cata{f:ftype}{a:type}
(alg: Algebra (f,a)): (Fix f ->> a) =
lam (f: Fix(f)) => alg(Map(cata(alg))(Fix_unfold(f)))
//
(* ****** ****** *)
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/28a6f4f6-a0ee-4065-a5cb-b3bfa34a36c3%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "ats-lang-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ats-lang-users/tfEnAR2vIOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
sortdef ftype = t@ype -> t@ype
typedef Functor (f:ftype) = {a,b:t@ype} (a -> b) -> f a -> f b
typedef Algebra (f:ftype, a:t@ype) = f a -> a
datatype Fix (f:ftype) = Fx of f (Fix f)
extern fun fix_unfold {f:ftype} : Fix f -> f (Fix f)
implement fix_unfold f = let val+ Fx f = f in f end
extern fun cata : {f:ftype}{a:t@ype} Functor f -> Algebra (f, a) -> (Fix f -> a)
implement cata map = lam alg => lam f => alg (map (cata map alg) (fix_unfold f))
datatype Fexpr (a:t@ype)
= Int of int
| Add of (a, a)
| Mul of (a, a)
typedef Expr = Fix Fexpr
extern val fexpr_map : Functor Fexpr
implement fexpr_map f = lam e0 => ( case+ e0 of
| Int i => Int i
| Add (a, b) => Add (f a, f b)
| Mul (a, b) => Mul (f a, f b))
extern fun fexpr_eval : Fexpr int -> int
implement fexpr_eval e0 = ( case+ e0 of
| Int i => i
| Add (a, b) => a + b
| Mul (a, b) => a * b)
val I1 = Fx (Int 1): Expr
val I2 = Fx (Int 2): Expr
val I3 = Fx (Int 3): Expr
val Mul_2_3 = Fx (Mul (I2, I3)): Expr
val Add_1_Mul_2_3 = Fx (Add (I1, Mul_2_3)): Expr
val Ans = cata fexpr_map fexpr_eval Add_1_Mul_2_3
val () = println! ("eval(1+2*3) = ", Ans)
implement main0 () = ()
extern fun fix_unfold {f:ftype} : Fix f -> f (Fix f)
implement fix_unfold (Fx f) = f
extern fun fexpr_eval : Algebra (Fexpr, int)
sortdef ftype = t@ype -> t@ype
typedef Functor (f:ftype) = {a,b:t@ype} (a -> b) -> f a -> f b
typedef Algebra (f:ftype, a:t@ype) = f a -> a
datatype Fix (f:ftype) = Fx of f (Fix f)
extern fun fix_unfold {f:ftype} : Fix f -> f (Fix f)
implement fix_unfold (Fx f) = f
extern fun cata : {f:ftype} {a:t@ype} Functor f -> Algebra (f, a) -> (Fix f -> a)
implement cata map = lam alg => lam f => alg (map (cata map alg) (fix_unfold f))
datatype Fexpr (a:t@ype)
= Int of int
| Add of (a, a)
| Mul of (a, a)
typedef Expr = Fix Fexpr
extern val fexpr_map : Functor Fexpr
implement fexpr_map f = lam e0 => ( case+ e0 of
| Int i => Int i
| Add (a, b) => Add (f a, f b)
| Mul (a, b) => Mul (f a, f b))
extern fun fexpr_eval : Algebra (Fexpr, int)
implement fexpr_eval e0 = ( case+ e0 of
| Int i => i
| Add (a, b) => a + b
| Mul (a, b) => a * b)
val I1 = Fx (Int 1) : Expr
val I2 = Fx (Int 2) : Expr
val I3 = Fx (Int 3) : Expr
val Mul_2_3 = Fx (Mul (I2, I3)) : Expr
val Add_1_Mul_2_3 = Fx (Add (I1, Mul_2_3)) : Expr
val Ans = cata fexpr_map fexpr_eval Add_1_Mul_2_3
val () = println! ("eval(1+2*3) = ", Ans)
implement main0 () = ()
--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/a59e0bc4-6148-4e0f-bab8-b2a7b2104bc5%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLqU6hSBn9mmjRpoy%3DXA1rZq8YMvzWYkMnfB7uuSu73AAA%40mail.gmail.com.