Made some type annotations optional in the for: macros.

This commit is contained in:
Vincent St-Amour 2010-09-15 19:22:21 -04:00
parent 8b60085a17
commit 9f87b5a7e6
4 changed files with 37 additions and 10 deletions

View File

@ -0,0 +1,16 @@
#lang typed/racket
;; test for optional annotation on for:-bound variables
(for: ([i (in-range 10)] ; no annotation
[j : Integer (in-range 10 20)])
(display (+ i j)))
(for/fold: : Integer ([acc 0])
([i (in-range 10)])
(+ i acc))
(let ((x '(1 3 5 7 9)))
(do: : Number ((x x (cdr x))
(sum 0 (+ sum (car x))))
((null? x) sum)))

View File

@ -14,6 +14,17 @@
#:with ty (syntax-property #'name 'type-label)
#:with ann-name #'name))
(define-splicing-syntax-class optionally-annotated-name
#:attributes (name ann-name)
#:description "optionally type-annotated identifier"
#:literals (:)
(pattern n:annotated-name
#:with name #'n.name
#:with ann-name #'n.ann-name)
(pattern n:id
#:with name #'n
#:with ann-name #'n))
(define-splicing-syntax-class (param-annotated-name trans)
#:attributes (name ty ann-name)
#:description "type-annotated identifier"

View File

@ -9,13 +9,13 @@
(define-splicing-syntax-class for-clause
;; single-valued seq-expr
(pattern (~and c (var:annotated-name seq-expr:expr))
(pattern (~and c (var:optionally-annotated-name seq-expr:expr))
#:with (expand ...) (list (syntax/loc
#'c
(var.ann-name seq-expr))))
;; multi-valued seq-expr
;; currently disabled because it triggers an internal error in the typechecker
#;(pattern (~and c (((v:annotated-name) ...) seq-expr:expr))
#;(pattern (~and c (((v:optionally-annotated-name) ...) seq-expr:expr))
#:with (expand ...) (list (syntax/loc
#'c
((v.ann-name ...) seq-expr))))
@ -26,14 +26,14 @@
;; intersperses "#:when #t" clauses to emulate the for* variants' semantics
(define-splicing-syntax-class for*-clause
;; single-valued seq-expr
(pattern (~and c (var:annotated-name seq-expr:expr))
(pattern (~and c (var:optionally-annotated-name seq-expr:expr))
#:with (expand ...) (list (syntax/loc
#'c
(var.ann-name seq-expr))
#'#:when #'#t))
;; multi-valued seq-expr
;; currently disabled because it triggers an internal error in the typechecker
#;(pattern (~and c (((v:annotated-name) ...) seq-expr:expr))
#;(pattern (~and c (((v:optionally-annotated-name) ...) seq-expr:expr))
#:with (expand ...) (list (quasisyntax/loc
#'c
((v.ann-name ...) seq-expr))

View File

@ -268,7 +268,7 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-syntax (define-typed-struct/exec stx)
(syntax-parse stx #:literals (:)
[(_ nm ((~describe "field specification" [fld:annotated-name]) ...) [proc : proc-ty])
[(_ nm ((~describe "field specification" [fld:optionally-annotated-name]) ...) [proc : proc-ty])
(with-syntax*
([proc* (syntax-property #'(ann proc : proc-ty) 'typechecker:with-type #t)]
[d-s (syntax-property (syntax/loc stx (define-struct nm (fld.name ...)
@ -415,7 +415,7 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-syntax (do: stx)
(syntax-parse stx #:literals (:)
[(_ : ty
((var:annotated-name rest ...) ...)
((var:optionally-annotated-name rest ...) ...)
(stop?:expr ret ...)
c:expr ...)
(syntax/loc
@ -452,11 +452,11 @@ This file defines two sorts of primitives. All of them are provided into any mod
;; single-valued seq-expr
;; unlike the definitions in for-clauses.rkt, this does not include
;; #:when clauses, which are handled separately here
(pattern (var:annotated-name seq-expr:expr)
(pattern (var:optionally-annotated-name seq-expr:expr)
#:with expand #'(var.ann-name seq-expr))
;; multi-valued seq-expr
;; currently disabled because it triggers an internal error in the typechecker
#;(pattern ((v:annotated-name ...) seq-expr:expr)
#;(pattern ((v:optionally-annotated-name ...) seq-expr:expr)
#:with expand #'((v.ann-name ...) seq-expr)))
(syntax-parse clauses
[(head:for-clause next:for-clause ... #:when rest ...)
@ -532,7 +532,7 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-syntax (for/lists: stx)
(syntax-parse stx #:literals (:)
[(_ : ty
((var:annotated-name) ...)
((var:optionally-annotated-name) ...)
(clause:for-clause ...)
c:expr ...)
(syntax-property
@ -545,7 +545,7 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-syntax (for/fold: stx)
(syntax-parse stx #:literals (:)
[(_ : ty
((var:annotated-name init:expr) ...)
((var:optionally-annotated-name init:expr) ...)
(clause:for-clause ...)
c:expr ...)
(syntax-property