Ada Lab Examples
Fifteen runnable Ada programs that follow the track's lesson order, grouped by phase. Every file is self-contained and fully commented. Compile them with GNAT (
gnatmake file.adb — see the run notes at the bottom) or open them in the code viewer and change one thing, then compile again. That second run is where the learning happens.
Foundations
Hello world and the typed declaration discipline — the two files every later demo assumes you have seen.
| # | Description | Link |
|---|---|---|
| 1 | Hello Ada — main procedure, context clause, Put_Line | View |
| 2 | Types and ranges — new types, subtypes, Constraint_Error | View |
Basic Constructs
Selection, repetition, text, and enumerations — the everyday logic vocabulary.
| # | Description | Link |
|---|---|---|
| 3 | Conditionals — if/elsif and a compiler-checked case | View |
| 4 | Loops — for, reverse, while, loop/exit, labels | View |
| 5 | Strings — fixed, slices, unbounded growth | View |
| 6 | Enumerations — attributes, case coverage, representation | View |
Data & Logic
Arrays, records with variants, safe references, and decomposition into subprograms.
| # | Description | Link |
|---|---|---|
| 7 | Arrays — aggregates, slices, enum-indexed arrays | View |
| 8 | Records — nesting, variant discriminants, defaults | View |
| 9 | Access types — allocation, dereference, null safety | View |
| 10 | Subprograms — modes, overloading, expression functions | View |
Modular Systems
The first multi-file demos: a package with a private type, exception flow, and a generic container.
| # | Description | Link |
|---|---|---|
| 11 | Package spec (geometry.ads) — private type + API | View |
| 12 | Package body (geometry.adb) — the implementation | View |
| 13 | Client of the package + exception handling | View |
| 14 | Exceptions — raise, propagate, messages | View |
| 15 | Generics — a generic stack, instantiated twice | View |
Advanced Paradigms
Tagged dispatch, contracts, concurrency, and the standard containers.
| # | Description | Link |
|---|---|---|
| 16 | OOP — tagged types, overriding, class-wide dispatch | View |
| 17 | Contracts — pre/postconditions and predicates | View |
| 18 | Tasking — tasks, protected bounded buffer | View |
| 19 | Standard library — Ada.Containers.Vectors and maps | View |
How to run them. Compile straight from the demo folder:
gnatmake demo/foundations/01_hello.adb then ./01_hello. The multi-file package pair compiles together: gnatmake demo/modular/12_package_main.adb (GNAT finds the spec and body automatically). Demos 3–14 need only GNAT; 17's delays make the interleaving visible; 18 needs any GNAT with the standard library. Larger multi-file study projects are on the Study Projects page.