Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions typed-racket-lib/typed-racket/base-env/prims-contract.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@
(pattern (~seq (~optional (~seq (~and key (~or #:extra-constructor-name #:constructor-name))
name:id))
(~optional (~seq #:type-name type:id) #:defaults ([type struct-name])))
#:attr ctor-value (if (attribute key) #'(key name)
(if legacy
#`(#:extra-constructor-name
#,(format-id struct-name "make-~a" struct-name))
#'()))))
#:attr ctor-value (cond
[(attribute key) #'(key name)]
[legacy
#`(#:extra-constructor-name
#,(format-id struct-name "make-~a" struct-name))]
[else #'()])))

(define-syntax-class (struct-clause legacy)
#:attributes (nm type (body 1) (constructor-parts 1) (tvar 1))
Expand Down Expand Up @@ -384,11 +385,14 @@
;; will have added the casted expression's original type to the cast-table, so
;; that `(cast-table-ref id)` can get that type here.
(λ ()
(define types (cast-table-ref id))
(define type-stx
(let ([types (cast-table-ref id)])
(cond [(not types) #f]
[(null? (cdr types)) (car types)]
[else (quasisyntax/loc (car types) (U #,@types))])))
(cond
[(not types) #f]
[(null? (cdr types)) (car types)]
[else
(quasisyntax/loc (car types)
(U #,@types))]))
`#s(contract-def ,type-stx ,flat? ,maker? typed ,te-mode))))

(define define-predicate
Expand Down Expand Up @@ -653,17 +657,22 @@

(make-struct-info
(lambda ()
#,(if (syntax-e #'parent)
(let-values (((parent-type-des parent-maker parent-pred
parent-sel parent-mut grand-parent)
(apply values
(extract-struct-info/checked #'parent))))
#`(struct-info-list
(list #,@(map maybe-add-quote-syntax parent-sel))
(list #,@(map maybe-add-quote-syntax parent-mut))))
#`(let-values (((new-sels new-muts)
(id-drop orig-sels orig-muts num-fields)))
(struct-info-list new-sels new-muts)))))))
#,(cond
[(syntax-e #'parent)
(define-values (parent-type-des
parent-maker
parent-pred
parent-sel
parent-mut
grand-parent)
(apply values (extract-struct-info/checked #'parent)))
#`(struct-info-list
(list #,@(map maybe-add-quote-syntax parent-sel))
(list #,@(map maybe-add-quote-syntax parent-mut)))]
[else
#`(let-values ([(new-sels new-muts)
(id-drop orig-sels orig-muts num-fields)])
(struct-info-list new-sels new-muts))])))))

(define-syntax nm
(if id-is-ctor?
Expand Down
11 changes: 4 additions & 7 deletions typed-racket-lib/typed-racket/base-env/top-interaction.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@
(λ (type)
(match type
[(tc-result1: (and t (? Fun?)) f o)
(let ([cleaned (cleanup-type t (parse-type #'desired-type) #f)])
#`(display
#,(match cleaned
[(Fun: '())
"Desired return type not in the given function's range.\n"]
[(Fun: _)
(pretty-format-rep cleaned)])))]
(define cleaned (cleanup-type t (parse-type #'desired-type) #f))
#`(display #,(match cleaned
[(Fun: '()) "Desired return type not in the given function's range.\n"]
[(Fun: _) (pretty-format-rep cleaned)]))]
[_ (error (format "~a: not a function" (syntax->datum #'op)))]))
"must be applied to exactly two arguments"))
4 changes: 1 addition & 3 deletions typed-racket-lib/typed-racket/core.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
(with-refinements?))
(unless (eq? te-mode deep)
(raise-arguments-error
(string->symbol (format "typed/racket/~a"
(keyword->string
(syntax-e te-attr))))
(format-symbol "typed/racket/~a" te-attr)
"#:with-refinements unsupported")))])
(tc-module/full te-mode stx pmb-form
(λ (new-mod pre-before-code pre-after-code)
Expand Down
4 changes: 1 addition & 3 deletions typed-racket-lib/typed-racket/env/global-env.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@

(define (maybe-finish-register-type id)
(define v (free-id-table-ref the-mapping id))
(if (box? v)
(register-type id (unbox v))
#f))
(and (box? v) (register-type id (unbox v))))

(define (unregister-type id)
(free-id-table-remove! the-mapping id))
Expand Down
7 changes: 3 additions & 4 deletions typed-racket-lib/typed-racket/env/init-envs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,9 @@

(define (bound-in-this-module id)
(define binding (identifier-binding id))
(if (and (list? binding) (module-path-index? (car binding)))
(let-values ([(mp base) (module-path-index-split (car binding))])
(not mp))
#f))
(and (and (list? binding) (module-path-index? (car binding)))
(let-values ([(mp base) (module-path-index-split (car binding))])
(not mp))))

(define (make-init-code map f)
(define (bound-f id v)
Expand Down
24 changes: 12 additions & 12 deletions typed-racket-lib/typed-racket/env/type-env-structs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,27 @@


(define (env-replace-props e props)
(match-let ([(env tys otys _ als) e])
(env tys otys props als)))
(match-define (env tys otys _ als) e)
(env tys otys props als))

(define (env-lookup-id e key fail)
(match-let ([(env tys _ _ _) e])
(free-id-table-ref tys key (λ () (fail key)))))
(match-define (env tys _ _ _) e)
(free-id-table-ref tys key (λ () (fail key))))

(define (env-lookup-obj e key fail)
(match-let ([(env _ otys _ _) e])
(hash-ref otys key (λ () (fail key)))))
(match-define (env _ otys _ _) e)
(hash-ref otys key (λ () (fail key))))


;; like hash-set, but for the type of an ident in the lexical environment
(define (env-set-id-type e ident type)
(match-let ([(env tys otys ps als) e])
(env (free-id-table-set tys ident type) otys ps als)))
(match-define (env tys otys ps als) e)
(env (free-id-table-set tys ident type) otys ps als))

;; like hash-set, but for the type of an object in the lexical environment
(define (env-set-obj-type e obj type)
(match-let ([(env tys otys ps als) e])
(env tys (hash-set otys obj type) ps als)))
(match-define (env tys otys ps als) e)
(env tys (hash-set otys obj type) ps als))

;; extends an environment with types and aliases
;; e : the 'env' struct to be updated
Expand Down Expand Up @@ -157,6 +157,6 @@
(env tys otys ps als))

(define (env-lookup-alias e key fail)
(match-let ([(env _ _ _ als) e])
(free-id-table-ref als key (λ () (fail key)))))
(match-define (env _ _ _ als) e)
(free-id-table-ref als key (λ () (fail key))))

11 changes: 5 additions & 6 deletions typed-racket-lib/typed-racket/logic/ineq.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
[(lexp: c ts)
(define coeffs (terms-coeffs ts))
(if (zero? c) coeffs (cons c coeffs))]
[_ (error 'lexp-scalars "given invalid lexp ~a" exp)]))
[_ (raise-arguments-error 'lexp-scalars "given invalid lexp" "exp" exp)]))

(module+ test
(check-true (and (equal? (sort (lexp-vars (lexp* 17 '(42 x) '(2 z))) symbol<?)
Expand Down Expand Up @@ -439,10 +439,9 @@
;; --> (ax - bx) <= ...3 + ...4 - ...1 - ...2
[else
(define lhs* (lexp 0 (make-terms x (- x-lhs-coeff x-rhs-coeff))))
(define rhs*
(let ([rhs-c* (- rhs-c lhs-c)]
[rhs-h* (terms-subtract rhs-ts lhs-ts)])
(lexp rhs-c* (terms-remove rhs-h* x))))
(define rhs-c* (- rhs-c lhs-c))
(define rhs-h* (terms-subtract rhs-ts lhs-ts))
(define rhs* (lexp rhs-c* (terms-remove rhs-h* x)))
(leq lhs* rhs*)])]))

; x lhs
Expand Down Expand Up @@ -497,7 +496,7 @@
;; leq1: lexp1 <= bx
;; leq2: cx <= lexp2
[(and (eqv? 0 a) (eqv? 0 d)) (leq (lexp-scale l1 c) (lexp-scale r2 b))]
[else (error 'leq-join "cannot join ~a and ~a by ~a" leq1 leq2 x)])))
[else (raise-arguments-error 'leq-join "cannot join and by" "leq1" leq1 "leq2" leq2 "x" x)])))
Copy link
Contributor

Choose a reason for hiding this comment

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

Well that's not right.


(module+ test
(check-equal? (leq-join (leq (lexp* '(2 x))
Expand Down
65 changes: 32 additions & 33 deletions typed-racket-lib/typed-racket/typed-reader.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,39 @@
(define (parse port read-one src)
(skip-whitespace port)
(define name (read-one))
(begin0 (begin
(skip-whitespace port)
(let ([next (read-one)])
(case (syntax-e next)
;; type annotation
[(:)
(skip-whitespace port)
(type-label-property name (syntax->datum (read-one)))]
[(::)
(skip-whitespace port)
(datum->syntax name `(ann ,name : ,(read-one)))]
[(@)
(let ([elems (let loop ([es '()])
(skip-whitespace port)
(if (equal? #\} (peek-char port))
(reverse es)
(loop (cons (read-one) es))))])
(datum->syntax name `(inst ,name : ,@elems)))]
;; arbitrary property annotation
[(PROP)
(skip-whitespace port)
(begin0 (let ([next (read-one)])
(case (syntax-e next)
;; type annotation
[(:)
(skip-whitespace port)
(type-label-property name (syntax->datum (read-one)))]
[(::)
(skip-whitespace port)
(datum->syntax name `(ann ,name : ,(read-one)))]
[(@)
(let ([elems (let loop ([es '()])
(skip-whitespace port)
(if (equal? #\} (peek-char port))
(reverse es)
(loop (cons (read-one) es))))])
(datum->syntax name `(inst ,name : ,@elems)))]
;; arbitrary property annotation
[(PROP)
(skip-whitespace port)
(let* ([prop-name (syntax-e (read-one))])
(skip-whitespace port)
(let* ([prop-name (syntax-e (read-one))])
(skip-whitespace port)
(syntax-property name prop-name (read-one)))]
;; otherwise error
[else
(let-values ([(l c p) (port-next-location port)])
(raise-read-error (format "typed expression ~a must be followed by :, ::, or @"
(syntax->datum name))
src
l
c
p
1))])))
(syntax-property name prop-name (read-one)))]
;; otherwise error
[else
(define-values (l c p) (port-next-location port))
(raise-read-error (format "typed expression ~a must be followed by :, ::, or @"
(syntax->datum name))
src
l
c
p
1)]))
(skip-whitespace port)
(let ([c (read-char port)])
(unless (equal? #\} c)
Expand Down
3 changes: 2 additions & 1 deletion typed-racket-test/succeed/shallow/pr241-variation-5.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@
(filtered-in
(if WARN-MISSING
(lambda (str)
(when (not (known-string? str)) (printf "WARNING: Missing test for base type '~a'\n" str))
(unless (known-string? str)
(printf "WARNING: Missing test for base type '~a'\n" str))
#f)
(lambda (str) #f))
typed-racket/base-env/base-types))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(module ty-foo typed/racket/shallow
(require/typed (submod ".." foo) [prop:hi (Struct-Property (-> Self Any))] [hi-ref (-> Any (-> Any Void))])
(struct bar () #:property prop:hi (λ ([self : bar])
(display (format "instance bar\n" ))))
(display "instance bar\n")))
(hi-ref (bar))
)

Expand Down
Loading