Skip to content

Commit

Permalink
correcting the behavior of implicit lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Yoo committed Mar 26, 2012
1 parent cb49367 commit 97bf43a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
5 changes: 0 additions & 5 deletions lang/reader.rkt
@@ -1,7 +1,2 @@
#lang s-exp syntax/module-reader
(planet dyoo/arctangent/language)



;; During compilation, tell the system that we're allowing set! from outside a module.
(compile-enforce-module-constants #f)
57 changes: 39 additions & 18 deletions language.rkt
Expand Up @@ -13,9 +13,6 @@
"runtime.rkt")


(define-syntax-parameter arc-lambda-placeholder
(lambda (stx)
(raise-syntax-error #f "placeholder is being used outside of a function template" stx)))



Expand All @@ -37,7 +34,9 @@
#t])))



;; looks-like-composition?: identifier-syntax -> boolean
;;
;; Returns true if the identifier looks like function composition.
(define-for-syntax (looks-like-composition? id)
(let ([name (symbol->string (syntax-e id))])
(let ([pieces (regexp-split #rx":" name)])
Expand Down Expand Up @@ -108,7 +107,11 @@
(cond
[(eq? #f (identifier-binding expanded-lhs))
(quasisyntax/loc stx
(begin (define #,expanded-lhs rhs)
;; Note: we create a definition and then set! it so
;; that we convince Racket that it shouldn't be
;; treated as a constant.
(begin (define #,expanded-lhs #f)
(set! #,expanded-lhs rhs)
#,expanded-lhs))]
[else
(quasisyntax/loc stx
Expand Down Expand Up @@ -185,8 +188,11 @@
name))]
[else
(syntax/loc stx
(begin (define name (fn args
body ...))
;; Note: we create a definition and then set! it so
;; that we convince Racket that it shouldn't be
;; treated as a constant.
(begin (define name #f)
(set! name (fn args body ...))
name))])]))

(define-syntax (fn stx)
Expand Down Expand Up @@ -332,38 +338,53 @@




(define-for-syntax (contains-lambda-placeholder? los)
(ormap (lambda (stx) (and (identifier? stx)
(free-identifier=? #'arc-lambda-placeholder stx)))
los))
;; Returns true if the syntax looks like it has square brackets.
(define-for-syntax (square-bracketed? stx)
(eq? (syntax-property stx 'paren-shape) #\[))


;; application sees if the expression is an implicit lambda
(define-syntax (arc-app stx)
(syntax-case stx ()
[(_ operator+operands ...)
[(_ operator operands ...)
(cond
[(contains-lambda-placeholder? (syntax->list #'(operator+operands ...)))
[(square-bracketed? stx)
(with-syntax ([(id) (generate-temporaries #'(_))])
(syntax/loc stx
(fn (id)
(syntax-parameterize ([arc-lambda-placeholder (make-rename-transformer #'id)])
(#%app operator+operands ...)))))]
(#%app operator operands ...)))))]
[else
(syntax/loc stx
(#%app operator+operands ...))])]
(#%app operator operands ...))])]
[(_ . operator+operands)
(syntax/loc stx
(#%app . operator+operands))]
(#%app . operator+operands))]
[(_)
(identifier? stx)
(syntax/loc stx
#%app)]))




(define-syntax-parameter arc-lambda-placeholder
(lambda (stx)
(syntax-case stx ()
;; Edge case: if the placeholder itself is used like [_], then we really do want it to go
;; through the #%app macro.
[(elts ...)
(square-bracketed? stx)
(syntax/loc stx
[arc-app elts ...])]

;; Otherwise, highlight the placeholder symbol itself in the error message.
[(placeholder-symbol elts ...)
(raise-syntax-error #f "placeholder is being used outside of a function template" #'placeholder-symbol)]

[else
(raise-syntax-error #f "placeholder is being used outside of a function template" stx)])))





Expand Down
24 changes: 24 additions & 0 deletions manual.scrbl
@@ -0,0 +1,24 @@
#lang scribble/manual

@title{Arctangent: a glance at language hacking}
@author+email["Danny Yoo" "dyoo@hashcollision.org"]


@section{Introduction}



@section{Starting up}

@subsection{Setting up a PLaneT link}

@subsection{}

@subsection{The macro system}




@section{Structure properties}

@subsection{Strings as functions}
10 changes: 8 additions & 2 deletions test-arctangent.rkt
Expand Up @@ -282,12 +282,18 @@

---

;; Implicit lambdas, by using the underscore notation.
((+ _ 10) 3) ==> 13
;; Implicit lambdas, by using the bracket notation.
([+ _ 10] 3) ==> 13

(map [+ _ 10] '(1 2 3)) ==> (arc-cons 11 (arc-cons 12 (arc-cons 13 nil)))


---

(= my-app-zero [_ 0])
(my-app-zero (fn (x) x)) ==> 0


)))


Expand Down

0 comments on commit 97bf43a

Please sign in to comment.