4

I have a following piece of Haskell code:

foo :: Num a => (a -> a) -> Either Integer Double -> Either Integer Double
foo f x = case x of
  Left i -> Left $ f i
  Right d -> Right $ f d

It doesn't compile with error "Couldn't match type Integer with Double". I understand that type of f is computed as Integer -> Integer and Double -> Double in Left and Right expressions respectively, creating a collision.

How can I change this function, so that correct version of f would be used each time?

1 Answer 1

6

You need RankNTypes.

{-# LANGUAGE RankNTypes #-}

foo :: (forall a. Num a => a -> a) -> Either Integer Double -> Either Integer Double
foo f x = case x of
    Left i -> Left $ f i
    Right d -> Right $ f d
Sign up to request clarification or add additional context in comments.

Comments

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.