# Non-empty list

### From HaskellWiki

(Inserted text from the Haskell-Cafe discussion.) |
(non-empty list packages) |

(4 intermediate revisions by 4 users not shown) |

## Revision as of 16:08, 7 September 2012

Errors such as takingempty list in Haskell are equivalent to the dereferencing of the zero
pointer in C/C++ or `NullPointerException`

in Java. These
errors occur because the true domain of the function is smaller than
the function's type suggests. For example, the type of

reality, it can be meaningfully applied only to non-empty lists. One can eliminate such errors by giving functions

that.

It must be emphasized that we can eliminate head-of-empty-list errors
**now**, without any modification to the Haskell type system, without
developing any new tool. In fact, it is possible in Haskell98! The
same technique applies to OCaml and even Java and C++. The *only*
required advancement is in our thinking and programming style.

Maybe, you are also interested in advocacy of this style.

## 1 Safe list functions

Here's the 0th approximation of the advocated approach:

{-# Haskell98! #-} -- Safe list functions module NList (FullList, fromFL, indeedFL, decon, head, tail, Listable (..) ) where import Prelude hiding (head, tail) newtype FullList a = FullList [a] -- data constructor is not exported! fromFL (FullList x) = x -- Injection into general lists -- The following is an analogue of `maybe' indeedFL :: [a] -> w -> (FullList a -> w) -> w indeedFL x on_empty on_full | null x = on_empty | otherwise = on_full $ FullList x -- A possible alternative, with an extra Maybe tagging -- indeedFL :: [a] -> Maybe (FullList a) -- A more direct analogue of `maybe', for lists decon :: [a] -> w -> (a -> [a] -> w) -> w decon [] on_empty on_full = on_empty decon (h:t) on_empty on_full = on_full h t -- The following are _total_ functions -- They are guaranteed to be safe, and so we could have used -- unsafeHead# and unsafeTail# if GHC provides though... head :: FullList a -> a head (FullList (x:_)) = x tail :: FullList a -> [a] tail (FullList (_:x)) = x -- Mapping over a non-empty list gives a non-empty list instance Functor FullList where fmap f (FullList x) = FullList $ map f x -- Adding something to a general list surely gives a non-empty list infixr 5 !: class Listable l where (!:) :: a -> l a -> FullList a instance Listable [] where (!:) h t = FullList (h:t) instance Listable FullList where (!:) h (FullList t) = FullList (h:t)

Now we can write

import NList import Prelude hiding (head, tail) safe_reverse l = loop l [] where loop l accum = indeedFL l accum $ (\l -> loop (tail l) (head l : accum)) test1 = safe_reverse [1,2,3]

As we can see, the null test is algorithmic. After we've done it, head and tail no longer need to check for null list. Those head and tail functions are total. Thus we achieve both safety and performance.

We can also write

-- Again, we are statically assured of no head [] error! test2 = head $ 1 !: 2 !: 3 !: []

I should point to Lightweight dependent typing for justification and formalization, as well as for for further, more complex examples. We can also use the approach to ensure various control properties, e.g., the yield property: a thread may not invoke `yield' while holding a lock. We can assure this property both for recursive and non-recursive locks.

If there is a surprise in this, it is in the triviality of approach. One can't help but wonder why don't we program in this style.

## 2 Integrating with the existing list-processing functions

Jan-Willem Maessen wrote:

In addition, we have this rather nice assembly of functions which work on ordinary lists. Sadly, rewriting them all to also work on NonEmptyList or MySpecialInvariantList is a nontrivial task.

That's an excellent question. Indeed, let us assume we have a function

foo:: [a] -> [a]

(whose code, if available, we'd rather not change) and we want to write something like

\l -> [head l, head (foo l)]

\l -> indeedFL l onempty (\l -> [head l, head (foo l)])

the latter function explicitly, which keeps the typesystem simple, free of subtyping and implicit coercions. One may regard

write explicitly, in any code with more than one sort of integral numbers (e.g., Int and Integer, or Int and CInt).

If we are not sure if our function foo maps non-empty lists to non-empty lists, we really should handle the empty list case:

\l -> indeedFL l onempty $ \l -> [head l, indeedFL (foo $ fromFL l) onempty' head]

If we have a hunch that foo maps non-empty lists to non-empty lists, but we are too busy to verify it, we can write

\l -> indeedFL l onempty $ \l -> [head l, indeedFL (foo $ fromFL l) (error msg) head] where msg = "I'm quite sure foo maps non-empty lists to " ++ "non-empty lists. I'll be darned if it doesn't."

That would get the code running. Possibly at some future date (during the code review?) I'll be called to justify my hunch, to whatever degree of formality (informal argument, formal proof) required by the policies in effect. If I fail at this justification, I'd better think what to do if the result of foo is really the empty list. If I succeed, I'd be given permission to update the module NList with the following definition

nfoo (FullList x) = FullList $ foo x

after which I could write

\l -> indeedFL l onempty (\l -> [head l, head (nfoo l)])

with no extra empty list checks.

Excerpted from the discussion on Haskell-Cafe, November 2006.

## 3 Packages

These packages implement non-empty lists: