yong
yong

Reputation: 3633

What is this data ... where trying to accomplish? (Haskell)

In the random-fu package, there is this data declaration:

data Multinomial p a where
    Multinomial :: [p] -> a -> Multinomial p [a]

I understand that this is a GADT, but what is it trying to accomplish? Is it placing restrictions on p or a, etc?

Upvotes: 3

Views: 157

Answers (2)

shang
shang

Reputation: 24802

As for the "why", the Distribution type-class defines rvar as

class Distribution d t where
    rvar :: d t -> RVar t

So the type parameter of the given distribution determines the type of the samples you get out of the RVar. So using the GADT, the Multinomial distribution is defined as one that always returns multiple values per sample even though it's constructed with just a single value of a.

Upvotes: 5

David Young
David Young

Reputation: 10793

It changes the return type of the constructor. If it was defined like this

data Multinomial p a = Multinomial [p] a

our constructor would have the type

Multinomial :: [p] -> a -> Multinomial p a

The GADT changes the second type argument in the result type of the constructor to [a].

Upvotes: 7

Related Questions