我必须使用大量扩展来为Haskell中的嵌入式语言创建安全表示。在我引入相互功能依赖的某个时刻,类型推断停止以找出类型变量的正确替换。请参阅以下示例:
-- create a bidirectional connection between Unit' and ()
class Connect t r | t -> r, r -> t where
data Unit'
instance Connect Unit' () where
-- define a GADT that has only a MyGADT Unit' instance
data MyGADT t where
MyGADT :: () -> MyGADT Unit'
class Clas a where
type RetTyp a :: *
eval' :: Connect (RetTyp a) r => a -> r
instance Clas (MyGADT t) where
type RetTyp (MyGADT t) = t
eval' (MyGADT a) = a -- cannot figure out that "a :: ()"
有趣的是,当我为Connect
使用TypeFamily时,它很好:
class Connect' t where
type Repr t :: *
instance Connect' Unit' where
type Repr Unit' = ()
class Clas' a where
type RetTyp' a :: *
eval'' :: Connect' (RetTyp a) => a -> (Repr (RetTyp' a))
instance Clas' (MyGADT t) where
type RetTyp' (MyGADT t) = t
eval'' (MyGADT a) = a -- ok
两种情况下的类型分辨率有什么区别?