首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell类型实例无法解析

Haskell类型实例无法解析
EN

Stack Overflow用户
提问于 2018-07-02 02:09:34
回答 1查看 90关注 0票数 2

根据Cofree this post的说法,我正试图通过非变质作用来创造出一种this post结构。但是编译器抱怨类型不匹配:

代码语言:javascript
复制
Expected type: Base (Cofree Term E) (E, Fix Term)
  Actual type: CofreeF Term E (E, Fix Term)

但是在recursion-schemes包的源代码中,有一个类型实例定义:

代码语言:javascript
复制
type instance Base (Cofree f a) = CofreeF f a

如何使haskell编译器成功地将该类型与此特定类型实例方程统一?

代码几乎与链接中的代码相同:

代码语言:javascript
复制
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

以及准确的错误信息:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-03 17:33:40

Cofree只是一个类型的同义词

代码语言:javascript
复制
newtype CofreeT f w a = CofreeT { runCofreeT :: w (CofreeF f a (CofreeT f w a)) }
type Cofree f = CofreeT f Identity

CofreeT有一个Base实例:

代码语言:javascript
复制
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包装你的余代数

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51128360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档