1. Homepage
  2. Coding
  3. COMP4161 T3/2024 Advanced Topics in Software Verification - Assignment 2: Block Representation of Binary Numbers

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

Get in Touch with Our Experts
UNSWCOMP4161Advanced Topics in Software VerificationIsabelle

COMP4161 T3/2024 Advanced Topics in Software Verification Assignment Writing Service

Assignment 2 Assignment Writing Service

This assignment is released on October 11th, and is due on November 1st 17:59:59. We will accept Isabelle theory (.thy) files only. You are allowed to make late submissions up to five days (120 hours) after the deadline, but at a cost: -5 marks per day. Assignment Writing Service

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 Assignment Writing Service

Submit using give on a CSE machine: give cs4161 a2 files ... Assignment Writing Service

For example: Assignment Writing Service

     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. Assignment Writing Service

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. Assignment Writing Service

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. Assignment Writing Service

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. Assignment Writing Service

Hint: make sure that you read through the hints provided at the end of this document. Assignment Writing Service

1 Block Representation of Binary Numbers (12 marks) Assignment Writing Service

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: Assignment Writing Service

datatype block = Zeros nat | Ones nat Assignment Writing Service

A list of such block represents a binary number if it satifies the following well-formedness con- siderations: Assignment Writing Service

• number 0 should be represented only by an empty list; 1 Assignment Writing Service

• each block represents non-empty sequence of 0s or 1s; Assignment Writing Service

• blocks of 0s and 1s should alternate in a list. Assignment Writing Service

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: Assignment Writing Service

type-synonym bnat = block list inductive wf :: bnat bool where Assignment Writing Service

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) Assignment Writing Service

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. Assignment Writing Service

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. Assignment Writing Service

primrec to-nat :: bnat nat where to-nat [] = 0 Assignment Writing Service

| to-nat (b # blks) = (case b of Assignment Writing Service

Zeros i 2^i (to-nat blks)
| Onesi2^i1+2^i(to-natblks)) Assignment Writing Service

(a) Prove simplification rules for is0hd and list append (@) (1 marks): is0hd (x @ y) = (if x = [] then is0hd y else is0hd x) Assignment Writing Service

(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) Assignment Writing Service

(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) Assignment Writing Service

2 Successor and Predecessor (40 marks) Assignment Writing Service

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. Assignment Writing Service

primrec add-block0 :: nat block block list where add-block0 i (Zeros j) = [Zeros (i+j)] Assignment Writing Service

| add-block0 i (Ones j) = (if i = 0 then [Ones j] else [Zeros i,Ones j]) Assignment Writing Service

primrec add0s :: nat bnat bnat where add0s i [] = [] Assignment Writing Service

| add0s i (b # blks) = (add-block0 i b) @ blks
2 Assignment Writing Service

  1. (a)  Prove simplification rules for applying add0s twice in a row. (2 marks) Assignment Writing Service

  2. (b)  Prove that add0s and add1s return well-formed bnat numbers. (4 marks) Assignment Writing Service

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

  4. (d)  Prove the correctness of add1s, similarly as above. (5 marks) Assignment Writing Service

Now we can define succ and pred as follows: Assignment Writing Service

primrec succ :: bnat bnat where succ [] = [Ones 1] Assignment Writing Service

| succ (b # blks) = (case b of Assignment Writing Service

Zeros i add1s 1 (add0s (i1) blks) | Ones i Zeros i # succ blks) Assignment Writing Service

primrec pred :: bnat bnat where pred [] = [] Assignment Writing Service

| pred (b # blks) = (case b of Assignment Writing Service

Ones i add0s 1 (add1s (i1) blks) | Zeros i Ones i # pred blks) Assignment Writing Service

(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) Assignment Writing Service

(g) Prove the correctness of succ. (4 marks)
(h) Prove that
pred preserves the well-formedness conditions. (4 marks) Assignment Writing Service

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

3 Conversion from Natural Numbers (30 marks) Assignment Writing Service

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. Assignment Writing Service

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. Assignment Writing Service

primrec from-nat :: nat bnat where from-nat 0 = [] Assignment Writing Service

| from-nat (Suc n) = succ (from-nat n) Assignment Writing Service


(a)  Prove that from-nat generates a well-formed bnat number. (2 marks) Assignment Writing Service

(b)  Prove that to-nat is the inverse of from-nat. (2 marks) Assignment Writing Service

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

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

Assignment Writing Service

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


Addition and Multiplication (18 marks) Assignment Writing Service

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. Assignment Writing Service

primrec simple-add-worker :: nat bnat bnat bnat where Assignment Writing Service

simple-add-worker 0 i j = []
| simple-add-worker (Suc fuel) i j = Assignment Writing Service

(if i = [] then j
else simple-add-worker fuel
(pred i) (succ j)) Assignment Writing Service

definition simple-add :: bnat bnat bnat
where simple-add i j = simple-add-worker (to-nat i + 2) i j Assignment Writing Service

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

  2. (b)  Prove the correctness of simple-add-worker. (3 marks) Assignment Writing Service

  3. (c)  Prove the correctness of simple-add. (2 marks) Assignment Writing Service

Now, we can define a multiplication function, using a similar auxiliary function that uses sim- ple-add. Assignment Writing Service

(d) Complete the definition of simple-mul-worker using simple-add. (3 marks) (e) Complete the definition of simple-mul. (2 marks) Assignment Writing Service

(f) Prove the correctness of simple-mul-worker. (3 marks) (g) Prove the correctness of simple-mul. (2 marks) Assignment Writing Service

5 Hints Assignment Writing Service

• 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. Assignment Writing Service

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

                           apply (induct rule: wf.induct)

which allows you to specify which variables should not be all-eliminated using e.g. Assignment Writing Service

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

Assignment Writing Service

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

meta_spec. Assignment Writing Service

联系辅导老师!
私密保护
WeChat 微信
UNSW代写,COMP4161代写,Advanced Topics in Software Verification代写,Isabelle代写,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编程代写,UNSW作业答案,COMP4161作业答案,Advanced Topics in Software Verification作业答案,Isabelle作业答案,