Created
January 29, 2013 20:22
-
-
Save tpolecat/4667404 to your computer and use it in GitHub Desktop.
This file contains 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
import scalaz._ | |
import scalaz.effect._ | |
import scalaz.effect.Effect._ | |
import scalaz.effect.IO._ | |
import scalaz.syntax.monad._ | |
import ST._ | |
object StTest extends App { | |
// ST actions must be parameterized by their thread "S", which always remains free | |
def a[S]: ST[S, Int] = returnST(66).map(_ + 1) | |
// In order to run an action, you need to construct a Forall, which is awkward. | |
// Note that it fixes the return type. | |
val forall = new Forall[({ type λ[σ] = ST[σ, Int] })#λ] { def apply[S] = a } | |
// We can abstract out the lambda type if we want to | |
type ForallST[A] = Forall[({ type λ[σ] = ST[σ, A] })#λ] | |
val forall2 = new ForallST[Int] { def apply[S] = a } | |
// They should be equivalent (N.B. might crash presentation compiler but does work) | |
implicitly[Forall[({ type λ[σ] = ST[σ, Int] })#λ] =:= ForallST[Int]] | |
// this is pure | |
runST(forall) | |
// this is in IO (no forall required) | |
val z = for { | |
n <- IO(1) | |
v <- newVar[RealWorld](20) | |
_ <- v |= 4 | |
_ <- v.mod(_ * 2) | |
x <- a // calling a pure ST action from IO | |
y <- v.read | |
_ <- putStrLn("x was " + x) | |
} yield (n, x, y) | |
println(z.unsafePerformIO) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment