Вы находитесь на странице: 1из 5

Exercise Sheet 9 (due 29.06.

2012)
Prof.aa Dr. J. Giesl Notes: M. Brockschmidt, F. Emmes

Functional Programming SS12

Please solve these exercises in

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

The solutions must be handed in

course on Friday, 29.06.2012, 10:00, in lecture hall

a box which is located right next to Prof. Giesl's oce (until the exercise course starts).

Please write the

names

and

immatriculation numbers

please staple the individual sheets! Exercises marked with 

version of the lecture. Unmarked exercises are relevant for

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)

starting with the following term:

(x y.plus y x) ((z.times 3 z ) 13) 3


b) Give all reduction sequences with

with at most 3 steps starting with the following term:

((x y.y (x x y )) (x y.y (x x y ))) (x.x)


Hints:


Hints:

You may write

instead of

could then be represented as

(x y.y (x x y )) (A A) B .

and

instead of

x.x

to save space. The start term

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

can be represented as:

t4

     t2 ? ??  ??  ??   

t1 ? ?? ?? ??      

t3

t5

Exercise 2 (Conuence of -reduction):


Recall the notion of  -reduction, which is dened as follows: A set (1)

(3 points)
is called a delta-rule set if

of rules of the form

c t1 . . . tn r

with

c C , t1 , . . . , t n , r

t1 , . . . , t n , r

are closed lambda terms

Exercise Sheet 9 (due 29.06.2012)


(2) all (3) the (4) in

Functional Programming SS12

ti ti

are in

-normal

form

do not contain any left-hand side of a rule from

c t1 . . . tm r
with

there exist no two dierent rules

c t1 . . . tn r

and

mn

For such a set

we dene the relation

as the smallest relation with

l r
if

for all

lr (t1 r) (t2 r), (r t1 ) (r t2 ) and and

t1 t2 ,

then also

y.t1 y.t2

for all

r , y V .

We denote the combination of

-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

there exist no two dierent rules

c t1 . . . tn r

and

c t1 . . . tm r

with Would

mn

and

r=r

still be conuent? Please explain your answer.

Exercise 3 (V3M  Pure Lambda Calculus):


We use the representation of boolean values in the pure represented as functions:

(1 + 1 + 1 + 1 = 4 points)
-calculus
presented in the lecture, i.e.,

xy.x

and

False

True

is

as

xy.y .

Using this representation give pure

-terms

for the following

a) and b) or c) not d) xor

Exercise 4 (A Simple Haskell Interpreter):


Download the le to complete the task, including

(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

-----

Variable Constant Application Lambda Abstraction

data Constant = B Bool | I Integer | D DeltaOp data DeltaOp = Plus | Times | LessThan | If | Fix

Exercise Sheet 9 (due 29.06.2012)


A variation of this data type was already used in exercise 3 on sheet 2 (where

Functional Programming SS12

LV

was called

no constants were represented). For example, the representation of the lambda term  (x.3)y  is

(LAbstr "x" (LC (I 3))) (LV "y").

LVar and LApp

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

the arity of its corresponding

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

Contains the data type

Haskell,

which omits user-dened data types.

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

is used as in the denition of

Parser.hs

contains the function

of type

SHTerm.

parse ::

String -> SHTerm,

To try this function, start the

Haskell interpreter with the command ghci

which transforms input

*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 ...

SHC (D Plus). lam ::

The keyword  if is not handled this way, since

DeltaOp.

In the example, you can see this, as

is a built-in syntax construct of Simpler

Haskell.

if ...

plus then ...

a)

Implement the function

need to make use of the function

SHTerm -> LTerm, which is dened subst from the le Lambda.hs.

as

Lam

in the lecture. Here, you

After you completed this, the following should work after you start your

Solution.hs:

Haskell-interpreter using ghci

*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)

Implement the function

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 \.

on sheet 2. Ex. 3c) on sheet 2.

Exercise Sheet 9 (due 29.06.2012) c)


Every lambda term of type

Functional Programming SS12

constants, @ (for application), and

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

leftmost path of the tree corresponding to

-term t as the rst t such that r

subterm

that is reached when following the

is not an application. This corresponds to the

rst subterm from the left in the textual representation of

-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 ::

consisting of the active subterm and the active length of

LTerm -> (LTerm, Int) t. LTerm -> Maybe LTerm.

active t

returns the tuple

d )

Implement the function evaluate to term

Nothing, resulting from t


is a

The function call

if

is in WHNF. Otherwise, it should return a term

Just t ,

such that

whnoRed t should t is the

by one evaluation step in the WHNO-reduction.

For this, use the following algorithm: 1) If 2) If

t t
of

-operator

of arity

4 0, return

Just t ,

where

is the right-hand side of the

-rule

of

t.

is an application of the form

(t1 t2 ),

we rst determine the active subterm

and the active length

t. s
is a

i. If

-operator,

we determine its arity

a.

a) If

is smaller than

d,

we try to recursively apply

whnoRed

to the left-hand side

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

on the right. Otherwise, we return

Nothing.

This corresponds to the case that in the next step of a WHNO-reduction, only some parts of

need to be considered and hence, we descend to the left side. equal to

a is greater than or t. If that succeeds, t1

d,

we try to recursively apply

whnoRed

to the left-hand side

t1

we can return an application with the obtained redex on the left-hand

and the unchanged right-hand side

t2

of

on the right.

cannot be reduced, we try to recursively apply

whnoRed

to the right-hand side

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

For example, if the active length is 3 and the arity of the

Plus),

the shaded part in the following diagram would be replaced:

4 To

determine the arity of

-operators,

you can use the predened function

deltaArity.

Exercise Sheet 9 (due 29.06.2012)


@ @ @ t2 t1 t3

Functional Programming SS12

Plus

ii. Otherwise, apply the the le

Lambda.hs.

-reduction

to apply

to its argument, by using the function

betaRed

in

The part that would be replaced by a

-reduction

is shaded in the diagram

from c). 3) Otherwise, return

Nothing. Main fak.sh


should return the following terms

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

Вам также может понравиться