by koolala · 3y ago · 0 comments
monad 1 | monad 2 | monad 3
It doesn't have to be complicated.
The article is saying that by judicious use of a "strength" function, you can in fact write any monadic computation as such a pipeline (or composition of monadic functions).
I love this subject but it's traditionally over-complex, so I appreciate the correction and explaination.
How could "strength" be applied generically to programs outside of Haskell? Could the concept of pipelines be extended? Is programming too 1 dimensional? There must be ways to do this.
do x <- computation1
y <- computation2 x
computation3 x y
...turning a one-dimensional sequence of operations into a data-flow graph represented textually.But, and perhaps this is an academic question, what if you're stuck in a context where you can only create computations by composing functions in a sequence, without giving names to intermediate results? Shell pipelines, for example, without using temporary files or other tricks.
Then, using this idea of strong and weak monads, and if you were used to thinking of computations as elements of a monad type, you might realize that all you need to add is a "strength" operator, and you would regain the full expressivity of data flow graphs. This operator would take a computation and bundle it with value, producing a new computation that does the same computation, but bundles its result with the extra value you supplied. So you've given every computation a side-channel that ferries any data you like through it, unchanged. Computations further down the chain can access both values.
I lack the imagination to see how this example works out in shell, because there we don't have the luxury of multiple input and output values (just single pipes of data). But it's nice to know that just the strength operation is enough to express arbitrary computation graphs, with names being given to inputs and outputs.
a -> b -> c
\_______^
so c gets the results of both a and b "for free".Couldn't this same idea be applied generically to all computer programs? Could its valuable situational properties ever be done in a way that is language neutral with arbitrary program monads?
There are some examples of attempted implementations the list monad in other languages on RosettaCode https://rosettacode.org/wiki/Monads/List_monad
Caveat emptor, though. A lot of that page is erroneous.
A few years ago I fixed up the definition for F#, which is perhaps the closest thing to a language for ordinary Joes in which true monadic style can be defined/achieved.
That is what I mean - it isn't a fair thing for me to say - but I feel like they would be so much more tangible if we could think about them as literal Things like this. Which is why my original example was composition of Programs instead of composition in a specific Programming Language.
[1, 2] >>= \x -> ([x, 10*x] >>= \y -> [x*y])
There is no associativity of >>=, any more than there's associativity of + in: 12 + 34*(56 + 7*8)
(Probably less, since + and * are distributive.) test3 = [1, 2] >>= (\x -> [x, 10*x] >>= \y -> [x*y])
which to a non-Haskeller like me does make it look more right-associative than it actually is – and as the article showed, you can actually make it "left-associative" in the obvious way, which would indeed compile fine and be equivalent to the "right-associative" way if the y lambda didn't refer to x. test3 = ([1, 2] >>= \x -> [x, 10*x]) >>= \y -> [x*y]https://gjoncas.github.io/posts/2020-05-30-monad-associativi...