most recent semester
hw08
Prolog, shadowing, and function-application
Due Dec.01 (Mon) in class23:59.
It is intended that you can finish Friday before the break,
but I will have it due afterwards.
(Note that the last homework will be available before the break,
should you want to work on that.)
Submit all files on D2L, plus hardcopy of any parts you added for P3/P4.
We continue to build on
the language implementation started in hw06.
You can implement this homework in either Java or Racket.
Please indicate in your submitted file, what sections of code are udpated
and what is unchanged from hw07/hw07-soln.
You don't need to turn in any hardcopy of unchanged-code
(but submit a fully-working copy in the drop-box).
- (1pt)
Add another person's drink preferences to the
Prolog drink-preference knowledge base from lecture.
Make sure that at least two different people like pepsi.
- (5pts)
We say that
chaperone(A,B,C)
(“A and B can be chaperoned by C”)
iff
A,B,C are three different people who all have some drink preference in common,
and A and B are opposite genders.
-
define chaperone(A,B,C)
-
What query will match all the people who
could be a chaperone for alice
and bob?
-
What query will match all pairs of people who
could be chaperoned by dee?
P3 is just like P2, except we now
allow one variable to shadow another.
For example, we might have a fornow x be … in … stopit
which in turn contains another fornow x be … in … stopit
inside of it.
In that case the inner x should shadow the outer one:
fornow x be 3 in fornow x be 5 in (:x +++ 3:) stopit stopit ⇒
fornow x be 5 in (:x +++ 3:) stopit1
⇒
(:5 +++ 3:) ⇒
8.
And of course,
shadowing may occur between non-adjacent scopes:
fornow x be 3 in <fornow y be 4 in <fornow x be 5 in … stopit>stopit>stopit.
Thus, when substituting,
only substitute “free occurrences” of an Id
in E1,
not any “bound occurrences”2.
(7pts)
Fill in the following blanks:
-
fornow y be 3 in fornow x be 5 in (:x +++ y:) stopit stopit
⇒
⇒
⇒
8
-
fornow y be 3 in fornow x be y in (:x +++ y:) stopit stopit
⇒
⇒
⇒
6
-
fornow x be 5 in fornow y be 3 in (:fornow x be y in (:x +++ y:)
stopit +++ x:)stopit stopit
⇒
⇒
⇒
⇒
⇒
11
Re-indent
the following two P3 expressions
so that each fornow statement has its three parts equally-indented, on different lines:
fornow Id be
Expr in
Expr stopit
|
Your answer should be 2n+1 lines long, where n is the number of fornow
expressions you have.
Q:
In each case, what does the expression evaluate to?
-
fornow x be 5 in <fornow x be (:x +++ 1:) in (:x +++ 2:) stopit> stopit
-
fornow y be fornow z be 4 in <fornow y be 99 in z stopit>stopit in <fornow z be
5 in (:<fornow z be 10 in y stopit> +++ (:y +++ z:):) stopit> stopit
Put your fill-in-blanks in comments next to your test-cases
(with the last two indented as requested).
Also, turn all five of the above into runnable test-cases:
not just for eval (where the last blank is the expected result),
but also for subst (where the first blank is the expected result for a
corresponding call to subst).
You might find it helpful to try to explain (in English) to a friend,
exactly when you do and don't substitute.
-
(5pts)
Update P2 to P3,
by the necessary changes to to enable shadowing.
(You are encouraged to build on your own previous solution,
but you can also use the
P2 solution (../../../../Teaching/ITEC380/2015fall-ibarland/Homeworks/Project/P2.rkt, ../../../../Teaching/ITEC380/2015fall-ibarland/Homeworks/Project/expr-test-P2.rkt; email me ASAP for Java solution).
The change should be quite small, but is surgically precise.
P4 adds (non-recursive) functions and function-application to our language:
Expr ::= … | FuncExpr | FuncApplyExpr
FuncExpr ::= (Id) -> {Expr}
FuncApplyExpr ::= [Expr @ Expr] |
Here is an example of a function;
it happens to compute a number’s absolute value:
(x) -> { whenp x then x otherwise (:-1 *** x:) ! }
|
Just like numbers are self-evaluating,
so are FuncExprs.
If evaluating (an internal representation of) a
FuncExpr,
just return that same (internal representation of the) function.
We won't actually evaluate the body until
the function is applied.
(This is exactly how racket, python, javascript, etc. treat lambda values.)
A FuncApplyExpr represents calling a function.
Here are two expressions, both computing the absolute value of -5:
[ (x) -> {whenp x then x otherwise (:-1 *** x:)!} @ -5]
fornow abs be (x) -> {whenp x then x otherwise (:-1 *** x:)!}
in [abs @ -5] stopit
|
In FuncApplyExpr,
the first Expr had better evaluate to a function.
(That is, it might be a FuncExpr,
or an Id which gets substitued to a function value.
It could also be (say) an WhenZExpr or LetExpr
which evaluates to a function.)
-
First, write the following four functions as P4 programs.
(You can then modify them as part of your tests.)
-
A constant function that always returns (say) 17.
-
The absolute value function given above,
-
the function sqr, which squares its input,
-
the factorial function, written in P4.
Note:
You won't be able to evaluate function-applications
for recursive functions yet (see P5),
but we can still write the test cases!
(You can comment out that one test case for now,
since it'll trigger a run-time exception otherwise.)
and
-
The P4 equivalent of the following racket definition make-adder:
(define (make-adder n)
(lambda (m) (+ n m)))
; Two examples of applying this function:
;
(make-adder 3) ; evals to (lambda (m) (+ 3 m))
((make-adder 3) 4) ; evals to 7
|
Then, upgrade P3 so that it implements functions:
- (2pts) Add a struct/class for representing FuncExprs internally.
- (2pts) expr->string (and tests)
- (6pts) parse/string->expr (and tests).
hint:If you see ( while parsing a P4 program,
what types of expression might you have?
How do you differentiate between them?
You might have a cond branch for (,
and then inside of that you will need a nested if/cond.
- (2pts) eval (and tests)
-
Implement function-application.
- (2pts) Add a struct/class for representing FuncApplyExprs internally.
- (2pts) parse/string->expr (and tests)
- (2pts) expr->string (and tests)
- (9pts) eval (and tests).
Here, more than half the points are for tests,
since you want to try several situations involving shadowing variables.
The semantics of eval'ing the function-application
[Expr0 @ Expr1)]:
-
Evaluate Expr0; let’s call the result f.
(f had better be a function-value!)
-
Evaluate Expr1; let’s call the result arg.
-
Substitute f’s parameter with arg in f’s body;
call this new expression E′.
-
Evaluate E′ and return that value.
Hey, those semantics are practically the same as LetExprs’!
Indeed, it's not very different; the function holds the identifier and body;
when you eval a function-application then we do the same substitution.
Observe that our programs can now evaluate to either of two types:
numbers or functions.
In Java,
we'll need a class which can represent either,
as the return type for our eval.
That’s why the abstract class Value was included,
of which Number was one subclass.
Make test cases for
parse
(at least one, for each of functions and function-applictions).
Then, make test cases for
toString and eval:
Write each of the following functions in P4,
and then call the function for your test case:
Note that we're restricting P4
to only deal with unary functions (functions of one argument).
To think about:
If we wanted to 'fake' functions of 2 arguments in N4,
how could we do it?
For example, you might
think about how to write a function that effectively takes in
two numbers i,j and returns 2·i+j.
Think about how make-adder does this.
The interpreter project is based on the first chapters of
Programming Languages and Interpretation,
by Shriram Krishnamurthi.
As a result, this homework assignment is covered by the
Creative Commons
Attribution-NonCommercial-ShareAlike 3.0 United States License.
Although we're using a different dialect of racket than that book,
you might find it helpful to skim it.
1
The notation
“fornow x be 5 in (:x plus 3:) stopit
⇒ 5 plus 3 ⇒ 8”
is shorthand for
eval(parse("fornow x be 5 in (:x plus 3:) stopit"))
= eval(parse("(:5 plus 3:)"))
= eval(parse("8"))
|
Observe how we definitely don't write
“"fornow x be 5 in (:x plus 3:) stopit" = "(:5 plus 3:)" = 8”
since the two strings are not .equals(·) to each other,
and strings are never ints.
More specifically:
we distinguish between “⇒”
(“code evaluates to”)
and
“=” (“equals”,
just as “=” has meant since kindergarten).
↩2
nor any “binding occurrences”:
The first x in
fornow x be 5 in (:x +++ 3:) stopit
is a binding occurrence,
and the second x is a bound occurrence.
(We say that “a variable is bound inside the scope of its
binding occurrence”.)
↩
most recent semester