UPI Study › Courses › Software Engineering › Free lessons › Lesson 11.3 Free with a free account
Applying Formal Methods to Software Development
Software Engineering · Chapter 11: Requirements Validation, Dependability, and Formal MethodsSection 11.3 · lesson 74 of 1038 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Software Engineering.
Lesson complete
Earn the 3 college credits for Software Engineering
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Reliability Engineering: Definitions, Goals, and Getting Started →← What Makes a System Dependable? Core Properties and GoalsNext lesson: Reliability Engineering: Definitions, Goals, and Getting Started →
In this lesson
Proving correctness for every input - and when that rigor is worth its cost.
- Explain how formal verification differs from testing
- Describe formal specification, and why it uses mathematics
- Describe preconditions, postconditions, and invariants
- Judge when formal methods are worth applying
Transcript
Everything said in the video, section by section (1,040 words). Read along, or search it.
Requirements Validation, Dependability, and Formal Methods
Applying Formal Methods to Software Development
Testing can show that the cases you tried worked. It can never show that every case works. This section covers formal verification, which takes a fundamentally different approach. It covers formal specification, the building blocks of a correctness proof, and the tradeoffs that decide when this rigor is worth it.
Confidence vs. Certainty
50 Passing Tests
A payment-processing function passes fifty carefully chosen test cases, without a single issue. The team is confident. But confidence is not certainty. Somewhere among the inputs nobody tried, an edge case could still be lurking. A passing test shows that one case worked. It never shows that every case would. Formal verification takes a different approach. Instead of trying more inputs, it uses mathematical proof. It establishes that a piece of code behaves correctly for every input satisfying its precondition, not just the ones anyone thought to try.
Figure 1
Sampling vs. Proving
Testing samples the space of possible inputs. It runs a program against a chosen set of cases, and checks the results. It can show that a defect is present, but never that defects are completely absent. Formal verification constructs a mathematical proof instead. A successful proof covers every allowed input at once, by construction. That guarantee has an important limit. A proof only guarantees that the program matches its formal specification. If the specification itself does not capture the real need, that is a validation problem. Formal verification does not replace validation.
Table 1
Testing vs. Formal Verification
Here are the two approaches side by side. Testing checks a chosen sample of possible inputs. Formal verification checks every input the specification allows. A passing test means no defect was found in the cases tried. A successful proof means a mathematical guarantee across all cases. And the costs differ. Testing is moderate, and scales roughly with the effort spent. Formal verification is high, and grows steeply with complexity.
Formal Specification
Requirements in Mathematics
A formal specification states precisely, in mathematical notation like logic or set theory, what a program must do. Natural language is open to more than one reading. A well-formed mathematical statement has exactly one meaning. Take a requirement written in plain language. Return the larger of the two numbers. A formal version states it precisely. Given any two numbers x and y, the result equals x when x is greater than or equal to y, and equals y otherwise. Several formal notations exist. What matters here is the underlying idea, not any one syntax.
Table 3
The Building Blocks of a Proof
A correctness proof is built from three basic pieces. A precondition states what must be true before a piece of code runs. It is the starting assumption the proof is allowed to rely on. A postcondition states what the code guarantees will be true afterward. It is the claim the proof sets out to establish. And an invariant is a condition that stays true throughout every iteration of a loop. It is the link that connects each iteration to the next, and finally to the postcondition.
Figure 2
An Invariant at Work
Here is a loop that sums the numbers from one to n. The precondition says n is not negative. The postcondition says the total equals one plus two, and so on, up to n. The invariant says that at each step, the total equals the sum of the numbers up to the current counter. The argument always has the same shape. Show the invariant holds before the loop begins, and that every iteration preserves it. Then show that when the loop ends, the invariant together with the ending condition implies the postcondition.
Proving Correctness
A Rigorous Argument
So what does proving a program correct actually mean? It means constructing a rigorous logical argument, following the rules of formal logic, that connects the precondition to the postcondition, through every path the code could take. It does not necessarily require a mechanized theorem-proving tool. It means each step follows necessarily from the last, rather than being merely persuasive. The payoff is real. Mathematical certainty, covering every case the specification allows. The cost is real too. It takes specialized mathematical skill and significant time, and it still depends on the specification having been validated.
Figure 3
Why Proofs Don't Scale Gracefully
There is another reason formal methods are applied selectively. They do not scale gracefully. This chart is illustrative. As a component grows in size and complexity, testing effort grows roughly in step. Proof effort grows far more steeply. That is why formal methods are rarely applied to an entire large system, end to end.
Table 4
When Formal Methods Earn Their Cost
Formal methods tend to earn their cost in specific situations. Small, well-bounded components, where the consequence of failure is severe, where testing alone struggles to build confidence, and where the expertise is actually available. Flight control software fits. A defect could be catastrophic, and the core logic is often small and stable. So do security kernels and protocols, where a single flaw can undermine everything built on top. Medical device firmware fits, because patient safety depends on correctness. And so do industrial safety interlocks, which are small, and carry physical risk.
Apply It
Proving Every Utility Function
Let's apply that. A team decides to formally verify every function in a large, ordinary business application, including simple, low-stakes utility functions. It consumes months of specialized effort, with little reduction in defects after release. What went wrong? They applied formal verification as a default, rather than selectively. Proof effort scales steeply. For low-stakes code, testing alone would have built enough confidence, far more cheaply.
Key Terms
The Vocabulary of This Section
Let's lock in the key terms. Formal verification uses mathematical proof to establish that a program satisfies its specification, for every input the specification allows. A formal specification is a precise statement, in mathematical notation, of what a program must do. A precondition is what must be true before code runs. A postcondition is what the code guarantees afterward. And an invariant stays true through every iteration of a loop. A proof is a rigorous logical argument, connecting a precondition to a postcondition through every path the code could take.
Key Takeaways
What to Carry Forward
To pull this together. Testing samples the space of inputs. Formal verification proves correctness for every allowed input at once. Formal specifications use mathematical notation to remove the ambiguity of natural language. Preconditions, postconditions, and invariants are the building blocks a proof connects together. A proof guarantees that a program matches its specification, not that the specification is right. And formal methods are applied selectively, to small, high-consequence components, because proof effort scales so steeply.
About Software Engineering at UPI Study
Software Engineering (Computer Science 301) is a self-paced online course of 103 video lessons in 15 chapters, from process models and agile to requirements, design, user interfaces, reliability, security and project management. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Why take it here
Why students take Software Engineering with UPI Study
Where the credit goes
Universities that work with NCCRS
Software Engineering carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Applying Formal Methods to Software Development
How is formal verification different from software testing?
What is a formal specification and why does it use mathematics?
How does a loop invariant help prove a loop correct?
Why is formal verification rarely applied to an entire large system?
About the Software Engineering course
How many college credits is Software Engineering?
How much does Software Engineering cost and what is included?
Which universities accept UPI Study credits?
How long does Software Engineering take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Software Engineering.
103 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →