1. Homepage
  2. Homework
  3. COMP4161 T3/2024 Advanced Topics in Software Verification - Assignment 2: Block Representation of Binary Numbers
This question has been solved

COMP4161 T3/2024 Advanced Topics in Software Verification - Assignment 2: Block Representation of Binary Numbers

Engage in a Conversation
UNSWCOMP4161Advanced Topics in Software VerificationIsabelle

COMP4161 T3/2024 Advanced Topics in Software Verification CourseNana.COM

Assignment 2 CourseNana.COM

The assignment is take-home. This does NOT mean you can work in groups. Each submission is personal. For more information, see the plagiarism policy: https://student.unsw.edu.au/plagiarism CourseNana.COM

Submit using give on a CSE machine: give cs4161 a2 files ... CourseNana.COM

For example: CourseNana.COM

     give cs4161 a2 a2.thy

For this assignment, all proof methods and proof automation available in the standard Isabelle distribution is allowed. This includes, but is not limited to, simp, auto, blast, force, and fastforce. CourseNana.COM

However, if you’re going for full marks, you shouldn’t use ”proof” methods that bypass the inference kernel, such as sorry. We may award partial marks for plausible proof sketches where some subgoals or lemmas are sorried. CourseNana.COM

If you use sledgehammer, it’s important to understand that the proofs suggested by sledgehammer are just suggestions, and aren’t guaranteed to work. Make sure that the proof suggested by sledgehammer actually terminates on your machine (assuming an average spec machine). If not, you can try to reconstruct the proof yourself based on the output, or apply a few manual steps to make the subgoal smaller before using sledgehammer. CourseNana.COM

Note: this document contains explanations of the problems and your assignment tasks. The full set of definitions can be found in the associated Isabelle theory files (this document may not contain all the definitions). You are not allowed to modify the various definitions and the lemma names and their statements provided in the Isabelle theory files, unless you are instructed to do so. You are however allowed to add appropriate attributes to those lemmas to facilitate automation as you need. You are also allowed to prove your own additional lemmas and use them in your solutions to the assignment questions. CourseNana.COM

Hint: make sure that you read through the hints provided at the end of this document. CourseNana.COM

1 Block Representation of Binary Numbers (12 marks) CourseNana.COM

In this assignment, we consider one representation of binary numbers where the bits in a number is segmented into alternating sequences (or blocks) of 1s or 0s. For instance, ”1110011” consists of ”three 1s, two 0s, and two 1s”. Formally, we define such blocks as follows: CourseNana.COM

datatype block = Zeros nat | Ones nat CourseNana.COM

A list of such block represents a binary number if it satifies the following well-formedness con- siderations: CourseNana.COM

• number 0 should be represented only by an empty list; 1 CourseNana.COM

• each block represents non-empty sequence of 0s or 1s; CourseNana.COM

• blocks of 0s and 1s should alternate in a list. CourseNana.COM

The first point means that lists such as [Zeros 3] or [Ones 0] are not well-formed. The second point means that 0 < i should hold for any block Zeros i and Ones i. The third point says that lists that have consecutive blocks of 1s or 0s, eg. [Ones i, Ones j, ...], are ill-formed. We name the type of our binary number representation as bnat. The inductive predicate wf gives the well-formedness conditions as discussed above: CourseNana.COM

type-synonym bnat = block list inductive wf :: bnat bool where CourseNana.COM

