SlideShare a Scribd company logo
1 of 12
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com
Formal Methods:
Loop proof using induction
method Example
0
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Induction reasoning
1
We will add two important induction principles to our rules. Those principles are very
important when dealing with integers especially positive integers.
• First Principle:
Second Principle:
• Major difference is in the second statement.
– Use the second principle when assuming P(k) is not enough to prove P(k+1).
– Assuming P(r) for any r where 1 r  k gives more ammunition to prove the relation.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
2
The pattern for a loop function is as the diagram.
And The Hoare triple rule for iteration is
{Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬B}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
3
Example : Prove the Euclidean algorithm finds the greatest common divisor of two positive
integers a and b, using induction method?
• The greatest common divisor of a and b, denoted by gcd(a, b), is
the largest integer n such that a/n and b/n. For example, gcd(12, 18)
= 6 and gcd(420, 66) = 6.
• The Euclidean algorithm works by a succession of divisions. To
find gcd(a, b)
– assuming a >= b, so “a” is the dividend and “b” is the divisor
– you first divide a by b, getting a quotient and a remainder.
– Next, you divide the divisor, b, by the remainder (means the previous
divisor become the new dividend and the previous reminder become
the new divisor) and keep doing this until the remainder is 0, at This
point the greatest common divisor is the last divisor used.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
4
Preparedby:SharifOmarSalem–ssalemg@gmail.com
5
Loop proof using induction reasoning
Preparedby:SharifOmarSalem–ssalemg@gmail.com
6
• Theorem ≔ {Q} while (B) {P} {Q∧¬B}  gcd(a,b)
• Loop condition (B) ≔ (remainder is not 0) ≔ (j != 0 )
Solution Steps: (Four Steps)
Step 1: From Loop description find some definitions for the relation between
variables.
Definitions:
• The greatest common divisor of any two integers (dividend i and divisor j) is
equal to the greatest common advisor of the divisor j and remaining r
≔ gcd(i, j) = gcd(j, r)
• At every iteration The greatest common divisor of temporary dividend i and
divisor j is equal to the greatest common divisor of original two integers a & b
≔ gcd(i, j) = gcd(a, b)
Loop proof using induction reasoning
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
7
Step 2: Define the loop invariant.
Loop invariant:
Define the loop invariant which is true before and after the loop and relate all the
variables inside the program. In our case it is
Q ≔ gcd(i, j) = gcd(a, b)
Step 3: Now we have to prove that Q is valid for all cases possible inside the loop.
So, we use induction to prove:
Proof:
Q(n) ≔ gcd(in, jn) = gcd(a, b) for all n  0.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
8
− Q(0) is gcd(i0, j0) = gcd(a, b) is true because when we first get to the loop
statement, i and j have the values a and b.
– Assume Q(k): gcd(ik, jk) = gcd(a, b).
– Show Q(k + 1): gcd(ik + 1, jk + 1) = gcd(a, b).
– By the assignment statements within the loop body, we know that
• ik + 1 = jk
• jk + 1 = rk
– Then, by the additional fact on the previous slide:
– gcd(ik + 1, jk + 1) = gcd(jk, rk) = gcd(ik, jk)
– By the inductive hypothesis, the above is equal to gcd(a, b)
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Loop proof using induction
reasoning
9
Step 4: Now we have to prove that at loop termination the post
condition will imply to the conclusion.
Termination + Loop Invariant = Goal
At loop termination
• gcd(i, j) = gcd(a, b) and j = 0,
• so gcd(i, 0) = gcd(a, b).
• But gcd(i, 0) is i, so i = gcd(a, b).
As we prove that Q is valid for all cases inside the loop and the loop
termination imply to the conclusion. Therefore, function GCD is
correct.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com
End of Lecture
10
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com
Next Lecture:
ProLogic
11

More Related Content

What's hot

UGC NET Computer Science & Application book.pdf [Sample]
UGC NET Computer Science & Application book.pdf  [Sample]UGC NET Computer Science & Application book.pdf  [Sample]
UGC NET Computer Science & Application book.pdf [Sample]DIwakar Rajput
 
Discrete Structures lecture 2
 Discrete Structures lecture 2 Discrete Structures lecture 2
Discrete Structures lecture 2Ali Usman
 
Discrete structures & optimization unit 1
Discrete structures & optimization unit 1Discrete structures & optimization unit 1
Discrete structures & optimization unit 1SURBHI SAROHA
 
Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Ikhtiar Khan Sohan
 
Pigeonhole Principle
Pigeonhole PrinciplePigeonhole Principle
Pigeonhole Principlenielsoli
 
Chapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound StatementsChapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound Statementsguestd166eb5
 
Translating English to Propositional Logic
Translating English to Propositional LogicTranslating English to Propositional Logic
Translating English to Propositional LogicJanet Stemwedel
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & QuantifiersCMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiersallyn joy calcaben
 
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDiscrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDr. Khaled Bakro
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)kevinwu1994
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisAmrinder Arora
 
