Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Tuple in `newtype` Argument

Learn You a Haskell discusses newtype.

How does its signature of Pair b a mean that the passed-in argument must be a tuple?

ghci> newtype Pair b a = Pair { getPair :: (a, b) }
ghci> let p = Pair (5, 10)

I'm confused how b a indicates a tuple.

like image 562
Kevin Meredith Avatar asked Dec 02 '22 19:12

Kevin Meredith


1 Answers

The reason you pass in a tuple is not in the type, but in its constructor:

Pair { getPair :: (a, b) }

This is using record syntax to define a Pair constructor with a single field called getPair, which contains a tuple. You could get a very similar effect by breaking it into two parts:

newtype Pair b a = Pair (a, b)

getPair (Pair (x, y)) = (x, y)

So the b a does not force it to be a tuple; that's what the { getPair :: (a, b) } does.

like image 147
Tikhon Jelvis Avatar answered Jan 08 '23 10:01

Tikhon Jelvis