wf-nil: wf []
|wf-ones:i>0=wf [Onesi]
| wf-zeros: i > 0 =wf blks =is1hd blks =wf (Zeros i # blks) |wf-ones2:i>0=wfblks=is0hdblks=wf (Onesi#blks) CourseNana.COM

where each of the primrec functions is1hd and is0hd tests if the head of the given list is a 1-block or 0-block, respectively. CourseNana.COM

We then define a function to-nat that computes the value as natural number (converts a bnat number to a natural number). Note that we interpret the head of a list as the least significant (block of) digits of the number. CourseNana.COM

primrec to-nat :: bnat nat where to-nat [] = 0 CourseNana.COM

| to-nat (b # blks) = (case b of CourseNana.COM

Zeros i 2^i (to-nat blks)
| Onesi2^i1+2^i(to-natblks)) CourseNana.COM

(a) Prove simplification rules for is0hd and list append (@) (1 marks): is0hd (x @ y) = (if x = [] then is0hd y else is0hd x) CourseNana.COM

(b) State and prove similar simplification rules for is1hd and list append (@). (2 marks) (c) Prove that is1hd n and is0hd n cannot be the same for any n. (2 marks) CourseNana.COM

(d) Provethat,foranywell-formedn,thevalue0isrepresentedby[]andnothingelse.(4marks) (e) Prove that, for any well-formed n, if is1hd n holds, the value of n must be positive. (3 marks) CourseNana.COM

2 Successor and Predecessor (40 marks) CourseNana.COM

Next, we define a successor function (succ) and a predecessor function (pred) for bnat. In order to do so, we need to define functions add0s and add1s that add a block of 0s or 1s, respectively, at the head of a bnat number in a correct way, so that, when applied to a well-formed bnat number, those functions will return a well-formed bnat number. CourseNana.COM

primrec add-block0 :: nat block block list where add-block0 i (Zeros j) = [Zeros (i+j)] CourseNana.COM

| add-block0 i (Ones j) = (if i = 0 then [Ones j] else [Zeros i,Ones j]) CourseNana.COM

primrec add0s :: nat bnat bnat where add0s i [] = [] CourseNana.COM

| add0s i (b # blks) = (add-block0 i b) @ blks
2 CourseNana.COM

  1. (a)  Prove simplification rules for applying add0s twice in a row. (2 marks) CourseNana.COM

  2. (b)  Prove that add0s and add1s return well-formed bnat numbers. (4 marks) CourseNana.COM

  3. (c)  Prove the correctness of add0s, i.e., the bnat numbers it return have correct values as natural numbers. (4 marks) CourseNana.COM

  4. (d)  Prove the correctness of add1s, similarly as above. (5 marks) CourseNana.COM

Now we can define succ and pred as follows: CourseNana.COM

primrec succ :: bnat bnat where succ [] = [Ones 1] CourseNana.COM

| succ (b # blks) = (case b of CourseNana.COM

Zeros i add1s 1 (add0s (i1) blks) | Ones i Zeros i # succ blks) CourseNana.COM

primrec pred :: bnat bnat where pred [] = [] CourseNana.COM

| pred (b # blks) = (case b of CourseNana.COM

Ones i add0s 1 (add1s (i1) blks) | Zeros i Ones i # pred blks) CourseNana.COM

(e) Prove that succ will turn a is0hd list into a is1hd list and vice versa. (4 marks) (f) Prove that succ preserves the well-formedness conditions. (3 marks) CourseNana.COM

(g) Prove the correctness of succ. (4 marks)
(h) Prove that
pred preserves the well-formedness conditions. (4 marks) CourseNana.COM

(i) Prove the correctness of pred. (6 marks)
(j) Prove
pred (succ n) = n for a well-formed n. (4 marks) CourseNana.COM

3 Conversion from Natural Numbers (30 marks) CourseNana.COM

We now consider converting natural numbers into our block binary representation. The function from-nat does this conversion using the function succ that we defined in the previous section. CourseNana.COM

Throughout this assignment it may be helpful to state your own intermediate lemmas. This is especially the case for parts (c), (d) and (e) of this question. CourseNana.COM

primrec from-nat :: nat bnat where from-nat 0 = [] CourseNana.COM

| from-nat (Suc n) = succ (from-nat n) CourseNana.COM


(a)  Prove that from-nat generates a well-formed bnat number. (2 marks) CourseNana.COM

(b)  Prove that to-nat is the inverse of from-nat. (2 marks) CourseNana.COM

(c)  Prove that adding a 0-block (add0s i (from-nat n)) gives the representation of the number 2i n. (8 marks) CourseNana.COM

(d) Prove that a single element 1-block [Ones i] represents the natural number 2i 1 assuming i > 0. (8 marks) CourseNana.COM

CourseNana.COM

(e) Prove that from-nat is the inverse of to-nat for well-formed bnat numbers. (10 marks) CourseNana.COM


Addition and Multiplication (18 marks) CourseNana.COM

Here we consider addition and multiplication of our block binary representation. The simple-add below defines such addition that uses an auxiliary worker function with an extra variable. CourseNana.COM

primrec simple-add-worker :: nat bnat bnat bnat where CourseNana.COM

simple-add-worker 0 i j = []
| simple-add-worker (Suc fuel) i j = CourseNana.COM

(if i = [] then j
else simple-add-worker fuel
(pred i) (succ j)) CourseNana.COM

definition simple-add :: bnat bnat bnat
where simple-add i j = simple-add-worker (to-nat i + 2) i j CourseNana.COM

  1. (a)  Prove that simple-add-worker return a well-formed bnat number if its last argument is well-formed. (3 marks) CourseNana.COM

  2. (b)  Prove the correctness of simple-add-worker. (3 marks) CourseNana.COM

  3. (c)  Prove the correctness of simple-add. (2 marks) CourseNana.COM

Now, we can define a multiplication function, using a similar auxiliary function that uses sim- ple-add. CourseNana.COM

(d) Complete the definition of simple-mul-worker using simple-add. (3 marks) (e) Complete the definition of simple-mul. (2 marks) CourseNana.COM

(f) Prove the correctness of simple-mul-worker. (3 marks) (g) Prove the correctness of simple-mul. (2 marks) CourseNana.COM

5 Hints CourseNana.COM

• Many proofs will require induction of one kind or the other. Other than inducting on datatypes directly, you may find it useful to do induction on inductively defined relations wf. The induction rules for these are automatically generated by Isabelle. CourseNana.COM

You can apply these induction rules as elimination rules, e.g. apply (erule wf.induct), but a more convenient and flexible alternative is CourseNana.COM

                           apply (induct rule: wf.induct)

which allows you to specify which variables should not be all-eliminated using e.g. CourseNana.COM

                   apply (induct arbitrary: x y rule: wf.induct)

CourseNana.COM

• Not everything needs an induction.
• The equivalent of
spec for the meta-logic universal quantifier, if you need it, is called CourseNana.COM

meta_spec. CourseNana.COM

  • For some exercises, you will likely need additional lemmas to make the proof go through. CourseNana.COM

    Part of the assignment is figuring out which lemmas are needed. CourseNana.COM

  • Make use of the find_theorems command to find library theorems. You are allowed to use all theorems proved in the Isabelle distribution. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
UNSW代写,COMP4161代写,Advanced Topics in Software Verification代写,Isabelle代写,UNSW代编,COMP4161代编,Advanced Topics in Software Verification代编,Isabelle代编,UNSW代考,COMP4161代考,Advanced Topics in Software Verification代考,Isabelle代考,UNSWhelp,COMP4161help,Advanced Topics in Software Verificationhelp,Isabellehelp,UNSW作业代写,COMP4161作业代写,Advanced Topics in Software Verification作业代写,Isabelle作业代写,UNSW编程代写,COMP4161编程代写,Advanced Topics in Software Verification编程代写,Isabelle编程代写,UNSWprogramming help,COMP4161programming help,Advanced Topics in Software Verificationprogramming help,Isabelleprogramming help,UNSWassignment help,COMP4161assignment help,Advanced Topics in Software Verificationassignment help,Isabelleassignment help,UNSWsolution,COMP4161solution,Advanced Topics in Software Verificationsolution,Isabellesolution,