Return specific type within Haskell

前端 未结 3 2024
我寻月下人不归
我寻月下人不归 2020-12-01 14:36

I have a pretty general question about Haskell\'s type system. I\'m trying to become familiar with it, and I have the following function:

getN :: Num a =>         


        
相关标签:
3条回答
  • 2020-12-01 15:08

    To add to sth's answer: Haskell is not object-oriented. It's not true that Double is a subclass of Num, so you cannot return a Double if you promise to return a polymorphic Num value, like you can in, say, Java.

    When you write getN :: Num a => a you promise to return a value that is fully polymorphic within the Num constraint. Effectively this means that you can only use functions from the Num type class, such as +, *, - and fromInteger.

    0 讨论(0)
  • 2020-12-01 15:08

    Check out Existentially quantified types.

    One way to solve it would be to define a new data type

    data NumBox = forall n. Num n => NumBox n
    

    You'll need -XExistentialQuantification to get this to work.

    Now you can write something like

    getN :: NumBox
    getN = NumBox (5.0 :: Double)
    

    You can also define a NumBox-list as

    let n3 = [NumBox (4.0 :: Double), NumBox (1 :: Integer), NumBox (1 :: Int) ]
    
    0 讨论(0)
  • 2020-12-01 15:12

    A function with signature Num a => a is expected to work for any type in the class Num. The implementation 5.0 :: Double just works for one type, not for all types of the class, so the compiler complains.

    An example of a generic function would be:

    square :: (Num a) => a -> a
    square x = x * x
    

    This works for any type that is a Num. It works for doubles, integers and whatever other numbers you want to use. Because of that it can have a generic type signature that just requires the parameter to be in class Num. (Type class Num is necessary because the function uses multiplication with *, which is defined there)

    0 讨论(0)
提交回复
热议问题