Formal methods 1 - introduction

14
Formal Methods in Software Lecture 1. Introduction Vlad Patryshev SCU 2014

description

My course of Formal Methods at Santa Clara University, Winter 2014. Automata, Z notation, Pi calculus, Category theory.

Transcript of Formal methods 1 - introduction

Page 1: Formal methods   1 - introduction

Formal Methods in Software

Lecture 1. Introduction

Vlad PatryshevSCU2014

Page 2: Formal methods   1 - introduction

Content of this course

• FSM, deterministic/non-deterministic; (P/NP); regexp; code sample (the one-liner), problem with the code; p!=np

• Stack machine, context-free languages; general languages; Turing machine; some Turing stuff, examples of Turing machine

• Z specification language• Pi calculus and its fate;• Monoid, Group, Groupoid, Category, Monomorphisms, Epimorphisms,

Isomorphisms, show it on sets and monoids; Scala (Java) category• terminal object, initial object; products, unions; show code samples;

equalizers, code samples; notation {x|f(x)==g(x)}; pullbacks, sql• functors examples (diagrams; product; exponentiations); currying/yoneda

lemma; example with integers/rationals; monad?

Page 3: Formal methods   1 - introduction

What you have to know

• first-order logic; quantifiers (see COEN260 slides)

• some set theory; binary relationships, currying; injection/surjection/bijection (see COEN260 slides)

• some intro to intuitionistic logic (see COEN260 slides)

• javascript or java or scala

Page 4: Formal methods   1 - introduction

Formal Methods

• Need them in:o medical deviceso spaceo militaryo communications

• Don’t need them in:o social networkso ads and saleso startups

Page 5: Formal methods   1 - introduction

Example of Algorithm Proof

Euclidean Algorithm:Find GCD(a,b), where a and b are two natural numbers.

1. If a < b, use the fact that GCD(a,b)=GCD(b,a)

2. GCD(a,0) = a; done.

3. GCD(a,b) = GCD(a-b,a)

4. Repeat until done.

Proof:

A. Prove that GCD(a,b)=GCD(b,a)

B. Prove that GCD(a,0) = a

C. Prove that GCD(a,b) = GCD(a-b,a)

D. Prove that it takes not more than 2*max(a,b) steps.

Page 6: Formal methods   1 - introduction

You are asked to double a cube

Have a cube, V=x3

Build a cube of size 2*V, using a divider and a ruler

Informally: approximate; we only have doubles in Java.

Formally: have to calculate √3(2)

The thing is: You Can’t

Page 7: Formal methods   1 - introduction

A similar problem

Need an algorithm to find roots of a 5th degree polynomial

Explanation: 5 roots, S5 is the group of all permutations, and it cannot be represented via a chain of +/- symmetries.

x5-x+1 = 0

Page 8: Formal methods   1 - introduction

Gödel’s First Theorem

Meaning, if we have a theory T, we can always come up with a statement in T that cannot be proved.

How?

• enumerate all sentences; then all proofs.

• isProvable(n) ≡ F (n=#(F) ∃ ∧ F is provable)

• (diagonal lemma) F p (p ↔ F(#(p))∀ ∃

• how about p ↔ ¬isProvable(#(p))

A theory that contains arithmetics cannot be at the same time consistent and complete.

Page 9: Formal methods   1 - introduction

Entscheidungsproblem

Is there an algorithm that, given a first-order theory, takes a statement and checks whether the statement is true?

(The essence of the proof of program validity.)

The answer is: NO

Church proved that there is no computable function which decides for two given λ calculus expressions whether they are equivalent or not.

Turing proved that it reduces to Halting Problem

Page 10: Formal methods   1 - introduction

Halting Problem

Given a program, can we decide if it ever ends?

Answer: NO

http://ro-che.info/ccc/03

Page 11: Formal methods   1 - introduction

Collatz Conjecture

function(n) {

while (n > 1) {

println(n);

n = n%2==0 ? n/2 : (3*n+1)

}

}

E.g. 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Will it ever stop?

Nobody Knows.

Page 12: Formal methods   1 - introduction

So, what can we do?

• limit ourselves by only provable algorithms

• count on the finiteness of the universe (is it finite?)

• count on the finiteness of time (is it finite?)

• write some unittests and pray (is it a formal method?!)

Page 13: Formal methods   1 - introduction

Unittests

• You cannot prove the program is right, but you can find errors

• Can cover main and corner cases

• Can check behavior independently from environment

• Can feel safe doing refactorings

Some people tried to formalize unittest creation (agitar.com) (see e.g. halting problem)

Question: can you write a unittest for random numbers generator?

Page 14: Formal methods   1 - introduction