Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for negated predicates in assert and with-asserts. #1324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NoahStoryM
Copy link
Contributor

This PR adds support for negated predicates in assert and with-asserts in Typed Racket. With this change, programmers can use (not/p pred) to assert that a value does not satisfy the given predicate pred.

And before this change, if a value failed an assertion in assert or with-asserts, the error message did not show which assertion failed.

Here are some examples:

Before:

Welcome to Racket v8.8 [cs].
> (assert 123 string?)
Assertion #<procedure:string?> failed on 123 [,bt for context]
> (define x 123)
> (with-asserts ([x string?]) x)
Assertion failed [,bt for context]

After:

Welcome to Racket v8.8 [cs].
> (assert 123 string?)
assert: Assertion (assert 123 string?) failed
  expected: 'string?
  given: 123
 [,bt for context]
> (assert 123 (not/p string?))
- : Integer [more precisely: Positive-Byte]
123
> (define x 123)
> (with-asserts ([x string?]) x)
with-asserts: Assertion (assert x string?) failed [,bt for context]
> (with-asserts ([x (not/p string?)]) x)
- : Integer [more precisely: Positive-Byte]
123

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 2 files in this pull request and has added suggestions.

Comment on lines 734 to 739
(define pred-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx])))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define-lambda-to-define: The define form supports a shorthand for defining functions (including function-returning functions).

Suggested change
(define pred-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx])))
(define (pred-parser stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx]))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("    (define (pred-parser stx)"
       "      (syntax-parse stx"
       "        #:datum-literals (not/p)"
       "        [(not/p (not/p p)) (pred-parser #'p)]"
       "        [_ stx]))")
  #:original-lines
    '#("    (define pred-parser"
       "      (λ (stx)"
       "        (syntax-parse stx"
       "          #:datum-literals (not/p)"
       "          [(not/p (not/p p)) (pred-parser #'p)]"
       "          [_ stx])))")
  #:start-line 734)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:do-make-syntax-introducer>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/snapshot/pkgs/resyntax/default-recommendations/function-definition-shortcuts.rkt:87:3 (define (pred-parser (ORIGINAL-SPLICE stx)) (ORIGINAL-GAP (stx) (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser (syntax p))) (_ stx))) (ORIGINAL-SPLICE (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser...>
  #:original-syntax
    #<syntax:typed-racket-lib/typed-racket/base-env/prims.rkt:734:4 (define pred-parser (λ (stx) (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser (syntax p))) (_ stx))))>)

Comment on lines 740 to 745
(define assert-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))])))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define-lambda-to-define: The define form supports a shorthand for defining functions (including function-returning functions).

Suggested change
(define assert-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))])))
(define (assert-parser stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))]))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("    (define (assert-parser stx)"
       "      (syntax-parse stx"
       "        #:datum-literals (not/p)"
       "        [[x (not/p p)] #'(p x)]"
       "        [[x p] #'(not (p x))]))")
  #:original-lines
    '#("    (define assert-parser"
       "      (λ (stx)"
       "        (syntax-parse stx"
       "          #:datum-literals (not/p)"
       "          [[x (not/p p)] #'(p x)]"
       "          [[x p] #'(not (p x))])))")
  #:start-line 740)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:do-make-syntax-introducer>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/snapshot/pkgs/resyntax/default-recommendations/function-definition-shortcuts.rkt:87:3 (define (assert-parser (ORIGINAL-SPLICE stx)) (ORIGINAL-GAP (stx) (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax (p x))) ((x p) (syntax (not (p x)))))) (ORIGINAL-SPLICE (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax...>
  #:original-syntax
    #<syntax:typed-racket-lib/typed-racket/base-env/prims.rkt:740:4 (define assert-parser (λ (stx) (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax (p x))) ((x p) (syntax (not (p x)))))))>)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 2 files in this pull request and found no issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant