SlideShare a Scribd company logo
1 of 14
Formal Methods in
Software
Lecture 1. Introduction
Vlad Patryshev
SCU
2014
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?
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
Formal Methods
• Need them in:
o medical devices
o space
o military
o communications
• Don’t need them in:
o social networks
o ads and sales
o startups
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.
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
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
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.
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
Halting Problem
Given a program, can we decide if it ever ends?
Answer: NO
http://ro-che.info/ccc/03
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.
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?!)
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?
Formal methods   1 - introduction

More Related Content

What's hot

Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
chidabdu
 

What's hot (20)

P versus NP
P versus NPP versus NP
P versus NP
 
P vs NP
P vs NP P vs NP
P vs NP
 
Np Completeness
Np CompletenessNp Completeness
Np Completeness
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
the halting_problem
the halting_problemthe halting_problem
the halting_problem
 
Np completeness h4
Np completeness  h4Np completeness  h4
Np completeness h4
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
 
Class 36: Halting Problem
Class 36: Halting ProblemClass 36: Halting Problem
Class 36: Halting Problem
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Teori pnp
Teori pnpTeori pnp
Teori pnp
 
Np completeness
Np completenessNp completeness
Np completeness
 
Finite automata
Finite automataFinite automata
Finite automata
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
 
Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5
 
Automata Theory
Automata TheoryAutomata Theory
Automata Theory
 
np complete
np completenp complete
np complete
 

Viewers also liked

Formal Methods lecture 01
Formal Methods lecture 01Formal Methods lecture 01
Formal Methods lecture 01
Sidra Ashraf
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
koolkampus
 

Viewers also liked (8)

Formal Methods
Formal MethodsFormal Methods
Formal Methods
 
Formal Methods lecture 01
Formal Methods lecture 01Formal Methods lecture 01
Formal Methods lecture 01
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
 
Using formal methods in Industrial Software Development
Using formal methods in Industrial Software DevelopmentUsing formal methods in Industrial Software Development
Using formal methods in Industrial Software Development
 
Industrial use of formal methods
Industrial use of formal methodsIndustrial use of formal methods
Industrial use of formal methods
 
Ten Commandments of Formal Methods: A decade later
Ten Commandments of Formal Methods: A decade laterTen Commandments of Formal Methods: A decade later
Ten Commandments of Formal Methods: A decade later
 
#1 formal methods – introduction for software engineering
#1 formal methods – introduction for software engineering#1 formal methods – introduction for software engineering
#1 formal methods – introduction for software engineering
 
Software quality
Software qualitySoftware quality
Software quality
 

Similar to Formal methods 1 - introduction

UNIT-V.pdf daa unit material 5 th unit ppt
UNIT-V.pdf daa unit material 5 th unit pptUNIT-V.pdf daa unit material 5 th unit ppt
UNIT-V.pdf daa unit material 5 th unit ppt
JyoReddy9
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Incompleteness without Godel Numberings
Incompleteness without Godel NumberingsIncompleteness without Godel Numberings
Incompleteness without Godel Numberings
Satvik Beri
 
Scala design pattern
Scala design patternScala design pattern
Scala design pattern
Kenji Yoshida
 
MACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULEMACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULE
DrBindhuM
 

Similar to Formal methods 1 - introduction (20)

2009 CSBB LAB 新生訓練
2009 CSBB LAB 新生訓練2009 CSBB LAB 新生訓練
2009 CSBB LAB 新生訓練
 
UNIT-V.pdf daa unit material 5 th unit ppt
UNIT-V.pdf daa unit material 5 th unit pptUNIT-V.pdf daa unit material 5 th unit ppt
UNIT-V.pdf daa unit material 5 th unit ppt
 
Basic_concepts_NP_Hard_NP_Complete.pdf
Basic_concepts_NP_Hard_NP_Complete.pdfBasic_concepts_NP_Hard_NP_Complete.pdf
Basic_concepts_NP_Hard_NP_Complete.pdf
 
tutorial.ppt
tutorial.ppttutorial.ppt
tutorial.ppt
 
Discrete Math Lecture 02: First Order Logic
Discrete Math Lecture 02: First Order LogicDiscrete Math Lecture 02: First Order Logic
Discrete Math Lecture 02: First Order Logic
 
UNIT -IV DAA.pdf
UNIT  -IV DAA.pdfUNIT  -IV DAA.pdf
UNIT -IV DAA.pdf
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Tutorial - Introduction to Rule Technologies and Systems
Tutorial - Introduction to Rule Technologies and SystemsTutorial - Introduction to Rule Technologies and Systems
Tutorial - Introduction to Rule Technologies and Systems
 
