我想学习使用派生的更多类型。目前,我经常遇到的一个问题是,泛型表示是不同的,但是如果转换到类型表示的转换深入到嵌套类型,则是相等的。
简单示例:
coerce @(Either () ()) @Bool不起作用,因为Rec0 () :+: Rec0 ()没有强迫U1 :+: U1,尽管:kind! Rep ()也给了U1。
更复杂的示例
data LeafTree a = Leaf a | Branch [LeafTree a]是与Free [] a同构的,也不会因为类似的原因而强制。我怎么才能在这两种人之间胁迫呢?我还知道如何在DerivingVia之间使用Rep1相等的类型,如果这有帮助的话。
发布于 2022-02-17 18:24:05
现在,对于iso-派生包,还可以派生出一个monad实例,如下所示:
{-# 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)https://stackoverflow.com/questions/70755977
复制相似问题