maxloo
maxloo

Reputation: 491

Haskell - Pattern Synonyms' use in record update

I'm now trying out a piece of code from:

https://mpickering.github.io/posts/2015-12-12-pattern-synonyms-8.html

{-# LANGUAGE PatternSynonyms #-}
pattern MyPoint :: Int -> Int -> (Int, Int)
pattern MyPoint{m, n} = (m,n)
m :: (Int, Int) -> Int
n :: (Int, Int) -> Int
test9 = (0,0) { m = 5 }

But test9 throws an error:

• ‘m’ is not a record selector
• In the expression: (0, 0) {m = 5}

How do I get test9 to work?

Upvotes: 4

Views: 142

Answers (1)

Aplet123
Aplet123

Reputation: 35540

When you write:

m :: (Int, Int) -> Int
n :: (Int, Int) -> Int

You're declaring m and n as functions and they lose their special place as a record selector. Just drop those lines entirely:

{-# LANGUAGE PatternSynonyms #-}
pattern MyPoint :: Int -> Int -> (Int, Int)
pattern MyPoint{m, n} = (m,n)
test9 = (0,0) { m = 5 } -- (5, 0)

Upvotes: 7

Related Questions