- Feature Name: Wraith
- Start Date: 2020-10-18
- RFC PR: racket/racket2-rfcs#0000
- Authors: Alex Knauth and Christine Lemmer-Webber, based on ideas from Arne Babenhauserheide
Wraith is a syntax proposal that eliminates parentheses but has a clean translation to s-expression syntax.
To try out the current implementation, use the Racket package manager to install git://github.com/cwebber/racket2-rfcs?path=wraith#wraith and use the lang line #lang wraith racket.
Many users are overwhelmed by parenthetical syntax and would like an alternative. Proposals such as Honu, Shrubbery are desirable in the sense of providing an alternative syntax that may be available to such users, but involve syntax transformations that involve significantly larger machinery. Meanwhile, s-expression syntax is well understood, studied, supported in the Racket community. This proposal expands on ideas from Wisp (examples) but with some refinements.
Consider the following Racket program, taken from the Racket quick introduction:
define (add-drawing p)
define drawer
make-pict-drawer p
new canvas%
parent f
style '(border)
paint-callback
lambda (self dc)
drawer dc 0 0This corresponds to the following Racket code:
(define (add-drawing p)
(define drawer
(make-pict-drawer p))
(new canvas%
[parent f]
[style '(border)]
[paint-callback
(lambda (self dc)
(drawer dc 0 0))]))The intuition here that a newline with indentation corresponds to a nested expression:
define drawer | (define drawer
make-pict-drawer p | (make-pict-drawer p))To properly correspond to intuition, a line with only one argument is assumed to not be an application, whereas a line with multiple arguments is:
displayln | (displayln
string-append "hello " | (string-append "hello "
name | name
"!" | "!"))displayln | (displayln
string-append "hello " | (string-append "hello "
string-upcase name | (string-upcase name)
"!" | "!"))But how to handle arguments that aren't composing a new expression, but
are on the same line?
For this, use .. double-dot at the start of the line:
list 1 2 3 | (list 1 2 3
.. 4 5 6 | 4 5 6)overlay/offset | (overlay/offset
rectangle 100 10 "solid" "blue" | (rectangle 100 10 "solid" "blue")
.. 10 10 | 10 10
rectangle 10 100 "solid" "red" | (rectangle 10 100 "solid" "red"))You can also use an & ampersand to separate expressions
that would otherwise be grouped by line or indentation:
overlay/offset | (overlay/offset
rectangle 100 10 "solid" "blue" | (rectangle 100 10 "solid" "blue")
10 & 10 | 10 10
rectangle 10 100 "solid" "red" | (rectangle 10 100 "solid" "red"))Keywords are implicitly considered to be continuing arguments in the previous expression:
standard-cat 100 90 | (standard-cat 100 90
#:happy? #t | #:happy? #t)In general, indentation level does not matter super strongly; as long as "greater" than the previous, it is "nested into the parent expression":
standard-cat | (standard-cat
.. 100 90 | 100 90
#:happy? #t | #:happy? #t)Parentheses can be used to build a new nested expression. For instance:
define drawer (make-pict-drawer p) | (define drawer (make-pict-drawer p))define (display-excitement str) | (define (display-excitement str)
format "I'm SO EXCITED about ~a!!!" | (format "I'm SO EXCITED about ~a!!!"
string-upcase str | (string-upcase str)))However, for the most part, lines within a parenthetical expression still follow expresions still generally follow Wraith's rules (note that this is a major departure from Sweet Expressions and Wisp!):
define (greeter name) | (define (greeter name)
let ((to-say | (let ((to-say
format "Hey there ~a! :D" | (format "Hey there ~a! :D"
name)) | name)))
displayln to-say | (displayln to-say)))However, that let looks very parenthetical still. If we're deviating from s-expressions, is there a way to make it less nested?
In traditional Racket S-Expression syntax, () and [] have only
been differentiated by convention.
In Wraith syntax, [] has a special meaning which can be described
as "a wrapped set of wrapped expressions".
This makes aesthetically more appealing let and for syntax examples.
define (greeter name) | (define (greeter name)
let [to-say | (let [(to-say
format "Hey there ~a! :D" | (format "Hey there ~a! :D"
name] | name))]
displayln to-say | (displayln to-say)))for [pet '("cat" "dog" "horse")] | (for [(pet '("cat" "dog" "horse"))]
printf "I love my ~a!\n" pet | (printf "I love my ~a!\n" pet))define (counting-letters-song letters) | (define (counting-letters-song letters)
for [letter letters | (for [(letter letters)
number (in-naturals 1)] | (number (in-naturals 1))]
printf "I like ~a, it's number ~a!" | (printf "I like ~a, it's number ~a!"
.. letter number | letter number)
(newline) | (newline))
displayln "Singing a letters song!" | (displayln "Singing a letters song!"))let* [animal "dog" | (let* [(animal "dog")
noise "barks" | (noise "barks")
player-hears | (player-hears
format "the ~a says: ~a!!!" | (format "the ~a says: ~a!!!"
.. animal noise] | animal noise))]
displayln player-hears | (displayln player-hears))However, in Wraith, new lines of content within parenthetical/bracketed expressions are not permitted to have less indentation than the column after the opening parenthesis. In other words, the following is an error and not permitted:
;; Not allowed!
define a-list
'(1 2
3)Finally, many potential users have grown up being taught mathematics using infix syntax. As such, a switch to prefix notation is seen as a major requirement for understandability. This is a reasonable request. However, infix notation also often introduces "order of operation problems", which confuse students and mathematicians everywhere and are generally solved by putting parentheses back around operations. Thus Wraith partially borrows from SRFI 105 for curly infix operations but only permits one infix operation per curly-grouping:
define (double x) | (define (double x)
{x * 2} | (* x 2))define (squared-minus-one x) | (define (squared-minus-one x)
{{x * x} - 1} | (- (* x x) 1)){1 + 2 + {9 / 3}} | (+ 1 2 (/ 9 3))There are 4 ways of grouping expressions together in Wraith:
- Indentation
a b_1 ... b_m c_1 ... c_n - Parentheses
or
(a b_1 ... b_m c_1 ... c_n)(a_1 ... a_n) - Square Brackets
[a_1 b_1_1 ... b_1_m c_1_1 ... c_1_n ... a_l b_l_1 ... b_l_m c_l_1 ... c_l_n] - Curly Braces
{a_1 op a_2 ... op a_n}
Indentation applies to expressions both outside and inside parens and brackets.
Within an indented expression:
a b_1 ... b_m
c_1
...
c_n
The indentation of each c expression must be:
- greater than the indentation of the
aexpression, and - less than or equal to the indentation of the first
bexpression and the previouscexpression (if it exists)
If a potential c has indentation less than that, it will not be considered part of a's expression, but part of a later expression. If a potential c has indentation greater than that, it will be associated with the b or c closest up and to the left of it.
For example, here the d is not associated with a because it does not have greater indentation than a:
a b_1 | (a b_1)
d | d
Here the d is associated with b_1 because it has greater indentation than b_1:
a b_1 | (a (b_1
d | d))
And here the d is associated with c_1 because it has greater indentation than c_1:
a b_1 | (a b_1
c_1 | (c_1
d | d))
The indentation does not group anything together if a is a #: keyword or a . dot.
Wraith does not include algebra-style function calls f(x,y), and does not include any precedence in infix notation, so {x + y * z} is not allowed.
Wraith also does not allow you to write extra parentheses anywhere around any expression without changing meaning, so (x) is different from x.
Wraith requires parentheses for zero-argument function calls, but not for one-or-more-argument calls, so add1 5 can be called without parens while (newline) still needs parens to make it a call. This may look inconsistent.