When compiling this:
data Rec t = Rec { intPt :: t Int, doublePt :: t Double } deriving Show
type Pt2 a = (a,a)
type Pt3 a = (a,a,a)
type Rec2 = Rec Pt2
type Rec3 = Rec Pt3
main = do
print $ Rec (1,2) (3.4,5.6)
print $ Rec (1,2,3) (5.6, 7.8, 9.0)
I got
Unexpected type `t a' where type variable expected
In the declaration of `Rec (t a)'
How to make this compile and work?
I cannot reproduce the exact error message you’re reporting, but I can see two issues with your example.
You are trying to derive
ShowforRecwhich is parameterized over a type constructor. This is too hard for GHC to do fully automatically at the moment, but you can make it work in practice by enabling a number of extensions.You are using partially applied type synonyms
Pt2andPt3as arguments toRec, which isn’t permitted. You can work around this by switching to datatypes.A bit more detail: To solve problem 1, you can say:
By using a standalone deriving clause, you can explicitly specify the preconditions on the
Showinstance. In this case, these preconditions require theFlexibleContextsandUndecidableInstancesextensions to also be enabled.To solve problem 2, you can do the following:
Datatypes can be partially applied, but type synonyms cannot. So using
Pt2as an argument toRecis only allowed ifPt2is a datatype. With these modifications, your main function typechecks (and works):