Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeral domains in Haskell

Tags:

math

haskell

Haskell is more mathematical than many languages because of lambda-calculus, but I think the domains are incomplete for number: we have Integer and Float, for example, but not Positive or Negative, or [1..5] as a domain. This sometimes makes functions unsafe while the compiler could have catched the type error. For example: 5mod0 outputs *** Exception: divide by zero at run-time only. mod :: Integral a => a -> a -> a but we could have something like mod :: Integral a, a != 0 => a -> a -> a; something like a guard or an interval or another datatype... In a game, I want my character to have a positive number for its life. Or from 0 to 100, not under, not upper. When he gets hit, I need to call the ugly positive x = if x > 0 then x else 0. Even C has signed and unsigned.

Is it a weakness or are they reasons why there are no "interval" domains? Is there a package fixing this?

like image 863
L01man Avatar asked Jul 15 '12 21:07

L01man


1 Answers

You're free to make such classes, but perhaps the reason why they weren't included in Haskell was because people couldn't find a way to make them frequently useful.

It's quite clear you want subtraction with your class, but you also want it to be closed.

Something like this maybe?

NonNegative x - NonNegative y = NonNegative (max (x - y) 0)

But then the identity x - y + y == x does not hold.

People have made alternative numeric hierarchies for Haskell, such as 'Numeric Prelude'. Haskell is quite friendly to customization, you can even replace the Prelude with your own definitions, but whether they're useful and don't cause more problems than they solve is another matter.

like image 51
Clinton Avatar answered Sep 22 '22 19:09

Clinton