Lisp HUG Maillist Archive

different results with different compiler settings

I'm trying to optimize insertion into a sorted list (LWwin 6.0.1 pro, based on [*])

(defun insert-sorted (item list)
  (if list
    (loop :with previous-tail := nil
          :for tail :on list
          :for element := (first tail)
          :do (if (<= item element)
                (if (= item element)
                  (progn
                    (print "already present")
                    (return list))
                  (if previous-tail
                    (progn
                      (setf (cdr previous-tail) (cons item tail))
                      (return list))
                    (return (cons item list))))
                (setf previous-tail tail))
          :finally
          (setf (cdr previous-tail) (cons item '()))
          (return list))
    (list item)))


(declaim (optimize (speed 3) (safety 3) (debug 3) (hcl:fixnum-safety 3)))

compile the function:
;;; Safety = 3, Speed = 3, Space = 1, Float = 1, Interruptible = 1
;;; Compilation speed = 1, Debug = 3, Fixnum safety = 3
;;; Source level debugging is on
;;; Source file recording is  on
;;; Cross referencing is on
; (LISPWORKS:TOP-LEVEL-FORM 0)
; INSERT-SORTED

---- Done ----

(insert-sorted 2.5 (list 1 2 3 4 5 6 7))

==>  (1 2 2.5 3 4 5 6 7)

which is the expected result.


(declaim (optimize (speed 3) (safety 0) (debug 0) (hcl:fixnum-safety 0)))

compile the function:
;;; Safety = 0, Speed = 3, Space = 1, Float = 1, Interruptible = 1
;;; Compilation speed = 1, Debug = 0, Fixnum safety = 0
;;; Source level debugging is off
;;; Source file recording is  on
;;; Cross referencing is on
; (LISPWORKS:TOP-LEVEL-FORM 0)
; INSERT-SORTED

---- Done ----

(insert-sorted 2.5 (list 1 2 3 4 5 6 7))

==> (1 2 3 4 5 6 7 2.5)

which is a different result.

My understanding is that optimization settings do not change the result of a correct program.
So the program is incorrect, but I don't see it.

Would someone please be so kind to assist me in finding the solution ?


Thank you

Frank


[*] http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/914dbc19927753ed/2ab553e452542cac?lnk=gst&q=+vertebrae#2ab553e452542cac
  

SMS schreiben mit WEB.DE FreeMail - einfach, schnell und   
kostenguenstig. Jetzt gleich testen! http://f.web.de/?mc=021192

Re: different results with different compiler settings

Hello Frank,

The problem lies in the hcl:fixnum-safety optimization.
According to this thread:
  http://comments.gmane.org/gmane.lisp.lispworks.general/4508

And this documentation page:
  http://www.lispworks.com/documentation/lw60/LW/html/lw-99.htm#pgfId-890345

With the optimized settings, the compiler expects the arguments to your function to be fixnums only.  This is why the comparison between the integer numbers and the float argument fail.


Best Regards,
Camille



Wednesday, January 11, 2012, 12:07:42 AM, you wrote:


I'm trying to optimize insertion into a sorted list (LWwin 6.0.1 pro, based on [*])

(defun insert-sorted (item list)
  (if list
    (loop :with previous-tail := nil
          :for tail :on list
          :for element := (first tail)
          :do (if (<= item element)
                (if (= item element)
                  (progn
                    (print "already present")
                    (return list))
                  (if previous-tail
                    (progn
                      (setf (cdr previous-tail) (cons item tail))
                      (return list))
                    (return (cons item list))))
                (setf previous-tail tail))
          :finally
          (setf (cdr previous-tail) (cons item '()))
          (return list))
    (list item)))


(declaim (optimize (speed 3) (safety 3) (debug 3) (hcl:fixnum-safety 3)))

compile the function:
;;; Safety = 3, Speed = 3, Space = 1, Float = 1, Interruptible = 1
;;; Compilation speed = 1, Debug = 3, Fixnum safety = 3
;;; Source level debugging is on
;;; Source file recording is  on
;;; Cross referencing is on
; (LISPWORKS:TOP-LEVEL-FORM 0)
; INSERT-SORTED

---- Done ----

(insert-sorted 2.5 (list 1 2 3 4 5 6 7))

==>  (1 2 2.5 3 4 5 6 7)

which is the expected result.


(declaim (optimize (speed 3) (safety 0) (debug 0) (hcl:fixnum-safety 0)))

compile the function:
;;; Safety = 0, Speed = 3, Space = 1, Float = 1, Interruptible = 1
;;; Compilation speed = 1, Debug = 0, Fixnum safety = 0
;;; Source level debugging is off
;;; Source file recording is  on
;;; Cross referencing is on
; (LISPWORKS:TOP-LEVEL-FORM 0)
; INSERT-SORTED

---- Done ----

(insert-sorted 2.5 (list 1 2 3 4 5 6 7))

==> (1 2 3 4 5 6 7 2.5)

which is a different result.

My understanding is that optimization settings do not change the result of a correct program.
So the program is incorrect, but I don't see it.

Would someone please be so kind to assist me in finding the solution ?


Thank you

Frank


[*] http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/914dbc19927753ed/2ab553e452542cac?lnk=gst&q=+vertebrae#2ab553e452542cac
  


SMS schreiben mit WEB.DE FreeMail - einfach, schnell und   
kostenguenstig. Jetzt gleich testen! http://f.web.de/?mc=021192





-- 
Best regards,
 Camille                            mailto:camille@osculator.net


Re: different results with different compiler settings

Unable to parse email body. Email id is 11431

Updated at: 2020-12-10 08:37 UTC