Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is associated data type in haskell?

In the routing section, the article says:

We can see that the RenderRoute class defines an associated data type providing the routes for our application.

What does associated data type mean? It means type families?

like image 281
softshipper Avatar asked Feb 27 '19 11:02

softshipper


People also ask

What is data type in Haskell?

The Haskell standard data type Maybe is typically declared as: data Maybe a = Just a | Nothing. What this means is that the type Maybe has one type variable, represented by the a and two constructors Just and Nothing. (Note that Haskell requires type names and constructor names to begin with an uppercase letter).

What are type classes in Haskell?

Type Classes are a language mechanism in Haskell designed to support general overloading in a principled way. They address each of the concerns raised above. They provide concise types to describe overloaded functions, so there is no expo- nential blow-up in the number of versions of an overloaded function.

What are type families in Haskell?

Indexed type families, or type families for short, are a Haskell extension supporting ad-hoc overloading of data types. Type families are parametric types that can be assigned specialized representations based on the type parameters they are instantiated with.

What is type declaration in Haskell?

Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.


1 Answers

Quoting code from the article:

instance RenderRoute HelloWorld where
    data Route HelloWorld = HomeR
        deriving (Show, Eq, Read)
    renderRoute HomeR = ([], [])

As you can see Route is an associated data type and yes, it means data families. Take a look at wiki example:

We define a type class whose instances are the types that we can use as keys in our generic maps:

class GMapKey k where  
    data GMap k :: * -> *  
    empty       :: GMap k v  
    lookup      :: k -> GMap k v -> Maybe v  
    insert      :: k -> v -> GMap k v -> GMap k v

The interesting part is the associated data family declaration of the class. It gives a kind signature (here * -> *) for the associated data type GMap k - analogous to how methods receive a type signature in a class declaration.

like image 197
Karol Samborski Avatar answered Oct 13 '22 04:10

Karol Samborski