What is this data ... where are they trying? (Haskell)

The random-fu package has this declaration data

:

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

      

I understand that this is GADT, but what is it trying to achieve? Does it impose restrictions on p

or a

etc.?

+3


source to share


2 answers


It changes the return type of the constructor. If it were defined as

data Multinomial p a = Multinomial [p] a

      

our constructor will be of type



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

      

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

.

+7


source


As for the "why", the type class Distribution

defines rvar

how

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

      



Thus, the type parameter of this distribution determines the type of samples that you selected from rvar

. Thus, using GADT, a distribution Multinomial

is defined as one that always returns multiple values ​​per sample, even if it is built with only one value a

.

+5


source







All Articles