Why should I add a constraint to a Haskell function even if I have implemented the necessary instances for all possible DataKinds?

64 Views Asked by At
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}

data RegStatus = Registered | Anonim

newtype UserId (r :: RegStatus) = UserId Int

class GetRegStatus (r :: RegStatus)  where
  getRegStatus :: UserId r -> RegStatus

instance GetRegStatus 'Registered where
  getRegStatus _ = Registered

instance GetRegStatus 'Anonim where
  getRegStatus _ = Anonim

getStatus :: forall (r :: RegStatus).  UserId r -> RegStatus
getStatus = getRegStatus

Error: No instance for (GetRegStatus r) arising from a use of `getRegStatus'

But I already implemented GetRegStatus for all possible RegStatus data kinds.

Well, if I add a constraint, it works well.

getStatus :: forall r. GetRegStatus r =>  UserId r -> RegStatus
getStatus = getRegStatus

Is there any way to prove to the compiler that the GetRegStatus constraint is not needed?

0

There are 0 best solutions below