r/haskell • u/buffyoda • Sep 27 '16
An Architecture for Modern Functional Programming: Part 2
http://degoes.net/articles/modern-fp-part-27
u/asellier Sep 27 '16
One thing I'd like to understand: without knowing how this compiles down in ghc, it would seem like using various "interpreters" within the program would be a huge performance hit. Is this the case? If not, why not?
6
u/ElvishJerricco Sep 27 '16
It gets a lot more efficient as you optimize the implementation of the free monad, but it's still a sizable overhead, yes.
1
u/fear-of-flying Sep 28 '16
What is "sizable"? Is it a linear slowdown? quadratic? Is it still practical for high-er performance programs?
3
u/ElvishJerricco Sep 28 '16
The performance overhead with the "fastest" free monads is a constant time overhead. But the constants are quite large and can be problematic. /u/edwardkmett has more experience with profiling free monads than I do.
1
u/fear-of-flying Sep 28 '16
Cool. I am mostly interested in the semi-high performance case like high performance web backends (cases where CPU actually does dominate DB lookups). But not things like graphics or fancy GUIs.
4
u/ElvishJerricco Sep 28 '16
Ultimately, the more you can do with just
fmap, the less negatively impactful the free Monad will be. Assuming you factor outfmap. That is, this function:fmap f . fmap g . fmap hwill incur performance problems, while this function:fmap (f . g . h)will be much faster. The more you wrap in a singlefmap, the closer to zero the performance cost becomes.2
u/bartavelle Sep 27 '16
The article seems to hint that you can regain some performance thanks to the inspection capabilities, in this excerpt:
No Introspection. MTL does not allow introspection of the structure of the program for the purpose of applying a dynamic transformation. For one, this means program fragments cannot be optimized. Other implications of this limitation are being explored as new ways are discovered to use free monads.
4
u/rredpoppy Sep 27 '16
Would be interesting to see if this idea, together with Tangible Values would actually improve the sorry state of functional GUIs. The power is there, need to apply elbow grease
9
u/natefaubion Sep 27 '16
We use a "lite" version of this in the slamdata UI. Specifically we use the MTL-like classes + Free interpreter at the edge. A common, real-world example for us that is solved with much anguish otherwise is around backend services and authentication.
We use a token and auth provider for authenticating backend requests. We get to write all of our business logic around requests as if there is no authentication using our API algebra. Then at runtime, if a request fails due to authentication, it can affect the UI, bringing up an authentication routine, reauthenticate the user, and retry the request in a manner completely transparent to the caller.
Previously we passed around everything we needed for this kind of stuff in a "final" manner with records, and it was awful. So I think there's definitely a lot of merit to this approach when building UIs.
2
u/fear-of-flying Sep 27 '16
What are you using for the UI? Scala.js/GHCJS/PureScript? Other? And how do you find the performance of what you are using and have you tested the performance of other things (I am mostly interested in GHCJS performance)
7
u/natefaubion Sep 27 '16
We use PureScript https://github.com/slamdata/slamdata with our own UI framework. Our main src tree is 40k SLOC of PureScript, supported by about 60k SLOC of PureScript core libraries and libraries we've written. Performance is definitely acceptable. We aren't building intense games or anything, but we do lots of mouse-move type inputs for dragging and all that, and we want to keep it smooth. Keep in mind this approach applies to all of our business-logic-component-state-transition type stuff, and most inputs don't incur a huge cost. This doesn't apply to performance intensive "effects" like low-level DOM diffing which occur outside of this framework.
2
u/fear-of-flying Sep 27 '16
Wow, thats a lot of PureScript! Pretty awesome that you've open sourced it.
I am still wondering about GHCJS performance and whether or not I should choose it or PureScript. I haven't seen nearly as large a project in GHCJS, and the browser-specific library situation isn't nearly as good -- but it is Haskell.
2
u/rtpg Sep 28 '16
could you go into a bit more detail about how the API logic and the UI logic communicated on the whole "bring up the auth routine" part ? Was it a bit ad-hoc or something more fundamental?
1
u/natefaubion Sep 28 '16
I don't really know what you are hoping for when you say "fundamental". We use the same machinery to expose event sources, implemented using something analogous to a broadcast channel.
3
u/Faucelme Sep 27 '16 edited Sep 27 '16
Free is a fixed-point type for describing value-producing programs whose unfolding structure depends on runtime values.
What this means is that you can leverage suitably-generic recursion schemes to analyze and transform Free programs!
Since the functor will include functions ("dependency on runtime values") it seems that vanilla recursion schemes won't be enough, you'll need something like bananas in space.
What is a good exposition of recursion schemes over free monads?
3
u/nicolast Sep 27 '16
2
u/Faucelme Sep 27 '16
That's an informative link, but the free monad discussed there doesn't have a function type.
How do you pretty print a free monad program that, at some point, requests input from the user?
2
u/nicolast Sep 28 '16
Had such a case very recently. Didn't find a solution then, can't think of one now :)
1
u/andrewthad Sep 28 '16
You cannot pretty print a free monad unless the base functor does not have any lambdas. The show instance for
Freehas:(Show (f (Free f a)), Show a) => Show (Free f a)It could alternatively be formulated as:
(Show1 f, Show a) => Show (Free f a)Basically, if you cannot pretty print the base functor, there's no hope of pretty printing
Free.
3
u/kamatsu Sep 28 '16
Free monads permit unlimited introspection and transformation of the structure of your program.
This isn't really true. You can't introspect past a lambda.
2
u/atc Sep 27 '16
Why all this talk of interpreters? It's assumed that's the domain, which isn't always the case? What have I missed?
5
u/ElvishJerricco Sep 27 '16
In the context of free monads, "interpreter" means something different. When you write a program in terms of a free monad, that program has to be "interpreted" back to some concrete answer; usually another monad like
IO. To do this interpretation, you write an interpreter that the free monad uses to walk through each effect and convert it to a useful result.1
u/atc Sep 27 '16
Thank you.
Any recommended resources on Free Monads I could read?
10
u/mgiles Sep 27 '16
4
u/atc Sep 27 '16
Aha my favourite Haskeller - thank you again.
2
u/bheklilr Sep 27 '16
As a follow up, I asked a relevant question on SO a while back that I think got two fantastic answers (one written by /u/tekmo) that lead into the Data Types a la Carte paper, and recently there was an improvement to this idea posted on reddit.
2
u/ElvishJerricco Sep 27 '16
I don't think there was any particular article I read that gave me the "aha!" on free monads, so I can't give you much more than what a google search would turn up. But I will say that it depends on the angle you like to attack from. If you're familiar at all with category theory, it'll probably help to think of them more abstractly. If you're more interested in the concrete, it might help to ask why they matter.
2
u/alexchandel Sep 29 '16
Is it just me or does this involve lots of duplicated code?
class Banking f where
accounts :: f (NonEmptyList Account)
balance :: Account -> f Amount
transfer :: Amount -> From Account -> To Account -> f TransferResult
withdraw :: Amount -> f Amountvs
data BankingF a
= Accounts (NonEmptyList Account -> a)
| Balance Account (Amount -> a)
| Transfer Amount (From Account) (To Account) (TransferResult -> a)
| Withdraw Amount (Amount -> a)
instance bankingBankingF :: Banking BankingF where
accounts = Accounts id
balance a = Balance a id
transfer a f t = Transfer a f t id
withdraw a = Withdraw a idvs
instance bankingFree :: (Banking f) => Banking (Free f) where
accounts = liftF accounts
balance a = liftF (balance a)
transfer a f t = liftF (transfer a f t)
withdraw a = liftF (withdraw a)5
u/Kametrixom Sep 27 '16
Old-fashioned Free code is monomorphic in the functor type. With functor injection, this becomes more polymorphic, but functor injection is just a special case of polymorphic functors whose capabilities are described by type classes.
Umm yes, of course.
2
Sep 27 '16
It sounds ridiculous, but the upside is that you can find things like functors in math/computer science literature. And of course polymorphism is just from regular computer programming.
1
u/TotesMessenger Sep 28 '16
1
u/muhbaasu Oct 01 '16
I'm currently trying to understand the code by reimplementing it in Haskell (his code seems to be written in Purescript). However, I can't get the example function to compile:
example :: forall f. (Inject Banking f) => Free f Amount
example = do
as <- accounts
b <- balance (head as)
return bThe error I get says:
• Expected a constraint, but ‘Inject Banking f’ has kind ‘*’
• In the type signature:
example :: (Inject Banking f) => Free f AmountI'm not entirely sure I understand the error correctly. For future reference, Inject is defined in another post of his:
type Inject f g = forall a. Control.Lens.Prism' (f a) (g a)This has kind (* -> *) -> (* -> *) -> *. The kind of Banking is (* -> *) -> Constraint, which means it doesn't match the expected kind of * -> * in the first argument of Inject.
I don't understand how this code should look like in Haskell or if the concept itself cannot be implemented at all.
1
u/bschroed Oct 24 '16
I've been working on a Haskell implementation of this stuff. Hopefully this helps: https://github.com/bts/free-transformers
1
5
u/andrewthad Sep 28 '16
This is not true. Or more accurately, it's only true when your base functor doesn't have any lambdas in it.