Skip to content

Commit 2f624ea

Browse files
committed
Add more Scheme type definitions
1 parent f548137 commit 2f624ea

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

equality.lisp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
;;;; -*- mode: common-lisp; -*-
22

33
;;;; Non-string equality testing
4+
;;;;
5+
;;;; The core three equivalents predicates are described in r7rs.pdf
6+
;;;; section 6.1.
47

58
(in-package #:airship-scheme)
69

scheme-types.lisp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
;;;; Define type definitions
66

7-
;;; TODO: The define-scheme-predicate could go here, too
8-
(defmacro define-scheme-type ((name &rest lambda-list) &body body)
7+
;;;; TODO: The define-scheme-predicate could go here, too
8+
(defmacro %define-scheme-type ((name &rest lambda-list) predicate &body body)
99
(let ((docstring (if (and (stringp (car body)) (cdr body))
1010
(list (car body))
1111
nil)))
@@ -14,7 +14,17 @@
1414
,@body)
1515
(define-function (,name :inline t) (object)
1616
,@docstring
17-
(and (typep object ',name) t)))))
17+
(and ,predicate t)))))
18+
19+
;;; For types with no built-in predicate
20+
(defmacro define-scheme-type ((name &rest lambda-list) &body body)
21+
`(%define-scheme-type (,name ,@lambda-list) (typep object ',name)
22+
,@body))
23+
24+
;;; For CL types that use a predicate instead of typep
25+
(defmacro define-scheme-type* ((name &rest lambda-list) predicate &body body)
26+
`(%define-scheme-type (,name ,@lambda-list) (,predicate object)
27+
,@body))
1828

1929
;;;; Type definitions
2030

@@ -49,6 +59,10 @@ known as #t or #f
4959
"A Scheme string is just a simple string."
5060
'simple-string)
5161

62+
(define-scheme-type (char?)
63+
"A Scheme char is just a character."
64+
'character)
65+
5266
(define-scheme-type (bytevector?)
5367
"A Scheme bytevector is just an octet vector"
5468
`(simple-array octet (*)))
@@ -57,6 +71,25 @@ known as #t or #f
5771
"Tests if an object is a Scheme symbol"
5872
`(and symbol (not null) (not boolean?)))
5973

74+
(define-scheme-type* (list?) a:proper-list-p
75+
"Scheme's list? tests for a proper list"
76+
`a:proper-list)
77+
78+
(define-scheme-type* (%list?) listp
79+
"
80+
A lower-level, faster list test that permits improper lists, which
81+
don't end in NIL.
82+
"
83+
`list)
84+
85+
(define-scheme-type* (pair?) consp
86+
"A pair? in Scheme is a cons cell."
87+
`cons)
88+
89+
(define-scheme-type* (null?) null
90+
"A null? in Scheme is nil."
91+
`null)
92+
6093
;;;; Type Conversion
6194

6295
(define-function (inexact :inline t) ((z number))

standard-procedures.lisp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
;;;; todo: add errors when errors are required
2525

2626
;;;; 6.1 - Equivalence predicates
27-
;;;;
28-
;;;; Note: These have to be implemented last to make sure that they
29-
;;;; follow Scheme equivalence rules... except for eq? which is
30-
;;;; sufficiently under-specified that being synonymous with EQ is not
31-
;;;; an issue.
3227

3328
(define-scheme-predicate (eqv? obj1 obj2)
3429
(eqv? obj1 obj2))
@@ -291,7 +286,7 @@
291286
;;; Basic cons pair procedures
292287

293288
(define-scheme-predicate (pair? obj)
294-
(consp obj))
289+
(pair? obj))
295290

296291
(define-scheme-procedure (cons obj1 obj2)
297292
(cons obj1 obj2))
@@ -341,10 +336,10 @@
341336
;;; List procedures
342337

343338
(define-scheme-predicate (null? obj)
344-
(null obj))
339+
(null? obj))
345340

346341
(define-scheme-predicate (list? obj)
347-
(a:proper-list-p obj))
342+
(list? obj))
348343

349344
(define-scheme-procedure (make-list k &optional (fill nil))
350345
(make-list k :initial-element fill))
@@ -412,7 +407,7 @@
412407
;;;; 6.6 Characters
413408

414409
(define-scheme-predicate (char? obj)
415-
(typep obj 'character))
410+
(char? obj))
416411

417412
(define-scheme-predicate (char=? char . more-chars)
418413
(apply #'char= char more-chars))

0 commit comments

Comments
 (0)