[Haskell-cafe] IO (Either a Error) question

David Menendez dave at zednenem.com
Sat May 8 02:14:50 EDT 2010


On Sat, May 8, 2010 at 1:16 AM, Ivan Lazar Miljenovic
<ivan.miljenovic at gmail.com> wrote:
> David Menendez <dave at zednenem.com> writes:
>
>> On Sat, May 8, 2010 at 12:15 AM, Ivan Lazar Miljenovic
>>> Well, any time you have a do-block like this you're using failable
>>> patterns:
>>>
>>> maybeAdd       :: Maybe Int -> Maybe Int -> Maybe Int
>>> maybeAdd mx my = do x <- mx
>>>                    y <- my
>>>                    return $ x + y
>>
>> This is true in the sense that the translation for the do syntax in
>> the Haskell report uses fail.
>>
>> do { p <- e; stmts } =
>>     let ok p = do { stmts }
>>         ok _ = fail "..."
>>     in e >>= ok
>>
>> However, it's also true that the fails introduced by the translation
>> of maybeAdd will never be invoked, since the two patterns are
>> irrefutable.
>
> Huh?  What about "maybeAdd (Just 2) Nothing" ?

That does not invoke fail.

Let's take a simpler example: do { x <- Nothing; stmt }. This translates to

let
    ok x = do { stmt }
    ok _ = fail "..."
in Nothing >>= ok

By the definition of (>>=) for Maybe, 'ok' is never called.

>> That is, maybeAdd would work exactly the same if the do syntax
>> translation were changed to read:
>>
>> do { p <- e; stmts } = e >>= \p -> do { stmts }
>
> Wait, are you using "irrefutable" as "it will still work if we make do
> blocks work the way I want"?

I am using "irrefutable" to refer to patterns which always match. From
the Haskell Report, section 3.17.2:

> It is sometimes helpful to distinguish two kinds of patterns. Matching an
> irrefutable pattern is non-strict: the pattern matches even if the value to be
> matched is _|_. Matching a refutable pattern is strict: if the value to be
> matched is _|_ the match diverges. The irrefutable patterns are as follows:
> a variable, a wildcard, N apat where N is a constructor defined by newtype
> and apat is irrefutable (see Section 4.2.3), var at apat where apat is irrefutable,
> or of the form ~apat (whether or not apat is irrefutable). All other patterns
> are refutable.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list