

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm learning Haskell and would like to impose the use of positive integers (1,2,3, ...) in some constructors, but I only seem to find the 'Int' and 'Integer' datatypes.

I could use the canonical

data Nat = Zero | Succ Nat

but then I couldn't use 1, 4, ... to denote them.

So I ask, is there a way to accomplish this? (which is like using 'unsigned' in C)

Thanks in advance.

EDIT: I'm going the way of hiding it inside a module, as explained by C. A. McCann. Also, I must add the following link: for a summary on the subject. Thanks for taking the time to answer!

share|improve this question
I'd still prefer your way. Although you can't use that sugary syntax, you can still fall back to regular old functions for generating lists. – Ken Wayne VanderLinde Jul 30 '11 at 15:51
up vote 14 down vote accepted

There's generally two approaches for this: The inductive definition you gave, or an abstract data type using something else for internal representation.

Note that the inductive representation is not terribly efficient for large numbers; however, it can be lazy, which lets you do things like see which of two nats is larger without evaluating further than the size of the smaller one.

An abstract data type is one which is defined in a separate module and does not export its constructors, examples being IO or Data.Set.Set. You can define something like this:

module Nat (Nat() {- etc. -} ) where

newtype Nat = Nat { unNat :: Integer }

...where you export various operations on Nat such that, even though the internal representation is just Integer, you ensure that no value of type Nat is constructed holding a negative value.

In both cases, if you want to use numeric literals, you'll need a definition of fromInteger, which is attached to the Num type class, which is completely wrong for natural numbers but oh well.

If you don't mind making a broken instance just to get syntactic niceties, you can do something like this:

instance Num Nat where
    Zero + n = n
    n + Zero = n
    (Succ n1) + (Succ n2) = Succ . Succ $ n1 + n2

    fromInteger 0 = Zero
    fromInteger i | i > 0 = Succ . fromInteger $ i - 1

...and so on, for the other functions. The same can be done for the abstract data type approach, just be careful to not use deriving to get an automatic Num instance, because it will happily break your non-negative constraint.

share|improve this answer

You can use Word32 from Data.Word, which corresponds to uint32_t in C.

With Word32 you get the same problems as with the unsigned types in C, especially over- and underflow. If you want to make sure that doesn't happen, you'd need to wrap it to a newtype and only export a smart constructor. Thus no addition, subtraction etc. would be possible and there's no risk of over- or underflow. If you wanted to support addition, for example, you could add and export a function for adding unsigned ints, but with a check for overflow (and with a performance penalty). It could then look like this:

module NT(UInt, addUInts) where

import Data.Word

newtype UInt = UInt Word32
  deriving (Show)

mkUInt :: Word32 -> UInt
mkUInt = UInt

addUInts :: UInt -> UInt -> Maybe UInt
addUInts (UInt u1) (UInt u2) =
  let u64 :: Word64
      u64 = fromIntegral u1 + fromIntegral u2
  in if u64 > fromIntegral (maxBound :: Word32)
       then Nothing
       else Just (UInt (fromIntegral u64))
share|improve this answer
I don't like that, but am I correct guessing there's no alternative? – Seymour Kooze Jul 30 '11 at 16:04
I added an alternative. – Antti Jul 30 '11 at 16:25

I can't remember whether it addresses your specific question, but you might like Colin Runciman's paper What about the natural numbers?. In case you can't get over the paywall, there seems to be a version at Citeseer.

share|improve this answer

Your Answer


