Introduction to Algebraic Specifications with CafeOBJ

22
Introduction to Algebraic Specifications with CafeOBJ Lecture 2 21/11/2012 National Technical University of Athens

description

Introduction to Algebraic Specifications with CafeOBJ. Lecture 2 21/11/2012 National Technical University of Athens. CafeOBJ Use Cases. TESLA Protocol Sensor Network Encryption Protocol MPEG-2 Encoding Algorithm  Social Networks Semantic Web DRM systems E-Government Systems - PowerPoint PPT Presentation

Transcript of Introduction to Algebraic Specifications with CafeOBJ

Page 1: Introduction to Algebraic Specifications with CafeOBJ

Introduction to Algebraic Specifications with CafeOBJ

Lecture 221/11/2012

National Technical University of Athens

Page 2: Introduction to Algebraic Specifications with CafeOBJ

CafeOBJ Use Cases

• TESLA Protocol• Sensor Network Encryption Protocol• MPEG-2 Encoding Algorithm • Social Networks • Semantic Web• DRM systems• E-Government Systems• Many more…

Page 3: Introduction to Algebraic Specifications with CafeOBJ

CafeOBJ Use Cases

• Take it slow…• Systems:– DATA TYPES– ACTIONS ON DATA TYPES

• Before we go into systems we must start simple:– Study the specification of simple data types

(Natural numbers)– Learn how to verify

Page 4: Introduction to Algebraic Specifications with CafeOBJ

Review of a ModuleModule Name

Signature

equations

operations

Variable definition

Sortmod! NATplus {

[Nat] op 0 : -> Nat op s_ : Nat -> Nat op _+_ : Nat Nat -> Nat

vars M N : Nateq 0 + N = N .eq (s M) + N = s(M + N) .

}

Page 5: Introduction to Algebraic Specifications with CafeOBJ

Example Factorial

• Define Factorial• What data types do we need??• Which operations on these data types ???

Page 6: Introduction to Algebraic Specifications with CafeOBJ

NO SORT DECLARED!!!!We only import the module PNAT:

This allows us to use everything defined in PNAT

Example Factorialmod! FACT {

pr(PNAT)

op _! : Nat -> Natvar X : Natceq X ! = s(0) if X = 0 .eq s(X) ! = s(X) * (X !) .

}

Page 7: Introduction to Algebraic Specifications with CafeOBJ

Example Factorial

• Reduction:open FACT

red 0 ! .red s(s(s(0))) ! .

close

Page 8: Introduction to Algebraic Specifications with CafeOBJ

Proofs – Example Associativity of +

• Data types required??• Operations on these data types???

Page 9: Introduction to Algebraic Specifications with CafeOBJ

Proofs – Example Associativity of +mod! PNAT { [Nat] op 0 : -> Nat op s : Nat -> Nat op _+_ : Nat Nat -> Nat {prec: 30} op _*_ : Nat Nat -> Nat {prec: 29} op _=_ : Nat Nat -> Bool {comm}

vars X Y : Nat -- _+_ eq 0 + Y = Y . eq s(X) + Y = s(X + Y) .

-- _*_ eq 0 * Y = 0 . eq s(X) * Y = Y + (X * Y) . -- _=_ eq (X = X) = true . eq (0 = s(Y)) = false . eq (s(X) = s(Y)) = (X = Y) . }

Page 10: Introduction to Algebraic Specifications with CafeOBJ

Proofs – Example Associativity of +

mod THEOREM-PNAT { pr(PNAT) -- arbitrary values ops x y z : -> Nat .-- Names of Theoremsop th1 : Nat Nat Nat -> Booleq th1(X,Y,Z) = ((X + Y) + Z = X + (Y + Z)) . }

Page 11: Introduction to Algebraic Specifications with CafeOBJ

Proofs – Example Associativity of +-- I. Base case.open THEOREM-PNAT

reduce when X = 0 .close---- II. Induction case.open THEOREM-PNAT-- check

if it holds for X then it should hold for S(X)(th1(X,Y,Z) implies th1(S(X),Y,Z) )

close

Page 12: Introduction to Algebraic Specifications with CafeOBJ

Proofs – Example Commutativity of +

• Previous proof was very easy– Minimum human interaction was required

• Are all proofs this easy??– 99.9% of the cases no.– Example Commutativity of +

Page 13: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• eq th2(X,Y) = (X + Y = Y + X) .• Open THEOREM-PNAT– red th2(0,Y) .close

• CafeOBJ returns : (y = (y + 0)):Bool• Conclude it cannot reduce y + 0 to 0• We must PROVE it

Page 14: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• eq th5(X) = (X + 0 = X) .open THEOREM-PNAT-- check red th5(0) .close---- II. Induction case.open THEOREM-PNAT-- check red th5(x) implies th5(s(x)) .close

Page 15: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• Use the New theorem to prove the base case of th2:open THEOREM-PNAT-- check red th5(y) implies th2(0,y) .close

Page 16: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• Inductive Step:– red th2(x,y) implies th2(s(x),y) .

• CafeOBJ returns:– ((((x + y) = (y + x)) and (s((x + y)) = (y + s(x)))) xor

(((x + y) = (y + x)) xor true)):Bool• No obvious Lemma:– SPLIT THE CASE

Page 17: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• Case Splitting is donned by:– Selecting a part of the returned term– Adding as assumptions that it is equal to true and

false respectively

red th2(x,y) implies th2(s(x),y) .

eq (x + y = y + x) = false .red th2(x,y) implies th2(s(x),y) .

eq (x + y = y + x) = true.red th2(x,y) implies th2(s(x),y) .

Page 18: Introduction to Algebraic Specifications with CafeOBJ

Commutativity of +

• open THEOREM-PNAT-- assumptions eq (x + y = y + x) = false .-- check red th2(x,y) implies th2(s(x),y) .close

Page 19: Introduction to Algebraic Specifications with CafeOBJ

Specification of a STACK

top

pushpop

Page 20: Introduction to Algebraic Specifications with CafeOBJ

Specification of a STACK

mod! STACK (X :: ELEMENT) { [ EmptyStack NonEmptyStack < Stack ] op empty : -> EmptyStack op push : Element Stack -> NonEmptyStack

op pop_ : NonEmptyStack -> Stack -- only applicable to NonEmptyStack op top_ : NonEmptyStack -> Element -- only applicable to NonEmptyStack

eq top push (E:Element, S:Stack) = E . eq pop push (E:Element, S:Stack) = S . }

Page 21: Introduction to Algebraic Specifications with CafeOBJ

Verification of a simple property

• Prove that :– pop(pop(push(E1,(push(E2,S)))) =

pop(pop(push(E2,(push(E1,S))))

Page 22: Introduction to Algebraic Specifications with CafeOBJ

Homework

• Associativity *• Commutativity *