5 ms·
This pattern isn't as complex as it looks, it's just the same pattern as you've always used with some mathematical terms added to it. Having a "stderr"-like out
by klodolph 8y ago
This pattern isn't as complex as it looks, it's just the same pattern as you've always used with some mathematical terms added to it. Having a "stderr"-like output on a filter can also be expressed as a Monad. Instead of Either or Maybe, or in general error monads, you could use a writer monad like WriterT. (Haskell, not sure about F#.)
You can then keep your call/return architectural style or use pipes/filters. Haskell and F# are pretty good at providing ways of describing these things mathematically and making them easy to compose, it's just that the underlying math is obscure enough that we get blog posts about it. Fortunately, there are enough mature libraries that you don't need to understand the math to leverage this stuff.
- mpweiher 8y ago> just the same pattern as you've always used What's the "pattern I've always used"? Anyway, the problem is one of call/return in general, whether expressed in an FP language or not. It really wants to return something. With a real dataflow approach, that just goes away. And nothing is simpler than something.
- stingraycharles 8y agoThrowing exceptions can be considered a (very) poor man’s Monad, so I suspect this is the pattern they’re talking about.
- klodolph 8y ago> What's the "pattern I've always used"? The pattern is to return a value and have some non-local return for errors. For example, having a return value in C++ but throw an exception for errors. By "always used", I'm just asserting that most people are familiar with this pattern and use it frequently. > Anyway, the problem is one of call/return in general, whether expressed in an FP language or not. It really wants to return something. With a real dataflow approach, that just goes away. And nothing is simpler than something. This is not true for languages like Haskell. When you are writing monadic functions, it doesn't "want" to return something, instead, you are free to create your own data flow primitives as the monad's interface. This lets you change how control flow works in these monads while providing enough common structure that the novel control flow doesn't doesn't surprise people using the monad. Common ways to do this are by composing monad transformers, returning special value types, or using continuations. That said, you get more flexibility to define data flow in Haskell if you combine the ordinary monadic parts with data flow libraries like pipes/conduit/machines. But you're still free to not return anything from a function (or technically a monadic action) in Haskell, as long as you're in a monad where returning nothing is a possibility.
- gowld 8y agoSince Haskell is non-strict, it has "dataflow", and deprecates "call/return" to some extent. (Parent poster might not have known that.) Function "calls" can be passed around but not executed unless needed. Monads like Maybe take advantage of this. Haskell functions always return something if they are called. Every Haskell program is "main :: IO ()", returning an IO action executed by the runtime, but in the Maybe monad that something can be Nothing.
- klodolph 8y agoWith monadic structure the non-strictness is not actually necessary. Haskell is a research language with a lot of features dumped into it, the fact that it’s non-strict has forced people to be more aware of whether their code is pure, and purity has motivated the research into things like monads. But you can do the same data flow programming even in an eager version of Haskell, it’s just that in eager languages programmers are more tempted to write impure functions in the first place.
- mpweiher 8y agoHmm...using the Maybe monad as counterexample to "call/return languages need a value to represent Nothing, because they need to return something" seems odd at best. "This is not a sentence."
- OJFord 8y ago> The pattern is to return a value and have some non-local return for errors. For example, having a return value in C++ but throw an exception for errors. By "always used", I'm just asserting that most people are familiar with this pattern and use it frequently. I think it's nonetheless a helpful model.
- chriswarbo 8y ago> the problem is one of call/return in general, whether expressed in an FP language or not. It really wants to return something This is certainly a problem with first-order programming, where we might have function signatures like: foo : A -> B bar : B -> C baz : C -> D And we plumb them together into: quux : A -> D quux x = baz (bar (foo x)) IIUC the problem you're describing is that a change causes one of these functions, say `bar`, to gain an error condition. We can represent that by: bar' : B -> Maybe C Our `quux` definition would then become: quux' : A -> Maybe D quux' x = case bar' (foo x) of Nothing -> Nothing Just y -> Just (baz y) I don't think it's too bad to have a failing function return a special value like `Nothing` to indicate that: `return Nothing` is no more difficult than `throw SomeException`, `exit 1`, etc. The problem is having everything in-between the error and its handler having to propagate it, like above. This sort of 'check if successful, return Nothing if not' (anti-)pattern seems to crop up a lot with e.g. Java programmers being introduced to `Optional`. It seems like we're going to drown in null checks (or Nothing checks, in this case). Yet this problem isn't so bad if we use higher-order programming (functions of functions, etc.). In particular, we can write the first `quux` as a higher-order combination of those other functions (where `f ∘ g` is composition of `f` and `g`, i.e. a function which applies `g` to its argument, then applies `f` to that): (f ∘ g) x = f (g x) quux : A -> D quux = baz ∘ bar ∘ foo This ability to compose is really powerful. The FP community (taking their terminology from maths) call these composable things "categories", and functions are one example of a category; another is circuitry, where we can wire the output pins of one circuit to the input pins of another. In the context of this article, a (non-branching) railroad track is an example of a category (we can place one piece of track after another). There's a nice description of categories at http://www.haskellforall.com/2012/08/the-category-design-pattern.html http://www.haskellforall.com/2012/08/the-category-design-pat... Now what if we then need to propagate error conditions? We can just treat that error-propagating ('check if successful, and return Nothing if not') functionality as an alternative form of composition, which I'll write as `⋅`: (f ⋅ g) x = case g x of Nothing -> Nothing Just y -> Just (f y) quux' : A -> Maybe D quux' = baz ⋅ bar ∘ foo In fact we can be a little more general by defining a `map` function instead; this lets us do error-handling even if we only have a single function, and we can re-use the normal ∘ composition: map f x = case x of Nothing -> Nothing Just y -> f y quux' : A -> Maybe D quux' = map baz ∘ bar ∘ foo This looks like the same amount of boilerplate as the first-order version, but operations like `map` are general enough that they'll probably be included in the language by default, or at least with the library that defines `Maybe`, `Just` and `Nothing`. Hence the only code we need to write to go from `quux` to `quux'` is `map`. Just like with composition, lots of things can be 'mapped'. FP calls such things "functors", although beware of other uses for that term (e.g. some languages use this phrase to mean function objects; some, like Ocaml, use it to refer to module transformers). Another example of a functor is lists, where `map` applies a function to all of a list's elements. In fact, if we think of lists as "containing any number of values" then we can think of `Maybe` as "containing at most one value". Going the other way, if we think of `Maybe` as being "a result which either failed or succeeded" then we can think of lists as being "a result which either failed or succeeded in any number of ways", i.e. lists are just non-deterministic computations (e.g. see Wadler's "How to Replace Failure by a List of Successes" https://rkrishnan.org/files/wadler-1985.pdf https://rkrishnan.org/files/wadler-1985.pdf ) Another example of a functor is functions: a function `g : A -> B` can be thought of as 'containing' some `B` return values, which we can 'access' by providing an `A` value as input. If we have a function `f : B -> C` we can apply it to those return values by doing `map f g : A -> C`. We get a new function 'containing' `C` return values, which we can 'access' by providing an `A` value. How does such a function work? It just takes the `A` value that we give it, passes it into `g` to get a `B` return value, then passes that into `f` to get the final `C` return value. In other words, the `map` function for functions is just the `∘` composition operation! Another example of a functor is an I/O action, for example reading a temperature sensor. I/O actions are like functions, in that they 'contain' return values (e.g. temperature readings), which can only be 'accessed' by executing the action (e.g. performing a measurement). Just like functions, we can `map` over these actions as a form of composition: if we have `a : IO Temperature` and `f : Temperature -> Either Hot Cold`, then we can do a form of composition using `map`, where `map f a : IO (Either Hot Cold)` is an action which can measure whether it's hot or cold (by executing `a` then feeding the result into `f`). Another thing we might want to do is 'unwrap' nested values. For example if we have a possibly-failing function `g : A -> Maybe B` and a possibly-failing function `f : B -> Maybe C` then we can use `map f g : Maybe (Maybe C)`, which might be `Nothing` (if `g` failed), or it might be `Just Nothing` (if `g` succeeded but `f` failed) or it might be `Just (Just something)` (if both succeeded). If we don't care about which one failed, we can use a function called `join : Maybe (Maybe A) -> Maybe A` to replace `Just Nothing` with `Nothing` and `Just (Just something)` with `Just something`. FP people call joinable things "monads", and in the case of `Maybe` we can think of it as concatenating together a list-of-at-most-one-(lists-of-at-most-one-element) to get a list-of-at-most-one-element; or as turning a combination of error cases into a single error case. For lists, we can think of `join` as concatenating lists-of-lists; or as turning chains-of-successful-results into successful-results. For functions we can think of `join` as making a function which re-uses its argument for the subsequent step. For I/O we can think of `join` as turning an action-which-measures-which-action-to-take-next into an action-which-measures-then-does-something.
- jecxjo 8y agoThe real "revelation" of this method of error handling is how it uses types to explicitly state how errors are handled. Having a return type of `Either String Int` let's the user know that this function does in fact have an error case that would potentially be short circuited to a far off error handlers, and that a `String` is the type returned on an error. In most other languages you either don't know if an exception could be thrown or you just assume every function throws one. Then when you are adding in things like promises which explicitly define success and failure paths the system must implicitly catch all exceptions and pass them to your error path...unless the developer chooses to change that functionality. Much nicer to have all that defined for you in the type system, even though it existed without it.