Make `(rnrs base)' independent of other rnrs modules.
[bpt/guile.git] / module / rnrs / base.scm
CommitLineData
bf745816
JG
1;;; base.scm --- The R6RS base library
2
7112615f 3;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
bf745816
JG
4;;
5;; This library is free software; you can redistribute it and/or
6;; modify it under the terms of the GNU Lesser General Public
7;; License as published by the Free Software Foundation; either
8;; version 3 of the License, or (at your option) any later version.
9;;
10;; This library is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;; Lesser General Public License for more details.
14;;
15;; You should have received a copy of the GNU Lesser General Public
16;; License along with this library; if not, write to the Free Software
17;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18\f
19
20(library (rnrs base (6))
21 (export boolean? symbol? char? vector? null? pair? number? string? procedure?
22
23 define define-syntax syntax-rules lambda let let* let-values
935c7aca 24 let*-values letrec letrec* begin
bf745816
JG
25
26 quote lambda if set! cond case
27
28 or and not
29
30 eqv? equal? eq?
31
32 + - * / max min abs numerator denominator gcd lcm floor ceiling
33 truncate round rationalize real-part imag-part make-rectangular angle
34 div mod div-and-mod div0 mod0 div0-and-mod0
35
36 expt exact-integer-sqrt sqrt exp log sin cos tan asin acos atan
37 make-polar magnitude angle
38
39 complex? real? rational? integer? exact? inexact? real-valued?
b98d5a5a 40 rational-valued? integer-valued? zero? positive? negative? odd? even?
bf745816
JG
41 nan? finite? infinite?
42
43 exact inexact = < > <= >=
44
45 number->string string->number
46
b98d5a5a
JG
47 boolean=?
48
bf745816
JG
49 cons car cdr caar cadr cdar cddr caaar caadr cadar cdaar caddr cdadr
50 cddar cdddr caaaar caaadr caadar cadaar cdaaar cddaar cdadar cdaadr
51 cadadr caaddr caddar cadddr cdaddr cddadr cdddar cddddr
52
53 list? list length append reverse list-tail list-ref map for-each
54
b3961e7a 55 symbol->string string->symbol symbol=?
bf745816
JG
56
57 char->integer integer->char char=? char<? char>? char<=? char>=?
58
59 make-string string string-length string-ref string=? string<? string>?
60 string<=? string>=? substring string-append string->list list->string
61 string-for-each string-copy
62
63 vector? make-vector vector vector-length vector-ref vector-set!
64 vector->list list->vector vector-fill! vector-map vector-for-each
65
66 error assertion-violation assert
67
68 call-with-current-continuation call/cc call-with-values dynamic-wind
69 values apply
70
71 quasiquote unquote unquote-splicing
72
73 let-syntax letrec-syntax
74
75 syntax-rules identifier-syntax)
c0f6c163 76 (import (rename (except (guile) error raise)
ff62c168
MW
77 (euclidean-quotient div)
78 (euclidean-remainder mod)
79 (euclidean/ div-and-mod)
80 (centered-quotient div0)
81 (centered-remainder mod0)
82 (centered/ div0-and-mod0)
7112615f 83 (inf? infinite?)
b98d5a5a
JG
84 (exact->inexact inexact)
85 (inexact->exact exact))
86 (srfi srfi-11))
87
88 (define (boolean=? . bools)
89 (define (boolean=?-internal lst last)
90 (or (null? lst)
91 (let ((bool (car lst)))
92 (and (eqv? bool last) (boolean=?-internal (cdr lst) bool)))))
93 (or (null? bools)
94 (let ((bool (car bools)))
95 (and (boolean? bool) (boolean=?-internal (cdr bools) bool)))))
96
97 (define (symbol=? . syms)
98 (define (symbol=?-internal lst last)
99 (or (null? lst)
100 (let ((sym (car lst)))
101 (and (eq? sym last) (symbol=?-internal (cdr lst) sym)))))
102 (or (null? syms)
103 (let ((sym (car syms)))
104 (and (symbol? sym) (symbol=?-internal (cdr syms) sym)))))
105
b98d5a5a
JG
106 (define (exact-integer-sqrt x)
107 (let* ((s (exact (floor (sqrt x)))) (e (- x (* s s)))) (values s e)))
108
8f2339c4
MW
109 (define (real-valued? x)
110 (and (complex? x)
111 (zero? (imag-part x))))
112
113 (define (rational-valued? x)
114 (and (real-valued? x)
115 (rational? (real-part x))))
116
117 (define (integer-valued? x)
118 (and (rational-valued? x)
119 (= x (floor (real-part x)))))
b01818d7 120
b24b7deb
JG
121 (define (vector-for-each proc . vecs)
122 (apply for-each (cons proc (map vector->list vecs))))
123 (define (vector-map proc . vecs)
124 (list->vector (apply map (cons proc (map vector->list vecs)))))
125
630b6588
LC
126 (define-syntax define-proxy
127 (syntax-rules (@)
128 ;; Define BINDING to point to (@ MODULE ORIGINAL). This hack is to
129 ;; make sure MODULE is loaded lazily, at run-time, when BINDING is
130 ;; encountered, rather than being loaded while compiling and
131 ;; loading (rnrs base).
132 ;; This avoids circular dependencies among modules and makes
133 ;; (rnrs base) more lightweight.
134 ((_ binding (@ module original))
135 (define-syntax binding
136 (identifier-syntax
137 (module-ref (resolve-interface 'module) 'original))))))
138
139 (define-proxy raise
140 (@ (rnrs exceptions) raise))
141
142 (define-proxy condition
23988e8c 143 (@ (rnrs conditions) condition))
630b6588 144 (define-proxy make-error
c0f6c163 145 (@ (rnrs conditions) make-error))
630b6588 146 (define-proxy make-assertion-violation
23988e8c 147 (@ (rnrs conditions) make-assertion-violation))
630b6588 148 (define-proxy make-who-condition
23988e8c 149 (@ (rnrs conditions) make-who-condition))
630b6588 150 (define-proxy make-message-condition
23988e8c 151 (@ (rnrs conditions) make-message-condition))
630b6588 152 (define-proxy make-irritants-condition
23988e8c 153 (@ (rnrs conditions) make-irritants-condition))
c0f6c163
AR
154
155 (define (error who message . irritants)
156 (raise (apply condition
157 (append (list (make-error))
158 (if who (list (make-who-condition who)) '())
159 (list (make-message-condition message)
160 (make-irritants-condition irritants))))))
23988e8c
AR
161
162 (define (assertion-violation who message . irritants)
c0f6c163
AR
163 (raise (apply condition
164 (append (list (make-assertion-violation))
165 (if who (list (make-who-condition who)) '())
166 (list (make-message-condition message)
167 (make-irritants-condition irritants))))))
168
169 (define-syntax assert
170 (syntax-rules ()
171 ((_ expression)
172 (if (not expression)
173 (raise (condition
174 (make-assertion-violation)
175 (make-message-condition
176 (format #f "assertion failed: ~s" 'expression))))))))
23988e8c 177
b01818d7 178)