Skip to content

[Feature Request] Add unsafe-cast #1445

@NoahStoryM

Description

@NoahStoryM

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:

(let ()
  (define-type T (→ Any Void))
  (let loop ([p : (∪ (→ T) T) (λ () (λ (v) (displayln v)))]
             [tag : Natural 0])
    (case/eq tag
      [(0) (loop (p) 1)] ; p is (→ T) here
      [(1) (p 123)])))   ; p is known to be T here

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:

#lang typed/racket/base

(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-asserts
  (let ()
    (define-syntax-class unsafe-asserts-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:unsafe-asserts-clause ...) body ...+)
         (quasisyntax/loc stx
           (cond c.cond-clause
                 ...
                 [else body ...]))]))))

(require racket/case)
(let ()
  (define-type T (→ Any Void))
  (let loop ([p : (∪ (→ T) T) (λ () (λ (v) (displayln v)))]
             [tag : Natural 0])
    (case/eq tag
      [(0) (unsafe-asserts ([p (! T)]) (loop (p) 1))]
      [(1) (unsafe-asserts ([p T]) (p 123))])))

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 in typed/racket/unsafe/assert.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy