Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pchiusano committed Aug 17, 2011
0 parents commit 86f4b3e
Show file tree
Hide file tree
Showing 25 changed files with 434 additions and 0 deletions.
Empty file added README
Empty file.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

scalaVersion := "2.9.0-1"
8 changes: 8 additions & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
#Wed Aug 03 17:00:49 EDT 2011
project.organization=org.scalaz
project.name=scalaz
sbt.version=0.10.1
project.version=7.1
build.scala.versions=2.9.0-1
project.initialize=true
1 change: 1 addition & 0 deletions sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"
Binary file added sbt-launch.jar
Binary file not shown.
Binary file added src/main/scala/scalaz/.Syntax.scala.swp
Binary file not shown.
43 changes: 43 additions & 0 deletions src/main/scala/scalaz/Applicative.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scalaz

trait ApplicativeLike[F[_]] extends PointedLike[F] with ApplyLike[F] { self =>

// derived functions
def map[A,B](fa: F[A])(f: A => B): F[B] =
ap(fa)(pure(f))
def map2[A,B,C](fa: F[A], fb: F[B])(f: (A,B) => C): F[C] =
ap2(fa, fb)(pure(f))
def map3[A,B,C,D](fa: F[A], fb: F[B], fc: F[C])(f: (A,B,C) => D): F[D] =
map2(map2(fa, fb)((_,_)), fc)((ab,c) => f(ab._1, ab._2, c))
def map4[A,B,C,D,E](fa: F[A], fb: F[B], fc: F[C], fd: F[D])(f: (A,B,C,D) => E): F[E] =
map2(map3(fa, fb, fc)((_,_,_)), fd)((t,d) => f(t._1,t._2,t._3,d))
def map5[A,B,C,D,E,R](fa: F[A], fb: F[B], fc: F[C], fd: F[D], fe: F[E])(f: (A,B,C,D,E) => R): F[R] =
map2(map4(fa, fb, fc, fd)((_,_,_,_)), fe)((t,e) => f(t._1,t._2,t._3,t._4,e))
def lift2[A,B,C](f: (A,B) => C): (F[A],F[B]) => F[C] =
map2(_,_)(f)
def lift3[A,B,C,D](f: (A,B,C) => D): (F[A],F[B],F[C]) => F[D] =
map3(_,_,_)(f)
def lift4[A,B,C,D,E](f: (A,B,C,D) => E): (F[A],F[B],F[C],F[D]) => F[E] =
map4(_,_,_,_)(f)
def lift5[A,B,C,D,E,R](f: (A,B,C,D,E) => R): (F[A],F[B],F[C],F[D],F[E]) => F[R] =
map5(_,_,_,_,_)(f)

// impls of sequence, traverse, etc

override val syntax = new ApplicativeSyntax[F] {}
}
trait Applicative[F[_]] extends ApplicativeLike[F]
trait ApplicativeInstance[F[_]] extends Applicative[F] with ApplyInstance[F] with PointedInstance[F]

trait ApplicativeV[F[_],A] extends SyntaxV[F[A]] {
def map2[B,C](fb: F[B])(f: (A,B) => C)(implicit F: Applicative[F]) = F.map2(self,fb)(f)
def pair[B](fb: F[B])(implicit F: Applicative[F]) = F.map2(self, fb)((_,_))
}
trait ToApplicativeSyntax extends ToApplySyntax with ToPointedSyntax {
implicit def applicative[F[_],A](v: F[A]) = (new ApplicativeSyntax[F] {}).applicativeV(v)
}
trait ApplicativeSyntax[F[_]] extends ApplySyntax[F] with PointedSyntax[F] {
implicit def applicativeV[A](v: F[A]) = new ApplicativeV[F,A] { def self = v }
implicit def lift2V[A,B,C](f: (A,B) => C)(implicit F: Applicative[F]) = F.lift2(f)
implicit def lift3V[A,B,C,D](f: (A,B,C) => D)(implicit F: Applicative[F]) = F.lift3(f)
}
5 changes: 5 additions & 0 deletions src/main/scala/scalaz/ApplicativePlus.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package scalaz

