emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using the GNU GMP Library for Bignums in Emacs


From: Stefan Monnier
Subject: Re: Using the GNU GMP Library for Bignums in Emacs
Date: Mon, 09 Jul 2018 23:41:50 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>> Also, for purposes of cl-generic's dispatch it'd be better for type-of
>> to return `natnum` or `fixnum` when applicable.
> Isn't the issue of how finely type-of distinguishes integers separable from
> the issue of whether to have bignums? type-of could return 'natnum' now,
> even without bignums, but it doesn't.

Exactly.

I was pointing out that type-of already provides suboptimal results in
this respect, but also that there's some grey area when it comes to
decide what it should return.

The main problem is due to subtyping: natnum is (currently) a subtype of
fixnum which is a subtype of integer, so 5 is all three at the same time
and hence (type-of 5) could return any of those three and claim to
be correct.

In general, we'd want type-of to return the "most precise" type, so
you could argue that `natnum` is the better option.  Note that `natnum`
is not a valid type in CommonLisp, tho, so `fixnum` would probably be
a better option (especially if you want to consider positive bignums as
natnums, in which case natnum is not a subtype of fixnum).

> If we decide that type-of should return some value other than
> 'integer' for some integers, how would we decide what value to return?
> For example, if (natnump X) and (bignump X) both succeed, should
> type-of return 'natnum' or 'bignum' or both?

Based on the above (the fact that natnum is neither a subtype nor
a supertype of fixnum or bignum, and the fact that it's not even a type
in Common Lisp), I think we can ignore `natnum` for `type-of` purposes.

> All in all I expect Elisp would be a bit easier to use and explain if it
> treats 'integer' as the fundamental type, and 'characterp', 'fixnump',
> 'bignump', 'natnump' as predicates that succeed on only some members of
> that type.

As long as `eq` treats bignums differently from fixnums, I think type-of
should return different values for the two (ideally, I'd vote for
`bignum` and `fixnum` respectively).

When/if we completely hide the difference between the two (i.e. all
integers behave like bignums and fixnums are just an implementation
detail for optimization purposes), then I'd agree that `type-of` can
return just `integer` for those numbers regardless of their
internal representation.  But at that moment I'd also argue that
`fixnump` and `bignump` should disappear.

>> it's indeed safer to just keep returning `integer` for small integers
>> (and probably some other value for bignums, such as `bignum`).
> Why the latter? That is, when would it be useful in existing code for
> (type-of X) to return a value other than 'integer' when (integerp X)
> returns t?

Because it's generally better to return a more precise type than a more
generic one.
E.g. (type-of '(1 2)) returns `cons` not `list` and (type-of [1 2])
returns `vector` rather than `sequence`.


        Stefan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]