3
votes

I've just fallen on that piece of code :

-- | Gathers common slice operations.
class Slice a where
    type Loc a

    sliceEvents :: a -> [ResolvedEvent]
    -- ^ Gets slice's 'ResolvedEvent's.
    sliceDirection :: a -> ReadDirection
    -- ^ Gets slice's reading direction.
    sliceEOS :: a -> Bool
    -- ^ If the slice reaches the end of the stream.
    sliceFrom :: a -> Loc a
    -- ^ Gets the starting location of this slice.
    sliceNext :: a -> Loc a
    -- ^ Gets the next location of this slice.
    toSlice :: a -> SomeSlice
    -- ^ Returns a common view of a slice.

I don't understand what type Loc a is doing...

1
This isn't standard Haskll 2010; it's one of GHC's more exotic language extensions. (Associated Types, I think...)MathematicalOrchid
ho yeah that is the name..... and this is the extension ... {-# LANGUAGE TypeFamilies #-}Nicolas Henin

1 Answers

4
votes

Loc a is an associated type, which is a way of declaring a type family instance associated with a class instance. The type represented by Loc a is determined by the type of a, and is specified in the instance: e.g.

instance Slice Foo where
    type Loc Foo = Bar
    ...

Wherever Loc a appears in the class declaration, it would be replaced with the corresponding type in the instance - so the instance functions for Foo would look like

sliceEvents :: Foo -> [ResolvedEvent]
...
sliceFrom :: Foo -> Bar
...

The associated type can also be used in other functions outside the class declaration by giving a class constraint: e.g.

myFunction :: (Slice a) => a -> Loc a