trait ApplicativePlusLike[F[_]] extends ApplicativeLike[F] with PlusLike[F]
trait ApplicativePlus[F[_]] extends ApplicativeLike[F]
trait ApplicativePlusInstance[F[_]] extends ApplicativePlus[F] with PlusInstance[F] with ApplicativeInstance[F]
30 changes: 30 additions & 0 deletions src/main/scala/scalaz/Apply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scalaz

trait ApplyLike[F[_]] extends FunctorLike[F] { self =>
def ap[A,B](fa: F[A])(f: F[A => B]): F[B]

// derived functions
def ap2[A,B,C](fa: F[A], fb: F[B])(f: F[(A,B) => C]): F[C] =
ap(fb)(ap(fa)(map(f)(_.curried)))
def ap3[A,B,C,D](fa: F[A], fb: F[B], fc: F[C])(f: F[(A,B,C) => D]): F[D] =
ap(fc)(ap2(fa,fb)(map(f)(f => ((a:A,b:B) => (c:C) => f(a,b,c)))))
def ap4[A,B,C,D,E](fa: F[A], fb: F[B], fc: F[C], fd: F[D])(f: F[(A,B,C,D) => E]): F[E] =
ap(fd)(ap3(fa,fb,fc)(map(f)(f => ((a:A,b:B,c:C) => (d:D) => f(a,b,c,d)))))
def ap5[A,B,C,D,E,R](fa: F[A], fb: F[B], fc: F[C], fd: F[D], fe: F[E])(f: F[(A,B,C,D,E) => R]): F[R] =
ap(fe)(ap4(fa,fb,fc,fd)(map(f)(f => ((a:A,b:B,c:C,d:D) => (e:E) => f(a,b,c,d,e)))))

override val syntax = new ApplicativeSyntax[F] {}
}
trait Apply[F[_]] extends ApplyLike[F]
trait ApplyInstance[F[_]] extends Apply[F] with FunctorInstance[F]

trait ApplyV[F[_],A] extends SyntaxV[F[A]] {
def <*>[B](f: F[A => B])(implicit F: Apply[F]) = F.ap(self)(f)
}

trait ToApplySyntax extends ToFunctorSyntax {
implicit def apply[F[_],A](v: F[A]) = (new ApplySyntax[F] {}).applyV(v)
}
trait ApplySyntax[F[_]] extends FunctorSyntax[F] {
implicit def applyV[A](v: F[A]) = new ApplyV[F,A] { def self = v }
}
24 changes: 24 additions & 0 deletions src/main/scala/scalaz/Bind.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scalaz

trait BindLike[F[_]] extends ApplyLike[F] { self =>
def bind[A,B](fa: F[A])(f: A => F[B]): F[B]

def ap[A,B](fa: F[A])(f: F[A => B]): F[B] = bind(f)(f => map(fa)(f))
def join[A](ffa: F[F[A]]) = bind(ffa)(a => a)
}
trait Bind[F[_]] extends BindLike[F]
trait BindInstance[F[_]] extends Bind[F] with ApplicativeInstance[F]

trait BindV[F[_],A] extends SyntaxV[F[A]] {
def flatMap[B](f: A => F[B])(implicit F: Bind[F]) = F.bind(self)(f)
}
trait JoinV[F[_],A] extends SyntaxV[F[F[A]]] {
def join(implicit F: Bind[F]) = F.join(self)
}

trait ToBindSyntax extends ToApplySyntax {
implicit def bind[F[_],A](v: F[A]) = (new BindSyntax[F] {}).bindV(v)
}
trait BindSyntax[F[_]] extends ApplySyntax[F] {
implicit def bindV[A](v: F[A]) = new BindV[F,A] { def self = v }
}
35 changes: 35 additions & 0 deletions src/main/scala/scalaz/Functor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scalaz

