Also:
extern fun fexpr_eval : Algebra (Fexpr, int)
Sorry for the triple post!
So in all:
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 () = ()On Monday, September 21, 2015 at 9:12:19 PM UTC-7, Aistis Raulinaitis wrote:
Implementing F-Algebras in ATS, wondering what I’m doing wrong or where
should I be looking? So far I have:
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))
The last line does compile, says that map is out of scope. Where would I
get it? Mind you, f is a Functor.