Haskell 类型实例解析失败
Haskell type instance failed to resolve
根据 this post,我正在尝试通过变形创建一个 Cofree
结构。但是编译器抱怨类型不匹配:
Expected type: Base (Cofree Term E) (E, Fix Term)
Actual type: CofreeF Term E (E, Fix Term)
但是在recursion-schemes
包的源代码中,有一个类型实例定义:
type instance Base (Cofree f a) = CofreeF f a
如何使 haskell 编译器成功地将该类型与该特定类型实例方程统一起来?
代码与link中的代码几乎相同:
import qualified Control.Comonad.Trans.Cofree as COFREEF
type E = Int
type Term = Maybe
annotate :: E -> Fix Term -> COFREEF.Cofree Term E
annotate = curry (ana coalg)
where
coalg :: (E, Fix Term) -> COFREEF.CofreeF Term E (E, Fix Term)
coalg (environment, Fix term) = environment COFREEF.:< fmap ((,)
environment) term
以及确切的错误信息:
Couldn't match type ‘COFREEF.CofreeF Term E (E, Fix Term)’
with ‘Compose
Data.Functor.Identity.Identity
(COFREEF.CofreeF Maybe Int)
(E, Fix Term)’
Expected type: (E, Fix Term)
-> Base (COFREEF.Cofree Term E) (E, Fix Term)
Actual type: (E, Fix Term)
-> COFREEF.CofreeF Term E (E, Fix Term)
• In the first argument of ‘ana’, namely ‘coalg’
In the first argument of ‘curry’, namely ‘(ana coalg)’
In the expression: curry (ana coalg)
Cofree
只是一个类型同义词
newtype CofreeT f w a = CofreeT { runCofreeT :: w (CofreeF f a (CofreeT f w a)) }
type Cofree f = CofreeT f Identity
CofreeT
有一个 Base
实例:
type instance Base (CofreeT f w a) = Compose w (CofreeF f a)
-- Base (Cofree f a) ~ Base (CofreeT f Identity a) ~ Compose Identity (CofreeF f a)
问题中的等式在道德上是等价的,但它不是一个足够好的近似值。
用Compose . Identity
包裹你的余数
annotate :: E -> Fix Term -> Cofree Term E
annotate = curry $ ana $ Compose . Identity . coalg
where
coalg :: (E, Fix Term) -> CofreeF Term E (E, Fix Term)
coalg (environment, Fix term) = environment :< fmap (environment,) term
根据 this post,我正在尝试通过变形创建一个 Cofree
结构。但是编译器抱怨类型不匹配:
Expected type: Base (Cofree Term E) (E, Fix Term)
Actual type: CofreeF Term E (E, Fix Term)
但是在recursion-schemes
包的源代码中,有一个类型实例定义:
type instance Base (Cofree f a) = CofreeF f a
如何使 haskell 编译器成功地将该类型与该特定类型实例方程统一起来?
代码与link中的代码几乎相同:
import qualified Control.Comonad.Trans.Cofree as COFREEF
type E = Int
type Term = Maybe
annotate :: E -> Fix Term -> COFREEF.Cofree Term E
annotate = curry (ana coalg)
where
coalg :: (E, Fix Term) -> COFREEF.CofreeF Term E (E, Fix Term)
coalg (environment, Fix term) = environment COFREEF.:< fmap ((,)
environment) term
以及确切的错误信息:
Couldn't match type ‘COFREEF.CofreeF Term E (E, Fix Term)’
with ‘Compose
Data.Functor.Identity.Identity
(COFREEF.CofreeF Maybe Int)
(E, Fix Term)’
Expected type: (E, Fix Term)
-> Base (COFREEF.Cofree Term E) (E, Fix Term)
Actual type: (E, Fix Term)
-> COFREEF.CofreeF Term E (E, Fix Term)
• In the first argument of ‘ana’, namely ‘coalg’
In the first argument of ‘curry’, namely ‘(ana coalg)’
In the expression: curry (ana coalg)
Cofree
只是一个类型同义词
newtype CofreeT f w a = CofreeT { runCofreeT :: w (CofreeF f a (CofreeT f w a)) }
type Cofree f = CofreeT f Identity
CofreeT
有一个 Base
实例:
type instance Base (CofreeT f w a) = Compose w (CofreeF f a)
-- Base (Cofree f a) ~ Base (CofreeT f Identity a) ~ Compose Identity (CofreeF f a)
问题中的等式在道德上是等价的,但它不是一个足够好的近似值。
用Compose . Identity
annotate :: E -> Fix Term -> Cofree Term E
annotate = curry $ ana $ Compose . Identity . coalg
where
coalg :: (E, Fix Term) -> CofreeF Term E (E, Fix Term)
coalg (environment, Fix term) = environment :< fmap (environment,) term