I have a simple problem when it comes to writing cool classes that inherit from each other. I am trying to create a hierarchy of class types in order to achieve a certain level of abstract abstraction. Let me say that I need a collection collection class:
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances #-} class Collection ca where isMember :: a -> c -> Bool
And I determined the type of tree:
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Eq)
I want to make my tree a collection, so:
inOrder :: Tree a -> [a] inOrder Empty = [] inOrder (Node alr) = (inOrder l) ++ [a] ++ (inOrder r) instance (Eq a) => Collection (Tree a) a where isMember ac = a `elem` (inOrder c)
This is not entirely correct:
*Main> (isMember '2' Empty) <interactive>:1:1: No instance for (Collection (Tree a) Char) arising from a use of `isMember' at <interactive>:1:1-18 Possible fix: add an instance declaration for (Collection (Tree a) Char) In the expression: (isMember '2' Empty) In the definition of `it': it = (isMember '2' Empty)
Presumably, the value of typeclass would be lost if I had to create an implementation for each particular type. Therefore, I am not writing the instance declaration correctly. But I canβt understand how to act.
haskell
Ara vartanian
source share