Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Nov 10 13:50:25 EST 2008

```On Mon, 2008-11-10 at 18:20 +0000, Andrew Coppin wrote:
> Mitchell, Neil wrote:
> > In general:
> >
> > if boolean then [value] else []
> >
> > Can be written as:
> >
> > [value | boolean]
> >
>
> Is there any specific reason why this is valid?

It is due to the rules for the translation of list comprehensions:

[ e | True ]         = [ e ]
[ e | q ]            = [ e | q, True ]
[ e | b, Q ]         = if b then [ e | Q ] else []
[ e | p <- l, Q ]    = let ok p = [ e | Q ]
ok _ = []
in concatMap ok l
[ e | let decls, Q ] = let decls in [ e | Q ]

So [ value | boolean ] matches the second rule giving us
[value | boolean, True]
which matches the third rule
if boolean then [value | True] else []
which can be simplified via the first rule to
if boolean then [value] else []

These rules are slightly more complex than necessary because they avoid
using a null base case. We could simplify the first two rules if we were
to allow the degenerate list comprehension [ e | ] and let Q match
nothing. Then we'd use the rule:

[ e | ] = [ e ]

and translate [ value | boolean ] via the original 3rd rule with Q as
nothing:
if boolean then [value | ] else []
and directly to:
if boolean then [value ] else []

If you meant, why is it allowed rather than banned then I guess the
answer is because it is orthogonal. The rules naturally handle that case
and there was no particular reason to ban it, even if it is somewhat
unusual.

Duncan

```