---
title: "Hoare Logic and Proving a Program Meets Its Specification"
description: "A triple states what holds before a fragment runs and what holds after. Rules compose triples along the structure of the program, loops need an invariant that a person supplies, and the leftover verif"
canonical: https://lightmysky.com/learn/computing/hoare-logic-and-proving-a-program-meets-its-specification-mt_bJZnbBrUIP
source: https://lightmysky.com/learn/computing/hoare-logic-and-proving-a-program-meets-its-specification-mt_bJZnbBrUIP.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Hoare Logic and Proving a Program Meets Its Specification

A triple states what holds before a fragment runs and what holds after. Rules compose triples along the structure of the program, loops need an invariant that a person supplies, and the leftover verification conditions are what a solver is handed.

Subject: Computing · Area: Software Engineering · Ages 23 to 24
Page: https://lightmysky.com/learn/computing/hoare-logic-and-proving-a-program-meets-its-specification-mt_bJZnbBrUIP

## Ready when they can

- Prove a short straight-line fragment correct by composing triples
- Supply a loop invariant and show it is preserved and strong enough at exit
- Say why a tool computes the weakest precondition rather than the strongest postcondition

## Lesson: Proving code correct with Hoare triples

A Hoare triple says what holds before a fragment runs and what holds after, with the code in the middle. Rules compose triples along the shape of the program, so you prove a short straight-line fragment by chaining one triple after another.

Loops need something extra: an invariant that you supply yourself. It must hold before the loop, stay preserved by each pass, and be strong enough that, together with the loop ending, it implies what you want after.

**Example.** Take two assignments in a row. Prove the first triple, feed its ending claim as the start of the second, and chain them. The composed triple now covers both lines, which is how straight-line code is proved.

**Tip.** Proving by hand leaves small checks called verification conditions, and a solver discharges those. Tools work backwards with the weakest precondition rather than forwards with the strongest postcondition, because going backwards keeps the goal in view.

**Recap.** Chain triples along the code, guard loops with an invariant you supply, and let the solver close the small gaps.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Model Checking and the State-Space Explosion](https://lightmysky.com/learn/computing/model-checking-and-the-state-space-explosion-mt__pgZ0PzEZo)

## Opens up

- [Type Soundness: Progress and Preservation](https://lightmysky.com/learn/computing/type-soundness-progress-and-preservation-mt_nAAHVPNaVJ)
