Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท SPARK and Safety

What Is SPARK?

SPARK is a verifiable subset of Ada. Learn what it restricts, what GNATprove proves, the SPARK_Mode aspect, assurance levels and where SPARK is used, with C stand-ins.

Testing shows that a program works for the inputs you tried. Formal verification aims higher: it shows, by mathematical proof, that a program is free of certain errors for every input. SPARK is the technology that brings this to Ada. It is a carefully chosen subset of the language, plus a toolset (GNATprove) that analyzes your code and either proves properties or tells you why it cannot.

Important honesty note: the SPARK tools are not available on this site, so every Ada/SPARK listing here is read-only, and tool output is described rather than shown unless we are certain of it. The runnable examples are C stand-ins that explore the ideas, such as checking a property over all inputs of a small type.

The idea in one picture

SPARK programs are normal Ada programs. The difference is a promise: inside the SPARK subset, the code has no hidden behavior that would make analysis unsound. The tool reads your code and contracts and generates mathematical verification conditions; automatic provers (Alt-Ergo, CVC5, Z3) then try to discharge them.

From code to proof

Ada + contracts SPARK_Mode => On
GNATprove builds proof obligations
Provers Alt-Ergo, CVC5, Z3
Result proved or unproved

Unproved does not always mean wrong.

1 / 2

What the subset excludes

The SPARK language leaves out features that make reasoning hard or unsound:

  • Aliasing of mutable data, so two names for the same variable do not exist. Access types (pointers) are restricted by an ownership model (similar in spirit to Rust's borrowing).
  • Side effects in functions. A SPARK function has no side effects, so an expression always means the same thing.
  • Exception handlers. Exceptions are not handled; instead you prove they cannot occur.
  • Unrestricted tasking. Concurrency is limited to the Ravenscar and Jorvik profiles.
  • Some uses of controlled types and generics that are hard to analyze.

What stays is the large, useful core: strong typing, records, arrays, packages, generics (instantiated), discriminants, loops, and the contract aspects from the previous chapter.

Marking code as SPARK

You turn on checking with the SPARK_Mode aspect:

ADA
package Counters
  with SPARK_Mode => On
is
   Max : constant := 1_000;

   type Counter is range 0 .. Max;

   procedure Increment (C : in out Counter)
     with Pre  => C < Max,
          Post => C = C'Old + 1;
end Counters;

You can apply it to a whole project, to selected packages, or to the specification but not the body (leaving the implementation in full Ada, which is useful for code that calls into C). A mixed system, with SPARK for the critical core and plain Ada for the rest, is common.

Levels of assurance

The SPARK documentation describes progressive goals, which is a good way to adopt it gradually:

Level Goal
Stone the code is in the SPARK subset
Bronze data flow is correct, no uninitialized reads
Silver absence of run-time errors is proved
Gold key functional properties (contracts) are proved
Platinum full functional correctness

Most projects aim for Silver: proving that no overflow, division by zero, out-of-range index or other run-time check can fail. It is also where the investment pays off quickly, because the proofs mostly come from the types and a few well-chosen contracts. The next lessons walk through flow analysis (Bronze) and proving absence of run-time errors (Silver).

Where SPARK is used

SPARK is used where failure is very costly: avionics software, railway interlocking, space flight software, secure operating-system components, cryptographic libraries and medical devices. Standards such as DO-178C (aviation) allow formal proof to replace some testing activities, which can reduce cost on large code bases.

Stand-in: proving over every input (C, runnable)

Real SPARK proves properties for all values of a type symbolically. For a tiny type we can do the same by brute force: enumerate all inputs and check the postcondition. The 8-bit type below has only 256 values, so exhaustive checking is a kind of proof. We check that increment really adds one (as in the contract above) and find the single value where it does not, which is exactly what a precondition (C < Max) excludes.

C C stand-in: exhaustive check of a contract over uint8_t

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output is excluded by precondition: C = 255 followed by checked 255 inputs, 0 failures: without the precondition the contract would fail at 255 because the value wraps to 0. SPARK finds this kind of case without enumerating, so it works for types with 2^64 values too.

Stand-in: what a proof obligation looks like (C, runnable)

For X + Y on a 16-bit range, the tool must show no overflow: X + Y <= 32767 for every allowed X and Y. Here the check is run over bounded ranges for two choices of preconditions, a weak one that fails and a strong one that passes.

C C stand-in: checking an overflow obligation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Widening the range to the full type gives many failing pairs, while restricting both operands to half the range gives none. In SPARK you express the second choice as a precondition or as a narrower subtype, and the prover then discharges the obligation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is SPARK?

    SPARK restricts Ada to a analyzable subset and adds GNATprove.
  2. 2. Which feature is NOT allowed in SPARK code?

    SPARK proves that exceptions cannot happen instead of handling them.
  3. 3. What does the Silver level of assurance aim for?

    Silver: no overflow, range, index or division errors; Gold adds key functional properties.
  4. 4. How does SPARK differ from testing?

    A proof holds for every input satisfying the contracts, while tests sample some of them.

Summary

  • SPARK is a subset of Ada with a prover-based toolset (GNATprove).
  • It excludes aliasing, side-effecting functions, exception handlers and unrestricted tasking.
  • SPARK_Mode switches checking on per package or project; mixed Ada/SPARK systems are normal.
  • Assurance levels: Stone, Bronze, Silver, Gold, Platinum.
  • The tools do not run on this site; the C stand-ins model the idea with exhaustive checks.

Next: Flow Analysis and Data Dependencies