Positive integer type

后端 未结 6 1729
野性不改
野性不改 2020-11-28 10:17

In many articles about Haskell they say it allows to make some checks during compile time instead of run time. So, I want to implement the simplest check possible - allow a

6条回答
  •  执笔经年
    2020-11-28 11:04

    This—or actually, the similar desire for a type of natural numbers (including 0)—is actually a common complaints about Haskell's numeric class hierarchy, which makes it impossible to provide a really clean solution to this.

    Why? Look at the definition of Num:

    class (Eq a, Show a) => Num a where
        (+) :: a -> a -> a
        (*) :: a -> a -> a
        (-) :: a -> a -> a
        negate :: a -> a
        abs :: a -> a
        signum :: a -> a
        fromInteger :: Integer -> a
    

    Unless you revert to using error (which is a bad practice), there is no way you can provide definitions for (-), negate and fromInteger.

提交回复
热议问题