Marc:
There's a very nice little thread in comp.lang.lisp about how to calculate the number of partitions of a positive integer. The fastest algorithm by far uses Common Lisp's hash tables, so I thought I would try it with Gambit's tables, but it doesn't seem to work when compiled.
Here is the code:
(define (iota-reverse n) (let loop ((i 1) (result '())) (if (> i n) result (loop (+ i 1) (cons i result)))))
(define (make-list n #!optional init) (vector->list (make-vector n init)))
(define (break-partitions n break) (or (table-ref *break-cache* (cons n break) #f) (let ((result (cond ((= break 1) (list (make-list n 1))) ((= break n) (list (list n))) (else (let ((rem (- n break))) (map (lambda (i) (cons break (break-partitions rem i))) (iota-reverse (min break rem)))))))) (table-set! *break-cache* (cons n break) result) result)))
(define (breaks n) (apply append (map (lambda (break) (break-partitions n break)) (iota-reverse n)))))
It gives the correct result when interpreted, but not when compiled:
[descartes:~/programs/gambc40b13] lucier% gsi loading /usr/local/Gambit-C/gambcext.scm Gambit Version 4.0 beta 13
(load "partition.scm")
"/Users/lucier/programs/gambc40b13/partition.scm"
(pp (breaks 10))
((10) (9 (1)) (8 (2)) (8 (1 1)) (7 (3)) (7 (2 (1))) (7 (1 1 1)) (6 (4)) (6 (3 (1))) (6 (2 (2)) (2 (1 1))) (6 (1 1 1 1)) (5 (5)) (5 (4 (1))) (5 (3 (2)) (3 (1 1))) (5 (2 (2 (1))) (2 (1 1 1))) (5 (1 1 1 1 1)) (4 (4 (2)) (4 (1 1))) (4 (3 (3)) (3 (2 (1))) (3 (1 1 1))) (4 (2 (2 (2)) (2 (1 1))) (2 (1 1 1 1))) (4 (1 1 1 1 1 1)) (3 (3 (3 (1))) (3 (2 (2)) (2 (1 1))) (3 (1 1 1 1))) (3 (2 (2 (2 (1))) (2 (1 1 1))) (2 (1 1 1 1 1))) (3 (1 1 1 1 1 1 1)) (2 (2 (2 (2 (2)) (2 (1 1))) (2 (1 1 1 1))) (2 (1 1 1 1 1 1))) (2 (1 1 1 1 1 1 1 1)) (1 1 1 1 1 1 1 1 1 1))
(define a (time (breaks 500)))
(time (breaks 500)) 83976 ms real time 71372 ms cpu time (69503 user, 1869 system) 33 collections accounting for 7657 ms real time (6264 user, 124 system) 764582700 bytes allocated no minor faults no major faults [descartes:~/programs/gambc40b13] lucier% gsi loading /usr/local/Gambit-C/gambcext.scm Gambit Version 4.0 beta 13
(load "partition")
"/Users/lucier/programs/gambc40b13/partition.o2"
(pp (breaks 10))
((10) (9 (1)) (8 (2)) (8 (1 1)) (7 (3)) (7 (2 (1))) (7 (1 1 1)) (6 (4)) (6 (3 (1))) (6 (2 (2)) (2 (1 1))) (6 (1 1 1 1)) (5 (5)) (5 (4 (1))) (5 (3 (2)) (3 (1 1))) (5 (2 (2 (1))) (2 (1 1 1))) (5 (1 1 1 1 1)) (4 (4 (2)) (4 (1 1))) (4 (3 (3)) (3 (2 (1))) (3 (1 1 1))) (4 (2 (2 (2)) (2 (1 1))) (2 (1 1 1 1))) (4 (1 1 1 1 1 1)) (3 (3 (3 (1))) (3 (2 (2)) (2 (1 1))) (3 (1 1 1 1))) (3 (2 (2 (2 (1))) (2 (1 1 1))) (2 (1 1 1 1 1))) (3 (1 1 1 1 1 1 1)) (2 (2 (2 (2 (2)) (2 (1 1))) (2 (1 1 1 1))) (2 (1 1 1 1 1 1))) (2 (1 1 1 1 1 1 1 1)) (1 1 1 1 1 1 1 1 1 1))
(define a (time (breaks 500)))
*** ERROR IN break-partitions, "partition.scm"@15.7 -- (Argument 1) Instance of #<type #2 table> expected (table-ref 0 '(26 . 16) #f) 1> ,e n = 26 break = 16 (current-exception-handler) = primordial-exception-handler (current-input-port) = '#<input-output-port #3 (console)> (current-output-port) = '#<input-output-port #3 (console)> (current-directory) = "/Users/lucier/programs/gambc40b13/" 1> ,y 0 break-partitions "partition.scm"@15.7 (table-ref *break-cache... 1> ,i #<procedure #4 break-partitions> = (lambda (n break) (or (table-ref *break-cache* (cons n break) #f) (let ((result (cond ((= break 1) (list (make-list n 1))) ((= break n) (list (list n))) (else (let ((rem (- n break))) (map (lambda (i) (cons break (break-partitions rem i))) (iota-reverse (min break rem)))))))) (table-set! *break-cache* (cons n break) result) result))) 1> (table-length *break-cache*) 1404 1>
Do you know what's happening?
This is in MacOS 10.4.1, Apple's gcc-4.0, default configure and build.
I used the '(debug) option to compile the code (which works very nicely, by the way).
Brad
Afficher les réponses par date
On May 19, 2005, at 10:47 PM, Bradley Lucier wrote:
There's a very nice little thread in comp.lang.lisp about how to calculate the number of partitions of a positive integer. The fastest algorithm by far uses Common Lisp's hash tables, so I thought I would try it with Gambit's tables, but it doesn't seem to work when compiled.
Sorry, it appears to be a gcc-4.0 bug, I had no problems using gcc-3.3 on MacOS 10.4.1.
I'll investigate it more
Brad
On May 19, 2005, at 11:50 PM, Bradley Lucier wrote:
Sorry, it appears to be a gcc-4.0 bug, I had no problems using gcc-3.3 on MacOS 10.4.1.
I'll investigate it more
The FSF version of gcc-4.0.0 has no problem with the code, so it seems to be a problem with Apple's gcc-4.0.0. Grrrrr.
Brad
On MacOS 10.4.1 Tiger, the version of gcc 4.0.0 shipped with Tiger,
[descartes:~/programs/gcc] lucier% /usr/bin/gcc -v Reading specs from /usr/lib/gcc/powerpc-apple-darwin8/4.0.0/specs Configured with: /private/var/tmp/gcc/gcc-4061.obj~8/src/configure -- disable-checking --prefix=/usr --mandir=/share/man --enable- languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^+.-]*$/ s/$/-4.0/ --with-gxx-include-dir=/include/gcc/darwin/4.0/c++ -- build=powerpc-apple-darwin8 --host=powerpc-apple-darwin8 -- target=powerpc-apple-darwin8 Thread model: posix gcc version 4.0.0 20041026 (Apple Computer, Inc. build 4061)
miscompiles gambc40b13. gambc40b13 appears to work correctly with the FSF release of gcc-4.0.0 and with the cvs build of Apple's gcc, which is based on the released version of FSF gcc-4.0.0.
Here are some hints about how to build Apples current CVS version of gcc based on the FSF gcc-4.0.0 release.
First, download and install cctools-576:
http://gcc.gnu.org/ml/gcc/2005-03/msg01149.html
Then download the cvs version of Apple's GCC-4.0.0 sources:
cvs -z 9 co -r apple-local-200502-branch gcc
after following the instructions at
for setting up your environment variables and logging in. This will give you the following version of gcc:
const char version_string[] = "4.0.0 (Apple Computer, Inc. build 5018)";
It would seem too risky to build gcc Apple's way (which will overwrite /usr/bin/gcc on install ;-), so I built it the gcc way and installed it at /pkgs/gcc-4.0.0-apple. Java won't build (blah), so I configured it to build c, c++, and f95 with
#!/bin/tcsh /bin/rm -rf *; ../configure --prefix=/pkgs/gcc-4.0.0-apple --with- gmp=/pkgs/gmp-4.1.4 --with-mpfr=/pkgs/gmp-4.1.4 --enable-languages=c,c ++,f95 ; make -j 4 bootstrap >& build.log
If you don't have gmp or mpfr installed you can build it without f95.
Or, you can (a) install the official gcc-4.0.0 or (b) wait until Apple updates its developer tools.
Brad