[Haskell-cafe] tuple and pattern synonym

David Feuer david.feuer at gmail.com
Mon May 18 04:14:06 UTC 2020


You need to use a view pattern with an explicitly bidirectional pattern
synonym:

pattern P :: (Int,Int) -> Foo
pattern P xy <- ((\(Foo x y) -> (x, y)) -> xy)
  where
    P (x, y) = Foo x y

If the Foo type had more than one constructor, then you'd need to do
something a bit trickier, like

pattern P xy <- ((\case
                               Foo x y -> Just (x, y)
                               _ -> Nothing) -> Just xy)
  where
    P (x, y) = Foo x y

On Sun, May 17, 2020, 11:57 PM Kazu Yamamoto <kazu at iij.ad.jp> wrote:

> Hi,
>
> I have a question about PatternSynonyms. Suppose we have:
>
>    data Foo = Foo Int Int
>
> I would like to define a pattern as follows:
>
>    pattern P :: (Int,Int) -> Foo
>    pattern P (x,y)  = Foo x y
>
> But this results in "parse error on input ‘(’". Are there any ways to
> use tuple in the left side hand of patterns?
>
> This is important to maintain backward compatibility for the "network"
> library.
>
> If there is no way, I will define Foo as:
>
>    data Foo = Foo (Int,Int) -- awkward
>
> and define P as:
>
>    pattern P :: (Int,Int) -> Foo
>    pattern P xy = Foo xy
>
> Regards,
>
> --Kazu
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post. lol
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200518/9dbc1c47/attachment.html>


More information about the Haskell-Cafe mailing list