trait FunctorLike[F[_]] { self =>
def map[A,B](fa: F[A])(f: A => B): F[B]

// derived functions
def apply[A,B](f: A => B): F[A] => F[B] = map(_)(f)
def strengthL[A,B](a: A, f: F[B]): F[(A,B)] = map(f)(b => (a,b))
def strengthR[A,B](f: F[A], b: B): F[(A,B)] = map(f)(a => (a,b))
lazy val Functor: Functor[F] = new Functor[F] {
def map[A,B](fa: F[A])(f: A => B) = FunctorLike.this.map(fa)(f) }
val syntax = new FunctorSyntax[F] {}
}
trait Functor[F[_]] extends FunctorLike[F]
trait FunctorInstance[F[_]] extends Functor[F]

trait FunctorV[F[_],A] extends SyntaxV[F[A]] {
def self: F[A]
def map[B](f: A => B)(implicit F: Functor[F]) = F.map(self)(f)
def strengthL[B](b: B)(implicit F: Functor[F]) = F.strengthL(b,self)
def strengthR[B](b: B)(implicit F: Functor[F]) = F.strengthR(self,b)
}

trait LiftV[F[_],A,B] extends SyntaxV[A => B] {
def lift(implicit F: Functor[F]) = F(self)
}

trait ToFunctorSyntax {
implicit def functor[F[_],A](v: F[A]) = (new FunctorSyntax[F] {}).functorV(v)
implicit def lift[F[_],A,B](v: A => B) = (new FunctorSyntax[F] {}).liftV(v)
}
trait FunctorSyntax[F[_]] {
implicit def functorV[A](v: F[A]) = new FunctorV[F,A] { def self = v }
implicit def liftV[A,B](v: A => B) = new LiftV[F,A,B] { def self = v }
}
11 changes: 11 additions & 0 deletions src/main/scala/scalaz/Id.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scalaz

trait Ids {
type Id[X] = X
val id = new MonadInstance[Id] {
def pure[A](a: => A): A = a
def bind[A,B](a: A)(f: A => B): B = f(a)
}
}

object Id extends Ids
15 changes: 15 additions & 0 deletions src/main/scala/scalaz/List.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scalaz

trait Lists {
implicit val list = new MonadPlusInstance[List] with TraverseInstance[List] {
def pure[A](a: => A): List[A] = scala.List(a)
def bind[A,B](fa: List[A])(f: A => List[B]) = fa flatMap f
def empty[A] = scala.List()
def plus[A](a: List[A], b: => List[A]) = a ++ b
override def map[A,B](l: List[A])(f: A => B) = l map f
def traverseImpl[F[_],A,B](l: List[A])(f: A => F[B])(implicit F: Applicative[F]): F[List[B]] =
l.foldRight(F.pure(scala.List[B]()))((a,fbl) => F.map2(f(a),fbl)(_ :: _))
}
}

object List extends Lists
12 changes: 12 additions & 0 deletions src/main/scala/scalaz/Monad.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package scalaz

trait MonadLike[F[_]] extends ApplicativeLike[F] with BindLike[F] { derived =>
override def map[A,B](fa: F[A])(f: A => B) = bind(fa)(a => pure(f(a)))
}
trait Monad[F[_]] extends MonadLike[F]
trait MonadInstance[F[_]] extends Monad[F] with BindInstance[F] with PointedInstance[F]

trait ToMonadSyntax extends ToApplicativeSyntax with ToBindSyntax {
// implicit def monad[F[_],A](v: F[A]) = (new MonadSyntax[F] {}).monadV(v)
}
trait MonadSyntax[F[_]] extends ApplicativeSyntax[F] with BindSyntax[F]
11 changes: 11 additions & 0 deletions src/main/scala/scalaz/MonadPlus.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scalaz

trait MonadPlusLike[F[_]] extends MonadLike[F] with ApplicativePlusLike[F] with BindLike[F] {
def filter[A](fa: F[A])(f: A => Boolean) = bind(fa)(a => if (f(a)) pure(a) else empty[A])
}

