[Haskell-cafe] Prolog-style list syntax?

Anthony Clayden anthony.d.clayden at gmail.com
Mon Jun 28 06:17:42 UTC 2021


One of the annoying (to me) inconsistencies with Haskell syntax is that `[
]` means 'here comes a list'; but for the most common way to write a list,
you can't use `[  ]`:

>    elem x []        = False
>    elem x ( y: ys ) = ...

Prolog syntax has lists, signalled by `[   ]`: [], [1], [2, 3], [4, 5, 6],
[[7, 8], [9, 10, 11]] are all valid list literals. For cons it also uses
`[   ]` syntax

>    elem x [y | ys ] = ...

`[ x, y, z | zs ]` is also valid; pattern match or build a list with at
least 3 elements, where `zs` is the tail -- 4th element onwards (if any).
That structure is particularly ugly in Haskell syntax.

Neither `|` inside `[   ]` in a pattern nor a comma-list with `|` inside
`[   ]` is valid H98 syntax, nor valid in GHC extensions AFAICT.

In an expression, `[ x | xs ]` could be taken as a list comprehension,
which is I guess why Haskell didn't follow Prolog. I've come across another
theorem-prover that follows Prolog style, except it uses `:` instead of `|`
inside `[   ]`. Unfortunately ...

>    elem x [ y: ys ] = ...

Is valid syntax, where `:` is cons, so this is a nested list. I'm thinking
that's relatively uncommon, and means the same as `[ ( y: ys ) ]`.

Would anyone be violently pro or anti changing the meaning of `:` appearing
at top level in `[   ]` to mean cons-tail?

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210628/cf77bbf3/attachment.html>


More information about the Haskell-Cafe mailing list