[英]How to create a Mk instance for GHC.Generics.U1?
I'm working through content at the blog posting Building data constructors with GHC Generics . 我正在使用GHC Generics在博客上发布构建数据构造函数的内容。 My previous question is here .
我以前的问题在这里 。
The posting has the following code to create a Rep
: 发布具有以下代码来创建
Rep
:
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Generics
import Data.Functor.Compose
class Functor f => Mk rep f | rep -> f where
mk :: f (rep a)
instance Mk (K1 i c) ((->) c) where
mk = \x -> K1 x
instance (Mk l fl, Mk r fr) => Mk (l :*: r) (Compose fl fr) where
mk = Compose (fmap (\l -> fmap (\r -> l :*: r) mk) mk)
instance (Mk f f') => Mk (M1 i c f) f' where
mk = M1 <$> mk
Is it possible to create a similar instance for U1
? 是否可以为
U1
创建类似的实例? If so, how? 如果是这样,怎么样?
Since U1
has no structure, you should be able to mk
one out of thin air in Identity
: 由于
U1
没有结构,你应该能够在Identity
中凭空mk
一个:
import Control.Monad.Identity
instance Mk U1 Identity where
mk = pure U1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.