# Laziness is not always good

Generally, since Haskell is a non-strict language, you should try to make a function least strict. This is in many cases the best semantics and the most efficient implementation. However, here is an important exception from the rule: Consider the `Monoid` instance of the null type `()`:

```mempty = ()
mappend _ _ = ()
```

These functions are least strict, but have a subtle problem: They do not generally satisfy the monoid laws. Remind you: `mempty` must be the identity element with respect to `mappend`:

```forall a. mappend mempty a = a
forall a. mappend a mempty = a
```

You find that it is not `mappend mempty undefined = undefined`, but `mappend mempty undefined = mempty`. Is this academic nitpicking or practically relevant? I think it is the latter one, because a `Monoid` instance implicitly promises that monoid laws can be applied in every case. A programmer expects that every occurence of `mappend mempty a` can be safely replaced by `a`. You might even create an optimizer rule doing this. The above implementation of `mappend` however evaluates its operands lazily, and this gets lost when the optimization is applied.

The solution of this issue is to define

```mempty = ()
mappend () () = ()

force :: () -> ()
force _ = ()
```

and write

```mappend (force a) (force b)
```

instead of `mappend a b`.

If you find that example too academic, you can choose any other data type with one constructor instead.

## Exercise

Find out whether it would help to define `mempty = undefined`.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8684839,"math_prob":0.8788139,"size":1666,"snap":"2021-31-2021-39","text_gpt3_token_len":388,"char_repetition_ratio":0.121540315,"word_repetition_ratio":0.0,"special_character_ratio":0.22388956,"punctuation_ratio":0.096666664,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9786592,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-28T20:46:59Z\",\"WARC-Record-ID\":\"<urn:uuid:6726bdde-61ad-4b9d-a957-29f157a40549>\",\"Content-Length\":\"18913\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:23c4f8a2-3000-4621-b410-ec5968c8b9a1>\",\"WARC-Concurrent-To\":\"<urn:uuid:dd453038-1b69-4cd6-92e4-b164aff4aa75>\",\"WARC-IP-Address\":\"199.232.65.175\",\"WARC-Target-URI\":\"https://wiki.haskell.org/index.php?title=Laziness_is_not_always_good&oldid=35063\",\"WARC-Payload-Digest\":\"sha1:JB47WP5PXPLNYRDOB4NAWR43HXIESLIM\",\"WARC-Block-Digest\":\"sha1:XCPWHZZIF24OZ7RG5SBJN5XVFKMJRCXO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780060882.17_warc_CC-MAIN-20210928184203-20210928214203-00639.warc.gz\"}"}