| Modelling Systems
Answers to Additional Exercises on Logic |
Name = token;
People = set of Name;
Lovers = map Name to People
inv m == not ({} in set rng m);
-- p |-> S is in the map, if p loves everybody in S
-- models a binary relation on a set from People
Result :: whom: [People]
flag: bool;
functions
Loves: Name * Name * Lovers -> bool
Loves(p,q,L) == -- p Loves q
p in set dom L and q in set L(p);
SILBE : Lovers * People -> Result
-- short for SomebodyIsLovedByEverybody
-- returns set of people loved by everybody and Boolean flag
SILBE(L,C) ==
let a = dinter {L(x)| x in set dom L}
in let b= (C=dom L) and (a <> {})
in
mk_Result(if b then a else nil, b);
-- Test Data
-- For CC1 (Empty, false) returned; for CC2 (Betty, true)
SILBE1: Lovers * People -> bool
-- an alternative solution involving function Loves
SILBE1(L,C) ==
exists i in set C & forall j in set
C & Loves(j,i,L);
NLE: Lovers * People -> bool
-- short of Nobody Loves Everybody
NLE(L,C) ==
forall i in set C &
exists j in set C &
not Loves(i,j,L);
-- Tests: for (CC1,C) - true, for (CC4,C) false.
TransLove: Lovers -> bool
-- Transitive Love
TransLove(L) ==
let R = dunion rng L in -- those who are loved
let D = dom L in -- those who love
forall i in set D &
forall j in set D & -- potentially loved
by i
Loves(i,j,L) and (j in set D) =>
-- if i loves j AND j loves somebody
exists k in set L(j)
& Loves(i,k,L);
-- Test cases: CC1, no j loves anybody
-- CC2 true, C3 loved by everybody
-- CC3 false: C1 loves C2, C2 loves C1,C3,C5, but C1 does not love
any of them
-- CC4: everybody loves everybody
-- CHALLENGE: Explain the result
values
C1: Name = mk_token("jim");
C2: Name = mk_token("joan");
C3: Name = mk_token("betty");
C4: Name = mk_token("john");
C5: Name = mk_token("phil");
C6: Name = mk_token("barb");
C : People = {C1,C2,C3,C4,C5,C6};
CC1: Lovers
= { C1 |-> {C2, C3}, C4 |-> {C5,C6}};
CC2: Lovers
= { C1 |-> {C2, C3}, C2 |-> {C1,C3,C5}, C3 |->
{C3},
C4 |-> {C3,C6}, C5 |->
{C3,C6}, C6 |-> {C2,C3}};
CC3: Lovers
= { C1 |-> {C2}, C2 |-> {C1,C3,C5}, C3 |-> {C3},
C4 |-> {C3,C6}, C5 |->
{C3,C6}, C6 |-> {C2,C3}};
CC4: Lovers = { x |-> C | x in set C};
| John.Fitzgerald@ncl.ac.uk | 6 June 1999 ... |