【发布时间】:2016-11-21 03:24:06
【问题描述】:
我有数据类型Tup2List 和GTag(来自How can I produce a Tag type for any datatype for use with DSum, without Template Haskell? 的答案)
我想为GTag t 编写一个GEq 实例,我认为这也需要为Tup2List 编写一个实例。这个实例怎么写?
我对它为什么不起作用的猜测是因为没有部分 Refl 这样的东西 - 你需要一次匹配整个结构以便编译器给你 Refl,而我正在尝试只需解开最外层的构造函数,然后递归。
这是我的代码,undefined 填写了我不知道如何编写的部分。
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
module Foo where
import Data.GADT.Compare
import Generics.SOP
import qualified GHC.Generics as GHC
data Tup2List :: * -> [*] -> * where
Tup0 :: Tup2List () '[]
Tup1 :: Tup2List x '[ x ]
TupS :: Tup2List r (x ': xs) -> Tup2List (a, r) (a ': x ': xs)
instance GEq (Tup2List t) where
geq Tup0 Tup0 = Just Refl
geq Tup1 Tup1 = Just Refl
geq (TupS x) (TupS y) =
case x `geq` y of
Just Refl -> Just Refl
Nothing -> Nothing
newtype GTag t i = GTag { unTag :: NS (Tup2List i) (Code t) }
instance GEq (GTag t) where
geq (GTag (Z x)) (GTag (Z y)) = undefined -- x `geq` y
geq (GTag (S _)) (GTag (Z _)) = Nothing
geq (GTag (Z _)) (GTag (S _)) = Nothing
geq (GTag (S x)) (GTag (S y)) = undefined -- x `geq` y
编辑:我已经改变了我的数据类型,但我仍然面临同样的核心问题。当前的定义是
data Quux i xs where Quux :: Quux (NP I xs) xs
newtype GTag t i = GTag { unTag :: NS (Quux i) (Code t) }
instance GEq (GTag t) where
-- I don't know how to do this
geq (GTag (S x)) (GTag (S y)) = undefined
【问题讨论】:
标签: haskell generic-programming