如何为GHC.Generics.U1创建一个Mk实例?

时间:2016-02-17 00:26:44

标签: haskell generics ghc-generics

我正在撰写博文Building data constructors with GHC Generics的内容。我之前的问题是here

发布时使用以下代码创建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

是否可以为U1创建类似的实例?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:3)

由于U1没有结构,您应该可以在mk Identity中凭空import Control.Monad.Identity instance Mk U1 Identity where mk = pure U1 创建一个:

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.0.BETA01</version>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        <version>1.3.2</version>
    </dependency>

Caused by: java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
at nz.net.ultraq.thymeleaf.LayoutDialect.<clinit>(LayoutDialect.groovy:49) ~[thymeleaf-layout-dialect-1.3.2.jar:na]