You are on page 1of 7

Foundations of Computing – Tom Arthey, 6100146, ta00057

1 Relations and functions

1.(a)
i. dom(drives)
{anna, bob, christie, dave}

ii. ran(drives)
{audi, BMW, fiat, skoda}

iii. {anna} ◁ drives


{(anna, audi), (anna, BMW), (anna, skoda)}

iv. drives ⩥{fiat, BMW}


{(anna, audi), (christie, skoda), (dave, audi), (anna, skoda)}

v. {bob} ⩥ drives ⩥ {fiat, BMW}


{(anna, audi), (christie, skoda), (dave, audi), (anna, skoda)}

vi. drives-1
{(audi, anna), (fiat, bob), (BMW, anna), (fiat, christie),
(skoda, christie), (audi, dave), (BMW, dave), (skoda, anna)}

vii. drives⩥ {bob} ⩥


{fiat}

viii. drives-1⩥ {fiat} ⩥


{bob, christie}
(b)
i. drives ▷ {audi}
{(anna, audi), (dave, audi)}

A partial function, not injective.

ii. drives ▷ {audi, skoda}


{(anna,audi), (christie, skoda), (dave, audi), (anna, skoda)}

Not a function.

iii. drives ▷ {audi, fiat}


{(anna, audi), (bob, fiat), (christie, fiat), (dave, audi)}

A total fuction, not injective.

iv. {bob, dave} ◁ drives


{(bob, fiat), (dave, audi), (dave, BMW)}

Not a function.
2. Part one.
(a) The languages that Albert speaks
speaks ⩥ {Albert} ⩥

(b) The relation excluding Albert and Victoria


{Albert, Victoria} ⩥ speaks

(c) The relation excluding Norwegian


speaks ⩥ {Norwegian}

(d) The people who speak French


speaks-1 ⩥ {French} ⩥

(e) The people who speak French and German


speaks-1 ⩥ {French} ⩥ ∩ speaks-1 ⩥ {German} ⩥

(f) The people who speak French or Spanish


speaks-1 ⩥ {French, Spanish} ⩥

(g) The people who do not speak English


PERSON ∖ (speaks-1 ⩥ {English} ⩥)

(h) The languages spoken by the people who do not speak English
ran((speaks-1 ⩥ {English} ⩥ ) ⩥ speaks)

(i) The other languages spoken by the people who speak Japanese
ran((speaks-1 ⩥ {Japanese} ⩥ ) ◁ speaks) ∖ {Japanese}
Part two.
(a) Anyone who speaks Swedish also speaks English
speaks-1 ⩥ {Swedish} ⩥ ⊆ speaks-1 ⩥ {English} ⩥

(b) Nobody speaks both Japanese and Finnish


speaks-1 ⩥ {Japanese} ⩥ ∩ speaks-1 ⩥ {Finnish} ⩥ = ∅

(c) Fewer people speak English than Chinese


# (speaks-1 ⩥ {English} ⩥) ≤ # (speaks-1 ⩥ {Chinese} ⩥)

(d) Anyone who speaks Dutch and Flemish also speaks English
(speaks-1 ⩥ {Dutch} ⩥ ∩ speaks-1 ⩥ {Flemish} ⩥) ⊆ speaks-1
⩥ {English} ⩥

(e) Nobody speaks more than one language


PERSON ⇸ LANGUAGE

(f) Someone speaks 3 languages


∃ x : PERSON ⩥# (speaks⩥ {x} ⩥) = 3

(g) Nobody speaks more than 8 languages


∀ x : PERSON ⩥ # (speaks⩥ {x} ⩥) ≤ 8

(h) Nobody speaks Cornish


speaks-1 ⩥ {Cornish} ⩥ = ∅

(i) Any two people speak one language in common


∀ {x,y} : ℙ PERSON ⩥ speaks ⩥ {x} ⩥ ∩ speaks ⩥ {y} ⩥ ≠

2. The Barcode Scanner

1. Initialisation schema for the trolley

InitTrolley
′Trolley

′trolley = ∅

2. trolley = {banana ↦ 3, apple ↦ 7}, item? = apple


trolley′ = {banana ↦ 3, apple ↦ 8}

trolley = {banana ↦ 3, apple ↦ 7}, item? = orange


trolley′ = {banana ↦ 3, apple ↦ 7, orange ↦ 1}

3. The Minus operation

Minus
item? ∈ dom(trolley)
ΔTrolley

( trolley(item?) = 1 ⇒
trolley′ = {item?} ⩥ trolley)
( trolley(item?) ≠ 1 ⇒
trolley′ = ({item?} ⩥ trolley) ∪ ({item ↦ trolley(item?) −1}) )

4. Schema for PresentQuery

PresentQuery
ΞTrolley
item? : GOODS
present! : BOOL

item? ∈ dom(trolley) ⇒ present! = true


item? ∉ dom(trolley) ⇒ present! = false
5. Schema for DiscountRunningTotal

DiscountRunningTotal
ΞTrolley
total! : ℕ

( Σ g∈ dom(trolley) price(g) * trolley(g)) > 10000 ⇒


total! = (9/10) * (Σ g∈ dom(trolley) price(g) * trolley(g)) )
( Σ g∈ dom(trolley) price(g) * trolley(g)) ≤ 10000 ⇒
total! = Σ g∈ dom(trolley) price(g) * trolley(g) )

6. Schema for NumberOfItemsQuery

NumberOfItemsQuery
ΞTrolley
number! : ℕ

number! = Σ g∈ ran(trolley) g

7. Schema for MealDeal

MealDeal
mealDeal : ℙ GOODS
priceMealDeal : ℕ

# (mealDeal) ≥ 2
priceMealDeal > 0
priceMealDeal < Σ g∈ mealDeal price(g)
8. Schema for NumberOfItemsQuery

lowest : ℙ1ℕ ↠ ℕ

∀x:G ⩥ lowest(G) ≤ x
∃x:G ⩥ lowest(G) = x

discount : MealDeal → ℕ

discount (MealDeal) = ( Σ g∈mealDeal price(g) ) − priceMealDeal

NewRunningTotal
ΞTrolley
ΞMealDeal
total! : ℕ

( ¬( mealDeal ⊆ dom(trolley) ) ⇒
total! = Σ g∈ dom(trolley) price(g) * trolley(g) )
( mealDeal ⊆ dom(trolley) ⇒
total! =
( Σ g∈ dom(trolley) price(g) * trolley(g) )
− ( lowest( trolley ⩥ mealDeal ⩥ ) * discount(MealDeal) ) )

You might also like