trait MonadPlus[F[_]] extends MonadLike[F]
trait MonadPlusInstance[F[_]] extends MonadPlus[F] with MonadInstance[F] with PlusInstance[F]

trait ToMonadPlusSyntax extends ToMonadSyntax with ToPlusSyntax
trait MonadPlusSyntax[F[_]] extends MonadSyntax[F] with PlusSyntax[F]
9 changes: 9 additions & 0 deletions src/main/scala/scalaz/MonadState.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalaz

trait MonadStateLike[F[_,_],S] extends MonadLike[({type f[x]=F[S,x]})#f] {
def init: F[S,S]
def put(s: S): F[S,S]
def modify(f: S => S): F[S,S] = bind(init)(s => put(f(s)))
}
trait MonadState[F[_,_],S] extends MonadStateLike[F,S]
trait MonadStateInstance[F[_,_],S] extends MonadState[F,S] with MonadInstance[({type f[x]=F[S,x]})#f]
9 changes: 9 additions & 0 deletions src/main/scala/scalaz/MonadTrans.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalaz

import Id.Id

trait MonadTrans[F[_[_], _]] {
def hoist[M[_],N[_]](f: M ~> N): ({type f[x]=F[M,x]})#f ~> ({type f[x]=F[N,x]})#f

def liftM[G[_]:Monad,A](a: G[A]): F[G,A]
}
8 changes: 8 additions & 0 deletions src/main/scala/scalaz/NaturalTransformation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package scalaz

trait ~>[F[_],G[_]] { self =>
def apply[A](fa: F[A]): G[A]
def compose[E[_]](f: E ~> F): E ~> G = new (E ~> G) {
def apply[A](ea: E[A]) = self(f(ea))
}
}
16 changes: 16 additions & 0 deletions src/main/scala/scalaz/Option.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scalaz

trait Options {
implicit val option = new MonadPlusInstance[Option] with TraverseInstance[Option] {
def pure[A](a: => A) = Some(a)
def bind[A,B](fa: Option[A])(f: A => Option[B]): Option[B] = fa flatMap f
override def map[A,B](fa: Option[A])(f: A => B): Option[B] = fa map f
def traverseImpl[F[_],A,B](fa: Option[A])(f: A => F[B])(implicit F: Applicative[F]) =
fa map (a => F.map(f(a))(Some(_): Option[B])) getOrElse F.pure(None)
def empty[A]: Option[A] = None
def plus[A](a: Option[A], b: => Option[A]) = a orElse b
}
}

object Option extends Options

19 changes: 19 additions & 0 deletions src/main/scala/scalaz/Plus.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package scalaz

trait PlusLike[F[_]] extends FunctorLike[F] {
def plus[A](a: F[A], b: => F[A]): F[A]
def empty[A]: F[A]
}
trait Plus[F[_]] extends PlusLike[F]
trait PlusInstance[F[_]] extends Plus[F] with FunctorInstance[F]

trait PlusV[F[_],A] extends FunctorV[F,A] {
def <+>(other: => F[A])(implicit F: Plus[F]) = F.plus(self, other)
}
trait ToPlusSyntax extends ToFunctorSyntax {
implicit def plus[F[_],A](v: F[A]) = (new PlusSyntax[F] {}).plusV(v)
}
trait PlusSyntax[F[_]] extends FunctorSyntax[F] {
implicit def plusV[A](v: F[A]) = new PlusV[F,A] { def self = v }
}

17 changes: 17 additions & 0 deletions src/main/scala/scalaz/Pointed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scalaz

trait PointedLike[F[_]] extends FunctorLike[F] {
def pure[A](a: => A): F[A]
}
trait Pointed[F[_]] extends PointedLike[F]
trait PointedInstance[F[_]] extends FunctorInstance[F] with Pointed[F]

trait PointedV[A] extends SyntaxV[A] {
def pure[F[_]](implicit F: Pointed[F]) = F.pure(self)
}
trait ToPointedSyntax extends ToFunctorSyntax {
implicit def pointed[A](v: A) = new PointedV[A] { def self = v }
}
trait PointedSyntax[F[_]] extends FunctorSyntax[F] {
implicit def pointedV[A](v: A) = new PointedV[A] { def self = v }
}
3 changes: 3 additions & 0 deletions src/main/scala/scalaz/Scalaz.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package scalaz

object Scalaz extends Ids with States with Lists with Options
56 changes: 56 additions & 0 deletions src/main/scala/scalaz/State.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package scalaz

import Id.Id

trait States {
type State[S,A] = StateT[S,Id,A]
def apply[S,A](f: S => (A,S)): State[S,A] = new StateT[S,Id,A] {
def apply(s: S) = f(s)
}
def init[S]: State[S,S] = State(s => (s,s))
def put[S](s: S): State[S,S] = State(_ => (s,s))
def modify[S](f: S => S): State[S,S] = State(s => { val r = f(s); (r,r) })

implicit def state[S]: MonadStateInstance[({type f[s,a]=State[s,a]})#f, S] =
stateTInstance[S,Id](Id.id)
implicit def stateT[S,F[_]:Monad] = stateTInstance[S,F]:
MonadState[({ type f[s,a]=StateT[s,F,a] })#f, S] with
Monad[({ type f[a]=StateT[S,F,a] })#f] with
Applicative[({ type f[a]=StateT[S,F,a] })#f] with
Apply[({ type f[a]=StateT[S,F,a] })#f] with
Bind[({ type f[a]=StateT[S,F,a] })#f]

private def stateTInstance[S,F[_]](implicit F: Monad[F]) =
new MonadStateInstance[({type f[s,a]=StateT[s,F,a]})#f,S] {
def pure[A](a: => A): StateT[S,F,A] = StateT(s => F.pure(a,s))
override def map[A,B](fa: StateT[S,F,A])(f: A => B): StateT[S,F,B] =
StateT(s => F.map(fa(s)) { case (a,s) => (f(a),s) } )
def bind[A,B](fa: StateT[S,F,A])(f: A => StateT[S,F,B]): StateT[S,F,B] =
StateT(s => F.bind(fa(s)) { case (a,s) => f(a)(s) })
def init: StateT[S,F,S] = StateT(s => F.pure((s,s)))
def put(s: S): StateT[S,F,S] = StateT(_ => F.pure((s,s)))
}
trait StateTF[S,G[_]] { type f[x] = StateT[S,G,x] }

implicit def StateMonadTrans[S] = new MonadTrans[({type f[g[_],a]=StateT[S,g,a]})#f] {
def liftM[G[_],A](ga: G[A])(implicit G:Monad[G]): StateT[S,G,A] =
StateT(s => G.map(ga)(a => (a,s)))
def hoist[M[_],N[_]](f: M ~> N) = new (StateTF[S,M]#f ~> StateTF[S,N]#f) {
def apply[A](action: StateT[S,M,A]) = StateT[S,N,A](s => f(action(s)))
}
}
}
trait StateTs {
def apply[S,F[_],A](f: S => F[(A,S)]): StateT[S,F,A] = new StateT[S,F,A] {
def apply(s: S) = f(s)
}
}

trait StateT[S,F[_],A] {
def apply(s: S): F[(A,S)]
def !(s: S)(implicit F: Functor[F]): F[A] =
F.map(apply(s))(_._1)
}

object State extends States
object StateT extends StateTs
15 changes: 15 additions & 0 deletions src/main/scala/scalaz/Syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scalaz

trait Syntaxes {
object functor extends ToFunctorSyntax
object pointed extends ToPointedSyntax
object apply extends ToApplySyntax
object applicative extends ToApplicativeSyntax
object bind extends ToBindSyntax
object monad extends ToMonadSyntax
object traverse extends ToTraverseSyntax
}

trait SyntaxV[A] { def self: A }

object Syntax extends Syntaxes
Loading

0 comments on commit 86f4b3e

Please sign in to comment.