Created
April 2, 2014 21:15
-
-
Save jroesch/9943334 to your computer and use it in GitHub Desktop.
TypeFamily example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package typefamily | |
| import scala.language._ | |
| /* From the paper: | |
| * http://research.microsoft.com/en-us/um/people/simonpj/papers/assoc-types/fun-with-type-funs/typefun.pdf | |
| * | |
| * class Mutation m where | |
| * type Ref m :: * -> * | |
| * newRef :: a -> m (Ref m a) | |
| * readRef :: Ref m a -> m a | |
| * writeRef :: Ref m a -> a -> m () | |
| * | |
| * instance Mutation IO where | |
| * type Ref IO = IORef | |
| * newRef = newIORef | |
| * readRef = readIORef | |
| * writeRef = writeIORef | |
| * | |
| * instance Mutation (ST s) where | |
| * type Ref (ST s) = STRef s | |
| * newRef = newSTRef | |
| * readRef = readSTRef | |
| * writeRef = writeSTRef | |
| */ | |
| /* Associated Type Instance */ | |
| trait Mutation[M[_]] { | |
| type Ref[M[_], A] | |
| def newRef[A](a: A): M[Ref[M, A]] | |
| def readRef[A](r: Ref[M, A]): M[A] | |
| def writeRef[A](r: Ref[M, A])(a: A): M[Unit] | |
| } | |
| trait IO[A] | |
| trait IORef[A] | |
| trait ST[S, A] | |
| trait STRef[S, A] | |
| object Instances { | |
| implicit val mutationIO = new Mutation[IO] { | |
| /* not sure how to do this without the bound */ | |
| type Ref[M[_] <: IO[_], A] = IORef[A] | |
| def newRef[A](a: A): IO[Ref[IO, A]] = ??? | |
| def readRef[A](r: Ref[IO, A]): IO[A] = ??? | |
| def writeRef[A](r: Ref[IO, A])(a: A): IO[Unit] = ??? | |
| } | |
| implicit def mutationST[S] = new Mutation[({ type λ[A] = ST[S, A] })#λ] { | |
| /* Make the Type Lambda less annoying */ | |
| type STWithS[A] = ST[S, A] | |
| type Ref[M[_] <: STWithS[_], A] = STRef[S, A] | |
| def newRef[A](a: A): STWithS[Ref[STWithS, A]] = ??? | |
| def readRef[A](r: Ref[STWithS, A]): STWithS[A] = ??? | |
| def writeRef[A](r: Ref[STWithS, A])(a: A): STWithS[Unit] = ??? | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment