Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - Pattern Synonyms' use in record update

Tags:

haskell

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?

like image 219
maxloo Avatar asked Jun 30 '21 12:06

maxloo


1 Answers

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)
like image 123
Aplet123 Avatar answered Sep 22 '22 17:09

Aplet123