Allow omitting type annotation in most of the for: forms.

This commit is contained in:
Vincent St-Amour 2011-06-13 16:58:03 -04:00
parent ef11f754e5
commit a5bccaffe1
3 changed files with 36 additions and 18 deletions

View File

@ -0,0 +1,9 @@
#lang typed/racket
(ann (for/list ([#{i : Number} (in-list '(1 2 3))]) i) (Listof Number))
(ann (for/list: ([i : Number (in-list '(1 2 3))]) i) (Listof Number))
(+ (for/fold: ([acc : Number 0])
([i (in-list '(1 2 3))])
(+ i acc))
1)

View File

@ -133,3 +133,11 @@
(~or rest:annotated-star-rest rest:annotated-dots-rest))) (~or rest:annotated-star-rest rest:annotated-dots-rest)))
#:with ann-formals #'(n.ann-name ... . rest.ann-name) #:with ann-formals #'(n.ann-name ... . rest.ann-name)
#:with (arg-ty ...) #'(n.ty ... . rest.formal-ty))) #:with (arg-ty ...) #'(n.ty ... . rest.formal-ty)))
(define-splicing-syntax-class standalone-annotation
#:literals (:)
(pattern (~seq : t)
#:with ty #'t))
(define-splicing-syntax-class optional-standalone-annotation
(pattern (~optional a:standalone-annotation)
#:with ty (if (attribute a) #'a.ty #f)))

View File

@ -491,6 +491,11 @@ This file defines two sorts of primitives. All of them are provided into any mod
(when guard (when guard
#,(loop #'(rest ...))))])))])) #,(loop #'(rest ...))))])))]))
(define-for-syntax (maybe-annotate-body body ty)
(if (syntax-e ty)
(syntax-property body 'type-ascription ty)
body))
;; Handling #:when clauses manually, like we do with for: above breaks ;; Handling #:when clauses manually, like we do with for: above breaks
;; the semantics of for/list and co. ;; the semantics of for/list and co.
;; We must leave it to the untyped versions of the macros. ;; We must leave it to the untyped versions of the macros.
@ -500,19 +505,17 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-for-syntax (define-for-variant name) (define-for-syntax (define-for-variant name)
(lambda (stx) (lambda (stx)
(syntax-parse stx #:literals (:) (syntax-parse stx #:literals (:)
[(_ : ty [(_ a:optional-standalone-annotation
(clause:for-clause ...) (clause:for-clause ...)
c:expr ...) c:expr ...)
(syntax-property (maybe-annotate-body
(quasisyntax/loc stx (quasisyntax/loc stx
(#,name (#,name
(clause.expand ... ...) (clause.expand ... ...)
#,@(syntax-property #,@(maybe-annotate-body
#'(c ...) #'(c ...)
'type-ascription #'a.ty)))
#'ty))) #'a.ty)])))
'type-ascription
#'ty)])))
(define-syntax (define-for-variants stx) (define-syntax (define-for-variants stx)
(syntax-parse stx (syntax-parse stx
[(_ (name untyped-name) ...) [(_ (name untyped-name) ...)
@ -537,17 +540,16 @@ This file defines two sorts of primitives. All of them are provided into any mod
;; clauses with these 2. ;; clauses with these 2.
(define-syntax (for/lists: stx) (define-syntax (for/lists: stx)
(syntax-parse stx #:literals (:) (syntax-parse stx #:literals (:)
[(_ : ty [(_ a:optional-standalone-annotation
((var:optionally-annotated-name) ...) ((var:optionally-annotated-name) ...)
(clause:for-clause ...) (clause:for-clause ...)
c:expr ...) c:expr ...)
(syntax-property (maybe-annotate-body
(quasisyntax/loc stx (quasisyntax/loc stx
(for/lists (var.ann-name ...) (for/lists (var.ann-name ...)
(clause.expand ... ...) (clause.expand ... ...)
c ...)) c ...))
'type-ascription #'a.ty)]))
#'ty)]))
(define-syntax (for/fold: stx) (define-syntax (for/fold: stx)
(syntax-parse stx #:literals (:) (syntax-parse stx #:literals (:)
[(_ : ty [(_ : ty
@ -591,15 +593,14 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-for-syntax (define-for*-variant name) (define-for-syntax (define-for*-variant name)
(lambda (stx) (lambda (stx)
(syntax-parse stx #:literals (:) (syntax-parse stx #:literals (:)
[(_ : ty [(_ a:optional-standalone-annotation
(clause:for-clause ...) (clause:for-clause ...)
c:expr ...) c:expr ...)
(syntax-property (maybe-annotate-body
(quasisyntax/loc stx (quasisyntax/loc stx
(#,name (clause.expand ... ...) (#,name (clause.expand ... ...)
c ...)) c ...))
'type-ascription #'a.ty)])))
#'ty)])))
(define-syntax (define-for*-variants stx) (define-syntax (define-for*-variants stx)
(syntax-parse stx (syntax-parse stx
[(_ (name no-*-name) ...) [(_ (name no-*-name) ...)