Can a pattern synonym have a different type signature in each direction, like numeric literals do?

152 Views Asked by At

Consider the numeric literal 1. It has a different type depending on whether it's used on the left or right of the equals sign. In f True = 1, 1 has type Num p => p. But in f 1 = True, 1 has type (Eq a, Num a) => a. Is it possible to create a pattern synonym that works the same way? For example:

pattern LooksLikeFoo :: (Read a, Show a) => a
pattern LooksLikeFoo <- (show -> "Foo")
  where LooksLikeFoo = read "Foo"

That pattern only works if it has both the Read and Show classes, no matter which side of the equals sign it's on. Is there a way to make it have type Show a => a on the left side of the equals sign, but Read a => a on the right side? Non-working pseudo-code:

pattern LooksLikeFoo :: Show a => a
pattern LooksLikeFoo <- (show -> "Foo")
  where LooksLikeFoo :: Read a => a
        LooksLikeFoo = read "Foo"
1

There are 1 best solutions below

0
On BEST ANSWER

This is not possible currently but it has been brought up by Edward Kmett as a possibility a few times, for example: https://www.reddit.com/r/haskell/comments/eqhcgx/comment/fetsa54