Skip to content

Commit a49b792

Browse files
committed
be more explicit about subtraction in the PE challenge. Also, change the handling of subtraction in PE for Lint
1 parent 14084c7 commit a49b792

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

book.tex

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
\def\racketEd{0}
2525
\def\pythonEd{1}
26-
\def\edition{0}
26+
\def\edition{1}
2727

2828
% material that is specific to the Racket edition of the book
2929
\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
@@ -1666,11 +1666,11 @@ \section{Example Compiler: A Partial Evaluator}
16661666
evaluator for the \LangInt{} language. The output of the partial evaluator
16671667
is a program in \LangInt{}. In figure~\ref{fig:pe-arith}, the structural
16681668
recursion over $\Exp$ is captured in the \code{pe\_exp} function,
1669-
whereas the code for partially evaluating the negation and addition
1670-
operations is factored into three auxiliary functions:
1671-
\code{pe\_neg}, \code{pe\_add} and \code{pe\_sub}. The input to these
1669+
whereas the code for partially evaluating the negation, addition,
1670+
and subtraction operations is factored into two auxiliary functions:
1671+
\code{pe\_neg} and \code{pe\_add}. The input to these
16721672
functions is the output of partially evaluating the children.
1673-
The \code{pe\_neg}, \code{pe\_add} and \code{pe\_sub} functions check whether their
1673+
The \code{pe\_neg} and \code{pe\_add} functions check whether their
16741674
arguments are integers and if they are, perform the appropriate
16751675
arithmetic. Otherwise, they create an AST node for the arithmetic
16761676
operation.
@@ -1689,18 +1689,13 @@ \section{Example Compiler: A Partial Evaluator}
16891689
[((Int n1) (Int n2)) (Int (fx+ n1 n2))]
16901690
[(_ _) (Prim '+ (list r1 r2))]))
16911691

1692-
(define (pe_sub r1 r2)
1693-
(match* (r1 r2)
1694-
[((Int n1) (Int n2)) (Int (fx- n1 n2))]
1695-
[(_ _) (Prim '- (list r1 r2))]))
1696-
16971692
(define (pe_exp e)
16981693
(match e
16991694
[(Int n) (Int n)]
17001695
[(Prim 'read '()) (Prim 'read '())]
17011696
[(Prim '- (list e1)) (pe_neg (pe_exp e1))]
17021697
[(Prim '+ (list e1 e2)) (pe_add (pe_exp e1) (pe_exp e2))]
1703-
[(Prim '- (list e1 e2)) (pe_sub (pe_exp e1) (pe_exp e2))]))
1698+
[(Prim '- (list e1 e2)) (pe_add (pe_exp e1) (pe_neg (pe_exp e2)))]))
17041699

17051700
(define (pe_Lint p)
17061701
(match p
@@ -1723,19 +1718,12 @@ \section{Example Compiler: A Partial Evaluator}
17231718
case _:
17241719
return BinOp(r1, Add(), r2)
17251720

1726-
def pe_sub(r1, r2):
1727-
match (r1, r2):
1728-
case (Constant(n1), Constant(n2)):
1729-
return Constant(sub64(n1, n2))
1730-
case _:
1731-
return BinOp(r1, Sub(), r2)
1732-
17331721
def pe_exp(e):
17341722
match e:
17351723
case BinOp(left, Add(), right):
17361724
return pe_add(pe_exp(left), pe_exp(right))
17371725
case BinOp(left, Sub(), right):
1738-
return pe_sub(pe_exp(left), pe_exp(right))
1726+
return pe_add(pe_exp(left), pe_neg(right))
17391727
case UnaryOp(USub(), v):
17401728
return pe_neg(pe_exp(v))
17411729
case Constant(value):
@@ -4204,11 +4192,11 @@ \section{Challenge: Partial Evaluator for \LangVar{}}
42044192
%
42054193
To accomplish this, the \code{pe\_exp} function should produce output
42064194
in the form of the $\itm{residual}$ nonterminal of the following
4207-
grammar. The idea is that when processing an addition expression, we
4208-
can always produce one of the following: (1) an integer constant, (2)
4209-
an addition expression with an integer constant on the left-hand side
4210-
but not the right-hand side, or (3) an addition expression in which
4211-
neither subexpression is a constant.
4195+
grammar. The idea is that when processing an addition or subtraction
4196+
expression, we can always produce one of the following: (1) an integer
4197+
constant, (2) an addition expression with an integer constant on the
4198+
left-hand side but not the right-hand side, or (3) an addition
4199+
expression in which neither subexpression is a constant.
42124200
%
42134201
{\if\edition\racketEd
42144202
\[

0 commit comments

Comments
 (0)