-
Notifications
You must be signed in to change notification settings - Fork 0
Free Monad
Algebraic Effects comparing effects and mtl style of composition
Free monads let you decompose any impure program into a pure representation of its behavior and a minimal impure interpreter. Source: Purify code using free monads
- No Silver Bullets in Functional Programming by Brian McKenna at Functional Conf 16 - about referential transparency and how to add it imperative programs; hint it use Free Monads
- Free Me — Exploring the Free Data Type
- Purify code using free monads
- Free Monads Are Simple - example using Haxl.
- Why free monads matter
- Purity in an impure language with the free monad – by example of a Tic-Tac-Toe backend with CQRS and event sourcing
- Way 12. Monadic control flow, in which we make decisions in the turtle workflow based on results from earlier commands. from Thirteen ways of looking at a turtle
- Free for DSLs, cofree for interpreters
- Free monad cheetsheet
- Free monads in 7 easy steps
- Free monads in category theory
- What is referential transparency
- The Interpreter Pattern Revisited - amusing video from Runar Bjarnason about the GoF book, the Interpreter Pattern and functional programming (Free Monad):
- Free monad considered harmful
- What does Free buy us?
As a counter from a comment in Dependency rejection:
I don't advocate the "free monad" style that's presently trendy in Scala-land because I find it unnecessarily complex. 90% of the purported advantages of free monads are already supported by simpler language features.
and:
The Haskell philosophy isn't about rejecting side effects outright - it's about measuring and controlling them. I wouldn't write tryAcceptComposition using IO. Instead I'd program to the interface, not the implementation, using an mtl-style class to abstract over monads which support saving and loading reservations.
where the code becomes:
class Monad m => MonadReservation m where
readReservations :: ConnectionString -> Date -> m [Reservation]
createReservation :: ConnectionString -> Reservation -> m ReservationId
tryAcceptComposition :: MonadReservation m => Reservation -> m (Maybe ReservationId)
tryAcceptComposition r = runMaybeT $ do
reservations <- lift $ readReservations connectionString (date r)
accepted <- MaybeT $ return $ tryAccept 10 reservations r
lift $ createReservation connectionString acceptedalso from A tale of two Monads: Free vs MTL:
What we learned is utilizing Free vs mtl is not universally good or bad. They are each fantastic abstractions and it really comes down to your use case. If your use case is going to end up building deeply nested graphs of Free data types then it probably isn’t for you. In the end we gained a lot clarity from prototyping with Free and a lot of performance from refactoring to mtl.