我不得不使用很多扩展来为 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 :: ()"
有趣的是,当我使用 TypeFamily 时Connect
它很好:
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
两种情况下的类型解析有什么区别?