Sum of Two Numbers - Ada-spark Typing CST Test
Loading…
Sum of Two Numbers — Ada-spark Code
A SPARK Ada program adding two numbers and printing the result.
with Ada.Text_IO;
procedure Sum_Numbers is
A, B, C : Integer;
begin
A := 5;
B := 7;
C := A + B;
Ada.Text_IO.Put_Line("Sum = " & Integer'Image(C));
end Sum_Numbers;Ada-spark Language Guide
SPARK is a formally verifiable subset of the Ada programming language designed for high-integrity and safety-critical systems. It enforces strong typing, design-by-contract, and static analysis to mathematically prove program correctness and eliminate entire classes of bugs.
Primary Use Cases
- ▸Avionics flight control software
- ▸Railway signaling and interlocking
- ▸Medical device firmware
- ▸Cybersecure embedded systems
- ▸Automotive safety ECUs
Notable Features
- ▸Design-by-contract with pre/postconditions
- ▸Strong static typing and modularization
- ▸GNATprove formal verification
- ▸Guaranteed absence of runtime errors (AoRTE)
- ▸High-integrity code generation support
Origin & Creator
Developed by Praxis (now Altran UK) and later extended by AdaCore, leading vendors of Ada technology for mission- and safety-critical software.
Industrial Note
SPARK is used where DO-178C Level A or IEC 61508 SIL-4 compliance is required, enabling mathematical proof of correctness for safety-critical and security-critical applications.
Quick Explain
- ▸SPARK is a subset of Ada that removes unsafe or ambiguous features.
- ▸Uses contracts (pre/postconditions, invariants) for formal correctness.
- ▸GNATprove tool performs static analysis and proofs.
- ▸Designed for avionics, medical, defense, and railway systems.
- ▸Guarantees freedom from runtime errors (overflow, null dereference, etc.) through proofs.
Core Features
- ▸Packages, procedures, functions
- ▸Subtype constraints and range checks
- ▸Contracts (pragma Assume, Assert, Post, Pre, Loop_Invariant)
- ▸SPARK proof annotations
- ▸Pure, deterministic code model
Learning Path
- ▸Learn Ada fundamentals
- ▸Understand SPARK restrictions
- ▸Add simple contracts
- ▸Use GNATprove
- ▸Master formal program design
Practical Examples
- ▸Prove array bounds safety
- ▸Build a verified stack ADT
- ▸Prove correctness of a PID controller
- ▸Motor controller with no runtime errors
- ▸Train braking logic verification
Comparisons
- ▸SPARK vs Ada: SPARK is the provable safe subset.
- ▸SPARK vs Rust: Rust focuses on memory safety; SPARK proves full correctness.
- ▸SPARK vs MISRA C: SPARK is more rigorous and verifiable.
- ▸SPARK vs SCADE: SPARK is code-level proofs; SCADE is model-based.
- ▸SPARK vs Eiffel: SPARK contracts are verifiable, not runtime only.
Strengths
- ▸Mathematical proof of correctness
- ▸Ultra-reliable for safety-critical domains
- ▸Eliminates runtime exceptions
- ▸Highly readable and maintainable syntax
- ▸Standardized and long-supported language
Limitations
- ▸Restricted subset compared to full Ada
- ▸Verification requires discipline and learning
- ▸Not suitable for rapid UI/desktop apps
- ▸Runtime features like dynamic allocation limited
- ▸Build times grow with extensive proofs
When NOT to Use
- ▸Rapid prototyping or scripting
- ▸Feature-rich GUIs or web apps
- ▸Highly dynamic memory systems
- ▸Non-critical hobby projects
- ▸Where verification costs outweigh benefits
Cheat Sheet
- ▸pragma SPARK_Mode(On)
- ▸with Pre => Condition
- ▸with Post => Condition
- ▸Loop_Invariant => Expression
- ▸Run: gnatprove -P project.gpr
FAQ
- ▸Is SPARK a separate language? -> No, a subset of Ada.
- ▸Can SPARK prove all logic? -> No, but proves safety properties.
- ▸Does SPARK allow pointers? -> Strictly controlled, mostly disallowed.
- ▸Can you mix Ada and SPARK? -> Yes, with boundaries.
- ▸Is runtime checking needed? -> Often eliminated after proofs.
30-Day Skill Plan
- ▸Week 1: Ada basics
- ▸Week 2: SPARK subset rules
- ▸Week 3: Contracts and subtypes
- ▸Week 4: GNATprove practice
- ▸Week 5: Verified ADTs and loops
Final Summary
- ▸SPARK is a verifiable subset of Ada for ultra-safe systems.
- ▸Used in avionics, defense, rail, and medical industries.
- ▸GNATprove enables mathematical correctness proofs.
- ▸Eliminates runtime exceptions and logic flaws.
- ▸Ideal where safety and reliability are non-negotiable.
Project Structure
- ▸src/ for code
- ▸gpr project file
- ▸spec (.ads) and body (.adb) files
- ▸proof annotations
- ▸test and harness modules
Monetization
- ▸Safety-critical software consulting
- ▸DO-178C verification services
- ▸Railway or avionics tool integration
- ▸Formal verification training
- ▸Certified code development
Productivity Tips
- ▸Write contracts early
- ▸Use subtypes to avoid runtime errors
- ▸Run proofs continuously
- ▸Minimize global dependencies
- ▸Keep procedures small and clear
Basic Concepts
- ▸Packages - modular code units
- ▸Subtypes - type-safe constraints
- ▸Contracts - correctness rules
- ▸Proof obligations - automatically generated checks
- ▸Abstract data types - encapsulation for safety