@@ -27,7 +27,8 @@ Let's start out with defining the grammar and binding rules for basic typed expr
2727 x:typed-var
2828 n:number
2929
30- (#%lambda ([x:typed-var (~datum :) t:type] ... ) body:typed-expr)
30+ (#%lambda ([x:typed-var (~datum :) t:type] ... )
31+ body:typed-expr)
3132 #:binding (scope (bind x) ... body)
3233 (#%app fun:typed-expr arg:typed-expr ... )
3334
@@ -111,20 +112,25 @@ Now let's define @racket[infer-expr-type]:
111112 (define f-type (infer-expr-type #'f ))
112113 (match f-type
113114 [(function-type expected-arg-types return-type)
114- (unless (= (length expected-arg-types) (length (attribute arg)))
115- (raise-syntax-error 'infer-expr-type
116- (format "arity error. expected ~a arguments, but got ~a "
117- (length expected-arg-types)
118- (length (attribute arg)))
119- this-syntax))
115+ (unless (= (length expected-arg-types)
116+ (length (attribute arg)))
117+ (raise-syntax-error
118+ 'infer-expr-type
119+ (format
120+ "arity error. expected ~a arguments, but got ~a "
121+ (length expected-arg-types)
122+ (length (attribute arg)))
123+ this-syntax))
120124 (for ([expected-type expected-arg-types]
121125 [arg (attribute arg)])
122126 (check-expr-type arg expected-type))
123127 return-type]
124- [_ (raise-syntax-error 'infer-expr-type
125- (format "type mismatch. expected a function type, but got ~a "
126- (type->datum f-type))
127- #'f )])]
128+ [_ (raise-syntax-error
129+ 'infer-expr-type
130+ (format
131+ "type mismatch. expected a function type, but got ~a "
132+ (type->datum f-type))
133+ #'f )])]
128134 [((~datum :) e t-stx)
129135 (define t (parse-type #'t-stx ))
130136 (check-expr-type #'e t)
@@ -136,19 +142,24 @@ Now let's define @racket[infer-expr-type]:
136142 (infer-expr-type #'body )]))
137143
138144 (define (get-identifier-type x)
139- (symbol-table-ref types x (lambda () (raise-syntax-error #f "untyped identifier " x))))
145+ (symbol-table-ref
146+ types x
147+ (lambda () (raise-syntax-error #f "untyped identifier " x))))
140148
141149 (define (extend-type-environment! x t)
142- (void (symbol-table-ref types x (lambda () (symbol-table-set! types x t)))))
150+ (void (symbol-table-ref
151+ types x
152+ (lambda () (symbol-table-set! types x t)))))
143153
144154 (define (check-expr-type e expected-type)
145155 (define actual-type (infer-expr-type e))
146156 (unless (equal? expected-type actual-type)
147- (raise-syntax-error 'infer-expr-type
148- (format "type mismatch. expected ~a, but got ~a "
149- (type->datum expected-type)
150- (type->datum actual-type))
151- e)))
157+ (raise-syntax-error
158+ 'infer-expr-type
159+ (format "type mismatch. expected ~a, but got ~a "
160+ (type->datum expected-type)
161+ (type->datum actual-type))
162+ e)))
152163
153164 (define (parse-type t-stx)
154165 (syntax-parse t-stx
@@ -208,7 +219,8 @@ Finally, we can write macros for @racket[let] and @racket[lambda]:
208219
209220(define-stlc-syntax lambda
210221 (syntax-parser
211- [(_ ([x (~datum :) t] ... ) body) #'(#%lambda ([x : t] ... ) body)]))
222+ [(_ ([x (~datum :) t] ... ) body)
223+ #'(#%lambda ([x : t] ... ) body)]))
212224
213225(define-stlc-syntax let*
214226 (syntax-parser
@@ -307,8 +319,10 @@ Let's do it!
307319 (match t
308320 [(number-type) #'number? ]
309321 [(function-type arg-types return-type)
310- (define/syntax-parse (arg-type-stx ... ) (map type->contract-stx arg-types))
311- (define/syntax-parse return-type-stx (type->contract-stx return-type))
322+ (define/syntax-parse (arg-type-stx ... )
323+ (map type->contract-stx arg-types))
324+ (define/syntax-parse return-type-stx
325+ (type->contract-stx return-type))
312326 #'(-> arg-type-stx ... return-type-stx)])))
313327
314328(define-syntax compile-expr
@@ -346,7 +360,8 @@ Let's run some example programs now:
346360 1 ))
347361(eval:error
348362 (stlc/expr
349- (let ([app (lambda ([f : (-> Number Number)] [x : Number]) (f x))])
363+ (let ([app (lambda ([f : (-> Number Number)] [x : Number])
364+ (f x))])
350365 (rkt (app "not a function " 1 ) : Number))))
351366]
352367
@@ -462,7 +477,8 @@ Now let's update the rest of our code:
462477(define-syntax compile-defn-or-expr/top
463478 (syntax-parser
464479 [(_ ((~datum #%define) x:id _ body))
465- #`(define x (compile-expr/top body #,(get-identifier-type #'x ) #t ))]
480+ #`(define x (compile-expr/top
481+ body #,(get-identifier-type #'x ) #t ))]
466482 [(_ ((~datum begin ) body ...+))
467483 #'(begin (compile-defn-or-expr/top body) ... )]
468484 [(_ e)
@@ -501,7 +517,8 @@ Now let's update the rest of our code:
501517 (syntax-parser
502518 [(_ x:id (~datum :) t e)
503519 #'(#%define x t e)]
504- [(_ (f:id [arg:id (~datum :) arg-type] ... ) (~datum -> ) return-type body ... )
520+ [(_ (f:id [arg:id (~datum :) arg-type] ... )
521+ (~datum -> ) return-type body ... )
505522 #'(#%define f (-> arg-type ... return-type)
506523 (lambda ([arg : arg-type] ... )
507524 body
0 commit comments