Sunteți pe pagina 1din 3

Exercise Sheet 7 (due 13.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
of all (two) students on your solution. Also

The solutions must be handed in

course on Wednesday, 13.06.2012, 11:45, in lecture hall

into 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!

Exercise 1 (Domain Lifts):

(2 + 2 = 4 points)
D.
Prove that

a) b)

Let

be a domain and

D a complete partial order on

D is a complete partial order

for the lift Let

of

D.
D1 , . . . , D1 .
Prove that

D1 , . . . , Dn

be domains with complete partial orders

D1 ...Dn is a

complete partial order on

D1 . . . Dn .

Exercise 2 (Domain Construction):


Consider the following data type declaration for natural numbers:

(2 + 2 + 1 + 2 = 7 points)

data Nats = Zero | Succ Nats


A graphical representation of the rst four levels of the domain for

Nats

could look like this:

Succ (Succ Zero)

Succ (Succ (Succ ))

Succ Zero

Succ (Succ )

Zero

Succ

Now consider the following data type declarations:

data Unit = U data Foo = A Unit Unit | B Bool data UnitList = N | C Unit UnitList
(a) Give a graphical representation of the whole domain for the type

Foo.

Exercise Sheet 7 (due 13.06.2012)


(b) Give a graphical representation of the rst three levels of the domain for the type level contains the element

Functional Programming SS12

C U ,

UnitList.

The third

for example.

(c) How many elements does the fourth level of the domain for the type

UnitList

contain?

(d) Give Haskell expressions that correspond to the following elements of the domain for the type

UnitList,

i.e., for each of these elements, give a Haskell expression that has this element as its semantics:

 C  C U (C N)  C U (C U ( . . .(C U ). . .))
innitely often

Exercise 3 (Enumerating Domains):


We want to implement a all necessary values, we use the following data type:

(2 + 2 = 4 points)

Haskell program that generates the domain for Nats up to a given depth. To represent
(Succ ) Succ Bot.

data NatsDomain = Bot | Zero | Succ NatsDomain deriving Show


Here,

Bot

represents

and the element

of the domain can be represented as

a)

Write a

Haskell function
d
all strictly

moreDefinedSuccessors :: NatsDomain -> [ NatsDomain ]


which takes some value from the domain and returns all direct successors, i.e., for some more dened values Some examples:

e1 , . . . , e k

are returned such that there exists no

with

ei

for any

1 i k.

moreDefinedSuccessors Bot = [Zero, Succ Bot] moreDefinedSuccessors Zero = [] moreDefinedSuccessors (Succ Bot) = [Succ Zero, Succ (Succ Bot)]

b)

Write a

Haskell function
generate n
returns the rst

generate :: Int -> [[ NatsDomain ]]


such that

levels of the domain for

Nats,

with the highest level rst and

the lowest level last. For example:

generate 1 = [[Bot]] generate 2 = [[Zero,Succ Bot],[Bot]] generate 3 = [[Succ Zero,Succ (Succ Bot)],[Zero,Succ Bot],[Bot]]

Exercise 4 (Simple Haskell):

(2 + 2 + 2 + 2 = 8 points)

We dene the following algebraic data types and the following functions.

data Nats = Zero | Succ Nats data List a = Nil | Cons a ( List a ) pred :: Nats -> Nats pred ( Succ x ) = x isZero :: Nats -> Bool isZero Zero = True

Exercise Sheet 7 (due 13.06.2012)


isZero _ = False

Functional Programming SS12

isSucc :: Nats -> Bool isSucc ( Succ x ) = True isSucc _ = False head :: List a -> a head ( Cons x _ ) = x tail :: List a -> List a tail ( Cons _ xs ) = xs isNil :: List a -> Bool isNil Nil = True isNil _ = False fst :: (a , b ) -> a fst (x , y ) = x snd :: (a , b ) -> b snd (x , y ) = y
Give simple

Haskell denitions that are equivalent to the following (full) Haskell denitions.

You may use the

functions dened above and also the usual Boolean and arithmetic functions. You do not have to use the transformation rules which will be presented later in the lecture, and you also do not need to prove equivalence of your solution to the original denition.

a) minus :: Nats -> Nats -> Nats

minus Zero _ = Zero minus x Zero = x minus ( Succ x ) ( Succ y ) = minus x y

b) div :: Nats -> Nats -> Nats

div Zero _ = Zero div x ( Succ Zero ) = x div x ( Succ y ) = Succ ( div ( minus x ( Succ y )) ( Succ y ))
Hints:

Note that

div x Zero

is not dened. You may use the predened function

undefined

to model

this in your solution.

c) unzip :: List (a , b ) -> ( List a , List b )

unzip Nil = ( Nil , Nil ) unzip ( Cons (x , y ) zs ) = ( Cons x xs , Cons y ys ) where ( xs , ys ) = unzip zs

d) isPowerOfTwo :: Int -> Bool

isPowerOfTwo 0 = False isPowerOfTwo 1 = True isPowerOfTwo n = if ( mod n 2) == 1 then False else isPowerOfTwo ( div n 2)

S-ar putea să vă placă și