代做COMP1600/6260 Foundations of Computing: Week 3代写R编程

COMP1600/6260 Foundations of Computing: Week 3

S2 2025

Practical Session 3

• This tutorial will give you further practice at constructing proofs using predicate logic in Dafny.

• Use the Dafny file 03-tutorial-handout.dfy for the Dafny and mini-assignment questions.

• Upload a plain text file called u123456.dfy (replace by your uID) to Wattle, you might lose (some) marks otherwise.

• Submit the exercises marked MA by Wednesday 13th August, 2025, 09:00 via Wattle. Late submis-sions will score 0.

Exercise 1            Interpretation of First Order Logic

We only consider variables of type Z, one unary relation E(x), and a binary relation D(x, y), where

E(X) ⇐⇒ x is even;

D(x, y) ⇐⇒ x is divisible by y.

For each of the following formulae, determine the free variables, and determine precisely for which values of x and y the formulae are true. Justify your answer briefly (in a single sentence).

(a) D(x, x)

(b) ∃x.D(x, x)

(c) ∀x.∃y.D(x, y)

(d) ∃y.E(y) ∧ D(x, y)

(e) ∀y.¬E(y) → D(y, x)

(f) ∀y.E(y) ∧ D(x, y)

Exercise 2             Formalisation

For each of the formulae A(x, y) above, define a predicate P(x, y) such that P(x, y) holds when x and y satisfy the constraint you have found in the previous exercise, and verify your answer using Dafny.

For example, for Part (a), you can do this using the following stub:

ghost predicate E(x: int) { exists k: int :: x == 2 * k }

ghost predicate D(x: int, y: int) { exists k: int :: x == y * k }

ghost predicate spec_2a (x: int, y: int) { D(x, x) } // given

ghost predicate true_2a (x: int, y: int) { /* your answer here */ }

lemma tut2_ex2a ()

ensures forall x: int, y : int :: spec_2a(x, y) <==> true_2a(x, y)

{ /* your proof here if not automatic */ }

Hint. For part (c) you might want to introduce a new predicate, say Q(x) = ∃y.D(x, y) (i.e. a ghost predicate in Dafny) and reformulate part (c) as ∀x.Q(x). This is related to the use of triggers for quantifiers in Dafny.

Dafny Hacks. The following might make your (coding) life easier:

• You can write A <==> B for (A ==> B) && (B ==> A). To prove a statement of the form. A <==> B one proves both directions separately, i.e. if (A) { ... } and if (B) { ... } where the . . . are a proof of B (using the assumption A), or a proof of A using the assumption B, respectively.

• We can abbreviate sequences of the same quantifier type. That means we can write forall x1 : T1, x2 : T2 :: P instead of the more lengthy forall x1: T1 :: forall x2 : T2 :: P.

• To prove sequences of quantifiers of the same type, say for example forall x1 : T1, x2 : T2 :: P, we can also introduce new variables using the shorthand notation forall x1 : T1, x2 : T2 ensures P(x1, x2) { ... } rather than using two forall ... ensures blocks.

Exercise 3                  Proof in Dafny

If R is a binary predicate, then (∀x.∃y.R(x, y)) ∨ (¬∀x.R(x, x)) is valid in predicate logic if the variables range over a non-empty type. Give a brief informal argument of why the statement holds, and give a proof of this in Dafny by completing the following code snippet. (The declaration type T(0) tells Dafny that the type T is non-empty.)

type T(0)

ghost predicate R(x: T, y: T)

/* your informal argument */

lemma tut2_ex3()

ensures (forall x :: exists y :: R(x, y)) || ! forall x :: R(x, x) {

/* your code here */

}

Exercise 4                  Knights and Knaves with Treasure           (MA, 5+5 credits)

On an island there are two kinds of people: knights, who always tell the truth, and knaves, who always lie. We encounter two people, a and b. We do not know whether they are knights or knaves. They may be both knaves, both knights, or one knave and one knight. There are two boxes, b1 and b2. There is a treasure in one of the boxes. The treasure must be in one of the boxes and cannot be in both.

The people make the following statements:

• a says: If Person b is a knave, then the treasure is in Box 1.

• b says: Person a is a knight.

• b makes a second statement: The treasure is in Box 2.

Part (i): Your first task is to work out what types A and B are (Knight/Knave) and which box the treasure is in, using Dafny. Extend the following Dafny code snippet and insert logical assertions that express the situation above. Use this to determine the solution to the problem.

Note: When modelling what Person b said, use two separate statements. If you join them with a conjunc-tion, it will not be modelled correctly. Also note that you will need to introduce more types/variables to model the boxes and whether or not they contain the treasure. If you are stuck, make sure to watch the lectures in Weeks 2 and 3, where several similar examples were shown.

type Person(0)

ghost predicate Knight (x: Person)

ghost predicate Knave (x: Person)

ghost predicate Says (x : Person)

const a : Person const b : Person

lemma treasure()

requires /* Your assertions go here. */

requires /* There may be possibly more than one. */

ensures /* Your solution goes here. */

{ /* your proof if not automatic */ }

Part (ii): Your second task is to write out the proof steps that would be required to solve this problem, if Dafny had not automatically proved it. Write out the steps as assertions in the lemma, as shown in the lectures. Use comments to explain what you have attempted to do.




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图