[Haskell-cafe] Non-linear patterns.

Serguey Zefirov sergueyz at gmail.com
Tue May 6 08:01:08 UTC 2025


There are artifacts of old Haskell standards that are a matching against
integer value (2 + 2 = 5) and a matching against arithmetic expression (f
(n+1) = f n).

Both are interesting in requiring a class over type to match.

f 10 = 11 has type f :: (Eq a1, Num a1, Num a2) => a1 -> a2

f (n+1) = f n has type f :: Integral t1 => t1 -> t2

In that vein it is possible to add non-linear patterns by adding an
equality constraint on the non-linear matches.

E.g., f x x = x would have type f :: Eq a => a -> a -> a

For example:

data Logic = True | False | Not Logic | Or Logic Logic | And Logic Logic
deriving Eq

simplify (And x x) = simplify x
simplify (Or x x) = simplify x
simplify (Not (Not x)) = simplify x
simplify x = x

Patterns like that are common in simplification/optimization rules. Also,
by making them legal, it would be possible to use them in Template Haskell
to make better embedded languages.

On the implementation side, it can be implemented much like the (n+k)
pattern. I also think that pattern matching completeness checker should not
change.

What would you say?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20250506/d4d6719f/attachment.html>


More information about the Haskell-Cafe mailing list