
Hi, What is the proper way to implement a non-monadic function that checks whether a given value is correct and gives a proper error message otherwise ? What is the recommended option ? * Either String a check val | valid val = Right val | otherwise = Left errorMsg * Maybe String check val | valid val = Nothing | otherwise = Just errorMsg Cheers, Jose

On Mon, Aug 19, 2013 at 9:48 PM,
Hi,
Hello!
What is the proper way to implement a non-monadic function that checks whether a given value is correct and gives a proper error message otherwise ? What is the recommended option ?
I am not sure, what do you mean by non-monadic. Both (Either String) and Maybe are monads. You can pick up whatever option you like, depending on which option, in your opinion, suits you better for your specific case. There is also a helpful errors [1] package that provide convenient means of converting between the results of the two approaches. Control.Error.Util.hush :: Either a b -> Maybe b Control.Error.Util.note :: a -> Maybe b -> Either a b
* Either String a
check val | valid val = Right val | otherwise = Left errorMsg
* Maybe String
check val | valid val = Nothing | otherwise = Just errorMsg
Cheers, Jose
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[1] http://hackage.haskell.org/package/errors -- Sincerely yours, -- Daniil

Yeah, non-monadic is not the best term... The problem is that it's always so hard to communicate when you want to say a total function that is not in the context of the IO monad. There should be a simple, short name for these functions, so we can easily talk about them. What ends up happening a lot of the times is that people on the mailing list call these "pure" functions, and immediately they get an email saying that Haskell is pure, so everything is a pure function. In the end, it doesn't really help... Jose On Mon, Aug 19, 2013 at 10:09:13PM +0400, Daniel F wrote:
On Mon, Aug 19, 2013 at 9:48 PM,
wrote: Hi,
Hello!
What is the proper way to implement a non-monadic function that checks whether a given value is correct and gives a proper error message otherwise ? What is the recommended option ?
I am not sure, what do you mean by non-monadic. Both (Either String) and Maybe are monads.
You can pick up whatever option you like, depending on which option, in your opinion, suits you better for your specific case. There is also a helpful errors [1] package that provide convenient means of converting between the results of the two approaches.
Control.Error.Util.hush :: Either a b -> Maybe b Control.Error.Util.note :: a -> Maybe b -> Either a b
* Either String a
check val | valid val = Right val | otherwise = Left errorMsg
* Maybe String
check val | valid val = Nothing | otherwise = Just errorMsg
Cheers, Jose
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[1] http://hackage.haskell.org/package/errors
-- Sincerely yours, -- Daniil

On Mon, Aug 19, 2013 at 02:20:23PM -0400, jabolopes@google.com wrote:
Yeah, non-monadic is not the best term... The problem is that it's always so hard to communicate when you want to say a total function that is not in the context of the IO monad. There should be a simple, short name for these functions, so we can easily talk about them.
Why, what would be different in your question for a non-total function or one in the context of the IO monad? Tom

I'd say that if you were in the context of the IO monad, maybe you'd prefer to use exceptions instead of 'Either' or 'Maybe'. Jose On Mon, Aug 19, 2013 at 07:41:48PM +0100, Tom Ellis wrote:
On Mon, Aug 19, 2013 at 02:20:23PM -0400, jabolopes@google.com wrote:
Yeah, non-monadic is not the best term... The problem is that it's always so hard to communicate when you want to say a total function that is not in the context of the IO monad. There should be a simple, short name for these functions, so we can easily talk about them.
Why, what would be different in your question for a non-total function or one in the context of the IO monad?
Tom
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Aug 19, 2013 at 2:59 PM,
I'd say that if you were in the context of the IO monad, maybe you'd prefer to use exceptions instead of 'Either' or 'Maybe'.
Even in IO, exceptions should be reserved for truly exceptional conditions (of the "program cannot safely continue" variety), not merely for error checking when this can be described as a normal flow of evaluation. Exceptions are not simply alternative flow of control, even in procedural languages; they are *disruptions* of flow of control. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Except that people generally don't seem to agree what constitutes
'exceptional', even when disregarding the python world...
On Aug 19, 2013 9:24 PM, "Brandon Allbery"
On Mon, Aug 19, 2013 at 2:59 PM,
wrote: I'd say that if you were in the context of the IO monad, maybe you'd prefer to use exceptions instead of 'Either' or 'Maybe'.
Even in IO, exceptions should be reserved for truly exceptional conditions (of the "program cannot safely continue" variety), not merely for error checking when this can be described as a normal flow of evaluation. Exceptions are not simply alternative flow of control, even in procedural languages; they are *disruptions* of flow of control.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Aug 19, 2013 at 1:48 PM,
What is the proper way to implement a non-monadic function that checks whether a given value is correct and gives a proper error message otherwise ? What is the recommended option ?
* Either String a
Preferred, usually, since Nothing is regarded as an error condition of sorts: the Monad instance for Maybe associates Nothing with `fail`, which is invoked on failed pattern matches; likewise it's mzero for MonadPlus and mempty for Monoid, both of which use it (differently) to reflect certain "failure" scenarios). If nothing else, it would be highly confusing to see Nothing associated with success given its widespread association with failure. Alternatively, have you considered using your own ADT? `data Validity = Success | Failure String` would give you more readable / comprehensible code without needing to worry about assumptions or common usage. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Mon, Aug 19, 2013 at 2:09 PM, Brandon Allbery
Alternatively, have you considered using your own ADT? `data Validity = Success | Failure String` would give you more readable / comprehensible code without needing to worry about assumptions or common usage.
Or possibly Valid and Invalid as the constructors.... This also means you can easily extend it later to include multiple errors, or position information, or other annotations. You could also use it with Monoid and/or the Writer monad to track success/failure in the most appropriate way for your project, instead of being constrained to the behavior of an existing instance. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (5)
-
Brandon Allbery
-
Daniel F
-
jabolopes@google.com
-
Tobias Dammers
-
Tom Ellis