Ada contracts
last modified November 23, 2026
A contract is a promise written next to a declaration and checked by the
compiler. Ada expresses it with aspects: Pre and
Post for subprograms, Type_Invariant for a private
type, and predicates for a subtype. The examples show what each check catches
and when it runs; the checks are enabled with -gnata.
Preconditions and postconditions
Deposit requires a positive amount and promises that the balance
grows by exactly that amount. Both aspects appear after the procedure
declaration, before the semicolon. Withdraw has a stronger
precondition: the amount must be positive and must not exceed the balance.
package Accounts is
type Account is tagged record
Balance : Integer := 0;
end record;
procedure Deposit (A : in out Account; Amount : Integer)
with Pre => Amount > 0,
Post => A.Balance = A.Balance'Old + Amount;
procedure Withdraw (A : in out Account; Amount : Integer)
with Pre => Amount > 0 and then Amount <= A.Balance,
Post => A.Balance = A.Balance'Old - Amount;
end Accounts;
A.Balance'Old is the value of the component on entry to the
call; on the postcondition side, A.Balance is the value on return.
The parameter modes still decide what the body may change, and the contracts
add the rules that the modes cannot express.
package body Accounts is
procedure Deposit (A : in out Account; Amount : Integer) is
begin
A.Balance := A.Balance + Amount;
end Deposit;
procedure Withdraw (A : in out Account; Amount : Integer) is
begin
A.Balance := A.Balance - Amount;
end Withdraw;
end Accounts;
The body contains the same arithmetic as before, with no checks written by hand. The promises are made in the specification and checked by the compiler around the call.
with Ada.Text_IO;
with Accounts;
use Ada.Text_IO;
procedure Accounts_Main is
A : Accounts.Account := (Balance => 100);
begin
A.Deposit (50);
A.Withdraw (30);
Put_Line ("balance =" & Integer'Image (A.Balance));
end Accounts_Main;
A deposit of 50 and a withdrawal of 30 leave 120. Both calls satisfy their contracts, and the assertion-enabled build accepts them.
$ gnatmake -gnat2022 -gnatwa -gnata accounts_main.adb $ ./accounts_main balance = 120
Checks require -gnata
Contracts are not enforced in an ordinary build. The aspects remain part
of the specification and are visible to tools, but the generated code does not
evaluate them. The switch -gnata turns the assertions on, and the
same source program then behaves differently when a contract is broken.
with Ada.Text_IO;
with Accounts;
use Ada.Text_IO;
procedure Bad_Main is
A : Accounts.Account := (Balance => 100);
begin
A.Deposit (0);
Put_Line ("balance =" & Integer'Image (A.Balance));
end Bad_Main;
The Accounts package is unchanged; only the main program is
new. Deposit (0) violates the precondition. Compiled without
-gnata, the call runs and the balance stays at 100. Compiled with
-gnata, the check runs on entry, raises
Assertion_Error, and the process exits with status 1. The message
names the file and line of the precondition.
$ gnatmake -gnat2022 -gnatwa bad_main.adb $ ./bad_main balance = 100 $ gnatmake -gnat2022 -gnatwa -gnata bad_main.adb $ ./bad_main raised ADA.ASSERTIONS.ASSERTION_ERROR : failed precondition from accounts.ads:7
Function results
A function can constrain both its arguments and its result. The attribute
'Result names the value that the function is about to return, so
the postcondition can compare it with the arguments and with
'Old.
package Accounts is
type Account is tagged record
Balance : Integer := 0;
end record;
procedure Deposit (A : in out Account; Amount : Integer)
with Pre => Amount > 0,
Post => A.Balance = A.Balance'Old + Amount;
procedure Withdraw (A : in out Account; Amount : Integer)
with Pre => Amount > 0 and then Amount <= A.Balance,
Post => A.Balance = A.Balance'Old - Amount;
function Interest (A : Account; Rate : Natural) return Integer
with Pre => Rate <= 100,
Post => Interest'Result in 0 .. A.Balance;
end Accounts;
Interest requires a rate of at most 100 percent and promises
that the result lies between zero and the balance. The promise is part of the
specification, next to the other contracts.
package body Accounts is
procedure Deposit (A : in out Account; Amount : Integer) is
begin
A.Balance := A.Balance + Amount;
end Deposit;
procedure Withdraw (A : in out Account; Amount : Integer) is
begin
A.Balance := A.Balance - Amount;
end Withdraw;
function Interest (A : Account; Rate : Natural) return Integer is
begin
return A.Balance * Rate / 100;
end Interest;
end Accounts;
The body computes whole-number interest. With a balance of 200 and a rate of 5, the result is 10, which satisfies the postcondition.
with Ada.Text_IO;
with Accounts;
use Ada.Text_IO;
procedure Accounts_Main is
A : Accounts.Account := (Balance => 200);
begin
Put_Line ("interest =" & Integer'Image (A.Interest (5)));
A.Deposit (50);
Put_Line ("balance =" & Integer'Image (A.Balance));
end Accounts_Main;
The call uses dot notation because Account is tagged.
Deposit then raises the balance to 250, and both contracts
hold.
$ gnatmake -gnat2022 -gnatwa -gnata accounts_main.adb $ ./accounts_main interest = 10 balance = 250
Type invariants
An invariant is a condition that every value of a type must satisfy. It is
written on a private type or on its full view, and it is checked at the
boundaries of the package that owns the type: on entry to and on return from
its subprograms, and on assignment. Inside the body a value may pass through a
temporary state that breaks the invariant, which is exactly what a procedure
such as Bump needs.
package Counters is
type Counter is private;
procedure Bump (C : in out Counter; Step : Natural);
function Value (C : Counter) return Natural;
private
type Counter is record
Value : Natural := 0;
end record
with Type_Invariant => Counter.Value <= 100;
end Counters;
The visible part only promises the invariant; the full view in the private
part shows the component and states the condition. A client cannot see
Value, but the compiler can check it wherever a
Counter crosses the package boundary.
package body Counters is
procedure Bump (C : in out Counter; Step : Natural) is
begin
C.Value := C.Value + Step;
end Bump;
function Value (C : Counter) return Natural is
begin
return C.Value;
end Value;
end Counters;
Bump adds the step without any check of its own. The check
happens when the procedure returns.
with Ada.Text_IO;
with Counters;
use Ada.Text_IO;
procedure Counters_Main is
C : Counters.Counter;
begin
Counters.Bump (C, 60);
Put_Line ("value =" & Natural'Image (Counters.Value (C)));
Counters.Bump (C, 60);
Put_Line ("value =" & Natural'Image (Counters.Value (C)));
end Counters_Main;
The first bump takes the value to 60, which is valid. The second takes it to
120, and the invariant fails on the way out of Bump, before the
second Put_Line runs. The message points at the invariant in
counters.ads.
$ gnatmake -gnat2022 -gnatwa -gnata counters_main.adb $ ./counters_main value = 60 raised ADA.ASSERTIONS.ASSERTION_ERROR : failed invariant from counters.ads:11
Predicates
A predicate restricts a subtype to a set of values that a range cannot
describe. Static_Predicate is written with a set of choices that
the compiler can evaluate statically; Dynamic_Predicate may use
any expression, including one that depends on a value. Both are checked on
assignment and on parameter passing when assertions are enabled.
package Ratings is
subtype Star is Integer range 1 .. 5
with Static_Predicate => Star in 1 | 2 | 3 | 5;
subtype Even is Integer
with Dynamic_Predicate => Even mod 2 = 0;
end Ratings;
Star has the range 1 to 5 but excludes 4, and Even
accepts any integer that is a multiple of two. The first predicate could have
been written as a set of choices, the second could not.
with Ada.Text_IO;
with Ratings;
use Ada.Text_IO;
procedure Ratings_Main is
procedure Set_Star (S : in out Ratings.Star; Value : Integer) is
begin
S := Value;
end Set_Star;
S : Ratings.Star := 2;
E : constant Ratings.Even := 4;
begin
Put_Line ("star =" & Integer'Image (S));
Put_Line ("even =" & Integer'Image (E));
Set_Star (S, 5);
Put_Line ("star =" & Integer'Image (S));
Set_Star (S, 4);
Put_Line ("star =" & Integer'Image (S));
end Ratings_Main;
Set_Star assigns through a parameter, so the check happens when
the assignment runs rather than when the program is compiled. Assigning 5 is
accepted; assigning 4 raises Assertion_Error and reports that the
static predicate failed.
$ gnatmake -gnat2022 -gnatwa -gnata ratings_main.adb $ ./ratings_main star = 2 even = 4 star = 5 raised ADA.ASSERTIONS.ASSERTION_ERROR : Static_Predicate failed at ratings_main.adb:8
An applied example: a bounded stack
The final program puts the pieces together. The stack has a fixed capacity,
Size reports how many values it holds, and Push and
Pop state how the size changes. The invariant forbids a top index
above the capacity.
package Stacks is
Max : constant := 5;
type Stack is private;
function Size (S : Stack) return Natural;
procedure Push (S : in out Stack; Value : Integer)
with Pre => Size (S) < Max,
Post => Size (S) = Size (S'Old) + 1;
function Pop (S : in out Stack) return Integer
with Pre => Size (S) > 0,
Post => Size (S) = Size (S'Old) - 1;
private
type Values is array (1 .. Max) of Integer;
type Stack is record
Data : Values := [others => 0];
Top : Natural := 0;
end record
with Type_Invariant => Stack.Top <= Max;
end Stacks;
The postconditions use Size (S'Old), the size before the call.
Push promises one more element and Pop promises one
less, so a body that forgets to update the top index is caught at the
boundary.
package body Stacks is
function Size (S : Stack) return Natural is
begin
return S.Top;
end Size;
procedure Push (S : in out Stack; Value : Integer) is
begin
S.Top := S.Top + 1;
S.Data (S.Top) := Value;
end Push;
function Pop (S : in out Stack) return Integer is
Value : constant Integer := S.Data (S.Top);
begin
S.Top := S.Top - 1;
return Value;
end Pop;
end Stacks;
The bodies are short because the contracts carry the rules. Push
increments the index before storing the value, and Pop reads the
value before decrementing it.
with Ada.Text_IO;
with Ada.Exceptions;
with Stacks;
use Ada.Text_IO;
use Ada.Exceptions;
procedure Stacks_Main is
S : Stacks.Stack;
begin
for I in 1 .. 3 loop
Stacks.Push (S, I * 10);
end loop;
Put_Line ("size =" & Natural'Image (Stacks.Size (S)));
Put_Line ("top =" & Integer'Image (Stacks.Pop (S)));
Put_Line ("size =" & Natural'Image (Stacks.Size (S)));
begin
for I in 1 .. 5 loop
Stacks.Push (S, I);
end loop;
exception
when Error : others =>
Put_Line (Exception_Name (Error) & ": " & Exception_Message (Error));
end;
Put_Line ("size =" & Natural'Image (Stacks.Size (S)));
end Stacks_Main;
Three pushes fill the stack to 3, and the pop returns 30. The second loop
tries to push five more values onto a stack that already holds 2: the pushes
that fit are accepted, and the next one fails its precondition. The handler
prints the exception name and message, and the final size shows that exactly
five elements are in the stack. An Assertion_Error is an ordinary
exception, so a program can recover from a broken contract.
$ gnatmake -gnat2022 -gnatwa -gnata stacks_main.adb $ ./stacks_main size = 3 top = 30 size = 2 ADA.ASSERTIONS.ASSERTION_ERROR: failed precondition from stacks.ads:9 size = 5
Next steps
The next article introduces tasks, the units of concurrent execution. Contracts and tasks can be combined, but the checks in this article all concern a single thread; the next one has to deal with several at once.
Source
In this article we have written preconditions and postconditions with
'Old and 'Result, enabled their checks with
-gnata, declared a type invariant on a private type, restricted
subtypes with static and dynamic predicates, and protected a bounded stack
with contracts. The exceptions article explains
the Assertion_Error that a failed check raises.
Author
List all Ada tutorials.