使用 GHC.Generics 时添加显示约束
Adding a Show constraint when using GHC.Generics
我正在使用 GHC 泛型。我的用例几乎与 the example in the wiki 相同,除了我正在编码和解码基因序列。
一切正常,直到我决定保留一份我已阅读的列表,以便在出现错误时向用户报告。这意味着我需要向我的默认 get
实现添加一个 Show
约束。问题是我不知道如何写约束。请参阅下面代码中的 -- HELP!!!
注释。
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
import Control.Monad.State.Lazy (StateT)
import qualified Control.Monad.State.Lazy as S (put, get, gets)
import Data.Functor.Identity (Identity)
import Data.Word (Word8, Word16)
import GHC.Generics
type Sequence = [Word8]
type Writer = StateT Sequence Identity
type Reader = StateT (Sequence, Int, [String]) Identity
class Genetic g where
-- | Writes a gene to a sequence.
put :: g -> Writer ()
default put :: (Generic g, GGenetic (Rep g)) => g -> Writer ()
put = gput . from
-- | Reads the next gene in a sequence.
get :: Reader (Either [String] g)
default get :: (Generic g, GGenetic (Rep g), Show (Rep g x???)) -- HELP!!!
=> Reader (Either [String] g)
get = do
(_, start, _) <- S.get
a <- gget
(xs, stop, trace) <- S.get
let msg = show start ++ ':' : show stop ++ ' ' : show a
S.put (xs, stop, msg:trace)
return $ fmap to a
class GGenetic f where
gput :: f a -> Writer ()
gget :: Reader (Either [String] (f a))
哦!我应该使用 show (fmap to a)
而不是 show a
。然后我所需要的只是添加 Show g
作为约束。这个简单的更改编译得很好:
default get :: (Generic g, GGenetic (Rep g), Show g)
=> Reader (Either [String] g)
get = do
(_, start, _) <- S.get
a <- gget
(xs, stop, trace) <- S.get
let result = fmap to a
let msg = show start ++ ':' : show stop ++ ' ' : show result
S.put (xs, stop, msg:trace)
return result
我正在使用 GHC 泛型。我的用例几乎与 the example in the wiki 相同,除了我正在编码和解码基因序列。
一切正常,直到我决定保留一份我已阅读的列表,以便在出现错误时向用户报告。这意味着我需要向我的默认 get
实现添加一个 Show
约束。问题是我不知道如何写约束。请参阅下面代码中的 -- HELP!!!
注释。
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
import Control.Monad.State.Lazy (StateT)
import qualified Control.Monad.State.Lazy as S (put, get, gets)
import Data.Functor.Identity (Identity)
import Data.Word (Word8, Word16)
import GHC.Generics
type Sequence = [Word8]
type Writer = StateT Sequence Identity
type Reader = StateT (Sequence, Int, [String]) Identity
class Genetic g where
-- | Writes a gene to a sequence.
put :: g -> Writer ()
default put :: (Generic g, GGenetic (Rep g)) => g -> Writer ()
put = gput . from
-- | Reads the next gene in a sequence.
get :: Reader (Either [String] g)
default get :: (Generic g, GGenetic (Rep g), Show (Rep g x???)) -- HELP!!!
=> Reader (Either [String] g)
get = do
(_, start, _) <- S.get
a <- gget
(xs, stop, trace) <- S.get
let msg = show start ++ ':' : show stop ++ ' ' : show a
S.put (xs, stop, msg:trace)
return $ fmap to a
class GGenetic f where
gput :: f a -> Writer ()
gget :: Reader (Either [String] (f a))
哦!我应该使用 show (fmap to a)
而不是 show a
。然后我所需要的只是添加 Show g
作为约束。这个简单的更改编译得很好:
default get :: (Generic g, GGenetic (Rep g), Show g)
=> Reader (Either [String] g)
get = do
(_, start, _) <- S.get
a <- gget
(xs, stop, trace) <- S.get
let result = fmap to a
let msg = show start ++ ':' : show stop ++ ' ' : show result
S.put (xs, stop, msg:trace)
return result