Permalink
Browse files

repository initialized

  • Loading branch information...
0 parents commit f7d473d1505b579ddad88b460a588a083a2e6f32 @ekmett committed Dec 24, 2010
@@ -0,0 +1,13 @@
+*.swo
+*.swp
+lib_managed
+project/boot
+full/project/boot
+src_managed
+target
+.DS_Store
+out
+tags
+*~
+build.log
+.history
@@ -0,0 +1,8 @@
+#Project properties
+#Wed Dec 22 20:17:14 EST 2010
+project.organization=com.comonad
+project.name=functorial
+sbt.version=0.7.4
+project.version=0.1
+build.scala.versions=2.8.1
+project.initialize=false
3 sbt
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+java $SBT_OPTS -Dfile.encoding=UTF-8 -Xss4M -Xmx1024M -XX:MaxPermSize=256M -XX:NewSize=128M -XX:NewRatio=3 -jar `dirname $0`/sbt-launch-0.7.4.jar @sbt.boot.properties "$@"
Binary file not shown.
@@ -0,0 +1,38 @@
+[scala]
+ version: 2.7.7
+ classifiers: sources
+
+[app]
+ org: org.scala-tools.sbt
+ name: sbt
+ version: read(sbt.version)
+ class: sbt.xMain
+ components: xsbti
+ cross-versioned: true
+
+[repositories]
+ local
+ maven-local
+ sbt-db: http://databinder.net/repo/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]
+ maven-central
+ scala-tools-releases
+ scala-tools-snapshots
+
+[boot]
+ directory: project/boot
+ properties: project/build.properties
+ prompt-create: Project does not exist, create new project?
+ prompt-fill: true
+ quick-option: true
+
+[log]
+ level: info
+
+[app-properties]
+ project.name: quick=set(test), new=prompt(Name), fill=prompt(Name)
+ project.organization: new=prompt(Organization)
+ project.version: quick=set(1.0), new=prompt(Version)[1.0], fill=prompt(Version)[1.0]
+ build.scala.versions: quick=set(2.7.7), new=prompt(Scala version)[2.7.7], fill=prompt(Scala version)[2.7.7]
+ sbt.version: quick=set(0.7.3), new=prompt(sbt version)[0.7.3], fill=prompt(sbt version)[0.7.3]
+ project.scratch: quick=set(true)
+ project.initialize: quick=set(true), new=set(true)
@@ -0,0 +1,2 @@
+set SCRIPT_DIR=%~dp0
+java %SBT_OPTS% -Dfile.encoding=UTF-8 -Xss4M -Xmx1024M -XX:MaxPermSize=256M -XX:NewSize=128M -XX:NewRatio=3 -jar "%SCRIPT_DIR%sbt-launch-0.7.4.jar" @sbt.boot.properties %*
@@ -0,0 +1,24 @@
+package functorial
+
+import scala.collection.generic
+
+trait Alternative[F[+_]] extends Empty[F] with Or[F] with Applicative[F] { module =>
+ def many[A](m: F[A]): F[List[A]] = {
+ lazy val many_m : F[List[A]] = lift2(m, many_m)(_ :: _) // [A,List[A],List[A]](m, many_m)(_ :: _)
+ many_m
+ }
+ def some[A](m: F[A]): F[List[A]] = lift2(m, many(m))(_ :: _)
+ def optional[A](m: F[A]): F[Option[A]] = (m map (Some(_):Option[A])) | pure(None)
+ override implicit def syntax[A](m: F[A]): Alternative.Syntax[F,A] = new Alternative.Syntax[F,A] {
+ val companion = module
+ def value = m
+ }
+}
+object Alternative {
+ trait Syntax[F[+_],+A] extends Empty.Syntax[F,A] with Or.Syntax[F,A] with Applicative.Syntax[F,A] with HasCompanion[Alternative[F]] { m =>
+ def * : F[List[A]] = companion.many(m)
+ def + : F[List[A]] = companion.some(m)
+ def ? : F[Option[A]] = companion.optional(m)
+ }
+}
+
@@ -0,0 +1,82 @@
+package functorial
+
+import scala.collection.generic
+import functorial.Extensions._
+
+trait Applicative[F[+_]] extends Functor[F] { module =>
+ // implicit def self: Applicative[F] = this
+ def ap[A,B](f: F[A => B], a: F[A]): F[B]
+ def pure[A](a: A): F[A]
+
+ def lift1[A,B](a: F[A])(f: A => B): F[B] =
+ f !! a
+ def lift2[A,B,C](a: F[A],b: F[B])(f: (A,B) => C): F[C] =
+ f !! a !* b
+ def lift3[A,B,C,D](a: F[A],b: F[B],c: F[C])(f: (A,B,C) => D): F[D] =
+ f !! a !* b !* c
+ 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
+
+ def unit: F[Unit] = pure[Unit](())
+
+ override implicit def syntax[A](m: F[A]): Applicative.Syntax[F,A] = new Applicative.Syntax[F,A] {
+ val companion = module
+ def value = m
+ }
+
+ implicit def staticFunction1[A,B](m: F[A => B]): Applicative.StaticFunction1[F,A,B]
+ = new Applicative.StaticFunction1[F,A,B] {
+ val companion = module
+ def value = m
+ }
+ implicit def staticFunction2[A,B,C](m: F[(A,B) => C]): Applicative.StaticFunction2[F,A,B,C]
+ = new Applicative.StaticFunction2[F,A,B,C] {
+ val companion = module
+ def value = m
+ }
+ implicit def staticFunction3[A,B,C,D](m: F[(A,B,C) => D]): Applicative.StaticFunction3[F,A,B,C,D]
+ = new Applicative.StaticFunction3[F,A,B,C,D] {
+ val companion = module
+ def value = m
+ }
+}
+
+object Applicative {
+ trait Syntax[F[+_],+A] extends Functor.Syntax[F,A]
+ with HasCompanion[Applicative[F]] { m =>
+ def <**>[B](n: F[A => B]): F[B] = companion.lift2(m,n)((a,f) => f(a))
+ def ~>[B](n: F[B]): F[B] = companion.lift2(m,n)((_,b) => b)
+ def <~[B](n: F[B]): F[A] = companion.lift2(m,n)((a,_) => a)
+ def ~[B](n: F[B]): F[(A,B)] = companion.lift2(m,n)((a,b) => (a,b))
+ def forever: F[Nothing] = {
+ lazy val loop : F[Nothing] = ~>(loop)
+ loop
+ }
+ // def replicate(n: Int): F[List[A]]
+ }
+ trait StaticFunction1[F[+_],-A,+B] extends Syntax[F,A => B] { m =>
+ def !!*(a: F[A]): F[B] = companion.ap(m, a)
+ def !*(a: F[A]): F[B] = companion.ap(m, a)
+ }
+ trait StaticFunction2[F[+_],-A,-B,+C] extends Syntax[F,(A,B) => C] { m =>
+ def !!*(a: F[A],b: F[B]): F[C] = {
+ val dict = companion; import dict._
+ apply[(A,B)=>C,A=>B=>C](_.curried, m) !* a !* b
+ }
+ def !*(a: F[A]): F[B => C] = {
+ val dict = companion; import dict._
+ apply[(A,B)=>C,A=>B=>C](_.curried, m) !* a
+ }
+ }
+ trait StaticFunction3[F[+_],-A,-B,-C,+D] extends Syntax[F,(A,B,C) => D] { m =>
+ def !!*(a: F[A],b: F[B], c: F[C]): F[D] = {
+ val dict = companion; import dict._
+ apply[(A,B,C)=>D,A=>B=>C=>D](f => f.curried, m) !* a !* b !* c
+ }
+ def !*(a: F[A]): F[(B,C) => D] = {
+ val dict = companion; import dict._
+ apply[(A,B,C)=>D,A=>(B,C)=>D](f => a => (b,c) => f(a,b,c), m) !* a
+ }
+ }
+}
+
@@ -0,0 +1,59 @@
+package functorial
+
+import scala.collection.generic
+
+/*
+// attempt to implement first order concepts by lowering higher order ones
+
+trait Semigroup[A] extends Or[({trait λ[+X] = A})#λ,A] {
+ override implicit def syntax[A](m: A): Semigroup.Syntax[A]
+ = new Semigroup.Syntax[A] {
+ def companion = module
+ def value = m
+ }
+}
+
+object Semigroup {
+ def apply[A](f: (A,A) => A) = new Semigroup[A] {
+ def or[B](a: A, b: A): A = f(a,b)
+ }
+ trait Syntax[A] extends Or.Syntax[({trait λ[+X] = A)#λ,A] with HasCompanion[Semigroup[A]] { m =>
+ // type argument required, even though not used due to lowering
+ def |[B](n: A): A = companion.or(m,n)
+ }
+}
+
+trait Unital[+A] extends Empty[({trait λ[+X] = A})#λ,A] {
+ override implicit def syntax[A](m: A): Unital.Syntax[A]
+ = new Unital.Syntax[A] {
+ def companion = module
+ def value = m
+ }
+}
+
+object Unital {
+ def apply[A](zero: => A) = new Unital[A] {
+ def empty = zero
+ }
+ trait Syntax[A] extends Empty.Syntax[({trait λ[+X] = A)#λ,A] with HasCompanion[Unital[A]]
+}
+
+trait Monoid[A] extends Semigroup[A] with Unital[A] {
+ override implicit def syntax[A](m: A): Monoid.Syntax[A]
+ = new Monoid.Syntax[A] {
+ def companion = module
+ def value = m
+ }
+}
+
+object Monoid {
+ def apply[A](e: A)(f: (A,A) => A) = new Monoid[A] {
+ def or[B](a: A, b: A): A = f(a,b)
+ def empty: A = e
+ }
+ trait Syntax[A] extends Semigroup.Syntax[A]
+ with Unital.Syntax[A]
+ with HasCompanion[Monoid[A]] {
+ }
+}
+*/
@@ -0,0 +1,20 @@
+package functorial
+
+import scala.collection.generic
+
+trait Bind[F[+_]] extends Companion { module =>
+ def bind[A,B](f: A => F[B], m: F[A]): F[B]
+ def join[A](f: F[F[A]]): F[A] = bind(identity[F[A]], f)
+ implicit def syntax[A](m: F[A]): Bind.Syntax[F,A] =
+ new Bind.Syntax[F,A] {
+ val companion = module
+ def value = 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] = companion.bind(f,m)
+ }
+}
+
@@ -0,0 +1,11 @@
+package functorial
+
+trait Companion {
+ // low priority implicit
+ implicit def self: this.type = this
+}
+
+trait HasCompanion[+A <: Companion] {
+ implicit val companion: A
+}
+
@@ -0,0 +1,18 @@
+package functorial
+
+import scala.collection.generic
+
+trait ContravariantFunctor[F[-_]] extends Companion { module =>
+ def apply[A,B](f: B => A, a: F[A]): F[B]
+ implicit def syntax[A](m: F[A]): ContravariantFunctor.Syntax[F,A] = new ContravariantFunctor.Syntax[F,A] {
+ val companion = module
+ def value = m
+ }
+}
+
+object ContravariantFunctor {
+ trait Syntax[F[-_],-A] extends HasCompanion[ContravariantFunctor[F]] with Wrapped[F[A]] {
+ def map[B](f: B => A): F[B] = companion.apply(f, value)
+ }
+}
+
@@ -0,0 +1,12 @@
+package functorial
+
+import scala.collection.generic
+
+trait Empty[F[+_]] extends Companion { module =>
+ def empty: F[Nothing]
+}
+object Empty {
+ trait Syntax[F[+_],+A] extends HasCompanion[Empty[F]]
+ with Wrapped[F[A]]
+}
+
@@ -0,0 +1,3 @@
+package functorial
+
+object Extensions extends RichFunctions
@@ -0,0 +1,13 @@
+package functorial
+
+import scala.collection.generic
+
+trait Filter[F[+_]] extends Companion { module =>
+ def filter[A](f: F[A], p: A => Boolean): F[A]
+}
+object Filter {
+ trait Syntax[F[+_],+A] extends Wrapped[F[A]] with HasCompanion[Filter[F]] { m =>
+ def filter(p: A => Boolean): F[A] = companion.filter(m, p)
+ }
+}
+
@@ -0,0 +1,15 @@
+package functorial
+
+trait Functor[F[+_]] extends Companion { module =>
+ def apply[A,B](f: A => B, a: F[A]): F[B]
+ implicit def syntax[A](m: F[A]): Functor.Syntax[F,A] = new Functor.Syntax[F,A] {
+ val companion = module
+ def value = m
+ }
+}
+
+object Functor {
+ trait Syntax[F[+_],+A] extends HasCompanion[Functor[F]] with Wrapped[F[A]] {
+ def map[B](f: A => B): F[B] = companion.apply(f, value)
+ }
+}
@@ -0,0 +1,7 @@
+package functorial
+
+trait FunctorReader[F[+_],E] extends Functor[F] {
+ def ask: F[E] = reader[E](x => x)
+ def reader[X](f: E => X): F[X]
+}
+
@@ -0,0 +1,10 @@
+package functorial
+
+trait FunctorState[F[+_],S] extends Functor[F] {
+ def get: F[S] = state[S](s => (s,s))
+ def gets[T](f: S => T): F[T] = state[T](s => (f(s),s))
+ def modify(f: S => S): F[S] = state[S](s => { val s2 = f(s); (s2,s2) })
+ def put(s: S): F[S] = state[S](_ => (s, s))
+ def state[A](f: S => (A,S)): F[A]
+}
+
@@ -0,0 +1,43 @@
+package functorial
+
+import scala.collection.generic
+
+// a toy list example monad
+class ListEmptyException extends Exception
+sealed abstract class List[+A] extends MonadPlus.Syntax[List,A] { xs =>
+ def head: A
+ def tail: List[A]
+ def isEmpty: Boolean
+ def uncons: Option[(A,List[A])]
+ val companion: MonadPlus[List] = List
+ def ::[B>:A](x: B): List[B] = Cons(x, xs)
+ def value = this
+}
+
+object List extends MonadPlus[List] {
+ // def filter[A]
+ def pure[A](a: A): List[A] = Cons(a, Nil)
+ def bind[A,B](f: A => List[B], a: List[A]) = a flatMap f
+ def empty = functorial.Nil
+ def or[A](xs: List[A], ys: List[A]): List[A] = xs | ys
+}
+
+case object Nil extends List[Nothing] {
+ def head: Nothing = throw new ListEmptyException
+ def tail: List[Nothing] = throw new ListEmptyException
+ def uncons: Option[(Nothing,List[Nothing])] = None
+ def isEmpty: Boolean = true
+
+ override def map[B](f: Nothing => B) = this
+ override def flatMap[B](f: Nothing => List[B]) = this
+ override def |[B](f: List[B]) = f
+}
+
+case class Cons[A](head: A, tail: List[A]) extends List[A] {
+ def uncons: Option[(A,List[A])] = Some(head, tail)
+ def isEmpty: Boolean = false
+
+ override def map[B](f: A => B) = f(head) :: (tail map f)
+ override def flatMap[B](f: A => List[B]) = f(head) | (tail flatMap f)
+ override def |[B>:A](xs: List[B]) = head :: (tail | xs)
+}
Oops, something went wrong.

0 comments on commit f7d473d

Please sign in to comment.