首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >深度评估类型表示后Rep仅相等时的胁迫

深度评估类型表示后Rep仅相等时的胁迫
EN

Stack Overflow用户
提问于 2022-01-18 12:57:50
回答 1查看 92关注 0票数 4

我想学习使用派生的更多类型。目前,我经常遇到的一个问题是,泛型表示是不同的,但是如果转换到类型表示的转换深入到嵌套类型,则是相等的。

简单示例:

代码语言:javascript
复制
coerce @(Either () ()) @Bool

不起作用,因为Rec0 () :+: Rec0 ()没有强迫U1 :+: U1,尽管:kind! Rep ()也给了U1。

更复杂的示例

代码语言:javascript
复制
data LeafTree a = Leaf a | Branch [LeafTree a]

是与Free [] a同构的,也不会因为类似的原因而强制。我怎么才能在这两种人之间胁迫呢?我还知道如何在DerivingVia之间使用Rep1相等的类型,如果这有帮助的话。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-17 18:24:05

现在,对于iso-派生包,还可以派生出一个monad实例,如下所示:

代码语言:javascript
复制
{-# LANGUAGE TypeOperators, FlexibleInstances, MultiParamTypeClasses, DeriveTraversable, DerivingVia #-}
import Control.Monad.Free
import Iso.Deriving

data LeafTree a = Leaf a | Branch [LeafTree a]
  deriving (Functor)
  deriving (Applicative, Monad) via Free [] `As1` LeafTree

instance Inject (Free [] a) (LeafTree a) where
  inj (Pure x ) = Leaf x
  inj (Free xs) = Branch (fmap inj xs)

instance Project (Free [] a) (LeafTree a) where
  prj (Leaf   x ) = Pure x
  prj (Branch xs) = Free (fmap prj xs)

instance Isomorphic (Free [] a) (LeafTree a)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70755977

复制
相关文章

相似问题

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