Type keyword in class and instance declarations

Viewed 932

I was looking through the source code of Data.Has and trying to figure out how it works. I believe the following code is intended to allow someone to "Join" two values, say a :: A and b :: B into a new value that has the functionality of both a and b.

I particularly don't understand what type means inside the class and instance declarations.

Also I don't know what the ~ symbol means below.

Could someone explain the below code from Data.Has.TypeList works?

-- | 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)
1 Answers
Related