NP completeness
NP completenessNP completeness
NP completeness
 
How Hard Can a Problem Be ?
How Hard Can a Problem Be ?How Hard Can a Problem Be ?
How Hard Can a Problem Be ?
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
GDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision MakingGDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision Making
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
 
App a
App aApp a
App a
 
Incompleteness without Godel Numberings
Incompleteness without Godel NumberingsIncompleteness without Godel Numberings
Incompleteness without Godel Numberings
 
Scala design pattern
Scala design patternScala design pattern
Scala design pattern
 
MACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULEMACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULE
 
Algorithms and Complexity: Cryptography Theory
Algorithms and Complexity: Cryptography TheoryAlgorithms and Complexity: Cryptography Theory
Algorithms and Complexity: Cryptography Theory
 
Big o
Big oBig o
Big o
 
Turbo prolog 2.0 basics
Turbo prolog 2.0 basicsTurbo prolog 2.0 basics
Turbo prolog 2.0 basics
 

More from Vlad Patryshev

More from Vlad Patryshev (20)

Formal methods 8 - category theory (last one)
Formal methods   8 - category theory (last one)Formal methods   8 - category theory (last one)
Formal methods 8 - category theory (last one)
 
Formal methods 6 - elements of algebra
Formal methods   6 - elements of algebraFormal methods   6 - elements of algebra
Formal methods 6 - elements of algebra
 
Formal methods 5 - Pi calculus
Formal methods   5 - Pi calculusFormal methods   5 - Pi calculus
Formal methods 5 - Pi calculus
 
Formal methods 4 - Z notation
Formal methods   4 - Z notationFormal methods   4 - Z notation
Formal methods 4 - Z notation
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
Formal methods 2 - languages and machines
Formal methods   2 - languages and machinesFormal methods   2 - languages and machines
Formal methods 2 - languages and machines
 
Formal methods 7 - category theory
Formal methods   7 - category theoryFormal methods   7 - category theory
Formal methods 7 - category theory
 
Truth, deduction, computation lecture i (last one)
Truth, deduction, computation   lecture i (last one)Truth, deduction, computation   lecture i (last one)
Truth, deduction, computation lecture i (last one)
 
Truth, deduction, computation lecture h
Truth, deduction, computation   lecture hTruth, deduction, computation   lecture h
Truth, deduction, computation lecture h
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Truth, deduction, computation lecture f
Truth, deduction, computation   lecture fTruth, deduction, computation   lecture f
Truth, deduction, computation lecture f
 
Truth, deduction, computation lecture e
Truth, deduction, computation   lecture eTruth, deduction, computation   lecture e
Truth, deduction, computation lecture e
 
Truth, deduction, computation lecture d
Truth, deduction, computation   lecture dTruth, deduction, computation   lecture d
Truth, deduction, computation lecture d
 
Truth, deduction, computation lecture c
Truth, deduction, computation   lecture cTruth, deduction, computation   lecture c
Truth, deduction, computation lecture c
 
Truth, deduction, computation lecture b
Truth, deduction, computation   lecture bTruth, deduction, computation   lecture b
Truth, deduction, computation lecture b
 
Truth, deduction, computation lecture a
Truth, deduction, computation   lecture aTruth, deduction, computation   lecture a
Truth, deduction, computation lecture a
 
Truth, deduction, computation lecture 9
Truth, deduction, computation   lecture 9Truth, deduction, computation   lecture 9
Truth, deduction, computation lecture 9
 
Truth, deduction, computation lecture 8
Truth, deduction, computation   lecture 8Truth, deduction, computation   lecture 8
Truth, deduction, computation lecture 8
 
Truth, deduction, computation lecture 7
Truth, deduction, computation   lecture 7Truth, deduction, computation   lecture 7
Truth, deduction, computation lecture 7
 
Truth, deduction, computation lecture 6
Truth, deduction, computation   lecture 6Truth, deduction, computation   lecture 6
Truth, deduction, computation lecture 6
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

Formal methods 1 - introduction

  • 1. Formal Methods in Software Lecture 1. Introduction Vlad Patryshev SCU 2014
  • 2. 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?
  • 3. 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
  • 4. Formal Methods • Need them in: o medical devices o space o military o communications • Don’t need them in: o social networks o ads and sales o startups
  • 5. 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.
  • 6. 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
  • 7. 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
  • 8. 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.
  • 9. 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
  • 10. Halting Problem Given a program, can we decide if it ever ends? Answer: NO http://ro-che.info/ccc/03
  • 11. 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.
  • 12. 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?!)
  • 13. 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?