Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I do `null (Just 5)` in Haskell?

The Hackage documentation for Maybe lists Foldable as one of Maybe's typeclasses. It also lists the following function:

null :: Maybe a -> Bool

It even links to this function's implementation (from Foldable):

null :: t a -> Bool
null = foldr (\_ _ -> False) True

...which seems rather reasonable. It works, too: I can, if I import qualified Data.Foldable, use foldr on Maybe values.

However, when I try to call null on a Maybe, Haskell thinks I want to use the null designed for lists:

Prelude> :t null
null :: [a] -> Bool
Prelude> null Nothing
<interactive>:3:6:
    Couldn't match expected type `[a0]' with actual type `Maybe a1'
    In the first argument of `null', namely `Nothing'
    In the expression: null Nothing
    In an equation for `it': it = null Nothing

I know there is isJust, I'm just wondering how to call a function like null for any Foldable.

like image 271
Wander Nauta Avatar asked Dec 20 '22 00:12

Wander Nauta


1 Answers

As it turns out, I was running an older version of GHC (the default version for my OS), while the documentation was for the newest version (of course).

In GHC 7.10.2 at least, the null that you get out of Prelude supports Foldables (like Maybe) without having to import anything:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> :t null
null :: Foldable t => t a -> Bool
like image 96
Wander Nauta Avatar answered Jan 09 '23 03:01

Wander Nauta