[Haskell-cafe] Maybe and partial functions

Neil Mitchell ndmitchell at gmail.com
Mon Mar 12 07:37:43 EDT 2007


Hi

> > head []
>
> return Nothing? I guess it's a bit of a silly suggestion, but it helps
> to highlight why we use Maybe in the first place. So --- where's the
> cutoff point in your code?

If that is what you want, then see my Safe library:
http://www-users.cs.york.ac.uk/~ndm/projects/libraries.php#safe

headMay :: [a] -> Maybe a

(note this function already exists and is called listToMaybe)

However, I think that partial functions are just great, although a lot
of people disagree with me.

There are some tools being developed to check things like head []:

Catch: http://www-users.cs.york.ac.uk/~ndm/projects/catch.php
ESC/Haskell: http://www.cl.cam.ac.uk/~nx200/research/escH-hw.ps

Catch also checks division by zero, explicit patterns etc. To see the
world of pain you would be in if you go down the "make everything
total" route, I suggest you try rewriting this program to be complete:

http://darcs.haskell.org/nofib/imaginary/digits-of-e2/Main.lhs

(if you do, please post the result to the list)

If you ignore getArgs and read (which are incomplete in the above
example), then Catch is able to prove all the remaining things
complete, without annotations or code modifications.

You might also want to try redoing that example in the type style,
discussed here: http://www.haskell.org/haskellwiki/Non-empty_list

Thanks

Neil


More information about the Haskell-Cafe mailing list