I get an error:
working.hs:186:25: error:
* Couldn't match expected type: Set (Set a)
with actual type: [[a]]
* In the expression:
union (map (insert x) (powerSet s)) (powerSet s)
In an equation for `powerSet':
powerSet (Node x s)
= union (map (insert x) (powerSet s)) (powerSet s)
* Relevant bindings include
s :: Set a (bound at working.hs:186:20)
x :: a (bound at working.hs:186:18)
working.hs:186:48: error:
* Couldn't match expected type: [[a]]
with actual type: Set (Set a)
* In the second argument of `map', namely `(powerSet s)'
In the first argument of `union', namely
`(map (insert x) (powerSet s))'
In the expression: union (map (insert x) (powerSet s)) (powerSet s)
* Relevant bindings include
s :: Set a (bound at working.hs:186:20)
x :: a (bound at working.hs:186:18)
working.hs:186:62: error:
* Couldn't match expected type: [[a]]
with actual type: Set (Set a)
* In the second argument of `union', namely `(powerSet s)'
In the expression: union (map (insert x) (powerSet s)) (powerSet s)
In an equation for `powerSet':
powerSet (Node x s)
= union (map (insert x) (powerSet s)) (powerSet s)
* Relevant bindings include
s :: Set a (bound at working.hs:186:20)
x :: a (bound at working.hs:186:18)
Failed, no modules loaded.
My function is:
powerSet :: Set a -> Set (Set a)
powerSet EmptyS = singleton EmptyS
powerSet (Node x s) = union (map (insert x) (powerSet s)) (powerSet s)
Why do I get the error?
As other contributors have mentioned in the comments to the question,
maponly works on lists. To "map"insert xoverpowerSet s, you would have to create a functor instance for theSettype, then usefmapinstead.Then you would have:
I did not see your code for
singleton, so I will include mine for completeness.I notice your type signature for
powerSetdoes not include theOrd aconstraint. You will need this sinceinsertrequiresOrd a. Then in order to use it on sets themselves you will also have to define instances ofOrdandEqforSet a. This is fairly straightforward: