2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 03:24:33 -04:00
|
|
|
|
|
|
|
(library (ikarus predicates)
|
2007-05-05 03:34:59 -04:00
|
|
|
|
2008-05-24 13:13:01 -04:00
|
|
|
(export fixnum? flonum? bignum? ratnum? compnum? cflonum?
|
2008-05-18 23:39:41 -04:00
|
|
|
number? complex? real? rational?
|
2007-09-03 00:34:53 -04:00
|
|
|
integer? exact? inexact? eof-object? bwp-object? immediate?
|
2007-05-15 12:42:52 -04:00
|
|
|
boolean? char? vector? bytevector? string? procedure? null? pair?
|
2008-11-11 00:39:02 -05:00
|
|
|
symbol? code? not weak-pair? eq? eqv? boolean=?
|
2007-09-12 03:10:54 -04:00
|
|
|
symbol=? finite? infinite? nan? real-valued?
|
2007-12-10 07:28:03 -05:00
|
|
|
rational-valued? integer-valued? transcoder?)
|
2007-05-05 03:24:33 -04:00
|
|
|
(import
|
2008-05-24 13:13:01 -04:00
|
|
|
(except (ikarus) fixnum? flonum? bignum? ratnum? compnum? cflonum?
|
2008-05-18 23:39:41 -04:00
|
|
|
number? complex? real?
|
2007-09-03 00:34:53 -04:00
|
|
|
rational? integer? exact? inexact? eof-object? bwp-object?
|
2007-05-15 12:42:52 -04:00
|
|
|
immediate? boolean? char? vector? bytevector? string? procedure?
|
2008-11-11 00:39:02 -05:00
|
|
|
null? pair? weak-pair? symbol? code? not eq? eqv?
|
2007-12-10 07:28:03 -05:00
|
|
|
transcoder? boolean=? symbol=?
|
2007-09-12 03:10:54 -04:00
|
|
|
finite? infinite? nan? real-valued? rational-valued?
|
|
|
|
integer-valued?)
|
2007-05-06 18:43:04 -04:00
|
|
|
(ikarus system $fx)
|
2007-06-13 02:03:30 -04:00
|
|
|
(ikarus system $flonums)
|
2007-05-06 18:43:04 -04:00
|
|
|
(ikarus system $pairs)
|
|
|
|
(ikarus system $chars)
|
|
|
|
(ikarus system $strings)
|
|
|
|
(ikarus system $vectors)
|
2008-05-24 13:13:01 -04:00
|
|
|
(only (ikarus system $compnums) $cflonum-real $cflonum-imag)
|
|
|
|
(rename (only (ikarus) fixnum? flonum? bignum? ratnum? compnum? cflonum?
|
2008-05-18 23:39:41 -04:00
|
|
|
eof-object?
|
2007-05-05 06:15:25 -04:00
|
|
|
bwp-object? immediate? boolean? char? vector? string?
|
2007-05-15 12:42:52 -04:00
|
|
|
bytevector? procedure? null? pair? symbol? code? eq?
|
2007-12-10 07:28:03 -05:00
|
|
|
transcoder?)
|
2007-05-05 03:24:33 -04:00
|
|
|
(fixnum? sys:fixnum?)
|
|
|
|
(flonum? sys:flonum?)
|
2007-05-05 03:27:53 -04:00
|
|
|
(bignum? sys:bignum?)
|
2007-05-20 23:23:54 -04:00
|
|
|
(ratnum? sys:ratnum?)
|
2008-05-18 23:39:41 -04:00
|
|
|
(compnum? sys:compnum?)
|
2008-05-24 13:13:01 -04:00
|
|
|
(cflonum? sys:cflonum?)
|
2007-05-05 03:27:53 -04:00
|
|
|
(eof-object? sys:eof-object?)
|
2007-05-05 06:15:25 -04:00
|
|
|
(bwp-object? sys:bwp-object?)
|
2007-05-05 03:34:59 -04:00
|
|
|
(immediate? sys:immediate?)
|
|
|
|
(boolean? sys:boolean?)
|
|
|
|
(char? sys:char?)
|
|
|
|
(vector? sys:vector?)
|
2007-05-15 12:42:52 -04:00
|
|
|
(bytevector? sys:bytevector?)
|
2007-05-05 03:34:59 -04:00
|
|
|
(string? sys:string?)
|
|
|
|
(procedure? sys:procedure?)
|
|
|
|
(null? sys:null?)
|
|
|
|
(pair? sys:pair?)
|
2007-05-05 05:09:15 -04:00
|
|
|
(symbol? sys:symbol?)
|
2007-05-06 22:48:10 -04:00
|
|
|
(code? sys:code?)
|
2007-05-05 17:44:24 -04:00
|
|
|
(eq? sys:eq?)
|
2007-12-06 05:05:26 -05:00
|
|
|
(transcoder? sys:transcoder?)
|
2007-05-05 17:44:24 -04:00
|
|
|
))
|
2007-05-05 03:24:33 -04:00
|
|
|
|
|
|
|
(define fixnum?
|
|
|
|
(lambda (x) (sys:fixnum? x)))
|
|
|
|
|
|
|
|
(define bignum?
|
|
|
|
(lambda (x) (sys:bignum? x)))
|
|
|
|
|
2007-05-20 23:23:54 -04:00
|
|
|
(define ratnum?
|
|
|
|
(lambda (x) (sys:ratnum? x)))
|
|
|
|
|
2007-05-05 03:24:33 -04:00
|
|
|
(define flonum?
|
|
|
|
(lambda (x) (sys:flonum? x)))
|
|
|
|
|
2008-05-18 23:39:41 -04:00
|
|
|
(define compnum?
|
|
|
|
(lambda (x) (sys:compnum? x)))
|
|
|
|
|
2008-05-24 13:13:01 -04:00
|
|
|
(define cflonum?
|
|
|
|
(lambda (x) (sys:cflonum? x)))
|
|
|
|
|
2007-05-05 03:24:33 -04:00
|
|
|
(define number?
|
|
|
|
(lambda (x)
|
|
|
|
(or (sys:fixnum? x)
|
|
|
|
(sys:bignum? x)
|
2007-05-20 23:23:54 -04:00
|
|
|
(sys:flonum? x)
|
2008-05-18 23:39:41 -04:00
|
|
|
(sys:ratnum? x)
|
2008-05-24 13:13:01 -04:00
|
|
|
(sys:compnum? x)
|
|
|
|
(sys:cflonum? x))))
|
2007-05-05 03:24:33 -04:00
|
|
|
|
|
|
|
(define complex?
|
|
|
|
(lambda (x) (number? x)))
|
|
|
|
|
|
|
|
(define real?
|
2008-05-18 23:39:41 -04:00
|
|
|
(lambda (x)
|
|
|
|
(or (sys:fixnum? x)
|
|
|
|
(sys:bignum? x)
|
|
|
|
(sys:flonum? x)
|
|
|
|
(sys:ratnum? x))))
|
2007-09-12 03:10:54 -04:00
|
|
|
|
|
|
|
(define real-valued?
|
2008-07-25 20:46:34 -04:00
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(real? x) #t]
|
|
|
|
[(cflonum? x) (fl=? ($cflonum-imag x) 0.0)]
|
|
|
|
[else #f])))
|
2007-09-12 03:10:54 -04:00
|
|
|
|
2007-05-05 03:24:33 -04:00
|
|
|
(define rational?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:fixnum? x) #t]
|
|
|
|
[(sys:bignum? x) #t]
|
2007-05-20 23:23:54 -04:00
|
|
|
[(sys:ratnum? x) #t]
|
2007-06-13 02:03:30 -04:00
|
|
|
[(sys:flonum? x) ($flonum-rational? x)]
|
2007-09-12 03:10:54 -04:00
|
|
|
[else #f])))
|
|
|
|
|
|
|
|
(define rational-valued?
|
2008-07-25 20:46:34 -04:00
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(rational? x) #t]
|
|
|
|
[(cflonum? x)
|
|
|
|
(and (fl=? ($cflonum-imag x) 0.0)
|
|
|
|
($flonum-rational? ($cflonum-real x)))]
|
|
|
|
[else #f])))
|
2007-05-05 03:24:33 -04:00
|
|
|
|
|
|
|
(define integer?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:fixnum? x) #t]
|
|
|
|
[(sys:bignum? x) #t]
|
2007-05-20 23:23:54 -04:00
|
|
|
[(sys:ratnum? x) #f]
|
2007-06-13 02:03:30 -04:00
|
|
|
[(sys:flonum? x) ($flonum-integer? x)]
|
2007-05-05 03:24:33 -04:00
|
|
|
[else #f])))
|
|
|
|
|
2007-09-12 03:10:54 -04:00
|
|
|
(define integer-valued?
|
2008-07-25 20:46:34 -04:00
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(integer? x) #t]
|
|
|
|
[(cflonum? x)
|
|
|
|
(and (fl=? ($cflonum-imag x) 0.0)
|
|
|
|
($flonum-integer? ($cflonum-real x)))]
|
|
|
|
[else #f])))
|
|
|
|
|
2007-09-12 03:10:54 -04:00
|
|
|
|
2007-05-05 03:24:33 -04:00
|
|
|
(define exact?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:fixnum? x) #t]
|
|
|
|
[(sys:bignum? x) #t]
|
2007-06-13 02:03:30 -04:00
|
|
|
[(sys:ratnum? x) #t]
|
2007-05-05 03:24:33 -04:00
|
|
|
[(sys:flonum? x) #f]
|
2008-05-18 23:39:41 -04:00
|
|
|
[(sys:compnum? x) #t]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(sys:cflonum? x) #f]
|
2007-05-05 03:24:33 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'exact? "not a number" x)])))
|
2007-05-05 03:27:53 -04:00
|
|
|
|
2007-09-03 00:34:53 -04:00
|
|
|
|
|
|
|
(define inexact?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:flonum? x) #t]
|
|
|
|
[(sys:fixnum? x) #f]
|
|
|
|
[(sys:bignum? x) #f]
|
|
|
|
[(sys:ratnum? x) #f]
|
2008-05-18 23:39:41 -04:00
|
|
|
[(sys:compnum? x) #f]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(sys:cflonum? x) #t]
|
2007-09-03 00:34:53 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'inexact? "not a number" x)])))
|
2007-09-03 00:34:53 -04:00
|
|
|
|
2007-09-12 02:44:19 -04:00
|
|
|
(define finite?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:flonum? x) (flfinite? x)]
|
|
|
|
[(sys:fixnum? x) #t]
|
|
|
|
[(sys:bignum? x) #t]
|
|
|
|
[(sys:ratnum? x) #t]
|
2008-05-18 23:39:41 -04:00
|
|
|
[(sys:compnum? x) #t]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(sys:cflonum? x)
|
|
|
|
(and
|
|
|
|
(flfinite? ($cflonum-real x))
|
|
|
|
(flfinite? ($cflonum-imag x)))]
|
2007-09-12 02:44:19 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'finite? "not a number" x)])))
|
2007-09-12 02:44:19 -04:00
|
|
|
|
|
|
|
(define infinite?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:flonum? x) (flinfinite? x)]
|
|
|
|
[(sys:fixnum? x) #f]
|
|
|
|
[(sys:bignum? x) #f]
|
|
|
|
[(sys:ratnum? x) #f]
|
2008-05-18 23:39:41 -04:00
|
|
|
[(sys:compnum? x) #f]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(sys:cflonum? x)
|
|
|
|
(or
|
|
|
|
(flinfinite? ($cflonum-real x))
|
|
|
|
(flinfinite? ($cflonum-imag x)))]
|
2007-09-12 02:44:19 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'infinite? "not a number" x)])))
|
2007-09-12 02:44:19 -04:00
|
|
|
|
|
|
|
(define nan?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(sys:flonum? x) (flnan? x)]
|
|
|
|
[(sys:fixnum? x) #f]
|
|
|
|
[(sys:bignum? x) #f]
|
|
|
|
[(sys:ratnum? x) #f]
|
2008-05-18 23:39:41 -04:00
|
|
|
[(sys:compnum? x) #f]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(sys:cflonum? x)
|
|
|
|
(or
|
|
|
|
(nan? ($cflonum-real x))
|
|
|
|
(nan? ($cflonum-imag x)))]
|
2007-09-12 02:44:19 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'nan? "not a number" x)])))
|
2007-09-12 02:44:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-05 03:34:59 -04:00
|
|
|
(define eof-object? (lambda (x) (sys:eof-object? x)))
|
2007-05-05 06:15:25 -04:00
|
|
|
(define bwp-object? (lambda (x) (sys:bwp-object? x)))
|
2007-12-06 05:05:26 -05:00
|
|
|
(define transcoder? (lambda (x) (sys:transcoder? x)))
|
2007-05-05 03:34:59 -04:00
|
|
|
(define immediate? (lambda (x) (sys:immediate? x)))
|
|
|
|
(define boolean? (lambda (x) (sys:boolean? x)))
|
|
|
|
(define char? (lambda (x) (sys:char? x)))
|
|
|
|
(define vector? (lambda (x) (sys:vector? x)))
|
2007-05-15 12:42:52 -04:00
|
|
|
(define bytevector? (lambda (x) (sys:bytevector? x)))
|
2007-05-05 03:34:59 -04:00
|
|
|
(define string? (lambda (x) (sys:string? x)))
|
|
|
|
(define procedure? (lambda (x) (sys:procedure? x)))
|
|
|
|
(define null? (lambda (x) (sys:null? x)))
|
|
|
|
(define pair? (lambda (x) (sys:pair? x)))
|
|
|
|
(define symbol? (lambda (x) (sys:symbol? x)))
|
2007-05-06 22:48:10 -04:00
|
|
|
(define code? (lambda (x) (sys:code? x)))
|
2007-05-05 06:18:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
(define weak-pair?
|
|
|
|
(lambda (x)
|
|
|
|
(and (pair? x)
|
|
|
|
(foreign-call "ikrt_is_weak_pair" x))))
|
|
|
|
|
2007-05-05 04:28:40 -04:00
|
|
|
(define not (lambda (x) (if x #f #t)))
|
2007-05-05 05:09:15 -04:00
|
|
|
|
|
|
|
(define eq? (lambda (x y) (sys:eq? x y)))
|
|
|
|
|
2007-05-05 05:10:38 -04:00
|
|
|
(define eqv?
|
|
|
|
(lambda (x y)
|
2008-05-24 13:13:01 -04:00
|
|
|
(import (ikarus))
|
|
|
|
(cond
|
|
|
|
[(eq? x y) #t]
|
2008-10-18 13:08:14 -04:00
|
|
|
[(flonum? x)
|
|
|
|
(and (flonum? y)
|
|
|
|
(if ($fl= x 0.0)
|
|
|
|
(and ($fl= y 0.0)
|
|
|
|
($fl= ($fl/ 1.0 x)
|
|
|
|
($fl/ 1.0 y)))
|
|
|
|
(fl=? x y)))]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(bignum? x) (and (bignum? y) (= x y))]
|
|
|
|
[(ratnum? x) (and (ratnum? y) (= x y))]
|
|
|
|
[(compnum? x)
|
|
|
|
(and (compnum? y)
|
|
|
|
(= (real-part x) (real-part y))
|
|
|
|
(= (imag-part x) (imag-part y)))]
|
|
|
|
[(cflonum? x)
|
|
|
|
(and (cflonum? y)
|
|
|
|
(= (real-part x) (real-part y))
|
|
|
|
(= (imag-part x) (imag-part y)))]
|
|
|
|
[else #f])))
|
2007-05-05 05:35:09 -04:00
|
|
|
|
2008-07-23 10:55:32 -04:00
|
|
|
|
|
|
|
(define-syntax define-pred
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ name pred? msg)
|
|
|
|
(begin
|
|
|
|
(define (err x) (die 'name msg x))
|
|
|
|
(define (g rest)
|
|
|
|
(if (sys:pair? rest)
|
|
|
|
(let ([a (car rest)])
|
|
|
|
(if (pred? a)
|
|
|
|
(g (cdr rest))
|
|
|
|
(err a)))
|
|
|
|
#f))
|
|
|
|
(define (f x rest)
|
|
|
|
(if (sys:pair? rest)
|
|
|
|
(let ([a (car rest)])
|
|
|
|
(if (sys:eq? x a)
|
|
|
|
(f x (cdr rest))
|
|
|
|
(if (pred? a)
|
|
|
|
(g (cdr rest))
|
|
|
|
(err a))))
|
|
|
|
#t))
|
|
|
|
(define name
|
|
|
|
(case-lambda
|
|
|
|
[(x y)
|
|
|
|
(if (pred? x)
|
|
|
|
(if (sys:eq? x y)
|
|
|
|
#t
|
|
|
|
(if (pred? y)
|
|
|
|
#f
|
|
|
|
(err y)))
|
|
|
|
(err x))]
|
|
|
|
[(x y z . rest)
|
|
|
|
(if (pred? x)
|
|
|
|
(if (sys:eq? x y)
|
|
|
|
(if (sys:eq? x z)
|
|
|
|
(f x rest)
|
|
|
|
(if (pred? z) #f (err z)))
|
|
|
|
(if (pred? y) #f (err y)))
|
|
|
|
(err x))])))]))
|
|
|
|
(define-pred symbol=? sys:symbol? "not a symbol")
|
|
|
|
(define-pred boolean=? sys:boolean? "not a boolean")
|
2007-05-05 05:35:09 -04:00
|
|
|
|
|
|
|
|
2007-05-05 17:44:24 -04:00
|
|
|
|
2007-05-05 03:27:53 -04:00
|
|
|
)
|