10

While reading Real world Haskell I came up with this note:

ghci> :info (+)
  class (Eq a, Show a) => Num a where
  (+) :: a -> a -> a
  ...
  -- Defined in GHC.Num
  infixl 6 +

But how can Haskell define + as a non-native function? At some level you have to say that 2 + 3 will become assembler i.e. machine code.

1
  • I understand this question as 'How can I define + as a non-native function without using assembly primitives at some level?'. Another possible variant is 'How actual Haskell implementation define +?'. Please edit your question to clarify which answer(s) you want. Commented Nov 18, 2011 at 14:13

2 Answers 2

21

The + function is overloaded and for some types, like Int and Double the definition of + is something like

instance Num Int where
    x + y = primAddInt x y

where primAddInt is a function the compiler knows about and will generate machine code for.

The details of how this looks and works depends on the Haskell implementation you're looking at.

Sign up to request clarification or add additional context in comments.

1 Comment

In GHC, the corresponding function is +# :: Int# -> Int# -> Int#. Thus, the implementation is something like I# a + I# b = I# (a +# b)
5

It is in fact possible to define numbers without ANY native primitives. There are many ways, but the simplest is:

data Peano = Z | S Peano

Then you can define instance Num for this type using pattern-matching. The second common representation of numbers is so called Church encoding using only functions (all numbers will be represented by some obscure functions, and + will 'add' two functions together to form third one).

Very interesting encodings are possible indeed. For example, you can represent arbitrary precision reals in [0,1) using sequences of bits:

data RealReal = RealReal Bool RealReal | RealEnd

In GHC of course it is defined in a machine-specific way by using either primitives or FFI.

11 Comments

That definition of RealReal is not very good if you want computable arithmetic.
Where do you see problems? As long as we don't pattern match digits they are not computed.
If you never plan to use any of the bits then you don't have to represent them at all. :)
Assuming you are going to look at some initial segment of the bits then you run into trouble definition addition, because you cannot bound the lookahead to determine the carry. E.g., 0.010101... + 0.0010101... What's the first after the .? You would have to look infinitely far to determine it. Which means addition cannot produce even a single bit of the result.
@nponeccop Streams of bits are a particular kind of Cauchy sequences, but they are unfortunately not closed under, for example, addition, as demonstrated by augustss above.
|

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.