我正在瀏覽Data.Has
的源代碼並試圖弄清楚它是如何工作的。我相信下面的代碼旨在允許某人將兩個值「加入」,例如a :: A
和b :: B
成爲具有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)
它們被稱爲關聯類型同義詞。這些結構類似於函數依賴關係,但使用類型函數而不是關係。 – 2012-08-03 06:04:23