[Openmcl-devel] Type-of and positive/negative integers
ron at flownet.com
Thu Oct 24 07:21:57 PDT 2019
It works because not all types have corresponding classes. FIXNUM does, but an integer range does not.
Your top-level goal of figuring out whether or not two objects are the “same type” is not well defined because objects can be members of multiple types. 3 is a fixnum, but it is also an (integer 0 most-positive-fixnum). It is also an (integer 0 10) and an (integer -3 852) and an (integer 2 5) and…
On Oct 24, 2019, at 2:40 AM, Steven Nunez <steve_nunez at yahoo.com> wrote:
> Huh. Interesting. That works, thanks very much. I wouldn't have thought of that. Of course now I have to go figure out why this works...
> On Thursday, October 24, 2019, 4:07:11 PM GMT+8, Daniel Kochmański <daniel at turtleware.eu> wrote:
> or, alternatively, just use class-of instead of type-of.
> Daniel Kochmański writes:
> > Hmm, maybe something in this spirit?
> > (let ((type (type-of x)))
> > (flet ((maximize-type (new-number)
> > (let ((new-type (type-of new-number)))
> > (cond ((subtypep new-type type)
> > t)
> > ((subtypep type new-type)
> > (setf type new-type)
> > t)))))
> > (assert (every #'maximize-type coefficients))))
> > Daniel
> > Steven Nunez writes:
> >> Okay, having a specialised representation for positive integers makes sense as a further specialisation of INTEGER. I wish this was documented somewhere, a 1 minute google search did not turn up anything.
> >> I still have the problem of ensuring that the parameters to a function, a sequence and a number, are all of the same type so I can dispatch on specialised (typed declared) versions of loop. At the moment I have:
> >> (assert (and (plusp (length coefficients))
> >> (every (lambda (elt)
> >> (typep elt (type-of x)))
> >> coefficients)))
> >> which works fine for single-float and double-float, but fails if any of the coefficients are negative because their type-of is FIXNUM whilst the X or other coefficients may be (INTEGER 0 4611686018427387903)
> >> Any ideas?
> >> On Thursday, October 24, 2019, 3:24:56 PM GMT+8, Daniel Kochmański <daniel at turtleware.eu> wrote:
> >>> CCL (1.11) has most-positive-fixnum of 1152921504606846975SBCL (1.5.6) has 4611686018427387903
> >>> However:
> >>> CL-USER> (subtypep 'fixnum 'integer)
> >>> T
> >>> T
> >>> CL-USER>
> >>> so FIXNUM is a subtype of INTEGER, and it should be the type returned if the implementation is following the CltL2 guidelines if I'm understanding the type system correctly.
> >> CLtL2 has been superseded by ANSI specification so it is not
> >> bounding. Anyway, more specific type here is
> >> (integer 0 #.most-positive-fixnum)
> >> Try (subtypep (integer 0 #.most-positive-fixnum) 'fixnum) and it will
> >> yield true as well.
> >> The reason why you have result 'fixnum for negative and '(integer 0
> >> #.most-positive-fixnum) for positive integers is probably because CCL
> >> (and other implementations in fact) have a specialized representation
> >> for unsinged fixnums (similar to i.e 'unsigned int' in C).
> >> Daniel
> Daniel Kochmański ;; aka jackdaniel | Przemyśl, Poland
> TurtleWare - Daniel Kochmański | www.turtleware.eu
> "Be the change that you wish to see in the world." - Mahatma Gandhi
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Openmcl-devel