Академический Документы
Профессиональный Документы
Культура Документы
2012)
Prof.aa Dr. J. Giesl Notes: M. Brockschmidt, F. Emmes
groups of two! directly before (very latest: at the beginning of) the exercise AH 2. Alternatively you can drop your solutions into
of all (two) students on your solution. Also
a box which is located right next to Prof. Giesl's oce (until the exercise course starts).
names
and
immatriculation numbers
V3M are only relevant for students attending the V3M (Master Informatik) all students.
Exercises marked with a star are voluntary bonus challenge exercises with advanced diculty. The points reachable in these exercises do not count towards the total number of reachable exercise points, so you may ignore them without any disadvantage. However, the bonus points count towards your point total, so you can work on them instead of working on the (more boring) standard exercises.
Exercise 1 ( -reduction):
a) Give all reduction sequences with
(2 + 2 = 4 points)
Hints:
instead of
(x y.y (x x y )) (A A) B .
and
instead of
x.x
You can also save space by representing the reduction sequences as (directed) graphs.
For example,
t1 t2 t4 , t1 t2 t5 ,
and
t1 t3 t5
t4
t2 ? ?? ?? ??
t1 ? ?? ?? ??
t3
t5
(3 points)
is called a delta-rule set if
c t1 . . . tn r
with
c C , t1 , . . . , t n , r
t1 , . . . , t n , r
ti ti
are in
-normal
form
c t1 . . . tm r
with
c t1 . . . tn r
and
mn
l r
if
for all
t1 t2 ,
then also
y.t1 y.t2
for all
r , y V .
-reduction
by
i.e.,
= .
is conuent. Now assume that
The four conditions (1), (2), (3), (4) are required in order to ensure that we replace condition (4) by the following condition: in
c t1 . . . tn r
and
c t1 . . . tm r
with Would
mn
and
r=r
(1 + 1 + 1 + 1 = 4 points)
-calculus
presented in the lecture, i.e.,
xy.x
and
False
True
is
as
xy.y .
-terms
(3 + 3 + 1 + 8* = 7 + 8* points) In this exercise you should implement an interpreter for a (slightly simplied) variant of Simple Haskell.
SimplerHaskell.zip from our homepage for this exercise. It contains several les needed Solution.hs, a framework for the solution. You only need to complete this le
to solve the exercise and do not need to look at the other les (of course, you may do so if you are interested). After successful completion of this exercise, compiling the le
Main.hs using the command ghc --make Main.hs should print no errors and produce the le Main (on Linux and Mac OS X) or Main.exe (on Windows). Lambda.hs
Contains the data type
LTerm
to represent
-terms.
data LTerm = LV String | LC Constant | LApp LTerm LTerm | LAbstr String LTerm
-----
data Constant = B Bool | I Integer | D DeltaOp data DeltaOp = Plus | Times | LessThan | If | Fix
LV
was called
no constants were represented). For example, the representation of the lambda term (x.3)y is
Lambda.hs also contains the function subst :: LTerm -> String -> LTerm -> LTerm, which, if called 1 as subst t x e, replaces all free occurrences of the variable x in the term t by the term e.
The function turns
betaRed :: LTerm -> Maybe LTerm, is also supplied. The call betaRed t either reJust t', where t' results from a one-step -reduction at the top position of t, or Nothing, if 2 For example, betaRed (LApp (LAbstr "x" (LC (I 3))) (LV "y")) this reduction is not possible. (i.e., betaRed applied to the term from above) returns Just (LC (I 3)), while betaRed (LApp (LApp (LAbstr "x" (LC (I 3))) (LV "y") (LV "z"))) (corresponding to betaRed applied to ((x.3)y )z ) returns Nothing, as no reduction is possible at the top position. (Here, -reduction would only be
possible below the top position.) Finally, the function
deltaArity :: DeltaOp -> Int is given. This function maps each -operator to -rules. For example deltaArity Fix = 0 and deltaArity Plus = 2. SHTerm
to represent expressions in a variant of simple
SimplerHaskell.hs
Haskell,
data SHTerm = SHV String | SHC Constant | SHApp SHTerm SHTerm | SHIf SHTerm SHTerm SHTerm | SHLet String SHTerm SHTerm | SHLambda String SHTerm
Here, the same data type
-------
Variable Constant Application If expressions Let expressions Lambda abstractions LTerm. Strings into terms Solution.hs:3
Constant
Parser.hs
of type
SHTerm.
parse ::
*Solution> parse "let x = 3 in \\y -> plus y x" SHLet "x" (SHC (I 3)) (SHLambda "y" (SHApp (SHApp (SHC (D Plus)) (SHV "y")) (SHV "x")))
The parser detects the keywords plus, times, lessThan, and fix and hence transforms them into the corresponding constants of the data type is transformed into
else ...
DeltaOp.
Haskell.
if ...
a)
SHTerm -> LTerm, which is dened subst from the le Lambda.hs.
as
Lam
After you completed this, the following should work after you start your
Solution.hs:
*Solution> lam (parse "let f = \x -> times x x in let x = 3 in f x") Fix (\f.(\x.(Times x x))) (Fix (\x.3))
b)
deltaRed :: LTerm -> Maybe LTerm. For the function application deltaRed t, the result should be Just t , if t results from applying one -rule from the lecture at the top position of t, or Nothing, if no -rule can be applied at the top. Implement deltaRed for all rules for every operator from the data type DeltaOp. Here, Plus, Times, LessThan should behave like their corresponding mathematical functions. The symbol If represents if-expressions and Fix is the xpoint operator as
dened in the lecture.
1 This function was already used in Ex. 3c) 2 This function was implemented as eval in 3 Note that \\ is needed instead of \.
LTerm can be represented as a binary tree. The nodes of the tree are variables, . For example, ((x.x)(f (h x)))z would be represented as:
@ @ @ x f h @ x z
We dene the
active subterm
of a
subterm
-terms.
of the path
So for the term in our example, we would follow the leftmost path until we reach a node dierent from @. Hence, the active subterm in our example is from the root of the tree to the active subterm. So for Implement the function
(x.x). The active length of t is the length t in our example, the length is 2.
such that
active :: whnoRed ::
active t
d )
if
Just t ,
such that
t t
of
-operator
of arity
4 0, return
Just t ,
where
-rule
of
t.
(t1 t2 ),
t. s
is a
i. If
-operator,
a.
a) If
is smaller than
d,
whnoRed
t1
of
t.
If
that succeeds, we return an application with the obtained redex on the left-hand and the unchanged right-hand side the current term b) If of If
t2
of
Nothing.
This corresponds to the case that in the next step of a WHNO-reduction, only some parts of
d,
whnoRed
t1
t2
of
on the right.
whnoRed
t2
of
t.
If
that succeeds, we can return an application with the unchanged left-hand side and the obtained redex on the right-hand side. If this fails and we try to apply a
t1
on the left
Nothing.
be
a = d, i.e., we have ensured that a arguments of our -operator are in WHNF, -rule. If that succeeds, we return the obtained redex, otherwise we return -operator is 2 (e.g. the -operator could
Plus),
4 To
-operators,
deltaArity.
Plus
Lambda.hs.
-reduction
to apply
betaRed
in
-reduction
If you implemented the functions correctly, a call to which result after each step in the WHNO-reduction:
Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) 1 \f.(f (Fix f)) (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) 1 \fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1))))) (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1))))))) 1 \x.(If (LessThan x 1) 1 (Times x (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) (Plus x -1)))) 1 If (LessThan 1 1) 1 (Times 1 (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) (Plus 1 -1))) If True 1 (Times 1 (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) (Plus 1 -1))) \x.(\y.x) 1 (Times 1 (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) (Plus 1 -1))) \y.1 (Times 1 (Fix (\fak.(\x.(If (LessThan x 1) 1 (Times x (fak (Plus x -1)))))) (Plus 1 -1))) 1