LightMySky
به انگلیسی

Hoare Logic and Proving a Program Meets Its Specification

A partial-correctness Hoare triple promises that execution starting in its precondition satisfies its postcondition if it terminates. Rules compose compatible triples. Loop invariants support initialization, preservation and exit checks; termination needs an additional argument. Tools can generate verification conditions, and some can infer invariants.

شروع یادگیری این موضوعبه حساب کاربری نیازی نیست. پیشرفت در همین مرورگر ذخیره می‌شود.

کارهایی که یادگیرنده بعد از این درس می‌تواند انجام دهد

  • Prove a short assignment sequence by composing triples with compatible intermediate assertions
  • Supply a loop invariant, check initialization and preservation, and derive the postcondition at exit
  • Compute an assignment's weakest precondition and distinguish partial correctness from termination

۱ · بخوان

You have checked executions against a specification. Hoare logic reasons symbolically about code. Write {P} C {Q}: precondition P, code C, postcondition Q. Partial correctness promises Q if execution starts in P and terminates. It does not promise termination. Here variables are mathematical integers without overflow, and := means assignment.

Try it together

For x := x + 2; y := 2*x, start with x = 3. The first line gives x = 5. That assertion supports the second line, which gives y = 10. To compose {P} C1 {R} and {S} C2 {Q}, R must imply S. Two valid triples cannot be joined using intermediate assertions that fail this implication.

Before: x = 3x := x + 2Link: x = 5y := 2*xAfter: y = 10
Read the code and assertions in order. The first postcondition supplies the second precondition.

Let n be a fixed nonnegative integer. Run i := 0; while i < n do i := i + 1. Invariant 0 <= i <= n holds initially. Under the guard i < n, integer increment preserves both bounds. At exit the false guard gives i >= n; with i <= n, this yields i = n. Check initialization, preservation under the guard, and the exit implication. A stronger invariant needs all three checks again.

Establish the invariantPreserve it under guardUse it with false guard
This is the proof workflow, not one execution of the loop.

Some tools infer invariants; others need annotations from you. A verification-condition generator turns proof obligations into formulas for a solver. The obligations must be valid. A solver result of unknown leaves an obligation unproved: it establishes neither correctness nor a program bug.

For an assignment that terminates, its weakest precondition is the least restrictive starting condition guaranteeing the chosen postcondition. Substitute the assigned expression for the variable in that postcondition. For x := x + 2 and goal x >= 7, obtain x + 2 >= 7, or x >= 5. This works backward from the goal. Forward reasoning is also valid.

Total correctness adds termination. In the counting loop, n - i is a nonnegative integer at each successful guard check and decreases on every iteration, so the loop cannot repeat forever. Invariant checks alone do not give this argument. The loop while true do skip preserves invariant true but never terminates; skip leaves the state unchanged.

Compose compatible triples. Check invariants at entry, through guarded steps and at exit. Total correctness also needs a termination argument.

Original lesson, questions and flashcards about Hoare logic, written for this dataset.

۲ · ببین

به انگلیسی

دور از صفحه‌نمایش

چاپ کاربرگکاغذ A4، با یک صفحهٔ جواب‌ها برای بزرگ‌ترها. بدون صفحه‌نمایش، بدون اینترنت.

جایگاه این موضوع

اول این‌ها را یاد بگیر

این موضوع راه این‌ها را باز می‌کند

سپس تمرین

۸ سؤال پشت این درس منتظرند، هر کدام با پاسخ توضیح‌داده‌شده. هر پاسخ آسمان را پر می‌کند: ستاره‌ها هنگام یادگیری روشن می‌شوند و وقتی زمان برگشتن است کمرنگ می‌شوند.

شروع یادگیری این موضوع