News for hackers. Stuff that matters.
Re:Monadic I/O and Unix shell programming (2001) by thinkpad20 on Sunday February 18, 2018 @10:51PM (#16410639)
The analogy has a couple of problems with it. There is the fact that programs can be run in parallel, with one program feeding the other output in real time. This one the article covers although I don’t completely buy their explanation.

For another example, ‘cmd | cat’ is often not the same thing as ‘cmd’, because a command can inspect whether stdout is a tty or not and alter its behavior. This is why ‘ls’ normally outputs all in one line, but changes to one-per-line when used with a pipe (e.g. grep).

The other issue that comes to mind is that bash provides multiple concepts of “monadic” sequencing; not just with pipes but with the && and || operators, if logic, for/while loops, etc, which form a more traditional “imperative programming” monad. Perhaps bash could be considered then to be a nested monad (monad transformer?) but I’m not sure how precisely you could model its semantics while keeping things elegant.

But yes, in general you can view the shell as a monad, and there are libraries in Haskell which do exactly this (Shelly and Turtle come to mind). In general, the divergences from a purely monadic model come from the fiddly bits of real-world programming which can cause edge cases.

There are no comments on this comment yet.