Propositions - Discrete Structures
Propositions - Discrete Structures Propositions - Discrete Structures
Propositions - Discrete Structures Drishti Bhalla
 
Toc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationToc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationnarayan dudhe
 
A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW) A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW) Jie Bao
 

What's hot (20)

UGC NET Computer Science & Application book.pdf [Sample]
UGC NET Computer Science & Application book.pdf  [Sample]UGC NET Computer Science & Application book.pdf  [Sample]
UGC NET Computer Science & Application book.pdf [Sample]
 
Discrete Structures lecture 2
 Discrete Structures lecture 2 Discrete Structures lecture 2
Discrete Structures lecture 2
 
Discrete structures & optimization unit 1
Discrete structures & optimization unit 1Discrete structures & optimization unit 1
Discrete structures & optimization unit 1
 
Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)
 
Pigeonhole Principle
Pigeonhole PrinciplePigeonhole Principle
Pigeonhole Principle
 
Undecidabality
UndecidabalityUndecidabality
Undecidabality
 
Chapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound StatementsChapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound Statements
 
Translating English to Propositional Logic
Translating English to Propositional LogicTranslating English to Propositional Logic
Translating English to Propositional Logic
 
CMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & QuantifiersCMSC 56 | Lecture 3: Predicates & Quantifiers
CMSC 56 | Lecture 3: Predicates & Quantifiers
 
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDiscrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
 
Hasse diagram
Hasse diagramHasse diagram
Hasse diagram
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Mathematical Logic
Mathematical LogicMathematical Logic
Mathematical Logic
 
Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Discrete Math Lecture 03: Methods of Proof
Discrete Math Lecture 03: Methods of ProofDiscrete Math Lecture 03: Methods of Proof
Discrete Math Lecture 03: Methods of Proof
 
Propositions - Discrete Structures
Propositions - Discrete Structures Propositions - Discrete Structures
Propositions - Discrete Structures
 
Toc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationToc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computation
 
A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW) A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW)
 

Similar to #7 formal methods – loop proof examples

555_Spring12_topic06.ppt
555_Spring12_topic06.ppt555_Spring12_topic06.ppt
555_Spring12_topic06.pptSnehSinha6
 
17-mathematical-induction.ppt
17-mathematical-induction.ppt17-mathematical-induction.ppt
17-mathematical-induction.pptRishabhNath3
 
17-mathematical-induction.ppt
17-mathematical-induction.ppt17-mathematical-induction.ppt
17-mathematical-induction.pptSintaVeDe
 
Yet another prime formula to prove open problems
Yet another prime formula to prove open problemsYet another prime formula to prove open problems
Yet another prime formula to prove open problemsChris De Corte
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic sliderainoftime
 
Is unit 4_number_theory
Is unit 4_number_theoryIs unit 4_number_theory
Is unit 4_number_theorySarthak Patel
 
Ads unit 3 ppt
Ads unit 3 pptAds unit 3 ppt
Ads unit 3 pptpraveena p
 
Poggi analytics - star - 1a
Poggi   analytics - star - 1aPoggi   analytics - star - 1a
Poggi analytics - star - 1aGaston Liberman
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Real number by G R Ahmed of KVK
Real number by G R Ahmed of KVKReal number by G R Ahmed of KVK
Real number by G R Ahmed of KVKMD. G R Ahmed
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework HelpExcel Homework Help
 
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...Md. Al-Amin Khandaker Nipu
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Shiang-Yun Yang
 
sem-UCT-hiring.pdf
sem-UCT-hiring.pdfsem-UCT-hiring.pdf
sem-UCT-hiring.pdfSuman144708
 
Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03宁 梅
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 

Similar to #7 formal methods – loop proof examples (20)

555_Spring12_topic06.ppt
555_Spring12_topic06.ppt555_Spring12_topic06.ppt
555_Spring12_topic06.ppt
 
17-mathematical-induction.ppt
17-mathematical-induction.ppt17-mathematical-induction.ppt
17-mathematical-induction.ppt
 
17-mathematical-induction.ppt
17-mathematical-induction.ppt17-mathematical-induction.ppt
17-mathematical-induction.ppt
 
Yet another prime formula to prove open problems
Yet another prime formula to prove open problemsYet another prime formula to prove open problems
Yet another prime formula to prove open problems
 
Binomial theorem
Binomial theorem Binomial theorem
Binomial theorem
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic slide
 
Is unit 4_number_theory
Is unit 4_number_theoryIs unit 4_number_theory
Is unit 4_number_theory
 
Ads unit 3 ppt
Ads unit 3 pptAds unit 3 ppt
Ads unit 3 ppt
 
Poggi analytics - star - 1a
Poggi   analytics - star - 1aPoggi   analytics - star - 1a
Poggi analytics - star - 1a
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Real number by G R Ahmed of KVK
Real number by G R Ahmed of KVKReal number by G R Ahmed of KVK
Real number by G R Ahmed of KVK
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework Help
 
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...
Efficient Scalar Multiplication for Ate Based Pairing over KSS Curve of Embed...
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)
 
