| 108 | |
| 109 | === GMP Library Implementation === |
| 110 | |
| 111 | [general notes, not finished] The GMP library, like most multi-precision libraries has a fundamental limitation that might seem odd if you are only familiar with Haskell--not likely, but it bears mention anyway! GMP functions require that their operands be separate entities. (Remember: an operation such as `a + b` has three entities: the two operands and the result.) That is, if you want to add `mpz_t a` to `mpz_t b`, and place the result in `mpz_t c`, you are fine but if you try to add `a` to `a` you will run into trouble. (The problem is, the multi-precision integer library functions are designed so the operands cannot alias; this is more of a problem for complex operations such as multiplication than simple operations like addition.) This limitation might be overcome by designing the API differently, i.e., compare the operands in Haskell, Cmm or whatever before you pass them to the _add function and create a separate copy of the operand if it is indeed the same then you are o.k. for the case where two operands cannot be the same. For the case where one of the operands and the result is the same you would have to check outside the general Haskell, Cmm or whatever function wrapping the _add function--you would seem to need a higher level construct. In any case, this is an annoying thing if you expect `Integer` to behave the same way as `Int`. Here is an example from GHCi: |
| 112 | {{{ |
| 113 | (in GHCi) |
| 114 | > let m_integer = 123456789 :: Integer |
| 115 | > let n_integer = m_integer + m_integer |
| 116 | -- this will show o.k., but the implementation involves *always* making extra temporaries |
| 117 | > n_integer |
| 118 | 246913578 |
| 119 | |
| 120 | -- a = a + a |
| 121 | > let m_integer = m_integer + m_integer |
| 122 | > let n_integer = m_integer + m_integer |
| 123 | |
| 124 | -- this will be fine until you try to evaluate it: |
| 125 | > n_integer |
| 126 | *** Exception: stack overflow |
| 127 | }}} |
| 128 | |
| 129 | ''The comment on being able to do this with plain Ints is wrong.'' In fact, plain Ints in ghci (not compiled code) will also cause a stack overflow: |
| 130 | {{{ |
| 131 | Prelude> let a = 5 |
| 132 | Prelude> let a = a + a |
| 133 | Prelude> let b = a + a |
| 134 | Prelude> b |
| 135 | *** Exception: stack overflow |
| 136 | }}} |