haskelltype-families

How would I allow polymorphic types in type families?


Given the natural numbers,

data Zero = Zero
  deriving (Show)
newtype Succ x = Succ x
  deriving (Show)

class MakeTuple num where 
    type TupleOf num
    makeTuplep :: TupleOf num 
-- This instance is invalid, as it is an "illegal polymorphic type".
instance MakeTuple (Succ (Succ Zero)) where
    type TupleOf (Succ (Succ Zero)) = forall v1 v2. v1 -> v2 -> (v1, v2)
    makeTuplep =  (,)

Can I lift this restriction? I actually decided to use type families as a functional dependency was impossible as the type of makeTuplep was polymorphic. Using polymorphic type in type family in haskell is the same error, however it is unanswered.


Solution

  • While it may not be possible with type families, here is a hacked up solution. It makes partial application tricky, but it is good enough.

    class CurryN num a b | num a -> b, b -> a where 
        curryNp :: b -> a
    
    makeTuple :: forall num a1 a2. CurryN num a1 (a2 -> a2) => a1 
    makeTuple = curryNp @num id
    instance CurryingN  (Succ (Succ Zero)) (v1 -> v2 -> v3) ((v1, v2) -> v3) where
        curryNp =curry
    ... for all tuples