我正在查看
我特别不理解类声明和实例声明中
此外,我不知道下面的
请问是否可以解释一下来自Data.Has.TypeList 的下面代码?
Data.Has
的源代码,试图弄清楚它是如何工作的。我认为下面的代码旨在允许将两个值(比如a :: A
和b :: B
)“Join”成一个新值,该新值具有a
和b
的功能。我特别不理解类声明和实例声明中
type
的含义。此外,我不知道下面的
~
符号表示什么。请问是否可以解释一下来自Data.Has.TypeList 的下面代码?
-- | Provides type-list functionality
module Data.Has.TypeList where
import Control.Applicative
import Data.Monoid (Monoid (..))
import Test.QuickCheck (Arbitrary (..), CoArbitrary (..))
import Data.Typeable
import Data.Data
-- | Cons a type onto type-list.
data a ::: b = a ::: b deriving (Show,Eq,Ord,Read,Bounded,Typeable,Data)
-- | The empty type-list.
data TyNil = TyNil deriving (Read,Typeable,Data)
-- | Appends a type-list and another.
class Append a b where
type a :++: b
(.++.) :: a -> b -> a :++: b
infixr 5 :++:
-- Implementation of Append
instance Append TyNil b where
type TyNil :++: b = b
_ .++. b = b
instance (Append y b) => Append (x ::: y) b where
type (x ::: y) :++: b = x ::: (y :++: b)
~(x ::: y) .++. b = x ::: (y .++. b)