b (13) - pku · b b bbook 12.2 “ ” 12.4 “ ” atelier b 8 architecture b atelier b project...
TRANSCRIPT
���
��� ��� ��� ��� ���:������ B ���������������������������
(13)
���������������������
���
�������������
2010���
������
������ B ������������������������������������������
• �����������• B ������������� B ����������
• ��������������– �������
– �� IMPORTS ������������������
• �������������– ����
– ����������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 1
B ���������������
���� B �����������������������B Book��12.2 � “��” � 12.4 � “�������”��� Atelier B ������8 � Architecture������
�� B �������� Atelier B ���� project���������� components ������������������
�����������������������safety���������������������
�������� B ����������������
���B ������������ B ������������������� B �����B �������������������
�� B �����������������������������������������������������������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 2
B ������������
Atelier B ��������� B ���� 3 ��1) ������������������������2) �����3) �����������
��\�� ����� ���� ����
������ � � �
���� � � �
���� � � �
������������������������������
�������������
�������������
���������������������������� ���������������������
����� ���������������������� ���������������������������������� B ���������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 3
B ������������
�������������
���������������������INCLUDES ��������������������������
����������������
������������������������������� B �� �������������������� �������������������B ���������
���������������������������������������������������� �������������������������������������������������� �������
������� B ����������������������� B ���� B0 ����������� ����� �������������� ��� ���������� �������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 4
B ������������
����� B ���� B �������������������������������������������
����������������
• ����������������������• ��������������������������� ���� �����������
• ���������� �����������• �������������� �������������
�������������
• ������������ INCLUDES �����������
• ������������������������������������� IMPORTS �� ����
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 5
���������������
���������������
• �������������������• ���������������� “.” ������
• ����������������� ����
�����������������������������������������������
�������������������������������������� � ����������
���������������� �������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 6
������������������������
���������������������������
INCLUDES �������
INCLUDES ���� B �� MN���������������Minst�� MN ��� �� Minst ������INCLUDES �����������������������
USES �������
��� B �� MN��������INCLUDES ������� ����� � ���������������� ������ USES �
������USES �������� ��� �����
INCLUDES � USES ��������������������������� INCLUDES � USES
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 7
������������������������
IMPORTS �������
IMPORTS ��� MN ������� M �������������M ��������M ���������������
MN �� M ���������� M ������������� M������ IMPORTS ����������� B ����������������� IMPORTS �����������
B ��� IMPORTS ��� B �������� IMPORTS ��������IMPORTS ������������� �������
��� IMPORTS ��������������������������������������������������� IMPORTS ����
����������������
��� IMPORTS �������� B �����������������������������������������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 8
���������������������������IMPORTS ���
������� IMPORTS ����� �� IMPORTS ��������
����������������� IMPORTS ���������������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 9
������������������������
�� B �������� IMPORTS ���������������������������� ��� IMPORTS ����� ��������������
����� B ��������������������������������� IMPORTS ����������
SEES �������
SEES ������ IMPORTS ����������������� B ������������������������������
��� SEES �������� IMPORTS �����
���� B �� SEES �������������� SEES ���
��� B ������� B ������������ SEES �� IMPORTS
������������ B ������������������ SEES
���SEES ���� SEES ����������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 10
���������������������������������
� SEES ���� ���
���� B �� SEES ������Mi����� SEES ��Mi �
����������
���������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 11
������ IMPORTS ��� USES ���������������
�� �������� B ������������������IMPORTS ����������������������� USES ���
����������
� ������ ����������� M0������� ����� ���� I0
���� I0 �� IMPORTS � �
����� M1, M2 � M3����������������
���M1, M2 � M3 ����������������������������� B ��
I0 �� IMPORTS ����� M1, M2� M3 ��������������������������� M1,M2 � M3 ����������
M0
I0
R0
M2M1 M3
REFINES
IMPORTS
REFINES
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 12
������������
�������� M1, M2� M3����������������� �������� I1,I2 � I3
�� M1 ��� I1 �������� M4 � M5
�� I2 ���� ����
�� I3 ����� M6
M4, M5 � M6 �������� �����������
M6
I6
M2M1 M3
REFINES
IMPORTS
REFINES
I2I1 I3
M5M4
I5I4
���M1, M2, M3, M4, M5, M6 ������������ M6 ������ M3 � I3�� I3 ������� M6�� I6 ��
�������������� B ����������������������������� B ����
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 13
������������������
������������������������
����������� 6 ���������
��������������������������
M 0
I 0
R 0
M 2M 1 M 3
R E F I N E S
I M P O R T S
R E F I N E S
M 6
I 6
R E F I N E S
I M P O R T S
R E F I N E S
I 2I 1 I 3
M 5M 4
I 5I 4
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 14
���������������������������
�������������
����� REFINES �� ���������������/������������
M 0I 0
M 2M 1 M 3
I M P O R T S
M 6I 6
I M P O R T S
I 2I 1 I 3
M 5M 4I 5I 4
�������������������������������
���IMPORTS ������������������������� �����
M 0
M 2M 1 M 3
I M P O R T S
M 6
I M P O R T S
M 5M 4
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 15
���������������������������
�����������������������������
���IMPORTS �������������������������������������� ������
I0
IMPORTS
I6
IMPORTS
I2I1 I3
I5I4
����������������
• ����������������������������• �������������������• ��������������� ������������������ �������������
���������������� ����������������������������� �����
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 16
B ��������������� USES ������
����������� ���������������������������������������������������������������������
���������� ���������� “�” �������������������������������������������
����������������� ������������������������� �������
������������������������ ������������������������������� ��
�������������� �������������������������������������
� B ������� SEES ��������������������� ������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 17
B ��������������� USES ������
�������������������������� I1 �I3 ���� I2 ��������� I2 � SEES ��
I 0
I M P O R T S
I 6
I M P O R T S
I 2I 1 I 3
I 5I 4
��������������� ������������������������ ��������������
����������M1 � M3 ��� M2���������
M2M1 M3
SEES SEES
�� SEES �����������������������M1 � M3 ������� M2 ����� �������
��� �������������� SEES ���� ���
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 18
B ��������������� USES ������
SEES ����������� M1 � M3 ������ M2 ���� ����� SEES ����������
������ M2 ������������ ���������M1 � M3 ������ M2 ������������������������ M2 �� ��
��������������� SEES �������� �� ������� ��������������
��������� M1 � M3 ��������� M2 ������������������ I0 ����� ���� M2 ����������� ������������������������
����� ����� �� ������������������ M2 ����� ��� �� M1 � M3 ������ M2��
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 19
B ��������������� USES ������
� M1 � M3 �� M2 ������������
�� M1 � M3 ������������������������ M2
���������������� I1 � I3 ������M2 ����� I2���� M2 ���� I2 �����
M2M1 M3
SEES SEES
M2R1 R3
M2I1 I3
REFINES
REFINES
�� I1 � I3 ����� M2 ����� I2 �����
����� M2 ���������� M2 ����������������������������� ������
���������� I1 � I3 �������� I2 ������������ M2 �� “���” �����
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 20
SEES ������
������������ B ����� SEES ����
• SEES � ���������������
• SEES ����������������� �� B �� SEES
• �� SEES ��������������� � ���IMPORTS����� IMPORTS ��
• � SEES ����� ��������� ��������� � ����� IMPORTS ���
�� SEES ���������� B �������������������B Book�� 12.2.3 ���� D�����������
• SEES �������������������
• SEES ��������������������� SEES ���
����������������������������
• �����������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 21
SEES ������
SEES �������
• �������� M ��� N�� M ��� M’ ��� N������ M’ ��� SEES ��� N
• SEES ����� ������������� ��� SEES �
�������� �������� SEES ��
� SEES ����� ��� ����
• ��������������������• ����������M �� SEES ��� N�� M ��� M’ �� SEES �� N�� M’ � N �� ��
• ��������� M1 � SEES ��� N������ ���N����������� M ����
������ SEES ��������������������� ��IMPORTS � �������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 22
���������������������
����� “����” �������������������������������� ������
����������������������� ������
����������������������
�������� ����
���� �������������������������� �����
�������������������������
�������������
M 0I 0
M 2M 1 M 3
I M P O R T S
M 6I 6
I M P O R T S
I 2I 1 I 3
M 5M 4I 5I 4
������������������ M5 � M6��������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 23
���������������������
���������� I5-6 ����������
���������� ���������������� ���
M 0I 0
M 2M 1 M 3
I M P O R T S
M 6
I M P O R T S
I 2I 1 I 3
M 5M 4
I 5 - 6I 4
R E F I N E S
Atelier B ������Atelier B ��� Introduction ��
“The B Language Reference Manual describes the B language supported byAtelier B release 3.6. This language is based on the language presented in TheB-Book, however some progresses such as trees, recursivity or multiplerefinements are not currently supported by B language.”
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 24
������������������������
�������� 8 ����� ���������������
MACHINE GoodsSETS GOODSEND
MACHINE PriceSEES GoodsVARIABLES priceINVARIANT price ∈ GOODS → N1
INITIALISATION price :∈ GOODS → N1
OPERATIONS
setprice(item, pr) =PRE item ∈ GOODS ∧ pr ∈ N1 THEN price(item) := pr END;
pr ←− pricequery(item) =PRE item ∈ GOODS THEN pr := price(item) END
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 25
MACHINE Shop SEES Goods, PriceVARIABLES takings INVARIANT takings ∈ N
INITIALISATION takings := 0OPERATIONS
sale(item) =PRE item ∈ GOODS THEN takings := takings + price(item) END;
tt ←− total = tt := takingsEND
MACHINE CustomerSEES Goods, PriceCONSTANTS limitPROPERTIES limit ∈ GOODS → N1
VARIABLES purchasesINVARIANT purchases ∈ P(GOODS)INITIALISATION purchases := {}OPERATIONS
pr ←− buy(item) = PRE item ∈ GOODS ∧ price(item) ≤ limit(item)THEN purchases := purchases ∪ {item} || pr := price(item) END
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 26
Customer ������������������
��� Customer ������� purchases�������������������������������� �������������� Set�
MACHINE Set(ELEM )VARIABLES setINVARIANT set ⊆ ELEMINITIALISATION set := ∅
OPERATIONS
add(elm) =PRE elm ∈ ELEM THEN set := set ∪ {elm} END;
res ←− member(elm) =PRE elm ∈ ELEMTHEN res := bool(elm ∈ set) END;
. . . . . .END
������������� Customer
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 27
Customer ������������������
�� IMPORTS ��� Set�������������� ��������������������� Goods � PriceIMPLEMENTATION Customer ImpREFINES CustomerSEES Goods, PriceIMPORTS Set(GOODS)VALUES limit = λ g . (g ∈ GOODS | 1000)INVARIANT set = purchasesOPERATIONS
pr ←− buy(item) =BEGIN
pr ←− pricequery(item);IF pr ≤ limit(item) THEN add(item) END
END
END
��� λ �������������������������������� limit ���������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 28
���������������������
��������� Fifo �����������
MACHINE Fifo(ELEM, capacity)CONSTRAINTS capacity : NAT1VARIABLES contentsINVARIANT contents : seq(ELEM) & size(contents) <= capacityINITIALISATION contents := []OPERATIONS
input(elm) =PRE elm : ELEM & size(contents) < capacityTHEN contents := contents <- elm END;
elm <-- output =PRE contents /= []THEN elm, contents := first(contents), tail(contents)END
END
���� �� ELEM ��������� ������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 29
���������������������
������������������������������Archive���������������� input �����������������������
MACHINEArchive(ELEM)
VARIABLES read, entriesINVARIANT read : NAT & entries : seq(ELEM)INITIALISATION read, entries := 0, []OPERATIONS
enter(elm) =PRE elm : ELEM THEN entries := entries <- elm END
elm <-- lookup =PRE read : NAT1 & read <= size(entries)THEN elm := entries(read) END;
incread = BEGIN read := read + 1 END;resetread = BEGIN read := 0 END;
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 30
���������������������
��� read ����� ��������� ����������� read ��������
�� Archive ����� Fifo�
IMPLEMENTATION Fifo_Imp(ELEM, capacity)REFINES FifoIMPORTS Archive(ELEM)INVARIANT entries \|/ read = contentsOPERATIONS
input ( elm ) = enter(elm) ;elm <-- output =
BEGINincread;elm <-- lookup
ENDEND
��������� entries�� read �������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 31
���������������������
������������������ �������� ������� ������� Fifo ������
�������������������������������������������������������
��������������� numberQ����������
�� add � remove ������������������������� ok ���������� ������� failed ������������������� ����������� �� �������
remove ������ ��� ����������� ���������������������� elem0���� ����
���� remove ����������������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 32
MACHINE RobustFifo(ELEM, capacity, elem0) /* elem0 ����� */CONSTRAINTS capacity : NAT1 & capacity < 1000 & elem0 : ELEMSETS REPORT = {ok, failed}VARIABLES queueINVARIANT queue : seq(ELEM) & (size(queue) <= capacity)INITIALISATION queue := []OPERATIONS
rpt <-- add(elm) =PRE elm : ELEMTHEN IF size(queue) < capacity
THEN rpt := ok || queue := queue <- elmELSE rpt := failed END
END;rpt, elm <-- remove =IF size(queue) = 0THEN rpt := failed || elm := elem0ELSE rpt := ok || elm := first(queue) || queue := tail(queue)END
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 33
���������������������
���������������
• ������ �� Fifo ��� �������� ����Counter �������
• �� Fifo ����������������������������� RobustFifo ����������������
������ Counter ����
MACHINE CounterVARIABLES counterINVARIANT counter : NATINITIALISATION counter :: NATOPERATIONS
num <-- number = num := counter;setzero = counter := 0;inc = counter := counter + 1;dec = PRE counter > 0 THEN counter := counter - 1 END
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 34
������������������������������
���� �����������������
IMPLEMENTATION RobustFifo_Imp(ELEM, capacity, elem0)REFINES RobustFifoIMPORTS count.Counter, Fifo(ELEM, capacity)INVARIANT count.counter = size(contents) &
count.counter = size(queue) & contents = queueINITIALISATION count.setzero
�������� Counter ������� PROMOTES � RobustFifo ����������������
����������������� IMPORTS ������
����������������������� IMPORTS ����������������������������������������������������� ��
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 35
OPERATIONSrpt <-- add ( elm ) =VAR num IN
num <-- count.number;IF num < capacityTHEN rpt := ok; input(elm); count.incELSE rpt := failedEND
END ;
rpt , elm <-- remove =VAR num IN
num <-- count.number;IF num > 0THEN rpt := ok; elm <-- output; count.decELSE rpt := failed; elm := elem0 END
ENDEND
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 36
������������������������������
������������ ���� Fifo ���������������� Archive�����������
������������ “����”���� ��� “��” ������������� � �� ��
• � �������������• �� “����” ��������� �����������
��
• ����������� � IMPORTS ������������� �������� VARIABLES ����
• ����� ����������� IMPORTS �������
• ����������� ������������ � IMPORTS
������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 37
������������������������
��� Fifo ���������������
������������������������� �������� �������� �
����������� capacity �����������������������������������
• ������������• ���� � ����• ���������������������� ���������������
�������������
• ����������� �����• ������������� capacity
• ������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 38
������������������������������������
��������� array�������� ��� pos ������������� size ��
������������ contents ��������������������
contents = ((array ↓ (pos − 1)) ^ (array ↑ (pos − 1))) ↑ size
��
• array ↓ (pos − 1) � array �� pos − 1 ���
• array ↓ (pos − 1) �� array �� pos − 1 ���
• �������� size ���������
�� � Fifo ������������� IMPORTS ������� � Fifo ��������
�����������������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 39
���������������
������������
���������������
MACHINE Varray( capacity, VALUE )CONSTRAINTS capacity : NAT1VARIABLES arrayINVARIANT array : 1..capacity --> VALUEINITIALISATION array :: 1..capacity --> VALUEOPERATIONS
set(ind, val) =PRE ind : 1..capacity & val : VALUETHEN array(ind) := val END;
val <-- get(ind) =PRE ind : 1..capacityTHEN val := array(ind) END
END
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 40
���������������
���� ����������������
MACHINE SizeCounter (maximum)CONSTRAINTS maximum : NAT1VARIABLES szeINVARIANT sze : NAT & sze <= maximumINITIALISATION sze:= 0OPERATIONS
sizeinc = PRE sze < maximum THEN sze := sze + 1 END;sizedec = PRE sze > 0 THEN sze := sze - 1 END;ss <-- sizeget = ss := sze
END
����� �����������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 41
���������������
���������������� �
MACHINE PosCounter (maximum)CONSTRAINTS maximum : NAT1VARIABLES posINVARIANT pos : NAT1 & pos <= maximumINITIALISATION pos:= 1OPERATIONS
posinc = pos := (pos mod maximum) + 1;pp <-- posget = pp := pos
END
������ �������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 42
Fifo ������������
������������
IMPLEMENTATION Fifo_Imp2(ELEM, capacity)REFINES FifoIMPORTS Varray(capacity, ELEM), SizeCounter(capacity),
PosCounter(capacity)INVARIANT
sze = size(contents) &((array \|/ (pos - 1)) ^ (array /|\ (pos - 1))) /|\ sze
= contents
���������������������
����� ��� Fifo � IMPORTS ����������������������
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 43
Fifo ������������
������ IMPORTS ��������
OPERATIONSinput(elm) =VAR sz, ps, pp IN
sz <-- sizeget; ps <-- posget;pp := (sz + ps - 1) mod (capacity - 1);set(pp, elm);sizeinc
END;elm <-- output =VAR pp IN
pp <-- posget;elm <-- get(pp);posinc;sizedec
ENDEND
Formal Methods: Software Development in B Qiu Zongyan (June 7, 2010) 44