- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 102
[Feature Request] Add unsafe-cast
#1445
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
Comments
What exactly is different between this and |
The distinction between I'm trying to implement tagged unions in typed racket: (struct (a b) variant ([value : a] [tag : b])
#:transparent
#:type-name Variant)
(define-syntax tag-case
(let ()
(define-syntax-class tag-case-clause
[pattern [(tag ...+) (~datum :) prop body ...+]]
[pattern [(tag ...+) body ...+] #:with prop #'(∪ tag ...)])
(λ (stx)
(syntax-parse stx
#:datum-literals (else)
[(_ exp name:id c:tag-case-clause ... [else body ...+])
(syntax/loc stx
(let* ([var : (Variant Any Sexp) exp]
[name (variant-value var)])
(case (variant-tag var)
[(c.tag ...)
(unsafe-asserts ([name c.prop]) c.body ...)]
...
[else body ...])))]
[(_ exp name:id c:tag-case-clause ...)
(syntax/loc stx
(tag-case exp name c ... [else (error "Fail")]))])))) Here are some examples using > (tag-case (variant 1234 'Integer) n
[(Integer) (displayln (list n (- n)))]
[else (displayln n)])
(1234 -1234)
> (tag-case (variant 'abc 0) s
[(0) : Symbol (displayln s)]
[else (displayln s)])
abc |
I think this is running together a few different issues.
For 1, we already have a solution. For 2, I think that would be a useful addition. For 3, it's possible but I'm not convinced that it belongs in the For 4, I am opposed -- unsafe code is sometimes necessary but we shouldn't need to be writing convenience macros for doing lots of it. For your actual use case, I think writing |
Thank you for the clarification! You're absolutely right - I overlooked that Regarding (module untyped-utils racket/base
(provide (rename-out [const unsafe-assert]
[const unsafe-assert-not]))
(define (const . _) #f))
(require typed/racket/unsafe)
(unsafe-require/typed 'untyped-utils
[unsafe-assert (∀ (a) (pred a))]
[unsafe-assert-not (∀ (a) (→ Any Boolean : #:+ (! a) #:- a))])
(require (for-syntax racket/base syntax/parse))
(define-syntax (unsafe-cast stx)
(syntax-parse stx
#:datum-literals (!)
[(_ e (! (! t)))
(syntax/loc stx
(unsafe-cast e t))]
[(_ e (! t))
(syntax/loc stx
(let ([v e])
(if ((inst unsafe-assert t) v)
(error "Assertion failed")
v)))]
[(_ e t)
(syntax/loc stx
(let ([v e])
(if ((inst unsafe-assert-not t) v)
(error "Assertion failed")
v)))]))
(define-syntax unsafe-with-casts
(let ()
(define-syntax-class casts-clause
[pattern [x:id ((~datum !) t)]
#:with cond-clause
(syntax/loc #'x
[((inst unsafe-assert t) x)
(error "Assertion failed")])]
[pattern [x:id t]
#:with cond-clause
(syntax/loc #'x
[((inst unsafe-assert-not t) x)
(error "Assertion failed")])])
(λ (stx)
(syntax-parse stx
[(_ (c:casts-clause ...) body ...+)
(syntax/loc stx
(cond c.cond-clause
...
[else body ...]))])))) Would this be an appropriate approach? If so, I'd be happy to submit a PR to provide them in |
unsafe-cast
Implementing |
Is your feature request related to a problem? Please describe.
Typed Racket uses predicates to refine types, but this approach has limitations since many types cannot have corresponding predicates. This creates situations where we can logically deduce more precise types for variables via tagged union, but have no way to express them in the type system.
Example:
Describe the solution you'd like
I propose adding operators that allow programmers to explicitly assert that a variable is or isn't of a certain type. This would solve the cases where predicates are insufficient.
Example:
Describe alternatives you've considered
Creating a separate package if this doesn't align with Typed Racket's design goals.
Do you want to contribute to this feature?
Yes, I am willing to implement this feature and submit a PR to provide
unsafe-asserts
intyped/racket/unsafe/assert
.The text was updated successfully, but these errors were encountered: