1

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?

8
  • 3
    What do you think the type of map is? And what is the type of insert? Commented Jan 1, 2024 at 22:47
  • 4
    It looks like you are mixing functions that operate of lists with functions that operate on sets. Your powerSet function code looks promising, but you need set versions of map and other helper functions for that to work. Commented Jan 1, 2024 at 23:08
  • Can you share the data definition of Set together with implementations for union, insert, etc.? Commented Jan 2, 2024 at 9:23
  • @willeM_VanOnsem I have got: insert :: (Ord a) => a -> Set a -> Set a insert x EmptyS = Node x EmptyS insert x (Node y s) | x == y = Node y s | otherwise = Node y (insert x s) union :: (Ord a) => Set a -> Set a -> Set a union EmptyS s2 = s2 union s1 EmptyS = s1 union s1 s2 = foldr insertSet s1 (toList s2) Otherwise I imported the toList and fromList from Data Set Commented Jan 3, 2024 at 4:22
  • 1
    Hmm there are EmptyS and Node, where do they come from? They are not in Data.Set. Is this your own Set? You need to post a minimal reproducible example. "Otherwise I imported the toList and fromList from Data Set" If you have your own Set you can't do that. Commented Jan 3, 2024 at 7:06

1 Answer 1

1

As other contributors have mentioned in the comments to the question, map only works on lists. To "map" insert x over powerSet s, you would have to create a functor instance for the Set type, then use fmap instead.

instance Functor Set where
    fmap f EmptyS      = EmptyS
    fmap f (Node x s') = Node (f x) (fmap f s')

Then you would have:

powerSet :: Ord a => Set a -> Set (Set a)
powerSet EmptyS = singleton EmptyS
powerSet (Node x s') = union (fmap (insert x) (powerSet s')) (powerSet s')

I did not see your code for singleton, so I will include mine for completeness.

singleton :: Ord a => a -> Set a
singleton x = Node x EmptyS

I notice your type signature for powerSet does not include the Ord a constraint. You will need this since insert requires Ord a. Then in order to use it on sets themselves you will also have to define instances of Ord and Eq for Set a. This is fairly straightforward:

instance (Ord a) => Eq (Set a) where
  (==) EmptyS EmptyS = True
  (==) EmptyS _      = False
  (==) _ EmptyS      = False
  (==) s0 s1         = (toList s0) == (toList s1)

instance (Ord a) => Ord (Set a) where
  compare s0 s1 = compare (toList s0) (toList s1)
Sign up to request clarification or add additional context in comments.

1 Comment

The Ord a constraint makes me wonder if the set representation is supposed to be sorted, or if there's some other non trivial invariant. In that case, your functor instance might break that invariant. (Hard to tell...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.