I cannot understand why, although I have declared the data types, the function does not accept the different return. I also tried to create a "Result" type but it was not enough to solve the problem.
Take a look at my code:
type Set a = [a]
type Bag a = [(a, Int)]
type Seq a = [a]
data ComposedType a = Set a | Bag [a] | Seq a deriving(Eq, Show)
data ComposedResult a = Res1 [a] | Res2 [(a, Int)]
-- This function get a list [a] and returns a list of tuples [(a, Int)]
occrs :: (Ord a) => [a] -> [(a, Int)]
occrs [] = []
occrs xs = toList (fromListWith (+) [(x, 1) | x <- xs])
--getElements :: ComposedType a -> ComposedResult a
getElements (Set a) = (Set (nub a))
getElements (Seq a) = (Seq (sort a))
getElements (Bag a) = (Bag (occrs a))
The error:
- Couldn't match type ([a], Int) with '[a]' Expected type: ComposedType [a] Actual type: ComposedType ([a], Int)
- In the expression: (Bag (occrs a)) In an equation for 'getElements': getElements (Bag a) = (Bag (occrs a))
set
usesa
, so that means that the first line needs aComposeType [a]
, not aComposeType a
., the same withseq
. But now theBag
needsb ~ (a, Int)
, hence the error. – Willem Van OnsemSet
both as a type alias and as a constructor name. These two uses are unrelated: the type definitiondata ComposedType a = Set a | Bag [a] | Seq a
does not use the typeSet a
at all. Ditto forBag
andSeq
. This is probably not what you wanted. – chiSet
intype Set a = [a]
and theSet
indata ComposedType a = Set a | ...
are related to each other? If so, that may be the core part of your confusion -- they are the same name, but in two distinct namespaces (the type namespace for the former and the computation namespace for the latter) -- and it might be worth adding an answer that discusses this. – Daniel Wagner