running free with the monads
DESCRIPTION
Free Monads are a powerful technique that can separate the representation of programs from the messy details of how they get run. I'll go into the details of how they work, how to use them for fun and profit in your own code, and demonstrate a live Free Monad-driven tank game. Supporting code at https://github.com/kenbot/freeTRANSCRIPT
Run free with the monads!
Free Monads for fun and profit
@KenScambler#scalamelb March 2014
The problem• Separation of concerns is paramount to software• In FP, we try to banish effects to the peripheries
of our programs• Results and decisions must be represented as
data, such as ADTs• Interpretation can happen later• Not super expressive though.
Decision/Interpretation
(tangled)def updateAccount(user: User, db: KVStore): Unit = { val account = db.get(user.id)
if (!account.suspended) db.put(user.id, account.updateSpecialOffers) else if (account.abandoned) db.delete(user.id)}
Decisions as data
sealed trait KVSAction
case class Put(key: String, value: String) extends KVSAction
case class Delete(key: String) extends KVSAction
case object NoAction extends KVSAction
Decisiondef chooseAction(user: User, account: Account): KVSAction = {
if (!account.suspended) Put(user.id, account.updateSpecialOffers) else if (account.abandoned) Delete(user.id) else NoAction}
Interpretationdef interpret(action: KVSAction): Unit = { action match { case Put(key, value) => db.put(key, value) case Delete(key) => db.delete(key) case NoAction => () }}
val account = db.get(bob,id)interpret(chooseAction(bob, account))
How far can we push it?
• Can our pure “decision” data be as sophisticated as a program?
• Can we create DSLs that can be run later in different ways?
• Can we manipulate & rewrite our “program” on the fly?
• Conditional logic?• Loops?• Coroutines?
How far can we push it?
def updateAccount(user: User): Unit = for { account <- getAccount(user.id) _ <- when(!account.suspended)( put(user.id, user.updated)) _ <- when(account.abandoned)( delete(user.id)) } yield ()
The class called “Free”
• Free is a data structure• Tree of computations
Free[F[_], A]
The class called “Free”
• Free is a data structure• Tree of computations
Free[F[_], A]
The class called “Free”
Suspend(F[Free[F,A]])
Return(A)
Free[F[_], A]
The class called “Free”
Suspend(F[Free[F,A]])
Return(A)
Free[F[_], A]
The class called “Free”
Suspend(F[Free[F,A]])
Return(A)
Free[F[_], A]
Why “free monads”?
Why “free monads”?
Why “free monads”?
Why “free monads”?If F[_] is a functor, Free is a
monad…… for free!
• This buys us a whole world of existing functionality
• Better abstraction• Sequential computations• Elegant imperative-style syntax
Remedial interlude
Functors• Functors are things you can map over• F[A] => (A => B) => F[B]
trait F[A] { def map(f: A => B): F[B]}
Functorstrait F[A] { def map(f: A => B): F[B]}
Functorstrait F[A] { def map(f: A => B): F[B]}
Functorstrait F[A] { def map(f: A => B): F[B]}
Monads• Monads have a flatMap method that allows you to
chain computations together sequentially
class M[A] { def map(f: A => B): M[B] def flatMap(f: A => M[B]): M[B]}
Monads• Nesting flatmaps allows sequential actions,
ignoring the specific context!
nbaTeams.flatMap { team => team.players.flatMap { player => player.gamesPlayed.map { game => BasketballCard(team, player, game) } }}
Monads• Neat comprehension syntax in Scala and Haskell• Makes it look like a regular program
for { team <- nbaTeams player <- team.players game <- player.gamesPlayed} yield BasketballCard(team, player, game)
Back to our regularly scheduled program…
“Free objects” in maths
• Important concept in maths!• Many free structures in Category Theory• Free Monoids, Free Monads, Free Categories, Free
Groups, etc• It only counts as “free” if the free thing gets
generated in the simplest possible way
Free Blargles from Fraxblatts
• A Fraxblatt is said to generate a Free Blargle if:
1. The Blargle doesn’t contain anything not directly produced from a Fraxblatt
2. The Blargle doesn’t contain anything beyond what it needs to be a Blargle
Free Blargles from Fraxblatts
• A Fraxblatt is said to generate a Free Blargle if:
1. NO JUNK
2. NO NOISE
Making an honest monad of it
case class Return[F[_], A](a: A) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = ???
}
• Define flatMap for Return:
Making an honest monad of it
case class Return[F[_], A](a: A) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = f(a)
}
Making an honest monad of it
• Define flatMap for Suspend:
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = ???}
Making an honest monad of it
• We need to map over the functor
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { F??? map ??? }}
F[???]
Making an honest monad of it
• “next” is the only F we have lying around
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { next map {free => ???} }}
F[Free[F, ???]]
Making an honest monad of it
• flatMap is almost the only thing we can do to a Free
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { next map {free => free.flatMap(???)} }}
F[Free[F, ???]]
Making an honest monad of it
• Mapping function f will turn our As into Free[F, B]s
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { next map {free => free.flatMap(f)} }}
F[Free[F, B]]
Making an honest monad of it
• Wrapping in Suspend matches the type signature!
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { Suspend(next map {free => free.flatMap(f)}) }}
Free[F, B]
Making an honest monad of it
• Cleaning up the syntax a bit…
case class Suspend[F[_], A](next: F[Free[F,A]]) extends Free[F, A] {
def flatMap(f: A => Free[F, B]): Free[F, B] = { Suspend(next map (_ flatMap f)) }}
Stepping through flatMap
Let’s plug in a really simple functor and see what happens.
case class Box[A](a: A)
Stepping through flatMap
Let’s plug in a really simple functor and see what happens.
case class Box[A](a: A) { def map[B](f: A => B) = Box(f(a))}
banana
Return(banana)
Box(Return(banana))
Suspend(Box(Return(banana)))
that.flatMap(banana => Return(banana.peel))
flatMap
that.flatMap(banana => Return(banana.peel))
map
that.flatMap(banana => Return(banana.peel))
flatMap
that.flatMap(banana => Return(banana.peel))
liftFLet’s automate creating the Suspend cell!
F[A] => Free[F, A]
=>
More flatmapping
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
1
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
liftF
1
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
flatMap
1
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
map 1
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
flatMap
1
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
2lift
F
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
2flatMap
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
2
ma
p
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
2flatMap
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
3
liftF
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
3
flatMap
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
3
map
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
3
flatMap
for { a <- liftF( Box(1) ) b <- liftF( Box(2) ) c <- liftF( Box(3) )} yield a + b + c
6
Free[Box, A]• Chain of nothings, resulting in a single value• Not very useful!
Free[List, A]
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
1 2 3
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
1 2 3
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
1 2 3 flatMap
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
1 2 3 map
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
1 2 3 flatmap
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
liftF1 2 2 4
3 6
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
flatMap
1 2 2 43 6
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
map1 2 2 4
3 6
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
flatMap
1 2 2 43 6
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
liftF
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
flatMap
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
map
for { a <- liftF( List(1,2,3) ) b <- liftF( List(a,a*2) ) c <- liftF( Nil )} yield a + b + c
abort
Free[List,A]• Branching tree shape, with data at the leaves• Empty lists can terminate the tree, not just
Return.• Again, not super useful.
The functor controls the branching factor!
Funky functions
• Functors are not just data structures that hold values
• They are computations!• Free’s real power is unleashed when the Functor
maps over functions!
Free[Function0, A]• No-arg functions, basically a lazy value• Flatmapping the free composes functions• Doesn’t actually run any code
Free[Function0, A]
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
=> 2 + 3
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
liftF
2 + 3
=>
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
flatMap
2 + 3
=>
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
map=>2 +
3
for { a <- liftF(() => 2 + 3) b <- liftF(() => a * 2) c <- liftF(() => a * b)} yield a + b + c
compos
e
=> =>2 + 3=>
Trampolines
Trampolines• Believe it or not, Free[Function0,A] is incredibly
useful!• Also known as Trampoline[A]• Moves tail calls onto the heap, avoiding stack
overflows• The best we can get for mutual tail recursion on
the JVM
Trampolines• Let’s take a look at some code…
Now for the power tool
Little languages• Small, imperative DSLs• Don’t directly do anything, can be interpreted
many ways• Functionally pure and type-safe
A key-value store DSL• A bit like the KVSAction ADT way back at the start• There’s a “type hole” for the next thing• That means…. we can make it a Functor!• Mechanical translation from corresponding API
functions
A key-value store DSLsealed trait KVS[Next]
case class Put[Next](key: String, value: String, next: Next) extends KVS[Next]
case class Delete[Next](key: String, next: Next) extends KVS[Next]
case class Get[Next](key: String, onValue: String => Next) extends KVS[Next]
A key-value store DSLsealed trait KVS[Next]
case class Put[Next](key: String, value: String, next: Next) extends KVS[Next]
case class Delete[Next](key: String, next: Next) extends KVS[Next]
case class Get[Next](key: String, onValue: String => Next) extends KVS[Next]
Just have a slot for the next thing, if we don’t care about a
result value
A key-value store DSLsealed trait KVS[Next]
case class Put[Next](key: String, value: String, next: Next) extends KVS[Next]
case class Delete[Next](key: String, next: Next) extends KVS[Next]
case class Get[Next](key: String, onValue: String => Next) extends KVS[Next]
Have a Result => Next function, if we
want to “return” some Result.
Which looks a bit like…
def put[A](key: String, value: String): Unit
def delete[A](key: String): Unit
def get[A](key: String): String
Which is a bit like…def put[A](key: String, value: String): Unit
def delete[A](key: String): Unit
def get[A](key: String): String
A functor for our KVSnew Functor[KVS] { def map[A,B](kvs: KVS[A])(f: A => B): KVS[B] = kvs match {
case Put(key, value, next) => Put(key, value, f(next))
case Delete(key, next) => Delete(key, f(next))
case Get(key, onResult) => Get(key, onResult andThen f)
} }
A functor for our KVSnew Functor[KVS] { def map[A,B](kvs: KVS[A])(f: A => B): KVS[B] = kvs match {
case Put(key, value, next) => Put(key, value, f(next))
case Delete(key, next) => Delete(key, f(next))
case Get(key, onResult) => Get(key, onResult andThen f)
} }
To map over the next value, just apply f
A functor for our KVSnew Functor[KVS] { def map[A,B](kvs: KVS[A])(f: A => B): KVS[B] = kvs match {
case Put(key, value, next) => Put(key, value, f(next))
case Delete(key, next) => Delete(key, f(next))
case Get(key, onResult) => Get(key, onResult andThen f)
} }
To map over a function yielding the next value, compose
f with it
Lifting into the Free Monad
def put(key: String, value: String): Free[KVS, Unit] = liftF( Put(key, value, ()) ) def get(key: String): Free[KVS, String] = liftF( Get(key, identity) ) def delete(key: String): Free[KVS, Unit] = liftF( Delete(key, ()) )
Lifting into the Free Monad
def put(key: String, value: String): Free[KVS, Unit] = liftF( Put(key, value, ()) ) def get(key: String): Free[KVS, String] = liftF( Get(key, identity) ) def delete(key: String): Free[KVS, Unit] = liftF( Delete(key, ()) )
Initialise with Unit, when we
don’t care about the value
Lifting into the Free Monad
def put(key: String, value: String): Free[KVS, Unit] = liftF( Put(key, value, ()) ) def get(key: String): Free[KVS, String] = liftF( Get(key, identity) ) def delete(key: String): Free[KVS, Unit] = liftF( Delete(key, ()) )
Initialise with the identity function, when we want to return a value
The payoff
Composable scriptsdef modify(key: String, f: String => String): Free[KVS, Unit] = for { v <- get(key) _ <- put(key, f(v)) } yield ()
Harmless imperative code
val script: Free[KVS, Unit] = for { id <- get(“swiss-bank-account-id”) _ <- modify(id, (_ + 1000000)) _ <- put(“bermuda-airport”, “getaway car”) _ <- delete(“tax-records”) } yield ()
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
KVStore is immutable
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
F[Free[F, A]] \/ A
Resume and fold…
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
KVS[Free[KVS, Unit]] \/ Unit
Resume and fold…
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
When resume finally returns Unit, return the table
Pure interpreterstype KVStore = Map[String, String]
def interpretPure(kvs: Free[KVS, Unit], table: KVStore): KVStore = kvs.resume.fold({ case Get(key, onResult) => interpretPure(onResult(table(key)), table)
case Put(key, value, next) => interpretPure(next, table + (key -> value))
case Delete(key, next) => interpretPure(next, table - key)
}, _ => table)
Effectful interpreter(s)type KVStore = mutable.Map[String, String]
def interpretImpure(kvs: Free[KVS,Unit], table: KVStore): Unit = kvs.go { case Get(key, onResult) => onResult(table(key))
case Put(key, value, next) => table += (key -> value) next
case Delete(key, next) => table -= key next }
Effectful interpreterstype KVStore = mutable.Map[String, String]
def interpretImpure(kvs: Free[KVS,Unit], table: KVStore): Unit = kvs.go { case Get(key, onResult) => onResult(table(key))
case Put(key, value, next) => table += (key -> value) next
case Delete(key, next) => table -= key next }
Mutable map
Effectful interpreterstype KVStore = mutable.Map[String, String]
def interpretImpure(kvs: Free[KVS,Unit], table: KVStore): Unit = kvs.go { case Get(key, onResult) => onResult(table(key))
case Put(key, value, next) => table += (key -> value) next
case Delete(key, next) => table -= key next }
def go(f: F[Free[F, A]] => Free[F, A]): A
Effectful interpreter(s)type KVStore = mutable.Map[String, String]
def interpretImpure(kvs: Free[KVS,Unit], table: KVStore): Unit = kvs.go { case Get(key, onResult) => onResult(table(key))
case Put(key, value, next) => table += (key -> value) next
case Delete(key, next) => table -= key next }
How-to summary1. Fantasy API2. ADT with type hole for next value3. Functor definition for ADT4. Lifting functions5. Write scripts6. Interpreter(s)
Tank game
Conclusion• Free Monads are really powerful• Separate decisions from interpretation, at a more
sophisticated level• Type-safe• Easy to use!
Conclusion• Express your decisions in a “little language”• Pause and resume programs, co-routine style• Rewrite programs macro-style• Avoid stack overflows with Trampolines
This is a great tool to have in your toolkit!
Further reading• Awodey, Category Theory• Bjarnason, Dead Simple Dependency Injection• Bjarnason, Stackless Scala with Free Monads• Doel, Many roads to Free Monads• Ghosh, A Language and its Interpretation:
Learning Free Monads• Gonzalez, Why Free Monads Matter• Haskell.org, Control.Monad.Free• Perrett, Free Monads, Part 1• Scalaz, scalaz.Free
Further reading
https://github.com/kenbot/free
Thank youHope you enjoyed hearing about Free Monads!