Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: list of elements with class restriction

Tags:

haskell

here's my question:

this works perfectly:

type Asdf = [Integer]
type ListOfAsdf = [Asdf]

Now I want to do the same but with the Integral class restriction:

type Asdf2 a = (Integral a) => [a]
type ListOfAsdf2 = (Integral a) => [Asdf2 a]

I got this error:

Illegal polymorphic or qualified type: Asdf2 a
Perhaps you intended to use -XImpredicativeTypes
In the type synonym declaration for `ListOfAsdf2'

I have tried a lot of things but I am still not able to create a type with a class restriction as described above.

Thanks in advance!!! =)

Dak

like image 921
dak Avatar asked Oct 19 '12 21:10

dak


1 Answers

Ranting Against the Anti-Existentionallists

I always dislike the anti-existential type talk in Haskell as I often find existentials useful. For example, in some quick check tests I have code similar to (ironically untested code follows):

data TestOp = forall a. Testable a => T String a

tests :: [TestOp]
tests = [T "propOne:" someProp1
        ,T "propTwo:" someProp2
        ]

runTests = mapM runTest tests
runTest (T s a) = putStr s >> quickCheck a

And even in a corner of some production code I found it handy to make a list of types I'd need random values of:

type R a = Gen -> (a,Gen)
data RGen = forall a. (Serialize a, Random a) => RGen (R a)
list = [(b1, str1, random :: RGen (random :: R Type1))
       ,(b2, str2, random :: RGen (random :: R Type2))
       ]

Answering Your Question

{-# LANGUAGE ExistentialQuantification #-}
data SomeWrapper = forall a. Integral a => SW a
like image 93
Thomas M. DuBuisson Avatar answered Oct 02 '22 01:10

Thomas M. DuBuisson