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

Patched require/untyped-contract to accept a language spec. #1330

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

Conversation

Rscho314
Copy link

Hi @bennn ,

This is a very modest proposal for #1286. I did not patch the docs yet, but I will do so if there is interest in this pull request. It seems to work with the following test code (changed language to typed/racket/no-check):

server.rkt

#lang typed/racket/no-check ;shallow ;optional ;no-check

(provide mynum check-array-shape)

(define mynum 42)

(define-type (MVec T) (Mutable-Vectorof T))

(define-type Indexes (MVec Index))
(define-type In-Indexes (U (MVec Integer) Indexes))

(: check-array-shape (In-Indexes (-> Nothing) -> Indexes))
(define (check-array-shape ds fail)
  (define dims (vector-length ds))
  (define: new-ds : Indexes (make-vector dims 0))
  (let loop ([#{i : Nonnegative-Fixnum} 0])
    (cond [(i . < . dims)
           (define di (vector-ref ds i))
           (cond [(index? di)  (vector-set! new-ds i di)
                               (loop (+ i 1))]
                 [else  (fail)])]
          [else  new-ds])))

main.rkt

#lang racket/base

(require typed/untyped-utils
         typed/racket/no-check
         (except-in "server.rkt"
                    check-array-shape))

(require/untyped-contract
 "server.rkt"
 'typed/racket/no-check
 [check-array-shape  #;((Mutable-Vectorof Integer) (-> Nothing) -> (Mutable-Vectorof Index))
                     ((Vectorof Integer) (-> Nothing) -> (Vectorof Index))])

(check-array-shape (vector 1 2 3) (lambda () (error 'die)))

Cheers!

@bennn
Copy link
Contributor

bennn commented Jun 15, 2023

Hi Raol, sorry I am so slow. Just want you to know that I know this PR exists and will take a look soon!

@bennn
Copy link
Contributor

bennn commented Jun 10, 2024

EDIT: nevermind, the problem was that I used 'typed/racket/no-check as the language spec. We should improve the message.

Tried running this on the example, and I'm getting an error about an (only-in quote):

only-in: identifier `quote' not included in nested require spec
  at: "server.rkt"
  in: (only-in "server.rkt" (quote quote3) (check-array-shape check-array-shape4))

'require)]
[*typed/racket (datum->syntax #'from-module-spec (syntax-e #'language-spec))]
[*require (datum->syntax #'from-module-spec 'require)]
[*language-spec (datum->syntax #'racket/base (syntax-e #'language-spec))]
Copy link
Contributor

@bennn bennn Jun 10, 2024

Choose a reason for hiding this comment

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

This was typed/racket before. I think it needs to be the language-spec.
EDIT: oh, I misremembered the argument order. This could just be #'here.

Copy link
Author

@Rscho314 Rscho314 Jun 11, 2024

Choose a reason for hiding this comment

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

@bennn Thanks for the feedback. I've changed the datum->syntax context to #'_. I'm not really up to good coding style conventions for Racket, so I hope I've done the right thing.

EDIT:

As for the error about an (only-in quote), I agree the message could be improved. From a cursory glance at the source, I think the most feasible way of doing it is to wrap require/untyped-contract in a with-handlers form and providing a custom message. Letting the exception bubble up to handle it more generally immediately brings us to core racket collections that I'd rather not touch. Thoughts ?

Is there anything else I can do to improve this PR ?

Copy link
Contributor

Choose a reason for hiding this comment

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

how about changing the syntax from typed/racket/shallow to #:lang typed/racket/shallow --- from :id to keyword +:expr ? that should improve the error and make this easier to use later.

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 1 file in this pull request and found no issues.

@@ -29,46 +29,52 @@
(stx-map (lambda (id) ((make-syntax-introducer) id)) ids))

(define-syntax (require/untyped-contract stx)
(syntax-parse stx #:literals (begin)
[(_ (begin form ...) from-module-spec:expr [name:id T:expr] ...)
Copy link
Contributor

Choose a reason for hiding this comment

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

keep the indentation like it was

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.

2 participants