• Home
  • Textbooks
  • Programming in Haskell
  • Reasoning about programs

Programming in Haskell

Graham Hutton

Chapter 16

Reasoning about programs - all with Video Answers

Educators


Chapter Questions

07:27

Problem 1

Show that add n (Succ m) = Succ (add n m), by induction on n.

Sirat Shah
Sirat Shah
Numerade Educator
00:51

Problem 2

Using this property, together with add n Zero = n, show that addition is commutative, add n m = add m n, by induction on n.

Kristen Frankie
Kristen Frankie
Numerade Educator
02:20

Problem 3

Using the following definition for the library function that decides if all elements of a list satisfy a predicate all p [] = True all p (x:xs) = p x && all p xs complete the proof of the correctness of replicate by showing that it produces a list with identical elements, all (== x) (replicate n x), by induction on
Hint: show that the property is always True.

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
04:28

Problem 4

Using the definition
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
verify the following two properties, by induction on xs:
xs ++ [] = xs
xs ++ (ys ++ zs) = (xs ++ ys) ++ zs

Abhishek Kumar
Abhishek Kumar
Numerade Educator
05:49

Problem 5

Using the above definition for ++, together with

show that take n xs ++ drop n xs = xs, by
simultaneous induction on the integer n —img— 0 and
the list xs. Hint: there are three cases, one for each
pattern of arguments in the definitions of take and
drop.

Chris Trentman
Chris Trentman
Numerade Educator
05:55

Problem 6

Given the type declaration
data Tree = Leaf Int $\mid$ Node Tree Tree
show that the number of leaves in such a tree is always one greater than the number of nodes, by induction on trees. Hint: start by defining functions that count the number of leaves and nodes in a tree.

Chris Trentman
Chris Trentman
Numerade Educator
03:51

Problem 7

Verify the functor laws for the Maybe type. Hint: the proofs proceed by case analysis, and do not require the use of induction.

Prathan Jarupoonphol
Prathan Jarupoonphol
Numerade Educator

Problem 8

Given the type and instance declarations below, verify the functor laws for the Tree type, by induction on trees.
data Tree a = Leaf a | Node (Tree a) (Tree a)

instance Functor Tree where
— fmap :: (a -> b) -> Tree a -> Tree b
fmap g (Leaf x) = Leaf (g x)
fmap g (Node l r) = Node (fmap g l) (fmap g
r)

Check back soon!

Problem 9

Verify the applicative laws for the maybe type.

Check back soon!

Problem 10

Verify the monad laws for the list type. Hint: the proofs can be completed using simple properties of list comprehensions.

Check back soon!
01:10

Problem 11

Given the equation comp' e c= comp e $++c$, show how to construct the recursive definition for comp', by induction on e.

Carson Merrill
Carson Merrill
Numerade Educator