Question

I encountered a strange problem when defining a default constraint. If a constraint is unit, the default instance is not chosen. In all other cases, it works as expected.

{-# LANGUAGE TypeFamilies, ConstraintKinds #-}
import qualified GHC.Exts as E

class Expression a where
  type Constr a v :: E.Constraint
  --type Constr a v = ()         -- with this line compilation fails
  --type Constr a v = v ~ v      -- compiles
  wrap :: Constr a v => a -> Maybe v

instance Expression () where
  wrap () = Just undefined

main = print (wrap () :: Maybe Int)

Can someone clarify the reasons of the typechecker behavior?

Was it helpful?

Solution

This is a bug with associated type defaults in 7.4.1. A few weeks ago, I was told on #haskell that it is a known bug that has been fixed, but I can't find mention of it on the GHC trac.

OTHER TIPS

Not really an answer, but this isn't about ConstraintKinds

class Expression a where
   type Type a v
   type Type a v = ()
   wrap :: (Type a v) ~ () => a -> Maybe v

instance Expression () where
   wrap () = Just undefined

main = print (wrap () :: Maybe Int)

does not compile, but

class Expression a where
   type Type a v
   type Type a v = v
   wrap :: (Type a v) ~ v => a -> Maybe v

instance Expression () where
   wrap () = Just undefined

main = print (wrap () :: Maybe Int)

does

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top