What’s the idiomatic type to represent multiple failure cases but only one success case?

10 idioms and their meanings with sentences
20 idioms with their meanings and sentences
100 idiomatic expressions
idioms examples for students
idioms dictionary
idioms and proverbs
modern idioms
50 phrases with meanings

The type Maybe a represents a computation that could fail, with the semantics that we don’t care about exactly how it failed. If the computation succeeds then any value of type a might be returned.

What about the inverse case, where the computation could fail for any number of reasons (and we want to preserve that information) but success doesn’t involve any information other than "yes, it succeeded"? I can think of two obvious ways to encode this kind of computation:

  • Maybe e, where Just e represents a failure and Nothing represents a success. This is so contrary to the usual use of Maybe that I would be reluctant to use it.
  • Either e (), where Left e represents a failure and Right () represents a success. This has the advantage of being explicit, but has the disadvantage of… being explicit. Writing () feels awkward, especially outside the context of a type signature.

Does Haskell have a more idiomatic way to represent "multiple failure cases but only one success case"?

A short way to go about this would be to use Either e () along with the pattern synonym

pattern Success :: Either e ()  -- Explicit type signature not necessary
pattern Success = Right ()

You could also include some other things as well, if it improves readability, such as

type FailableWith e = Either e ()

pattern FailedWith :: e -> FailableWith e
pattern FailedWith x = Left x

Unlike Maybe, Either has the advantage of having all the existing machinery already in place: the Functor, Applicative, Monad, Foldable, Traversable, Semigroup and even Ord (Left x < Right y should always hold) instances will likely already behave exactly how you would want for error handling. Generally, for this particular situation, Maybe will tend to do the opposite of what you want (usually you want to continue on a success and stop after the first failure, which is the opposite of what most of the Maybe machinery will provide for this scenario).

What is the idiomatic way to return an error from a function with no , I would expect Option<WorkError> only be used in cases like fn an operation that may fail doesn't return anything useful (e.g. many Result is a type that represents either success (Ok) or failure (Err) You can document that the "​return nothing" case means that the function was actually successful, but  Read and learn for free about the following article: Probabilities involving "at least one" success If you're seeing this message, it means we're having trouble loading external resources on our website.

Without seeing the actual code it is actually difficult to understand what you mean by failure. If it's a pure function then I don't see what using Maybe would be a problem. I never really see Nothing as failure but just as it is : Nothing. Depending on the context , I either return Nothing as well or, use a default value and carry on. I understand that it can be seen as a failure, but it more depends on the point of view if the caller than the function itself. Now, you want to represent a computation which can fails but returns nothing. If it is a pure function, that doesn't make sense. You function being pure, nothing has happened (no side effect) and you don't get a result. So in case of success, you actually computed nothing : that's not a success, that's nothing. ATHI If you fail, you've got a reason why it failed. That's no different from a simple check returning a Maybe.

For example you might need to check that a domain is not in a blacklist. For that you do a lookup in a list : Nothing means it's fine even though it means it's from your point of view and failure and need to stop your computation. The same code can be used to check your domain belongs to a white list. in that case Nothing is a failure : just depends on the context.

Now, if you are running a monadic action (like saving a file or something) it makes sense to return nothing but different failures can happened (disk full, path incorrect, etc). The standard signature for an IO which we don't care about the result is IO (), so you can either go for IO (Either e ()) (everybody will understand it) or go for IO () and raises exception (if they are genuinely exceptional).

Is there an English idiom for trying to do two things at the same time , spread yourself too thin: to try to do too many things at the same time, so that sort of stretched, if you know what I mean: like butter that has been scraped over too much bread." but both of which you fail at because you address them in parallel. Precisely what happens: you try to put one butt-cheek on each stool, but end  The outbreak of a mysterious illness that ravaged participants at the American Legion's July 1976 convention caused public attention. At first it seemed like the disase was highly fatal, but research found a much lower case fatality rate. Only about 15% of people who developed the disease died from it.

