Note on Exercise 2 (190117)

Note on Exercise 2 (190117)
In Isabelle/HOL, no type is defined for matrix. We define type “’a mat” as
follows.
type synonym ’a mat = (’a
list)
list
A matrix is defined as a list of lists or a list of column lists. For example,
0
list
[[a, b],[c,
d],[e, f ]] is of type a mat, and represents the following matrix
a
b
c
d
e
f
.
2.(a)
The following issue arises when working with matrices as list of lists. A matrix
of type “’a mat” has a dimension nr×nc, i.e. the number of rows is nr and the
number of columns is nc.
To make sure that we work with mathematically valid matrices, define a
predicate mat::nat⇒nat⇒’a mat⇒bool where where mat nr nc m checks that
matrix m has nr rows and nc columns.
Hint 1: Matrix [[a, b],[c, d],[e, f ]] is of dimension 2×3, which means that the
length of the outermost list is 3, and each column list should have 2 elements.
Hint 2: You can use function nth::’a list ⇒nat⇒’a returns the nth element
of a list. The infix operator of nth is !.
2.(b) & 2.(c)
We want to compute with matrices, for instance perform addition and multiplication. To that end, we need functions that allow us to extract columns and
rows. Can you define functions col and row such that col i m returns the ith
column of matrix m and row i m returns the ith row ofmatrix m. a c e
Example. Let m = [[a, b],[c, d],[e, f ]] be the matrix
.
b
• col 0 m = [a, b], col 1 m = [c, d], col 2 m = [e, f ]
• row 0 m = [a, c, e], row 1 m = [b, d, f ]
1
d
f
2.(d)
Isabelle is a higher-order theorem prover. It allows us to implement and manipulate higher-order functions. One basic higher-order function is the map
function that applies a given function to each element of a list.
Recall function add1 from the class.
fun add1::“nat list ⇒nat list”
where
“add1 [] = []” |
“add1 (Cons x xs) = Cons (Suc x) (add1 xs)”
We can define add1 in a different way. First let’s define a function add1 element
that add 1 to a natural number.
fun add1 elt::“nat ⇒nat”
where “add1 elt x = Suc x”
fun add1::“nat list ⇒nat list”
where “add1 m = map add1 elt m”
Given a list m of natural number, the expression “map add1 elt m” maps
function add1 elt to list m, which means it applies function add1 elt to all the
elements of the list m.
Example. map add1 elt [0,1,2] = [add1 elt 0, add1 elt 1, add elt 2] = [1, 2, 3]
In this exercise, try to complete the definition of function map.
2.(e)
The objective of this exercise is to define a simple operation on matrices. The
reverse of a matrix is the
reverse of its columns. Let m = [[a, b],[c, d],[e, f ]] be
the matrix
a
b
c
d
e
f
. Then, rev map m = [[b, a],[d, c],[f, e]].
Remember that you have already implemented function reverse on lists in
the last quiz. Can you use function reverse to define rev map?
2