Live data from Hacker News

Free monads from scratch

siraben.dev

11–17 of 17 posts

Re: Free monads from scratch

#11

> While it looks like boilerplate, we can more or less mechanically write out the instances… Yes so why doesn’t some GHC extension do this? The issue I have with this and Functors and Monads and Monad transformers is that there is a lot of boilerplate. I wish i could apply f x y instead of lift $ f x pure y. It would be awesome if there was a ghc extension which, whenever it encounters something like ‘(a -> b -> c) a…

Doing the magic stuff you say doesn't work well due to the ambiguities that could result.

Applicative style is so trivial that it's hard to say it's worth it to automate it at the cost of ambiguities.

Feel free to write a GHC proposal to solve this trivial deficiency - anyone! I dare you! I'd like to see this complaint's rubber hit the road.

Re: Free monads from scratch

#12
post #2

Monads are hard. I read many articles explaining what a monad is and what is good for but I never really grasped the concept. I probably have to learn Haskell.

Yes, and monads in Haskell seem a bit different from monoids in the category of endofunctors.

Re: Free monads from scratch

#13
post #10
post #2

Monads are hard. I read many articles explaining what a monad is and what is good for but I never really grasped the concept. I probably have to learn Haskell.

There's an endless stream of monad tutorials and I think they (almost) all misunderstand the point of confusion. No one gives a f*** about monads, they care about state. If you've internalized reasoning about a program's execution symbolically and in a time-independent way, then monads solve the problem of how you enforce a correct sequencing of otherwise time-independent operations at a library level without any fan…

Thanks for explanation. The point is that I do not really intend to learn Haskell (I don't have the motivation for), I've been just (very) curious to understand what's about these concept called monad: - Why does should exist? Why can't they be simply replaced with control structures (if, case, switch etc.) ? - Why there is so much fuss on the web, in the FP world, about monads?

Re: Free monads from scratch

#14
post #2

Monads are hard. I read many articles explaining what a monad is and what is good for but I never really grasped the concept. I probably have to learn Haskell.

I read once, somewhere, an advice from a computer scientist: one with OOP background should perceive a monad as a class that has everything contained: types, declarations, methods, error handling. Is it true ?

Re: Free monads from scratch

#15
post #2

Monads are hard. I read many articles explaining what a monad is and what is good for but I never really grasped the concept. I probably have to learn Haskell.

This was how it was taught at my school in Java [0], although thinking about monads as a container is weird since functions are monads as well.

[0] https://nus-cs2030s.github.io/2021-s2/33-monad.html

Re: Free monads from scratch

#16
post #10

Earlier quoted context omitted.

There's an endless stream of monad tutorials and I think they (almost) all misunderstand the point of confusion. No one gives a f*** about monads, they care about state. If you've internalized reasoning about a program's execution symbolically and in a time-independent way, then monads solve the problem of how you enforce a correct sequencing of otherwise time-independent operations at a library level without any fan…

Thanks for explanation. The point is that I do not really intend to learn Haskell (I don't have the motivation for), I've been just (very) curious to understand what's about these concept called monad: - Why does should exist? Why can't they be simply replaced with control structures (if, case, switch etc.) ? - Why there is so much fuss on the web, in the FP world, about monads?

They can't be explicitly replaced with control structures because implicit in that suggestion is the idea that when writing code you write what the code _does_. If you call the same function with the same inputs you can expect exactly the same machine level code to execute (perhaps taking different branches based on external state it reaches out for).

Contrast that with what happens in Haskell and friends. When writing code you define what the _inputs and outputs_ for particular functions should be. The actual code generating those inputs and outputs is free to be replaced or removed as the compiler sees fit. That buys you a lot of things (trivial parallelization, excellent type checking, ...), but it self inflicts an extra problem we didn't have in the previous paradigm:

In the real world, we don't in fact just want to run a program and get an output. The way we interact with, e.g., a GUI is an important part of the program's behavior. If your mental model of code is that we're sequentially doing a series of things then this isn't ever a problem you would even have because you would just write your code to do the right things at the right time, but if you've adopted a model where you're defining outputs for your inputs you need some way to shove state and order of effects into that system for it to be useful.

Enter stage-left: monads! Yes they're pretty and ubiquitous and whatever, but the problem they solve for us is specifying an order of events (by virtue of taking the entire monad in as an input and spitting it out as an output) in a way wholly compatible with the type system we've already developed.

Re: Free monads from scratch

#17
post #4

> While it looks like boilerplate, we can more or less mechanically write out the instances… Yes so why doesn’t some GHC extension do this? The issue I have with this and Functors and Monads and Monad transformers is that there is a lot of boilerplate. I wish i could apply f x y instead of lift $ f x pure y. It would be awesome if there was a ghc extension which, whenever it encounters something like ‘(a -> b -> c) a…

> I wish i could apply f x y instead of lift $ f x pure y. In "Applicative Programming with Effects"[0], the authors use such a notation, and even leave it as an exercise to implement it in Haskell using MultiParamTypeClasses: > Given Haskell extended with multi-parameter type classes, enthusiasts for overloading may replace ‘⟦’ and ‘⟧’ by identifiers i[ and ]i with the right behaviour. So they have code that looks l…

It is possible to derive Applicative.

The next release of GHC is able to derive Applicative for product types: `deriving Applicative via Generically1 F`.

If you have a sum type you can derive Applicative as well, but as you mention there are often multiple options so this requires user assistance: `deriving Applicative via Idiomatically F '[LeftBias Id]`.

Here is the library that allows that: https://hackage.haskell.org/package/idiomatic

Post reply on HN