Logic in compuer science Logic plays an imporan role in compuer science Logic is ofen called he calculus of compuer science Logic plays a similar role in compuer science o ha played by calculus in he physical sciences and radiional engineering disciplines (M. Vardi, 2007) Logic 1
Mah vs Compuer science Mahemaicians define synax and semanics of languages sudy heir expressive power and properies Compuer scieniss apply logic combine foundaional research on logic wih applicaions Logic 2
Logic There is a variey of logic languages, depending on he level of absracion w.r.. naural language Some examples: Proposiional Logic FOL Descripion Logic Temporal Logic Logic 3
Applicaions Logic is a universal formalism I can be applied in a variey of conexs: archiecure (logic gaes) sofware engineering (specificaion and verificaion) programming languages (semanics, logic programming) daabases (relaional calculus, Daalog) arificial inelligence (auomaic heorem proving) heory of compuaion (general noion of complexiy) Logic 4
Basic applicaions Proposiional logic is used in compuer science for circui design FOL (more powerful) is used in program verificaion and in arificial inelligence Some examples: definiion of languages saing program properies specifying sysem (iming) properies Logic 5
Logic 6 Noaion In mah Funcions are denoed wih funcional leers Predicaes are denoed wih predicaive leers In compuer science generally predicaes and funcions are denoed wih meaningful names Variables in programming languages
Logic 7 Languages Languages are ses of srings on an alphabe The se conains all he srings ha are par of he language Examples: {a,b} The language of srings on wih he same number n n of a s and b s and all he a s firs is he se { a b n The language of he srings on wih he same number of a s and b s, wih all he a s firs and wih a n n leas an a is he se * {a,b} { a b n 1} * 0}
In logic FOL can help in describing a language Ses can be seen as abbreviaions of FOL formulae Some problems: Wha should we describe? How can we define he differen pars? Wha can we ake as a primiive? Logic 8
Logic 9 Example n n Consider he se { a b n 1} The se abbreviaes he formula x ( x L n ( n 1 x = a Predicaes:,, = Funcions: concaenaion and power n Wha is x? n x (( n = 0 x n = ε) ( n > 0 n x.b n n n = )) x n 1. x))
Remarks We need o define all he non-elemenary predicaes and funcions The following are assumed o be already defined: Equaliies Inequaliies Se membership ( ) The basic arihmeic operaions (+, -, *, :) Basic sring operaions (concaenaion) Logic 10
Logic 11 L 1 = a * b * L 1 is he language of he srings on {a, b} wih all he a s firs More precisely, a sring is in L 1 if i is he empy sring, or i is decomposed in he prefix a and he suffix y (also belonging o L 1 ) i is decomposed in he prefix y (also belonging o L 1 ) and he suffix b This can be expressed as x(x L1 (x = ε) y (x = ay y L 1) y (x = yb y L 1))
Logic 12 L 2 = a * b * c * (1) L 2 is he language of he srings on {a, b, c} wih all he a s firs, hen all he b s and a he end all he c s L 2 can be seen as a * b *. b * c * a * b * is L 1 b * c * has he same srucure as L 1 (le s call i L 3 ) A sring belongs o L 2 if I is in L 1 or I is in L 3 or i can be decomposed in he prefix a and he suffix y (ha belongs o L 2 or o L 3 ) or i can be decomposed in he prefix y (ha belongs o L 1 or o L 2 ) and he suffix c
Logic 13 L 2 = a * b * c * (2) In FOL: x(x L 2 (x L 1 ) (x L y ((x = ay 3 ) (y L 2 y L 3 )) (x = yc (y L 2 y L 1 )))) x is in L 1 x is in L 3 I can be decomposed in he prefix a and he suffix y (ha belongs o L2 or o L3) i can be decomposed in he prefix y (ha belongs o L1 or o L2) and he suffix c do we need all of hese?
Noes and addiional remarks A magic formula for obaining FOL descripions of ses does no exis Some remarks When he order among leers in a language is imporan, he FOL formula defines he language by decomposing i recursive definiion When we need o coun he leers an addiional funcion can be defined Logic 14
Example L 4 ={x {a,b}* he number of a s is equal o he number of b s} #(x, a) is of ariy 2 and couns he number of occurrences of he symbol a in he sring x I can be defined formally as In FOL x y((x = ε #(x,a) = 0) (x = a.y #(x,a) = #(y,a) +1) (x = b.y #(x,a) = #(y,a))) x y((x = ε #(x,b) = 0) (x = b.y #(x,b) = #(y,b) +1) (x = a.y #(x,b) = #(y,b))) The definiion depends on he alphabe x(x L4 #(x,a) = #(x, b)) Logic 15
Precondiions and poscondiions When a funcion is programmed, i is imporan o precisely define wha i accomplishes, wihou necessarily describing how i works Precondiions and poscondiions serve his purpose The precondiion saemen indicaes wha mus be rue before he funcion is called The poscondiion saemen indicaes wha should be rue when he funcion finishes is work Logic 16
Logic 17 General srucure (Hoare s noaion) {Precondiion: Pre} Program or program fragmen: P {Poscondiion: Pos} The precondiion is verified before he execuion of P, while he poscondiion is checked afer i P mus be such ha if Pre holds before he execuion, hen Pos holds afer is execuion
How o define hem? Precondiions and poscondiions can be defined in differen ways Naural language Asserion languages Ad-hoc languages FOL can be used for his purpose Logic 18
Search algorihm (1) Le P be a program implemening he search of an elemen x in an ordered array a of n elemens Precondiion: he array is ordered Poscondiion: he found logical variable mus be rue if and only if he elemen x exiss in he array a Remark: P does no necessarily implemen a binary search algorihm Bu he precondiion is needed if i is he case Logic 19
Search algorihm (2) The precondiion can be formalized as { i(1 i n -1 a[i] a[i + 1])} The poscondiion is So he overall srucure is {found i(1 i n a[i] = x)} { i(1 P i n -1 {found i(1 i a[i] a[i + 1])} n a[i] = x)} Noe ha he elemens of an array are denoed wih he usual programming noaion Logic 20
Soring (1) Le ORD be a program ha sors an array a of n elemens ha does no conain repeiions Precondiion: he array does no conain repeiions Poscondiion: he obained array is ordered (if an elemen x precedes an elemen y in he array, hen i is smaller) Formally: { i, j(1 i n 1 j n i j a[ i] = a[ j])} ORD { i(1 i n 1 a[ i] a[ i + 1])} Logic 21
Soring (2) Is he specificaion adequae? Consider he following example: a before he execuion of ORD is [7 6 2 4 22] a afer he execuion of ORD is [2 6 22] I saisfies he poscondiion! The poscondiion has o sae ha all and only he elemens in he array o be sored are conained in he sored array Logic 22
Logic 23 Soring (3) An array b (no used in ORD) is used o refer o he array a before he execuion I is necessary o add in he precondiion ha b is exacly like he array a The soluion becomes ])))} [ ] [ ( ) ((1 (1 ]))) [ ] [ ( ) ((1 (1 1]) [ ] [ (1 { ])} [ ] [ (1 ]) [ ] [ 1 (1, { j b i a n i i n j j j b i a n j j n i i i a i a n i i ORD i b i a n i i j a i a j i n j n i j i = = + < = =
Remarks A specificaion mus be considered as a conrac I mus conain all he informaion No a priori assumpion When some condiion is eliminaed from he precondiion, he specificaion can become unsaisfacory Logic 24
Wha is a specificaion? A formal specificaion is a mahemaical descripion of he sysem I describes wha he sysem should do No (necessarily) how he sysem should do i There are many differen specificaion languages Logic 25
Logic 26 Logic for specificaion FOL can be used as specificaion language Logical approach Logical specificaion languages give a descripion of he sysem behavior Conjuncion of formulae = se of requiremens They say wha o do, no how FOL can be used also for iming requiremens
Logic 27 Example Lamp behavior: If I push he buon, he ligh goes on wihin Δ ime unis We need he predicaes: P_B(): push he buon a ime L_On(): ligh is on a ime FOL for he requiremen (P_B() 1(( 1 + Δ) L_On(1 ))) Is i correc?
Logic 28 Remarks A specificaion can be correc bu no realisic I is necessary o give all he needed consrains Formulae describe a se of behaviors, i.e., he behaviors ha saisfy hem (PB() 1 (( 1 + Δ) L_On( 1 ))) The ligh can be on even if no one presses he buon
Logic 29 Refinemen of he lamp specificaion (1) Skech of behavior L_On L_Off L_Off P_B() +k If he buon is pushed he ligh is on for k ime unis and is off he res of he ime L_Off(): he ligh is off ( (L_On() L_Off()))
Logic 30 Refinemen of he lamp specificaion (2) Formally (P_B() 1 (( 2 1 < (( + k + k) 2 ) L_On( L_Off( 2 1 )) )) Bu wha happens if he buon is pushed when he ligh is on? Can he ligh be on wihou pushing he buon? Wha if he buon is pushed wice?
Logic 31 Correc (?) specificaion of he lamp P_B() L_On L_Off L_Off +k )) ( _ )) ( _ ) (( ) ( _ (, )) ( _ )) ( _ ) (( )) ( _ ) ( _ (( 4 5 4 5 3 5 3 4 3 1 1 1 Off L B P Off L k Off L On L k Off L B P + + <
Logic 32 Problems in specifying sysems Known problems in AI The frame problem: How o express a dynamical domain in logic wihou explicily specifying wha condiions are NOT affeced by an acion Even exremely simple siuaions may require complex formalizaions Several calculi developed o address his issue: Fluen calculus, even calculus, siuaion calculus, We won delve ino hese issues in his course
Logic 33 How o specify a sysem Logic can be used o describe sysem requiremens For a sysem ha has o be analyzed, we need o idenify: Needed predicaes Relaionships among hem Temporal domain I is imporan o sae NOT only he sufficien condiions, bu also he necessary ones
Temporal domain There are differen dimensions ha need o be aken ino accoun The main difference is beween Discree domains Naural or ineger numbers Dense domains Raional numbers Coninuous domains Real numbers Logic 34
Logic 35 Wha are he predicaes? In he lamp example we had: P_B() L_On() L_Off() In general, predicaes used in a sysem specificaion can be divided ino: Predicaes ha represen a sae of he sysem (L_On(), L_Off()) Predicaes ha represen an even (P_B())
Sae A sae represens a snapsho of he sysem I holds over ime I is no insananeous (if ime is a dense domain) We can inroduce abbreviaed noaions for common siuaions Logic 36
Logic 37 Abbreviaions Up_o_now_S() δ(δ> 0 ( 1( δ< 1 < ) S(1))) I does no sae ha S holds a he presen ime From_now_on_S() δ(δ> 0 ( 1( 1 < + δ) S(1))) The presen is included
Logic 38 Even An even is an insananeous occurrence ha resuls in a changed sysem sae E The following axiom can be used o sae his behavior (abbreviaed wih Even_E): (E() δ(δ> 0 ( 1 ( δ< 1 < < 1 < + δ) E(1))))
Logic 39 Example (1) Consider he lamp example L_On L_Off L_Off P_B() +k We use abbreviaions o make he specificaion clearer
Logic 40 Example (2) ) L_Off( ))) P_B( ) (( ) (L_Off(, k)) L_Off( )) L_On( k) (( Up_o_now_L_Off()) ((P_B() _ )) ( _ ) ( _ ( 4 5 4 5 3 5 3 4 3 1 1 1 + + < B Even_P Off L On L
Wha can a specificaion be used for? A specificaion can be checked hrough formal verificaion I shows ha a candidae sysem design is correc wih respec o he specificaion. Remark: i does no prove ha he sysem is correc, bu i highlighs errors in candidae sysem designs Logic 41
Logic 42 A case sudy: Simplified railroad crossing There is jus a one-way rack When he rain is crossing I, he bar has o be closed: Train behavior Gae conroller
Logic 43 Daa of he problem Ar En Ex R has lengh d I has lengh x Ar and En are he endpoins of R En and Ex are he endpoins of I
Logic 44 Train Kinemaics (1) A rain can vary is speed in he inerval [V min,v max ] The ime aken o cross he crossing region varies consequenly (δ (δ (δ 1 3 = = min d V x V min min = d V ) (δ ) (δ max 2 4 ) (δ d + x = ) Vmax = x ) V max max d + x = ) V min
Logic 45 Train Kinemaics (2) If he rain eners he region R, i exis i a leas δ min and a mos δ 1 ime unis afer, and i exis l a leas δ 2 and a mos δ max ime unis afer (Ar() 1 (En(1) ( + δmin 1 + δ 1)) 2(Ex(2) ( + δ2 2 + δ If he rain exis he region R, i enered i a leas δ min and a mos δ 1 insans before, and if i exis l, i enered region R a leas δ 2 ime insans before max ))) and a mos δ max (En() (Ex() 1 1 (Ar( (Ar( 1 1 ) ( δ 1 ) ( δ max 1 1 δ min δ ))) 2 )))
Logic 46 Train Kinemaics (3) The rain is wihin he crossing region a ime if i enered R before and afer ha momen i didn exi l (In() (En(1 ) (1 ) 2(Ex( 2) (1 2 1 )))) Abbreviaions for recurring emporal consrucions could be inroduced
The gae conroller When he rain eners R i akes a leas δ min o ener l and a mos δ 1 o exi i: in his inerval he bar has o be closed (Ar() 1(( + δmin 1 + δmax) Down( 1))) The bar is closed only if a rain enered he region (no necessarily l) ( Down( ) 1(( δmax 1 δmin ) Ar( 1))) Up is he opposie of down ( Down( ) Up( )) Logic 47
Logic 48 Propery We wan (In() Down()) o hold in he sysem we specified The safey requiremen should be proved (derived) as a heorem from he formalizaion (he whole specificaion)