390 likes | 504 Views
Propositions have truth-values. Variables have types. Sets have elements. Schema assert truths. What do ∆ and Ξ mean?. ´ ´. 3. Combining schema This is so easy. Suppose we have schema A , B , B ′ :. B ′. A. B. a :. a , b. : Z. B :. Z. PZ. = 42. = b +2. 42 ∈ B. a. a. < 10. b.
E N D
Propositionshavetruth-values. Variableshavetypes. Setshaveelements. Schemaasserttruths.Whatdo∆andΞmean? ´´ 3
Combiningschema Thisissoeasy.SupposewehaveschemaA,B,B′: B′ A B a: a,b :Z B: Z PZ =42 =b+2 42∈B a a <10 b =A∧BfortheschemawhichassertsAandB! WriteAandB AandB a,b:Z =42∧(a=(b+2)∧b<10) a 4
Combiningschema Ofcourse!AandBestablishauniverseofdiscourseandassertsome propertiesofit.SoAandBjustcombinesthoseuniversesandthose truths. It’sjustlikeinphysicsclass,wherewemaydescribethebehaviourofa particlebya4-dimensionalvectorspaceplusitsequationsofmotion(3 dimensionsspace,1dimensiontime). Ifwewanttodescribethebehaviouroftwoparticles,why,wejustusea 7-dimensionalvectorspaceplusthetwoequationsofmotion. 5
Combiningschema Butwhystopat∧?Wehave∨,⇒,and⇔.Thepatternisalwaysthe same:combinetheuniversesofdiscourse(that’sthevariablesdeclared intheschema)andthepredicates. WriteAimpliesB′ =A⇒B′for AimpliesB′ a:Z,B:PZ =42⇒(42∈B) a =A∨B. TryAorB Thepossibilitiesareendless!!!!! 6
Recall:ClubState ClubState badminton:PSTUDENT hall:PSTUDENT hall⊆badminton #hall≤maxplayers Thissays: 7
ClubState •badmintonisasetofstudents(I’dbeguessing:thestudentsthat playbadminton). •hallisasetofstudents(thestudentsinthebadmintonhall,which hasacapacityof20?). •Studentsinthehallmustplaybadminton(sothey’veobviouslygota manonthedoorchecking?). •...andyoucan’thavemorepeopleinthehallthanitscapacity. 8
Recall:AddMember AddMember :PSTUDENT, :PSTUDENT badminton hall badminton′:PSTUDENT, newmember?:STUDENT hall′:PSTUDENT ⊆badminton #hall≤maxplayers hall hall′⊆badminton′ #hall′≤maxplayers′ newmember?∈badminton badminton′=badminton∪{newmember?} hall′=hall 9
Recall:AddMember Ormoresuccinctly: AddMember ∆ClubState newmember? :STUDENT newmember?∈badminton badminton′=badminton∪{newmember?} hall′=hall 10
Parentheticnote:Renaming Whatifyouwanttorenamevariablesinaschema? S[x/a,y/b,z/c]representsSwitharenamedtox—youcanguess therest. ClubState FootyClub :PSTUDENT :PSTUDENT badminton football :PSTUDENT pitch:PSTUDENT hall ⊆badminton ⊆football hall pitch #hall≤maxplayers #pitch≤maxplayers 11
Recall:AddMember Sowecanwrite ∆ClubState as ∧ClubState[hall′/hall,badminton′/badminton]. ClubState 12
RefiningAddMember Remember:hallisjustthestudentsinthebadmintonclubinthehall— hall⊆badmintonhintsatthat. Theremayindeedbeotherpeopleinthehall.What,youthoughtthat badmintonissopopularinthisschoolthatitcanfillanentiresportshall? Thisspecificationhastobeatleastalittlebitrealistic. Therearetherowersinthecornerontheirmachines,thehockey players,therock-climbers,maybeevenabitofping-pong. Ifoneofthesenon-badminton-playersseestheemptyfutilityoftheir non-badminton-playerways,theymayjointhebadmintonclub.This epiphanymightarriveatanytime;whilethey’reinthehall,orevenjust whilethey’reoutsidethehall,perhapsstudyingFormalSpec. 13
RefiningAddMember ::=inside|outside LOCATION AddMemberInHall ∆ClubState AddMemberOutHall ∆ClubState newmember? :STUDENT newmember? :STUDENT where? where? :LOCATION =inside where? where? :LOCATION =outside newmember?∈badminton #hall<maxPlayers ′ {newmember?} ′ newmember?∈badminton ′ {newmember?} ′ badminton=badminton∪ badminton=badminton∪ hall=hall hall=hall∪newMember? 14
RefiningAddMember Then =AddMemberInHall∨AddMemberOutHall. AddMemberAnywhere Ineffect,AddMemberAnywheredescribesaprogramwhichchecks wherethememberis(inside,ouside)anddoestherightthing accordingly. Isn’tthatabitmagic? Thisisacase-split.∨isacase-split.∨onschemaisacase-splitfor schema.∧islikeaparallelexecution. Buttheyarenot.Thereisnonotionofflowofcontrolorexecutionhere. Justspecifications. 15
InitialState What’stheinitialstateofthebadmintonclub(backin1934,probably). Maybesomethinglikethis: InitClubState ClubState′ badminton′ ={} hall′={} It’sjustconventiontouse‘after’(withprime;withdash)statevariablesin initialstate. 16
InitialState TheinitialstatehadbettersatisfytheconditionsforClubState. Thatis,hall′ ⊆badminton′and#hall′≤maxplayers. Solet’scheck{} ⊆{}and0≤maxplayers. 17
Totalisingoperations AddMember ∆ClubState newmember? :STUDENT newmember?∈badminton badminton′=badminton∪{newmember?} hall′=hall Notethepreconditionnewmember?∈badminton.AddMember specifiesaPARTIALfunction.Ifnewmember?∈badmintonthen AddMemberisnotdefined,asafunctiononclubstates. 18
Totalisingoperations Supposenewmember? ∈badmintonandsomebodytriesadding newmember?. Somebodyiseitherstupid,orplayingapracticaljoke...orperhapsthey purchasedlifetimemembership30yearspreviouslyandjustforgot. Perhapstheyarealecturerandtheirbrainhasbeenblastedbythe howlingwindsofathousanddataprojectors. Howdowemakethisspecificationofapartialfunction,intoa specificationofatotalfunction? 19
Totalisingoperations Recalltheno-op: ΞClubState ∆ClubState badminton′=badminton hall′=hall ΞClubState badminton,hall:PSTUDENT badminton′,hall′:PSTUDENT hall⊆badminton,#hall≤maxplayers hall′⊆badminton′,#hall′≤maxplayers′ hall′=hall,badminton′=badminton 20
Totalisingoperations ::=success|isMember SuccessMessage MESSAGE IsMember ΞClubState newMember?:STUDENT outcome!:MESSAGE newMember? ∈badminton outcome! =isMember Totalise: outcome! outcome! :MESSAGE =SUCCESS =(AddMember∧SuccessMessage)∨IsMember. TotalAddMember FormalSpecF22HO2,Lecture5 21
Totalisingoperations Aschemaistotalwhentheoutcomeisspecifiedforallpossibleinputs. ProgramsinCandJavaaretotal;theytakeaninput,giveanoutput. Specificationsoftheseprogramsmaybepartial;wemayonlyspecify partialinformation. Gothroughthepreviousspecs:RemoveMember,EnterHall,LeaveHall, NotInHall.Whichofthesearetotal?Totalisetheonesthatarenot. FormalSpecF22HO2,Lecture5 22
Hiding S\bistheschemaobtainedbyexistentiallyquantifyingbinS.Best explainedbyexample: A HideA B HideB :Z :Z :Z a a,b a ∃a:Z• ∃b:Z• =42 =b+2 a =42 a a b<10 (a=b+2∧ <10) b \a,bandsoon. SimilarlyforS FormalSpecF22HO2,Lecture5 23
Hiding Notethat∃b :Z.•(a=b+2∧b<10)meansthesamethingas a<12. SowecanequivalentlywriteHideBas: HideB a:Z <12 a FormalSpecF22HO2,Lecture5 24
Anotherexampleofhiding =AddMember\newMember?: AddWho ∆ClubState DefineAddWho AddMember ∆ClubState newmember? :STUDENT ∃newmember?:STUDENT• newmember?∈badminton badminton′=badminton∪ {newmember?} hall′=hall (newmember?∈badminton∧ ′ {newmember?}∧ =badminton∪ badminton ′ =hall) hall WhatdoesAddWhodo? FormalSpecF22HO2,Lecture5 25
Calculatingpreconditions =AddMember∧SuccessMessage. SuccessMessage outcome!:MESSAGE DefineSuccessAddMember AddMember ∆ClubState newmember? :STUDENT outcome! =SUCCESS newmember?∈badminton badminton′=badminton∪ {newmember?} hall′=hall Whatdoesthisspecificationsay?Let’ssee... FormalSpecF22HO2,Lecture5 26
Partiallyexpandthedefinition SuccessAddMember ∆ClubState newmember?:STUDENT outcome!:MESSAGE newmember?∈badminton badminton′=badminton∪{newmember?} hall′=hall outcome!=success That’sabitbetter—butnotgoodenough.Wewanttoexpandmore! FormalSpecF22HO2,Lecture5 27
Expandfurther SuccessAddMember ClubState badminton′,hall′:PSTUDENT newmember?:STUDENT outcome!:MESSAGE hall′⊆badminton′ #hall′≤maxPlayers newmember?∈badminton badminton′=badminton∪{newmember?} hall′=hall outcome!=success FormalSpecF22HO2,Lecture5 28
Calculatingpreconditions Recall: badminton′,hall′ badminton′,hall′ :PSTUDENTarethestateafter. :PSTUDENTarethestatebefore. newMember?istheinput. output!istheoutput. That’sjustourreadingofthemeaningofthesevariables,butitisa convention;′forafter,?forinput,!foroutput. IsAddMemberSuccessdefinedforallpossibleinputsandinputstates? FormalSpecF22HO2,Lecture5 29
\{badminton′,hall′,output!} SuccessAddMember preSuccessAddMember ClubState newmember? :STUDENT ∃badminton′,hall′:PSTUDENT;outcome!:MESSAGE• hall′ ⊆badminton′∧#hall′≤maxPlayers ∧newmember?∈badminton ∧badminton′=badminton∪{newmember?} ∧hall′=hall∧outcome!=success Sethall′ =hallanddropoutcome!.∃outcome!:MESSAGE•outcome!=successis trueandwedonotmentionoutcome!elsewhere. FormalSpecF22HO2,Lecture5 30
\{badminton′,hall′,output!},simplified SuccessAddMember preSuccessAddMember ClubState newmember? :STUDENT ∃badminton′• ∧newmember?∈badminton ′ ∧badminton′=badminton∪{newmember?} ∧hall=hall Wedrophall=hallandnotethat#hall≤maxPlayers,whichwasa conditiononhall′,isnowsomethingthat’salreadyinClubState. FormalSpecF22HO2,Lecture5 31
\{badminton′,hall′,output!},simplifiedmore SuccessAddMember preSuccessAddMember ClubState newmember? :STUDENT ∃badminton′• ′ ∧badminton′=badminton∪{newmember?} hall⊆badmintonbyClubStateand badminton′=badminton∪{newmember?},sohall guaranteed. FormalSpecF22HO2,Lecture5 ⊆badminton′is 32
\{badminton′,hall′,output!},simplifiedeven SuccessAddMember more preSuccessAddMember ClubState newmember? :STUDENT ∃badminton′• newmember?∈badminton ∧badminton′=badminton∪{newmember?} ∃badminton′•badminton′=badminton∪{newmember?}isas usefulasabarbershoponthestepsoftheguillotine;cutitoff. FormalSpecF22HO2,Lecture5 33
\{badminton′,hall′,output!},simplifiedridicu- SuccessAddMember lously preSuccessAddMember ClubState newmember?:STUDENT newmember? ∈badminton There’syourprecondition:newmember? ∈badminton. Wefoundabug!!Theprogramfailsifnewmember? FormalSpecF22HO2,Lecture5 ∈badminton. 34
preSuccessAddMember TheoperationdescribedbySuccessAddMemberisnottotal. Asafunctionfromclubstatestoclubstatestakinginputnewmember?, outputisnotdefinedifnewmember?∈badminton. FormalSpecF22HO2,Lecture5 35
Fact Fact.predistributesoverdisjunction: pre(S ∨T)=preS∨preT. SotocheckifTotalAddMemberreallyistotal,itsufficestocalculate preIsMemberandseeifitisnewMember?∈badminton. Let’sdoit:letoursloganbeexpand,hide,simplify. FormalSpecF22HO2,Lecture5 36
Expand,hide,simplify:IsMember IsMember ΞClubState newMember?:STUDENT outcome!:MESSAGE newMember?∈badminton outcome!=isMember FormalSpecF22HO2,Lecture5 IsMember ClubState badminton′,hall′:PSTUDENT newMember?:STUDENT outcome!:MESSAGE hall′⊆badminton′ #hall′≤maxPlayers newMember?∈badminton outcome!=isMember badminton′=badminton hall′=hall 37
Expand,hide,simplify:IsMember preIsMember ClubStatenewMember? :STUDENT ∃badminton′,hall′:PSTUDENT;outcome!:MESSAGE• hall′⊆badminton′ ∧#hall′≤maxPlayers ∧newMember?∈badminton ∧outcome!=isMember ∧badminton′=badminton ∧hall′=hall FormalSpecF22HO2,Lecture5 38
Expand,hide,simplify:IsMember preIsMember ClubState newMember?:STUDENT ∃outcome!:MESSAGE• hall⊆badminton ∧#hall≤maxPlayers ∧newMember?∈badminton ∧outcome!=isMember (Don’trushthis.Onestepatatime.) FormalSpecF22HO2,Lecture5 39
Expand,hide,simplify:IsMember preIsMember ClubStatenewMember? :STUDENT ⊆badminton hall ∧#hall≤maxPlayers ∧newMember?∈badminton FormalSpecF22HO2,Lecture5 40
Expand,hide,simplify:IsMember preIsMember ClubStatenewMember? :STUDENT newMember? ∈badminton That’sit,we’redone.TotalAddMemberistotal. =preSuccessAddMember∨preIsMember =newMember?∈badminton∨newMember?∈badminton =T 41 preTotalAddMember FormalSpecF22HO2,Lecture5