Skip to content

Commit 6f80b4f

Browse files
authored
Add a few improvements for predicates in cptype, in particular for zero? (cisco#960)
* Allow #f and (void) as results of primitive handlers in cptypes The speciliation handlers can return #f or (void) in case they don't want to make changes, and then the default is used. The use of #f is similar to `define-inline` in cp0, but (void) allows to use `when`or `cond` to select the useful cases. * Improve predicates handling in cptype When a primitive is marked as a predicate and has a handler, then try to reduce it as a predicate and then call the handler for more specific reductions. * Add rational? to cptypes * Improve support of zero? in cptypes In particular, reduce (if (zero? x) (zero? x) (zero? x)) => (if (zero? x) #t #f) => (zero? x) * Track 0.0 and -0.0 in cptypes It useful to reduce (cond [(eqv? x 0.0) ...] [(eqv? x -0.0) ...] [(flzero? x) ...] ; this can be eliminated [...])
1 parent 07b679b commit 6f80b4f

File tree

6 files changed

+315
-243
lines changed

6 files changed

+315
-243
lines changed

mats/cptypes.ms

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@
608608
(define (test-chain*/preamble l)
609609
(test-chain/preamble/self preamble #f l))
610610

611+
(define (test-all-imply l* p)
612+
(andmap (lambda (l) (test-chain (list l p))) l*))
613+
614+
(define (test-all-imply* l* p)
615+
(andmap (lambda (l) (test-chain* (list l p))) l*))
616+
611617
(define (test-disjoint/preamble/self preamble check-self? l)
612618
(let loop ([l l])
613619
(if (null? l)
@@ -663,6 +669,8 @@
663669
(test-chain '((lambda (x) (eqv? 0.0 x)) flonum? cflonum? number?))
664670
(test-chain* '((lambda (x) (or (eqv? x 0.0) (eqv? x 3.14))) flonum? real? number?))
665671
(test-chain* '((lambda (x) (or (eq? x 0) (eqv? x 3.14))) real? number?))
672+
(test-chain* '(fixnum? rational? real?))
673+
(test-chain* '(flzero? rational? real?))
666674
(test-chain '(gensym? symbol?))
667675
(test-chain '((lambda (x) (eq? x 'banana)) symbol?))
668676
(test-chain '(not boolean?))
@@ -683,6 +691,8 @@
683691
(test-disjoint '(integer? ratnum?))
684692
(test-disjoint '((lambda (x) (eq? x 'banana)) (lambda (x) (eq? x 'apple))))
685693
(test-disjoint* '(list? record? vector?))
694+
(test-all-imply* '(rational? finite?) 'real?)
695+
(test-all-imply* '(infinite? nan? flfinite? flinfinite? flnan?) 'flonum?)
686696
)
687697

688698
; use a gensym to make expansions equivalent
@@ -900,6 +910,15 @@
900910
(cptypes-equivalent-expansion?
901911
'(lambda (x) (when (fixnum? x) (fxzero? x)))
902912
'(lambda (x) (when (fixnum? x) (#3%fxzero? x))))
913+
(cptypes-equivalent-expansion?
914+
'(lambda (x) (if (zero? x) (zero? x) (zero? x)))
915+
'(lambda (x) (if (zero? x) #t #f)))
916+
(cptypes-equivalent-expansion?
917+
'(lambda (x) (if (flzero? x) (or (eqv? x -0.0) (eqv? x 0.0)) (or (eqv? x -0.0) (eqv? x 0.0))))
918+
'(lambda (x) (if (flzero? x) #t #f)))
919+
(cptypes-equivalent-expansion?
920+
'(lambda (x) (when (flonum? x) (if (or (eqv? x -0.0) (eqv? x 0.0)) (flzero? x) (flzero? x))))
921+
'(lambda (x) (when (flonum? x) (if (or (eqv? x -0.0) (eqv? x 0.0)) #t #f))))
903922
(not (cptypes-equivalent-expansion?
904923
'(lambda (x) (when (number? x) (#2%odd? x)))
905924
'(lambda (x) (when (number? x) (#3%odd? x)))))
@@ -909,6 +928,24 @@
909928
(not (cptypes-equivalent-expansion?
910929
'(lambda (x) (#2%exact? x))
911930
'(lambda (x) (#3%exact? x))))
931+
(cptypes-equivalent-expansion?
932+
'(lambda (x) (when (fixnum? x) (integer? x)))
933+
'(lambda (x) (when (fixnum? x) #t)))
934+
(cptypes-equivalent-expansion?
935+
'(lambda (x) (when (null? x) (integer? x)))
936+
'(lambda (x) (when (null? x) #f)))
937+
(cptypes-equivalent-expansion?
938+
'(lambda (x) (when (flonum? x) (#2%integer? x)))
939+
'(lambda (x) (when (flonum? x) (#3%flinteger? x))))
940+
(cptypes-equivalent-expansion?
941+
'(lambda (x) (when (flonum? x) (#2%rational? x)))
942+
'(lambda (x) (when (flonum? x) (#3%flfinite? x))))
943+
(cptypes-equivalent-expansion?
944+
'(lambda (x) (when (real? x) (#2%finite? x)))
945+
'(lambda (x) (when (real? x) (#3%finite? x))))
946+
(cptypes-equivalent-expansion?
947+
'(lambda (x) (when (flonum? x) (#2%finite? x)))
948+
'(lambda (x) (when (flonum? x) (#3%flfinite? x))))
912949
(cptypes-equivalent-expansion?
913950
'(lambda (x) (when (fixnum? x)
914951
(add1 x)))

mats/primvars.ms

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,15 @@
418418
[(fasl-strip-options) (fasl-strip-options inspector-source) (file-options compressed) #f]
419419
[(file-options) (file-options compressed) 1/2 #f]
420420
[(fixnum) -1 'q (+ (most-positive-fixnum) 1) (- (most-negative-fixnum) 1) #f]
421+
[(flinteger) 0.0 0 0.5 0.0+1.0i 'a #f]
421422
[(flonum) 0.0 0 0.0+1.0i 'a #f]
423+
[(flrational) 0.5 1/2 1+2i +inf.0 #f]
424+
[(flvector) '#vfl(0.0) "a" #f]
425+
[(flzero) 0.0 0 "a" #f]
422426
[(ftype-pointer) *ftype-pointer 0 *time #f]
423427
[(sub-ftype-pointer) no-good]
424428
[(fxvector) '#vfx(0) "a" #f]
425-
[(flvector) '#vfl(0.0) "a" #f]
429+
[(fxzero) 0 0.0 "a" #f]
426430
[(gensym) *genny 'sym #f]
427431
[(guardian) (make-guardian) values "oops" #f]
428432
[(hashtable) *eq-hashtable '((a . b)) #f]
@@ -432,6 +436,7 @@
432436
[(immutable-string) *immutable-string "a" 'a #f]
433437
[(immutable-vector) *immutable-vector '#(a) "a" #f]
434438
[(import-spec) '(chezscheme) 0 '(a . b) #f]
439+
[(infinite) +inf.0 +nan.0 +inf.0+inf.0i 0.0 0 "a" #f]
435440
[(input-port) (current-input-port) 0 *binary-output-port *textual-output-port #f]
436441
[(integer) 0.0 1/2 1.0+0.0i 'a #f]
437442
[(i/o-encoding-error) (make-i/o-encoding-error 17 23) (make-who-condition 'who) 1/2 #f]
@@ -462,6 +467,7 @@
462467
[(maybe-who) 'who 17]
463468
[(maybe-timeout) *time 371]
464469
[(message-condition) (make-message-condition 17) (make-who-condition 'who) 'q #f]
470+
[(nan) +nan.0 +inf.0 +nan.0+nan.0i 0.0 0 "a" #f]
465471
[(nonempty-bytevector) '#vu8(0) '#vu8() "a" #f]
466472
[(nonempty-flvector) '#vfl(0.0) '#vfl() "a" #f]
467473
[(nonempty-fxvector) '#vfx(0) '#vfx() "a" #f]
@@ -528,6 +534,7 @@
528534
[(vector) '#(a) "a" #f]
529535
[(stencil-vector) (stencil-vector 7 1 2 3) "a" #f]
530536
[(who-condition) (make-who-condition 'me) (make-message-condition "hello") 'the-who #f]
537+
[(zero) 0 1 1.0 "a" #f]
531538
[($exactnum) 1+1i 1.0+1.0i 1 1.0 'a #f]
532539
[($inexactnum) 1.0+1.0i 1+1i 1 1.0 'a #f])
533540
(meta-cond

release_notes/release_notes.stex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ and \scheme{inexact?}, .
137137
\subsection{Type recovery improvements (10.3.0)}
138138

139139
The type recovery pass has improved support for \scheme{cfl+} and
140-
similar functions.
140+
similar functions. Also improve the support of predicates, in
141+
particular integer?, zero? and similar predicates.
141142

142143
\subsection{Unicode 16.0.0 support (10.3.0)}
143144

0 commit comments

Comments
 (0)