From 4b2f78477a2f6144044fc2645750f07be923d85d Mon Sep 17 00:00:00 2001 From: Robby Findler Date: Sun, 19 Feb 2012 08:04:16 -0600 Subject: [PATCH] added nand, nor, and implies to racket/bool --- collects/mzlib/etc.rkt | 8 ---- collects/mzlib/scribblings/etc.scrbl | 4 +- collects/racket/bool.rkt | 20 +++++++++- collects/scribblings/reference/booleans.scrbl | 37 +++++++++++++++++++ collects/tests/racket/bool.rkt | 19 ++++++++++ 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/collects/mzlib/etc.rkt b/collects/mzlib/etc.rkt index 7314700272..da40ab976b 100644 --- a/collects/mzlib/etc.rkt +++ b/collects/mzlib/etc.rkt @@ -162,14 +162,6 @@ [(t a ...) (raise-syntax-error #f "invalid clause" stx s)])) (syntax->list (syntax (something ...))))])) -(define-syntax (nor stx) - (syntax-case stx () - [(_ expr ...) (syntax/loc stx (not (or expr ...)))])) - -(define-syntax (nand stx) - (syntax-case stx () - [(_ expr ...) (syntax/loc stx (not (and expr ...)))])) - (define-syntax (let+ stx) (syntax-case stx () [(_ [clause ...] body1 body ...) diff --git a/collects/mzlib/scribblings/etc.scrbl b/collects/mzlib/scribblings/etc.scrbl index c80f9afd1e..3acd10710a 100644 --- a/collects/mzlib/scribblings/etc.scrbl +++ b/collects/mzlib/scribblings/etc.scrbl @@ -22,7 +22,7 @@ @mzlib[#:mode title etc] -The @racketmodname[mzlib/etc] library re-exports the following +The @racketmodname[mzlib/etc] library re-exports the following from @racketmodname[scheme/base] and other libraries: @racketblock[ @@ -35,6 +35,8 @@ The @racketmodname[mzlib/etc] library re-exports the following compose local symbol=? + nand + nor ] @defform[(begin-lifted expr ...+)] diff --git a/collects/racket/bool.rkt b/collects/racket/bool.rkt index 029c9d209e..6158eb4f7d 100644 --- a/collects/racket/bool.rkt +++ b/collects/racket/bool.rkt @@ -1,8 +1,10 @@ -#lang scheme/base +#lang racket/base (provide true false false? boolean=? - symbol=?) + symbol=? + implies nand nor) +(require (for-syntax racket/base)) (define true #t) (define false #f) @@ -19,3 +21,17 @@ (raise-type-error 'symbol=? "symbol" (if (symbol? x) 1 0) x y)) (eq? x y)) +(define-syntax (implies stx) + (syntax-case stx () + [(implies x y) + (syntax/loc stx (if x y #t))] + [(implies x y z w ...) + (syntax/loc stx (if x (implies y z w ...) #t))])) + +(define-syntax (nor stx) + (syntax-case stx () + [(_ expr ...) (syntax/loc stx (not (or expr ...)))])) + +(define-syntax (nand stx) + (syntax-case stx () + [(_ expr ...) (syntax/loc stx (not (and expr ...)))])) diff --git a/collects/scribblings/reference/booleans.scrbl b/collects/scribblings/reference/booleans.scrbl index 08bdc5953d..7d8c78c647 100644 --- a/collects/scribblings/reference/booleans.scrbl +++ b/collects/scribblings/reference/booleans.scrbl @@ -1,6 +1,9 @@ #lang scribble/doc @(require "mz.rkt") +@(define bool-eval (make-base-eval)) +@(bool-eval '(require racket/bool)) + @title[#:tag "booleans"]{Booleans and Equality} True and false @deftech{booleans} are represented by the values @@ -242,3 +245,37 @@ Returns @racket[(equal? a b)] (if @racket[a] and @racket[b] are booleans).} Returns @racket[(not v)].} +@defform[(nand expr ...)]{ + Same as @racket[(not (and expr ...))]. + + @examples[#:eval + bool-eval + (nand #f #t) + (nand #f (error 'ack "we don't get here"))] +} + +@defform[(nor expr ...)]{ + Same as @racket[(not (or expr ...))]. + + @examples[#:eval + bool-eval + (nor #f #t) + (nor #t (error 'ack "we don't get here"))] + + +} + +@defform[(implies expr expr expr ...)]{ + Checks to be sure that the first + expression implies the second, implies + the third, etc. + + @examples[#:eval + bool-eval + (implies #f #t) + (implies #f #f) + (implies #f #f #t) + (implies #t #f) + (implies #f (error 'ack "we don't get here"))] + +} \ No newline at end of file diff --git a/collects/tests/racket/bool.rkt b/collects/tests/racket/bool.rkt index 47970d127e..33c5d6fc74 100644 --- a/collects/tests/racket/bool.rkt +++ b/collects/tests/racket/bool.rkt @@ -18,3 +18,22 @@ (check-false (false? #t)) (check-false (false? "11")) +(for ([x (in-list '(#f #t))]) + (for ([y (in-list '(#f #t))]) + (for ([z (in-list '(#f #t))]) + (check-equal? (implies x y z) + (or (not x) (or (not y) z)))))) +(check-equal? (implies #f (car 'x)) #t) + + +(check-equal? (nand #f #f) #t) +(check-equal? (nand #f #t) #t) +(check-equal? (nand #t #f) #t) +(check-equal? (nand #t #t) #f) +(check-equal? (nand #f (car 'x)) #t) + +(check-equal? (nor #f #f) #t) +(check-equal? (nor #t #f) #f) +(check-equal? (nor #f #t) #f) +(check-equal? (nor #t #t) #f) +(check-equal? (nor #t (car 'x)) #f)