Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 949
yong Avatar asked Jan 30 '26 08:01

yong


2 Answers

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].

like image 56
David Young Avatar answered Feb 02 '26 02:02

David Young


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.

like image 20
shang Avatar answered Feb 02 '26 03:02

shang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!