Its not clear from the question how the computation might fail.

If it is something like a compiler, which might produce a lot of error messages (rather than halting on the first one) then you want something like:

type MyResult a = Either [Error] a

which either succeeds with a result or fails with a list of reasons.

On the other hand if you have a non-deterministic computation where each variation might succeed or fail then you want something more like:

type MyResult a = [Either Error a]

Then search the list of results. If you find a Right then return it, otherwise assemble the list of Lefts.

What would be good/idiomatic way of naming functions that either , So coming up with functions that return nothing of significance or am not allowed to link that library here] for representing failed computations. But naming it transition-possible? give an impression that the either return a success case or a failure case so the functions can be There're so many cases. Idioms! What is an idiom? Learn idiom definition, common idioms list in English with meaning, idiom examples and ESL pictures. Native English speakers, or of any language for that matter, naturally inherit the knowledge to know what idioms mean because they have the benefit of hearing them every day as they grow up.

200 Common Idioms with Meanings, Examples, and 4 Quizzes , An idiom is a phrase or expression that has a meaning that in most cases You may also find list of 150 proverbs (with meanings and examples) and If you make no bones about something, you say clearly what you feel or think about it. Example: With only half the syllabus studied, I raced against the  Although the Steinhoff saga is being viewed in some quarters as one of the worst cases of business fraud that the world has ever seen, it might be argued by those implicated that the effect on the

[PDF] Idioms, Collocations, and Structure, many idioms in fact do seem to be compositional, just not literal. In such cases, I will classify the meaning in the expression as literal if it occurs I am not aware of any phrasal idioms of this type, but again adverbial phrases are There is also at least one case with no main verb, but a modal and adverb with an open. Synonyms for failure at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for failure.

(PDF) Idioms and language users : the effect of the characteristics of , PDF | On Jan 1, 2004, Katja Mäntylä and others published Idioms and language figurative multi-word expression whose meaning is different from the sum of English idioms and to what extent their interpretations agree with or There are bound to be borderline cases, and just as it is In each case, the conventional. We are programmed at an early age to think that failure is bad. That belief prevents organizations from effectively learning from their missteps. 1 / 2 Free Articles left Remaining Register for

Comments
  • You are looking for some kind of algebraic data type called Result defined by something like data Result = Ok | Error String. You can get fancier and use type variables for different kinds of errors and even add stuff to the Ok part. Elm has this type built in. Does Haskell? It might, look around. :)
  • Hmm, what about using Either e () and creating a pattern synonym pattern Success = Right ()?
  • If you're working with monadic computation, then ExceptT err m () would be my first instinct - an action that either succeeds with no result (success) or produces an error.
  • A computation that doesn't return any information other than "yes it succeeded" doesn't sound very useful outside of an IO action, and as far as I know the most idiomatic way to represent errors in that context is with exceptions, no? It really depends on what the function does, honestly.
  • System.Exit defines data ExitCode = ExitSuccess | ExitFailure Int, which is basically an exit-status-specific version of Ray Toal's Result suggestion. ExitCode also has an Exception instance, which implies the "standard" thing to do is define your own domain-specific type.
  • The pattern synonyms is a bit point less as one will use only if he already knows the semantics behind the Either e (). Itll be better using Either e Success with data Success = Success.
  • @mb14 Hmm, what makes that better? That just replaces Right () with Right Success instead of just Success. Also, I'm not sure I understand what you're saying. The reason for the pattern synonym and the type synonym would be to make things more clear to people who are not completely aware of the intent of the Either e ().
  • Type synonyms doesn't provide guarantee (or safety or semantic as they can disappear) and add unnecessary level of indirection. I think Eithe e () is clear enough, but Eithe e Success enforces a semantic if really needed.
  • The Monad implementation for Maybe treats Nothing as a failure.