sem-UCT-hiring.pdf
sem-UCT-hiring.pdfsem-UCT-hiring.pdf
sem-UCT-hiring.pdf
 
wddd
wdddwddd
wddd
 
Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03
 
MFCS-17.ppt
MFCS-17.pptMFCS-17.ppt
MFCS-17.ppt
 
2.ppt
2.ppt2.ppt
2.ppt
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

#7 formal methods – loop proof examples

  • 1. Preparedby:SharifOmarSalem–ssalemg@gmail.com Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com Formal Methods: Loop proof using induction method Example 0
  • 2. Preparedby:SharifOmarSalem–ssalemg@gmail.com Induction reasoning 1 We will add two important induction principles to our rules. Those principles are very important when dealing with integers especially positive integers. • First Principle: Second Principle: • Major difference is in the second statement. – Use the second principle when assuming P(k) is not enough to prove P(k+1). – Assuming P(r) for any r where 1 r  k gives more ammunition to prove the relation.
  • 3. Preparedby:SharifOmarSalem–ssalemg@gmail.com Loop proof using induction reasoning 2 The pattern for a loop function is as the diagram. And The Hoare triple rule for iteration is {Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬B}
  • 4. Preparedby:SharifOmarSalem–ssalemg@gmail.com Loop proof using induction reasoning 3 Example : Prove the Euclidean algorithm finds the greatest common divisor of two positive integers a and b, using induction method? • The greatest common divisor of a and b, denoted by gcd(a, b), is the largest integer n such that a/n and b/n. For example, gcd(12, 18) = 6 and gcd(420, 66) = 6. • The Euclidean algorithm works by a succession of divisions. To find gcd(a, b) – assuming a >= b, so “a” is the dividend and “b” is the divisor – you first divide a by b, getting a quotient and a remainder. – Next, you divide the divisor, b, by the remainder (means the previous divisor become the new dividend and the previous reminder become the new divisor) and keep doing this until the remainder is 0, at This point the greatest common divisor is the last divisor used.
  • 7. Preparedby:SharifOmarSalem–ssalemg@gmail.com 6 • Theorem ≔ {Q} while (B) {P} {Q∧¬B}  gcd(a,b) • Loop condition (B) ≔ (remainder is not 0) ≔ (j != 0 ) Solution Steps: (Four Steps) Step 1: From Loop description find some definitions for the relation between variables. Definitions: • The greatest common divisor of any two integers (dividend i and divisor j) is equal to the greatest common advisor of the divisor j and remaining r ≔ gcd(i, j) = gcd(j, r) • At every iteration The greatest common divisor of temporary dividend i and divisor j is equal to the greatest common divisor of original two integers a & b ≔ gcd(i, j) = gcd(a, b) Loop proof using induction reasoning
  • 8. Preparedby:SharifOmarSalem–ssalemg@gmail.com Loop proof using induction reasoning 7 Step 2: Define the loop invariant. Loop invariant: Define the loop invariant which is true before and after the loop and relate all the variables inside the program. In our case it is Q ≔ gcd(i, j) = gcd(a, b) Step 3: Now we have to prove that Q is valid for all cases possible inside the loop. So, we use induction to prove: Proof: Q(n) ≔ gcd(in, jn) = gcd(a, b) for all n  0.
  • 9. Preparedby:SharifOmarSalem–ssalemg@gmail.com Loop proof using induction reasoning 8 − Q(0) is gcd(i0, j0) = gcd(a, b) is true because when we first get to the loop statement, i and j have the values a and b. – Assume Q(k): gcd(ik, jk) = gcd(a, b). – Show Q(k + 1): gcd(ik + 1, jk + 1) = gcd(a, b). – By the assignment statements within the loop body, we know that • ik + 1 = jk • jk + 1 = rk – Then, by the additional fact on the previous slide: – gcd(ik + 1, jk + 1) = gcd(jk, rk) = gcd(ik, jk) – By the inductive hypothesis, the above is equal to gcd(a, b)
  • 10. Preparedby:SharifOmarSalem–ssalemg@gmail.com Loop proof using induction reasoning 9 Step 4: Now we have to prove that at loop termination the post condition will imply to the conclusion. Termination + Loop Invariant = Goal At loop termination • gcd(i, j) = gcd(a, b) and j = 0, • so gcd(i, 0) = gcd(a, b). • But gcd(i, 0) is i, so i = gcd(a, b). As we prove that Q is valid for all cases inside the loop and the loop termination imply to the conclusion. Therefore, function GCD is correct.
  • 11. Preparedby:SharifOmarSalem–ssalemg@gmail.com Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com End of Lecture 10
  • 12. Preparedby:SharifOmarSalem–ssalemg@gmail.com Prepared by: Sharif Omar Salem – ssalemg@gmail.comPrepared by: Sharif Omar Salem – ssalemg@gmail.com Next Lecture: ProLogic 11