Permalink
Browse files

switched from Wrapped[T] to scala.Proxy

  • Loading branch information...
1 parent b9ff138 commit 6854012f6d958dffed30da593524dc5c7d8f2822 @ekmett committed Dec 30, 2010
@@ -6,7 +6,7 @@ trait Alternative[F[+_]] extends Empty[F]
with ApplicativeOr[F] { module =>
override implicit def syntax[A](m: F[A]): Alternative.Syntax[F,A] = new Alternative.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Alternative {
@@ -11,7 +11,7 @@ trait Applicative[F[+_]] extends Pointed[F] { module =>
def lift4[A,B,C,D,E](a: F[A],b: F[B],c: F[C],d: F[D])(f: (A,B,C,D) => E): F[E] = f !! a !* b !* c !* d
override implicit def syntax[A](m: F[A]): Applicative.Syntax[F,A] = new Applicative.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
implicit def static1[A,B](m: F[A => B]): Applicative.Static1[F,A,B]
= new Applicative.Static1[F,A,B](module,m)
@@ -24,31 +24,31 @@ trait Applicative[F[+_]] extends Pointed[F] { module =>
object Applicative {
trait Syntax[F[+_],+A] extends Pointed.Syntax[F,A]
with HasCompanion[Applicative[F]] { m =>
- def <**>[B](n: F[A => B]): F[B] = F.ap(m)(n)
- def ~>[B](n: F[B]): F[B] = F.lift2(m,n)((_,b) => b)
- def <~[B](n: F[B]): F[A] = F.lift2(m,n)((a,_) => a)
- def ~[B](n: F[B]): F[(A,B)] = F.lift2(m,n)((a,b) => (a,b))
+ def <**>[B](n: F[A => B]): F[B] = F.ap(self)(n)
+ def ~>[B](n: F[B]): F[B] = F.lift2(self,n)((_,b) => b)
+ def <~[B](n: F[B]): F[A] = F.lift2(self,n)((a,_) => a)
+ def ~[B](n: F[B]): F[(A,B)] = F.lift2(self,n)((a,b) => (a,b))
def forever: F[Nothing] = {
lazy val loop : F[Nothing] = ~>(loop)
loop
}
// def replicate(n: Int): F[List[A]]
}
- sealed class Static1[F[+_],-A,+B](val F:Applicative[F], val value: F[A => B])
+ sealed class Static1[F[+_],-A,+B](val F:Applicative[F], val self: F[A => B])
extends Syntax[F,A => B] {
- final def !!*(a: F[A]): F[B] = F.ap(a)(value)
- final def !*(a: F[A]): F[B] = F.ap(a)(value)
+ final def !!*(a: F[A]): F[B] = F.ap(a)(self)
+ final def !*(a: F[A]): F[B] = F.ap(a)(self)
}
- sealed class Static2[F[+_],-A,-B,+C](val F:Applicative[F], val value: F[(A,B) => C])
+ sealed class Static2[F[+_],-A,-B,+C](val F:Applicative[F], val self: F[(A,B) => C])
extends Syntax[F,(A,B) => C] {
import F._
- final def !!*(a: F[A],b: F[B]): F[C] = F(value)(_.curried) !* a !* b
- final def !*(a: F[A]): F[B => C] = F(value)(_.curried) !* a
+ final def !!*(a: F[A],b: F[B]): F[C] = F(self)(_.curried) !* a !* b
+ final def !*(a: F[A]): F[B => C] = F(self)(_.curried) !* a
}
- sealed class Static3[F[+_],-A,-B,-C,+D](val F:Applicative[F], val value: F[(A,B,C) => D])
+ sealed class Static3[F[+_],-A,-B,-C,+D](val F:Applicative[F], val self: F[(A,B,C) => D])
extends Syntax[F,(A,B,C) => D] {
import F._
- final def !!*(a: F[A],b: F[B], c: F[C]): F[D] = F(value)(f => f.curried) !* a !* b !* c
- final def !*(a: F[A]): F[(B,C) => D] = F(value)(f => (a:A) => (b:B,c:C) => f(a,b,c)) !* a
+ final def !!*(a: F[A],b: F[B], c: F[C]): F[D] = F(self)(f => f.curried) !* a !* b !* c
+ final def !*(a: F[A]): F[(B,C) => D] = F(self)(f => (a:A) => (b:B,c:C) => f(a,b,c)) !* a
}
}
@@ -11,7 +11,7 @@ trait ApplicativeOr[F[+_]] extends Applicative[F]
override implicit def syntax[A](m: F[A]): ApplicativeOr.Syntax[F,A]
= new ApplicativeOr.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
@@ -20,8 +20,8 @@ object ApplicativeOr {
extends Applicative.Syntax[F,A]
with Or.Syntax[F,A]
with HasCompanion[ApplicativeOr[F]] {
- def * : F[List[A]] = F.many(value)
- def + : F[List[A]] = F.some(value)
- def ? : F[Option[A]] = F.optional(value)
+ def * : F[List[A]] = F.many(self)
+ def + : F[List[A]] = F.some(self)
+ def ? : F[Option[A]] = F.optional(self)
}
}
@@ -7,13 +7,14 @@ trait Bind[F[+_]] extends Companion { module =>
def join[A](f: F[F[A]]): F[A] = bind(f)(identity[F[A]])
implicit def syntax[A](m: F[A]): Bind.Syntax[F,A] = new Bind.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Bind {
- trait Syntax[F[+_],+A] extends HasCompanion[Bind[F]] with Wrapped[F[A]] { m =>
- def flatMap[B](f: A => F[B]): F[B] = F.bind(m)(f)
+ trait Syntax[F[+_],+A] extends HasCompanion[Bind[F]] with Proxy { m =>
+ def self: F[A]
+ def flatMap[B](f: A => F[B]): F[B] = F.bind(self)(f)
}
}
@@ -6,13 +6,14 @@ trait ContravariantFunctor[F[-_]] extends Companion { module =>
def apply[A,B](a: F[A])(f: B => A): F[B]
implicit def syntax[A](m: F[A]): ContravariantFunctor.Syntax[F,A] = new ContravariantFunctor.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object ContravariantFunctor {
- trait Syntax[F[-_],-A] extends HasCompanion[ContravariantFunctor[F]] with Wrapped[F[A]] {
- def map[B](f: B => A): F[B] = F(value)(f)
+ trait Syntax[F[-_],-A] extends HasCompanion[ContravariantFunctor[F]] with Proxy {
+ def self: F[A]
+ def map[B](f: B => A): F[B] = F(self)(f)
}
}
@@ -5,7 +5,7 @@ sealed class EitherT[M[+_],E,+A](val run: M[Either[E,A]])(implicit val M:Monad[M
with Monad.Transformer.Syntax[({type λ[N[+_],+B] = EitherT[N,E,B]})#λ,M,A]
with HasCompanion[EitherT.monadOr[M,E]] {
import M._
- final def value = this
+ final def self = this
final val F : EitherT.monadOr[M,E] = new EitherT.monadOr[M,E](M)
final override def flatMap[B](k: A => EitherT[M,E,B]): EitherT[M,E,B] = new EitherT[M,E,B](run flatMap {
case l : Left[_,_] => M.pure(l.asInstanceOf[Either[E,B]])
@@ -7,6 +7,8 @@ trait Empty[F[+_]] extends Companion { module =>
}
object Empty {
trait Syntax[F[+_],+A] extends HasCompanion[Empty[F]]
- with Wrapped[F[A]]
+ with Proxy {
+ def self: F[A]
+ }
}
@@ -6,12 +6,13 @@ trait Filter[F[+_]] extends Companion { module =>
def filter[A](f: F[A], p: A => Boolean): F[A]
implicit def syntax[A](m: F[A]): Filter.Syntax[F,A] = new Filter.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Filter {
- trait Syntax[F[+_],+A] extends Wrapped[F[A]] with HasCompanion[Filter[F]] { m =>
- def filter(p: A => Boolean): F[A] = F.filter(m, p)
+ trait Syntax[F[+_],+A] extends Proxy with HasCompanion[Filter[F]] {
+ def self: F[A]
+ def filter(p: A => Boolean): F[A] = F.filter(self, p)
}
}
@@ -4,12 +4,13 @@ trait Functor[F[+_]] extends Companion { module =>
def apply[A,B](a: F[A])(f: A => B): F[B]
implicit def syntax[A](m: F[A]): Functor.Syntax[F,A] = new Functor.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Functor {
- trait Syntax[F[+_],+A] extends HasCompanion[Functor[F]] with Wrapped[F[A]] {
- def map[B](f: A => B): F[B] = F(value)(f)
+ trait Syntax[F[+_],+A] extends HasCompanion[Functor[F]] with Proxy {
+ def self: F[A]
+ def map[B](f: A => B): F[B] = F(self)(f)
}
}
@@ -3,20 +3,37 @@ package functorial
import scala.collection.generic
trait Monad[F[+_]] extends Applicative[F] with Bind[F] { module =>
- override def apply[A,B](a:F[A])(f: A => B): F[B] = a flatMap (a0 => pure(f(a0)))
+ override def apply[A,B](a: F[A])(f: A => B): F[B] = a flatMap (a0 => pure(f(a0)))
def ap[A,B](m: F[A])(f: F[A => B]): F[B] = f flatMap (m map)
def when(cond: Boolean, s: F[Unit]): F[Unit] = if (cond) s else unit
def unless(cond: Boolean, s: F[Unit]): F[Unit] = if (cond) unit else s
override implicit def syntax[A](m: F[A]): Monad.Syntax[F,A] = new Monad.Syntax[F,A] {
val F: Monad[F] = module
- def value: F[A] = m
+ def self = m
}
}
object Monad {
trait Syntax[F[+_],+A] extends Applicative.Syntax[F,A] with Bind.Syntax[F,A] with HasCompanion[Monad[F]]
- trait Transformer [T[_[+_],+_]] extends Companion {
+ trait Transformer [T[_[+_],+_]] extends Companion { trans =>
def lift[M[+_]:Monad,A](m : M[A]): T[M,A]
+/*
+ def get[M[+_],S](implicit M:MonadState[M,S]): T[M,S] = lift(M.get)
+ def gets[M[+_],S,A](f: S => A)(implicit M:MonadState[M,S]): T[M,A] = lift(M.gets(f))
+ def state[M[+_],S,A](f: S => (A,S))(implicit M:MonadState[M,S]): T[M,S] = lift(M.state(f))
+ def ask[M[+_],E](implicit M:MonadReader[M,E]): T[M,E] = lift(M.ask)
+ def reader[M[_],E,A](f: E => A)(implicit M:MonadReader[M,E]): T[M,A] = lift(M.reader(f))
+ def liftReader[M[+_],E](implicit M:Monad[M],R:PointedReader[M,E]): PointedReader[({type λ[+X] = T[M,X]})#λ]
+ = new PointedReader[({type λ[+X] = T[M,X]})#λ] {
+ def reader[A](f: E => A): T[M,A] = lift(M.reader(f))
+ }
+ def liftState[M[+_],S](implicit M:Monad[M],S:PointedState[M,S]): PointedState[({type λ[+X] = T[M,X]})#λ]
+ = new PointedState[({type λ[+X] = T[M,X]})#λ] {
+ def state[A](f: S => (A,S)): T[M,A] = lift(M.state(f))
+ }
+ implicit def asReader[M[+_],E](T : this.type)(implicit M:Monad[M], R:PointedReader[M,E]): PointedReader[({type λ[+X] = T[M,X]})#λ,E] = readerT[M,E](M,R)
+ implicit def asState[M[+_],S](T : this.type)(implicit M:Monad[M], S:PointedState[M,S]): PointedState[({type λ[+X] = T[M,X]})#λ,S] = readerT[M,S](M,S)
+*/
}
object Transformer {
trait Syntax[T[_[+_],+_],M[+_],+A] extends HasCompanion[Monad.Transformer[T]] {
@@ -5,7 +5,7 @@ trait MonadOr[F[+_]] extends Monad[F]
override implicit def syntax[A](m: F[A]): MonadOr.Syntax[F,A]
= new MonadOr.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
@@ -8,7 +8,7 @@ trait MonadPlus[F[+_]] extends Alternative[F] with MonadOr[F] with Filter[F] { m
override implicit def syntax[A](m: F[A]): MonadPlus.Syntax[F,A]
= new MonadPlus.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
@@ -5,7 +5,7 @@ trait MonadRaise[F[+_],E] extends MonadOr[F]
override implicit def syntax[A](m: F[A]): MonadRaise.Syntax[F,E,A]
= new MonadRaise.Syntax[F,E,A] {
val F = module
- def value = m
+ def self = m
}
}
@@ -0,0 +1,13 @@
+package functorial
+
+trait MonadReader[F[+_],E] extends Monad[F] with PointedReader[F,E] { module =>
+ override implicit def syntax[A](m: F[A]): MonadReader.Syntax[F,E,A] = new MonadReader.Syntax[F,E,A] {
+ val F = module
+ def self = m
+ }
+}
+
+object MonadReader {
+ trait Syntax[F[+_],E,+A] extends Monad.Syntax[F,A] with HasCompanion[MonadReader[F,E]]
+}
+
@@ -5,7 +5,7 @@ sealed class OptionT[M[+_],+A](val run: M[Option[A]])(implicit val M:Monad[M])
with Monad.Transformer.Syntax[({type λ[N[+_],+B] = OptionT[N,B]})#λ,M,A]
with HasCompanion[OptionT.monadPlus[M]] {
import M._
- final def value = this
+ final def self = this
final val F : OptionT.monadPlus[M] = new OptionT.monadPlus[M](M)
final override def flatMap[B](k: A => OptionT[M,B]): OptionT[M,B] = new OptionT[M,B](run flatMap {
case None => M.pure(None)
@@ -4,12 +4,13 @@ trait Or[F[+_]] extends Companion { module =>
def or[A](a: F[A], b: => F[A]): F[A]
implicit def syntax[A](m: F[A]): Or.Syntax[F,A] = new Or.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Or {
- trait Syntax[F[+_],+A] extends HasCompanion[Or[F]] with Wrapped[F[A]] { m =>
- def |[B >: A](n: => F[B]): F[B] = F.or[B](m, n)
+ trait Syntax[F[+_],+A] extends HasCompanion[Or[F]] with Proxy {
+ def self: F[A]
+ def |[B >: A](n: => F[B]): F[B] = F.or[B](self, n)
}
}
@@ -4,7 +4,7 @@ trait Pointed[F[+_]] extends Functor[F] with Pure[F] { module =>
override implicit def syntax[A](m: F[A]): Pointed.Syntax[F,A]
= new Pointed.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
@@ -6,12 +6,15 @@ trait Pure[F[+_]] extends Companion { module =>
implicit def syntax[A](m: F[A]): Pure.Syntax[F,A]
= new Pure.Syntax[F,A] {
val F = module
- def value = m
+ def self = m
}
}
object Pure {
trait Syntax[F[+_],+A] extends HasCompanion[Pure[F]]
- with Wrapped[F[A]]
+ with Proxy {
+ def self: F[A]
+
+ }
}
@@ -6,13 +6,13 @@ trait Raise[F[+_],E] extends Or[F] { module =>
def or[A](a: F[A], b: => F[A]): F[A] = handle(a)(_ => b)
override implicit def syntax[A](m: F[A]): Raise.Syntax[F,E,A] = new Raise.Syntax[F,E,A] {
val F = module
- def value = m
+ def self = m
}
}
object Raise {
trait Syntax[F[+_],E,+A] extends Or.Syntax[F,A]
with HasCompanion[Raise[F,E]] { m =>
- def handling[B>:A](b: E => F[B]): F[B] = F.handle[B](value)(b)
+ def handling[B>:A](b: E => F[B]): F[B] = F.handle[B](self)(b)
}
}
@@ -1,12 +1,12 @@
package functorial
-sealed class Reader[E,+A](val value: E => A)
+sealed class Reader[E,+A](val self: E => A)
extends Monad.Syntax[({type λ[+X] = E => X})#λ, A]
with HasCompanion[Reader.monad[E]] { m =>
- final def apply(e: E): A = value(e)
+ final def apply(e: E): A = self(e)
final val F = new Reader.monad[E]
- final override def flatMap[B](g: A => E => B): E => B = e => g(value(e))(e)
- final override def map[B](f: A => B): E => B = e => f(value(e))
+ final override def flatMap[B](g: A => E => B): E => B = e => g(self(e))(e)
+ final override def map[B](f: A => B): E => B = e => f(self(e))
}
object Reader {
@@ -17,7 +17,7 @@ object Reader {
with PointedReader[({type λ[+X] = E => X})#λ,E] {
override def pure[A](a: A): Any => A = _ => a
def bind[A,B](m: E => A)(f: A => E => B): E => B = e => f(m(e))(e)
- def reader[A](f: E => A): E => A = e => f(e)
+ def reader[A](f: E => A): E => A = f
}
}
@@ -4,7 +4,7 @@ class ReaderT[M[+_],E,+A](f: E => M[A])(implicit val M:Monad[M])
extends Monad.Syntax[({type λ[+X] = ReaderT[M,E,X]})#λ, A]
with Monad.Transformer.Syntax[({type λ[N[+_],+B] = ReaderT[N,E,B]})#λ,M,A]
with HasCompanion[ReaderT.monad[M,E]]{ m =>
- final def value = this
+ final def self = this
final def apply(e: E): M[A] = f(e)
final val F = ReaderT.monad[M,E]
final override def flatMap[B](k: A => ReaderT[M,E,B]) = new ReaderT[M,E,B](e =>
@@ -2,7 +2,7 @@ package functorial
class State[S,+A](f: S => (A,S)) extends Monad.Syntax[({type λ[+X] = State[S,X]})#λ, A] { m =>
final val F = new State.monad[S]
- final def value = this
+ final def self = this
final def apply(s: S): (A,S) = f(s)
final override def flatMap[B](g: A => State[S,B]) = new State[S,B](s => { val (a,s1) = f(s); g(a)(s1) })
final override def map[B](g: A => B) = new State[S,B](s => { val (a,s1) = f(s); (g(a),s1) })
@@ -4,7 +4,7 @@ class StateT[M[+_],S,+A](f: S => M[(A,S)])(implicit val M:Monad[M])
extends Monad.Syntax[({type λ[+X] = StateT[M,S,X]})#λ, A]
with Monad.Transformer.Syntax[({type λ[N[+_],+B] = StateT[N,S,B]})#λ,M,A]
with HasCompanion[StateT.monad[M,S]]{ m =>
- final def value = this
+ final def self = this
final def apply(s: S): M[(A,S)] = f(s)
final val F = StateT.monad[M,S]
final override def flatMap[B](k: A => StateT[M,S,B]) = new StateT[M,S,B](s =>
@@ -1,12 +0,0 @@
-package functorial
-
-import scala.collection.generic
-
-trait Wrapped[+A] {
- def value: A
-}
-
-object Wrapped {
- implicit def unwrap[A](wrapped: Wrapped[A]): A = wrapped.value
-}
-

0 comments on commit 6854012

Please sign in to comment.