| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus predicates) |
| 18 |
|
| 19 |
(export fixnum? flonum? bignum? ratnum? compnum? cflonum? |
| 20 |
number? complex? real? rational? |
| 21 |
integer? exact? inexact? eof-object? bwp-object? immediate? |
| 22 |
boolean? char? vector? bytevector? string? procedure? null? pair? |
| 23 |
symbol? code? not weak-pair? eq? eqv? boolean=? |
| 24 |
symbol=? finite? infinite? nan? real-valued? |
| 25 |
rational-valued? integer-valued? transcoder?) |
| 26 |
(import |
| 27 |
(except (ikarus) fixnum? flonum? bignum? ratnum? compnum? cflonum? |
| 28 |
number? complex? real? |
| 29 |
rational? integer? exact? inexact? eof-object? bwp-object? |
| 30 |
immediate? boolean? char? vector? bytevector? string? procedure? |
| 31 |
null? pair? weak-pair? symbol? code? not eq? eqv? |
| 32 |
transcoder? boolean=? symbol=? |
| 33 |
finite? infinite? nan? real-valued? rational-valued? |
| 34 |
integer-valued?) |
| 35 |
(ikarus system $fx) |
| 36 |
(ikarus system $flonums) |
| 37 |
(ikarus system $pairs) |
| 38 |
(ikarus system $chars) |
| 39 |
(ikarus system $strings) |
| 40 |
(ikarus system $vectors) |
| 41 |
(only (ikarus system $compnums) $cflonum-real $cflonum-imag) |
| 42 |
(rename (only (ikarus) fixnum? flonum? bignum? ratnum? compnum? cflonum? |
| 43 |
eof-object? |
| 44 |
bwp-object? immediate? boolean? char? vector? string? |
| 45 |
bytevector? procedure? null? pair? symbol? code? eq? |
| 46 |
transcoder?) |
| 47 |
(fixnum? sys:fixnum?) |
| 48 |
(flonum? sys:flonum?) |
| 49 |
(bignum? sys:bignum?) |
| 50 |
(ratnum? sys:ratnum?) |
| 51 |
(compnum? sys:compnum?) |
| 52 |
(cflonum? sys:cflonum?) |
| 53 |
(eof-object? sys:eof-object?) |
| 54 |
(bwp-object? sys:bwp-object?) |
| 55 |
(immediate? sys:immediate?) |
| 56 |
(boolean? sys:boolean?) |
| 57 |
(char? sys:char?) |
| 58 |
(vector? sys:vector?) |
| 59 |
(bytevector? sys:bytevector?) |
| 60 |
(string? sys:string?) |
| 61 |
(procedure? sys:procedure?) |
| 62 |
(null? sys:null?) |
| 63 |
(pair? sys:pair?) |
| 64 |
(symbol? sys:symbol?) |
| 65 |
(code? sys:code?) |
| 66 |
(eq? sys:eq?) |
| 67 |
(transcoder? sys:transcoder?) |
| 68 |
)) |
| 69 |
|
| 70 |
(define fixnum? |
| 71 |
(lambda (x) (sys:fixnum? x))) |
| 72 |
|
| 73 |
(define bignum? |
| 74 |
(lambda (x) (sys:bignum? x))) |
| 75 |
|
| 76 |
(define ratnum? |
| 77 |
(lambda (x) (sys:ratnum? x))) |
| 78 |
|
| 79 |
(define flonum? |
| 80 |
(lambda (x) (sys:flonum? x))) |
| 81 |
|
| 82 |
(define compnum? |
| 83 |
(lambda (x) (sys:compnum? x))) |
| 84 |
|
| 85 |
(define cflonum? |
| 86 |
(lambda (x) (sys:cflonum? x))) |
| 87 |
|
| 88 |
(define number? |
| 89 |
(lambda (x) |
| 90 |
(or (sys:fixnum? x) |
| 91 |
(sys:bignum? x) |
| 92 |
(sys:flonum? x) |
| 93 |
(sys:ratnum? x) |
| 94 |
(sys:compnum? x) |
| 95 |
(sys:cflonum? x)))) |
| 96 |
|
| 97 |
(define complex? |
| 98 |
(lambda (x) (number? x))) |
| 99 |
|
| 100 |
(define real? |
| 101 |
(lambda (x) |
| 102 |
(or (sys:fixnum? x) |
| 103 |
(sys:bignum? x) |
| 104 |
(sys:flonum? x) |
| 105 |
(sys:ratnum? x)))) |
| 106 |
|
| 107 |
(define real-valued? |
| 108 |
(lambda (x) |
| 109 |
(cond |
| 110 |
[(real? x) #t] |
| 111 |
[(cflonum? x) (fl=? ($cflonum-imag x) 0.0)] |
| 112 |
[else #f]))) |
| 113 |
|
| 114 |
(define rational? |
| 115 |
(lambda (x) |
| 116 |
(cond |
| 117 |
[(sys:fixnum? x) #t] |
| 118 |
[(sys:bignum? x) #t] |
| 119 |
[(sys:ratnum? x) #t] |
| 120 |
[(sys:flonum? x) ($flonum-rational? x)] |
| 121 |
[else #f]))) |
| 122 |
|
| 123 |
(define rational-valued? |
| 124 |
(lambda (x) |
| 125 |
(cond |
| 126 |
[(rational? x) #t] |
| 127 |
[(cflonum? x) |
| 128 |
(and (fl=? ($cflonum-imag x) 0.0) |
| 129 |
($flonum-rational? ($cflonum-real x)))] |
| 130 |
[else #f]))) |
| 131 |
|
| 132 |
(define integer? |
| 133 |
(lambda (x) |
| 134 |
(cond |
| 135 |
[(sys:fixnum? x) #t] |
| 136 |
[(sys:bignum? x) #t] |
| 137 |
[(sys:ratnum? x) #f] |
| 138 |
[(sys:flonum? x) ($flonum-integer? x)] |
| 139 |
[else #f]))) |
| 140 |
|
| 141 |
(define integer-valued? |
| 142 |
(lambda (x) |
| 143 |
(cond |
| 144 |
[(integer? x) #t] |
| 145 |
[(cflonum? x) |
| 146 |
(and (fl=? ($cflonum-imag x) 0.0) |
| 147 |
($flonum-integer? ($cflonum-real x)))] |
| 148 |
[else #f]))) |
| 149 |
|
| 150 |
|
| 151 |
(define exact? |
| 152 |
(lambda (x) |
| 153 |
(cond |
| 154 |
[(sys:fixnum? x) #t] |
| 155 |
[(sys:bignum? x) #t] |
| 156 |
[(sys:ratnum? x) #t] |
| 157 |
[(sys:flonum? x) #f] |
| 158 |
[(sys:compnum? x) #t] |
| 159 |
[(sys:cflonum? x) #f] |
| 160 |
[else |
| 161 |
(die 'exact? "not a number" x)]))) |
| 162 |
|
| 163 |
|
| 164 |
(define inexact? |
| 165 |
(lambda (x) |
| 166 |
(cond |
| 167 |
[(sys:flonum? x) #t] |
| 168 |
[(sys:fixnum? x) #f] |
| 169 |
[(sys:bignum? x) #f] |
| 170 |
[(sys:ratnum? x) #f] |
| 171 |
[(sys:compnum? x) #f] |
| 172 |
[(sys:cflonum? x) #t] |
| 173 |
[else |
| 174 |
(die 'inexact? "not a number" x)]))) |
| 175 |
|
| 176 |
(define finite? |
| 177 |
(lambda (x) |
| 178 |
(cond |
| 179 |
[(sys:flonum? x) (flfinite? x)] |
| 180 |
[(sys:fixnum? x) #t] |
| 181 |
[(sys:bignum? x) #t] |
| 182 |
[(sys:ratnum? x) #t] |
| 183 |
[(sys:compnum? x) #t] |
| 184 |
[(sys:cflonum? x) |
| 185 |
(and |
| 186 |
(flfinite? ($cflonum-real x)) |
| 187 |
(flfinite? ($cflonum-imag x)))] |
| 188 |
[else |
| 189 |
(die 'finite? "not a number" x)]))) |
| 190 |
|
| 191 |
(define infinite? |
| 192 |
(lambda (x) |
| 193 |
(cond |
| 194 |
[(sys:flonum? x) (flinfinite? x)] |
| 195 |
[(sys:fixnum? x) #f] |
| 196 |
[(sys:bignum? x) #f] |
| 197 |
[(sys:ratnum? x) #f] |
| 198 |
[(sys:compnum? x) #f] |
| 199 |
[(sys:cflonum? x) |
| 200 |
(or |
| 201 |
(flinfinite? ($cflonum-real x)) |
| 202 |
(flinfinite? ($cflonum-imag x)))] |
| 203 |
[else |
| 204 |
(die 'infinite? "not a number" x)]))) |
| 205 |
|
| 206 |
(define nan? |
| 207 |
(lambda (x) |
| 208 |
(cond |
| 209 |
[(sys:flonum? x) (flnan? x)] |
| 210 |
[(sys:fixnum? x) #f] |
| 211 |
[(sys:bignum? x) #f] |
| 212 |
[(sys:ratnum? x) #f] |
| 213 |
[(sys:compnum? x) #f] |
| 214 |
[(sys:cflonum? x) |
| 215 |
(or |
| 216 |
(nan? ($cflonum-real x)) |
| 217 |
(nan? ($cflonum-imag x)))] |
| 218 |
[else |
| 219 |
(die 'nan? "not a number" x)]))) |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
(define eof-object? (lambda (x) (sys:eof-object? x))) |
| 225 |
(define bwp-object? (lambda (x) (sys:bwp-object? x))) |
| 226 |
(define transcoder? (lambda (x) (sys:transcoder? x))) |
| 227 |
(define immediate? (lambda (x) (sys:immediate? x))) |
| 228 |
(define boolean? (lambda (x) (sys:boolean? x))) |
| 229 |
(define char? (lambda (x) (sys:char? x))) |
| 230 |
(define vector? (lambda (x) (sys:vector? x))) |
| 231 |
(define bytevector? (lambda (x) (sys:bytevector? x))) |
| 232 |
(define string? (lambda (x) (sys:string? x))) |
| 233 |
(define procedure? (lambda (x) (sys:procedure? x))) |
| 234 |
(define null? (lambda (x) (sys:null? x))) |
| 235 |
(define pair? (lambda (x) (sys:pair? x))) |
| 236 |
(define symbol? (lambda (x) (sys:symbol? x))) |
| 237 |
(define code? (lambda (x) (sys:code? x))) |
| 238 |
|
| 239 |
|
| 240 |
(define weak-pair? |
| 241 |
(lambda (x) |
| 242 |
(and (pair? x) |
| 243 |
(foreign-call "ikrt_is_weak_pair" x)))) |
| 244 |
|
| 245 |
(define not (lambda (x) (if x #f #t))) |
| 246 |
|
| 247 |
(define eq? (lambda (x y) (sys:eq? x y))) |
| 248 |
|
| 249 |
(define eqv? |
| 250 |
(lambda (x y) |
| 251 |
(import (ikarus)) |
| 252 |
(cond |
| 253 |
[(eq? x y) #t] |
| 254 |
[(flonum? x) |
| 255 |
(and (flonum? y) |
| 256 |
(if ($fl< x y) |
| 257 |
#f |
| 258 |
(if ($fl> x y) |
| 259 |
#f |
| 260 |
(if ($fl= x 0.0) |
| 261 |
($fl= ($fl/ 1.0 x) ($fl/ 1.0 y)) |
| 262 |
#t))))] |
| 263 |
[(bignum? x) (and (bignum? y) (= x y))] |
| 264 |
[(ratnum? x) (and (ratnum? y) (= x y))] |
| 265 |
[(compnum? x) |
| 266 |
(and (compnum? y) |
| 267 |
(= (real-part x) (real-part y)) |
| 268 |
(= (imag-part x) (imag-part y)))] |
| 269 |
[(cflonum? x) |
| 270 |
(and (cflonum? y) |
| 271 |
(eqv? (real-part x) (real-part y)) |
| 272 |
(eqv? (imag-part x) (imag-part y)))] |
| 273 |
[else #f]))) |
| 274 |
|
| 275 |
|
| 276 |
(define-syntax define-pred |
| 277 |
(syntax-rules () |
| 278 |
[(_ name pred? msg) |
| 279 |
(begin |
| 280 |
(define (err x) (die 'name msg x)) |
| 281 |
(define (g rest) |
| 282 |
(if (sys:pair? rest) |
| 283 |
(let ([a (car rest)]) |
| 284 |
(if (pred? a) |
| 285 |
(g (cdr rest)) |
| 286 |
(err a))) |
| 287 |
#f)) |
| 288 |
(define (f x rest) |
| 289 |
(if (sys:pair? rest) |
| 290 |
(let ([a (car rest)]) |
| 291 |
(if (sys:eq? x a) |
| 292 |
(f x (cdr rest)) |
| 293 |
(if (pred? a) |
| 294 |
(g (cdr rest)) |
| 295 |
(err a)))) |
| 296 |
#t)) |
| 297 |
(define name |
| 298 |
(case-lambda |
| 299 |
[(x y) |
| 300 |
(if (pred? x) |
| 301 |
(if (sys:eq? x y) |
| 302 |
#t |
| 303 |
(if (pred? y) |
| 304 |
#f |
| 305 |
(err y))) |
| 306 |
(err x))] |
| 307 |
[(x y z . rest) |
| 308 |
(if (pred? x) |
| 309 |
(if (sys:eq? x y) |
| 310 |
(if (sys:eq? x z) |
| 311 |
(f x rest) |
| 312 |
(if (pred? z) #f (err z))) |
| 313 |
(if (pred? y) #f (err y))) |
| 314 |
(err x))])))])) |
| 315 |
(define-pred symbol=? sys:symbol? "not a symbol") |
| 316 |
(define-pred boolean=? sys:boolean? "not a boolean") |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
) |