Documente Academic
Documente Profesional
Documente Cultură
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
of all (two) students on your solution. Also
into a box which is located right next to Prof. Giesl's oce (until the exercise course starts).
names
and
immatriculation numbers
(2 + 2 = 4 points)
D.
Prove that
a) b)
Let
be a domain and
of
D.
D1 , . . . , D1 .
Prove that
D1 , . . . , Dn
D1 ...Dn is a
D1 . . . Dn .
(2 + 2 + 1 + 2 = 7 points)
Nats
Succ Zero
Succ (Succ )
Zero
Succ
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.
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
(2 + 2 = 4 points)
Haskell program that generates the domain for Nats up to a given depth. To represent
(Succ ) Succ Bot.
Bot
represents
a)
Write a
Haskell function
d
all strictly
e1 , . . . , e k
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
Nats,
generate 1 = [[Bot]] generate 2 = [[Zero,Succ Bot],[Bot]] generate 3 = [[Succ Zero,Succ (Succ Bot)],[Zero,Succ Bot],[Bot]]
(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
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.
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.
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
undefined
to model
unzip Nil = ( Nil , Nil ) unzip ( Cons (x , y ) zs ) = ( Cons x xs , Cons y ys ) where ( xs , ys ) = unzip zs
isPowerOfTwo 0 = False isPowerOfTwo 1 = True isPowerOfTwo n = if ( mod n 2) == 1 then False else isPowerOfTwo ( div n 2)