tuple and pattern synonym

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

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
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

By the way, I personally don't consider the syntax you need to use here
remotely obvious. I really wish we had something more intuitive for
defining these sorts of patterns!
On Mon, May 18, 2020, 12:14 AM David Feuer
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
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

On May 18, 2020, at 5:18 AM, David Feuer
wrote: By the way, I personally don't consider the syntax you need to use here remotely obvious. I really wish we had something more intuitive for defining these sorts of patterns!
Would https://github.com/ghc-proposals/ghc-proposals/pull/138 satisfy this wish? I remember really liking this proposal when it appeared, but I never put the time into seeing it through. Richard

Hi David,
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
Great! To maintain backward compatibility of the "network" library, I have created a PR: https://github.com/haskell/network/pull/455 Thank you for your suggestion! --Kazu
participants (3)
-
David Feuer
-
Kazu Yamamoto
-
Richard Eisenberg