Title: Schemas in Z
1Schemas in Z
- Chapter 6 in Potter Sinclair and Till
2Schemas
- Zs main representational mechanism of structure
and functions. - Example
- initialStock Stock
- initialStock lt maxlines
- ? i ITEM n N (i,n)einitalStock /\ nlt100
- stockItem P ITEM
- stockItems dom initialStock
3Schemas Describing Abstract States
Account Opened
Regular Withdrawal
AuthorizedWithdrawal
Account in Good Standing
AccountOverdrawn
Overdraft Paid Off
Deposit
Account closed
4Schemas Describing Abstract States
- Example 1
- BankAccount 1
- total, minBalance R
- total gt minBalance
- Example 2
- BankAccount 2
- total, minBalance R
- inGoodStanding BOOLEAN
- inGoodStanding ?(total gt minBalance)
-
5Schemas Describing Operations
- BankAccount with Operations
- total, minBalance R
- withdraw R BOOL
- overDraft R x BOOL R
- total gt minBalance
- ?x?Rout!Bool. total x? lt minBalance gt
out! withdraw(x?)/\out! TRUE /\ total
total-x - \/total x? gt minBalance gt out! FALSE /\
- out! withdraw(x?)/\ total total
6Schema Calculus Operations on Schemas
- Schemas can be built from other schemas.
- Schema calculus has some operations on schemas to
build new schemas from the old. - Operations
- Inclusion
- Decoration
- Disjunction
- Conjunction
- Negation
- composition
7Inclusion - 1
- BankAccount with Operations
- Bank Account 1
- Bank Overdraft
- Where Bank Overdraft can be defined separately
-
8Inclusion - 2
- Bank Regular Withdraw
- total, minBalance R
- withdraw R BOOL
- overDraft R x BOOL R
- total gt minBalance
- ?x?Rout!Bool. total x? lt minBalance gt
out! withdraw(x)/\out! TRUE /\ - minBalance minBalance-x?
- \/total x? gt minBalance gt out! FALSE /\
- out! withdraw(x?)/\ total total
9Decoration
- Adding ?, ! And to variables is referred to as
decoration. - Can use for schemas themselves.
- Example
- Double Min Balance
- Bank Account, Bank Account
- doubleMinBalance R R
- monBalance 2minBalance
10Full Expansion of a Decorated Schema
- Replace primed entities by their definitions
- Double Min Balance
- total, total, minBalance, minBalance R
- doubleMinBalance R R
- total gt minBalance /\ total gt minBalance
11Schema Disjunction
- Want to specify define two cases separately and
state that they are two cases of the same
thing. - Example
- Bank WithdrawCash Bank RegularWithdraw \/ Bank
Overdraft - Bank Regular Withdraw defined earlier
12Schema Disjunction - 2
- Bank Overdraft
- Bank Account 1, Bank Account 1
- overdraft R x BOOL BOOL
- x? R, approve? BOOL
- ?x?Rout!Bool. approve? gt (out! /\
totaltotal-x?) - total total-x?
- \/not approve? gt (not out! /\ totaltotal)
-
13Schema Conjunction
- When one schema can be written specified as two
specifications - Results in taking the conjunction of both
constraints - Example
- Bank Overdraft sendMessage/\adjustBalance
14Schema Conjunction - 2
- send Message
- Bank Account 1, Bank Account 1
- overdraft R x BOOL BOOL
- x? R, approve? BOOL
- ?x?Rout!Bool. approve? gt out!
/\totaltotal-x?) - \/? approve? gt (? out! /\ totaltotal)
-
15Schema Conjunction - 3
- Adjust Total
- Bank Account 1, Bank Account 1
- overdraft R x BOOL BOOL
- x? R, approve? BOOL
- ?x?Rout!Bool. approve? gt out! /\ total
ttotal-x? - \/? approve? gt (? out! /\ totaltotal)
-
16Schema Negation
- Example 1
- BankAccount 1
- total, minBalance R
- total gt minBalance
- Bad bank Account ? Bank Account 1
- Bad BankAccount
- total, minBalance R
- ?(total gt minBalance)
-
17Schema Hiding Operations
- Used to hide some variables and declarations of
already known schemas. - Use ? to hide variables.
- Format
- Decs Pred \ Hidden variables
- Reduced Decs ? Hidden Decs . Predicate
18Schemas Hiding Example -1
- BankAccount with Operations
- total, minBalance R
- withdraw R BOOL
- overDraft R x BOOL R
- total gt minBalance
- ?x?R ?app?out!Bool. total x? lt
minBalancegtout! overDraft x?, app? /\out! /\ - total total-x
- \/total x? lt minBalance gt out! FALSE /\
- out! overDraft(x?,app?)/\ totaltotal
19Schemas Hiding Example -2
- BankAccount with Operations
- total, minBalance R
- withdraw R BOOL
- overDraft R x BOOL R
- total gt minBalance
- ?x?R ? app?out!Bool. total x? lt
minBalancegtout! overDraft x?, app? /\out! /\ - total total-x
- \/total x? lt minBalance gt out! FALSE /\
- out! overDraft(x?,app?)/\ totaltotal
20Schema Composition
- Recall that function composition
- If f S1 -gt S2 and g S2 -gt S3 then
- fog S1 -gt S3
- Schema Composition imports this idea to schemas.
- Example Consider overdrawn bank account
- To close account
- Must pay up to minimum balance
- Then close account from account in good standing
21Schema Composition Method
- Replace primed variables ()in first schema with
double primed variables () - S/
- Replace un-primed variables in second schema with
double primed variables () - T/
- Existentially quantify variables in double primed
state () - ? State . S//\T/
22Schema Composition Example -1
- toGoodStanding
- D Bank Account 1
- toGoodStanding R -gt NULL
- total lt minBalance
- ? x?R ( xtotal gt minBalance) gt
- total total x?
-
23Schema Composition Example -2
- closeInGoodStanding
- D Bank Account 1
- close BOOL -gt R
- total gt minBalance
- ? x?BOOL, return!R ( totalgtminBalance) gt
return!total /\ total0 -
- closeFromDefault toGoodStanding o
- closeFromGoodStanding
24Schema Composition Example -3
- closeFromDefault
- D Bank Account 1
- close BOOL -gt R
- ? Bank Account
- ? x? R toGoodStanding x /\ close true return
-
25Schema Preconditions
- Applicable only to schemas representing
operations - Pre Op ? State Out! . Op
- Means that the schema should be applied to those
state that result in the given predicate
satisfies in the post state. - Here Op is the operation
26Schema Precondition Example
- Simple
- x,y N
- x lt y
- NonEndPoint
- D Simple Z!N
- x lt x lt z! lt y lt y
27Constructing pre NonEndPoint
- preNonEndPoint
- Simple
- ? x,y,z! N x lt x lt z lt y lt y
- Can simplify the statement
- ? x,y,z! N x lt x lt z lt y lt y to
- x3 lt y
28Schema Types
- A way to record data types by specifying filed
and their types of a structure. - Example
- Book
- title, author, isbn, publisher String
- Pubdate, pages Z
- slength title lt 80, slength author lt 80,
- (checksum isbn) mode 11 0
- O lt pages /\ 1800 lt pubdate lt 2050