Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple type parameters in type class?

Lets assume I have a type class Stack with one instance List:

class Stack a where
    push :: a -> Integer -> a
    pop :: a -> a
    last :: a -> Integer

data List = Empty | Element Integer List
instance Stack List where
    push list value = Element value list
    pop Empty = error "No elements"
    pop (Element _ list) = list
    last Empty = error "No elements"
    last (Element value _) = value

How Stack has to be defined in order for List to not be limited to Integer values?

-- class Stack (?) where ...
data List a = Empty | Element a (List a)
-- instance Show (List a) where ...
like image 303
Cubi73 Avatar asked Sep 18 '15 16:09

Cubi73


People also ask

Can generics take multiple type parameters?

Multiple parametersYou can also use more than one type parameter in generics in Java, you just need to pass specify another type parameter in the angle brackets separated by comma.

Can a generic class have multiple generic parameters Java?

Yes - it's possible (though not with your method signature) and yes, with your signature the types must be the same.

How many type parameters can a generic class introduce?

Generic Classes As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.

Can a parameterized type have several bounds?

A type parameter can have multiple bounds.


1 Answers

Consider using a higher-kinded class variable. Thus:

class Stack s where
    push :: s a -> a -> s a
    pop  :: s a -> s a
    last :: s a -> a

data List a = Empty | Element a (List a)

The instance remains exactly as you wrote it (though List now has kind * -> * instead of *):

instance Stack List where
    push list value = Element value list
    pop Empty = error "No elements"
    pop (Element _ list) = list
    last Empty = error "No elements"
    last (Element value _) = value

This approach is pure Haskell 2010 -- it requires no extensions.

Also, consider making your failures observable; for instance by changing the type of pop and last to return Maybe (s a) and Maybe a, respectively.

like image 52
Daniel Wagner Avatar answered Sep 29 '22 12:09

Daniel Wagner