[SOLVED] ocaml algorithm Eq : :

$25

File Name: ocaml_algorithm_Eq_:_:_____.zip
File Size: 254.34 KB

5/5 - (1 vote)

Eq : :
= :: . :: . :: .

r e f l : :: . Eq
= :: . :: .x : . x

symm : :: . :: . Eq Eq
= :: . :: .e: ( :: . ) . e [ :: .Eq ] ( r e f l [ ] )

t rans : :: . :: . :: . Eq Eq Eq
= :: . :: . :: .ab : Eq .bc : Eq . bc [ Eq ] ab

Zero : :
= : : .

Unit : :
= .

uni t : Unit
= .x : . x

Z : :
= Zero

S : :
= X.X+ Unit

Neq : :
= . . Eq Zero

s z d i s t i n c t : N. Neq (S N) Z
= N.eq : Eq (S N) Z . eq [X.X] ( i n r [N] un i t )

eq pred : M.N. Eq (S M) (S N) Eq M N

Figure 1: Definitions in System F

2

1 Types and type inference

(a) For each of the following System F terms either give a typing derivation or
explain why there is no such derivation

(i) ::*.f: .f f

(ii) f:(::*.).f [. ] [.] f

(iii) f:(::* *.. )..f [] []

(6 marks)

(b) Algorithm J is defined recursively over the structure of terms. The case for
function application (M N) is as follows:

J (, M N) =
where A = J (, M)
and B = J (, N)
and unify ({A = B }) succeeds
and is fresh

Give similar cases to handle the following constructs:

(i) Constructing a value of sum type using inl: inl M

(ii) Scrutinising a value of sum type: case L of x.M | y.N

(4 marks)

(c) Both the case for let in Algorithm J and the corresponding typing rule only
generalize (quantify) type variables that are not already in the context. Explain
by means of an example what problems would arise if type variables in the
context were also generalized.

(3 marks)

3

2 De Morgans laws in F

In lecture 2 we saw System F encodings of a number of types, including the type with
one inhabitant (equivalent to OCamls unit type), and the type with two inhabitants
(equivalent to bool). We can also encode the type with no inhabitants, following the
same pattern:

Zero ::
Zero = ::*.

Under the Curry-Howard correspondence, Zero represents the false proposition, and
the fact that it has no inhabitants corresponds to the fact that the false proposition
has no proof. The fold for Zero corresponds to the logical principle ex falso quodlibet
(from falsehood anything follows):

foldZero : Zero ::*.
foldZero = e:Zero.e

Using Zero, we can represent logical negation as a type operator :

Not :: * *
Not = A.A Zero

and using Not we can build System F types corresponding to De Morgans laws:

DM1 = ..Not ( ) (Not + Not )

DM2 = ..(Not + Not ) Not ( )

DM3 = ..Not ( + ) (Not Not )

DM4 = ..(Not Not ) Not ( + )

(a) Three of De Morgans laws have proofs in System F that is, it is possible
to write System F terms that have the corresponding types.

(i) Add a term to exercise1.f for each of those three types.

(ii) Identify the type for which it is not possible to define a term, and explain
(in a sentence or two) why it is not possible.

(b) De Morgans laws can be extended from binary connectives to quantified
propositions, treating universal quantification as a generalized product and
existential quantification as a generalized sum. For example, the following
definition may be viewed as a generalized version of DM1:

DM5 = ::* *.Not (. ) (.Not ( ))

(i) Add types DM6, DM7 and DM8 representing similarly extended versions of DM2,
DM3 and DM4 to exercise1.f.

4

(ii) For each of the types DM5, DM6, DM7 and DM8, either give a term of that type,
or explain why it is not possible to define such a term.

(11 marks)

5

3 Vectors in F

System F is a total languagei.e. the evaluation of every System F expression
completes without error, producing a value. For this reason a number of OCaml
functions cannot be defined using the System F encoding of datatypes seen in the
lectures. For example, here is the definition of a function hd that returns the first
element of a list or fails if the list is empty:

val hd : a list -> a
let hd = function

[] -> failwith hd
| a::_ -> a

Since evaluating hd v may fail, there is no equivalent of this function in System F.

However, it is possible to define a richer sequence type that distinguishes between
empty and non-empty lists, and for which hd can be defined. Here is a definition of
a List-like type function Vec, whose two arguments respectively represent the type of
the elements in the sequence and the length of the sequence:

Vec :: * * *
Vec = .M.::* *. Z (N. N (S N)) M

For example, a value of type Vec Nat (S (S Z)) represents a sequence of two Nat values.

We assume that we have available type operators Z (zero) and S (successor) for
constructing a representation of numbers:

Z :: *
S :: * *

Furthermore, it will be useful to have representations of some basic facts about
numbers. Making use of Eq from the lectures and Not from Question 2, we can define
a type rerepsenting the proposition that one number is the predecessor of another:

Pred :: * * * = M.N.Eq (S M) N

and a term representing the fact that zero is not the successor of any number:

sz_distinct : N.Not (Eql (S N) Z)

and a term representing the fact that successor is injective (i.e. that if S M is equal
to S N then M is equal to N):

eq_pred : M.N.Eq (S M) (S N) Eq M N

(a) Define Vec constructors nil and cons analogous to the constructors for lists, and
with the following types:

nil : .Vec Z
cons : .. Vec Vec (S )

6

(Hint: this is relatively straightforward, and you should not need to make use
of any of the facts about numbers.)

(b) Define a function head that extracts the first element of a non-empty vector.
Your function should have the following type:

head : ..Vec (S )

(Hint: this is harder. The key is to find an appropriate instantiation for in the
definition of Vec. Remember that the result type of may be a function type.)

(c) Define tail a function tail that removes the first element of a non-empty vector.
Your function should have the following type:

tail : ..Vec (S ) Vec

(Hint: this is really quite tricky! You may find it helpful to start by defining
an auxiliary function that returns a pair corresponding to the list itself together
with its tail.)

(d) The map function for Vec behaves similarly to map for lists: it changes the type
of each element, but preserves the length. Give a definition of map with the
following type:

map : …( ) Vec Vec

(6 marks)

7

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] ocaml algorithm Eq : :
$25