### Install SPARK from tarball on Linux/Mac Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Extract and install the SPARK tarball on Linux/Mac. Run these commands in sequence, then follow the displayed instructions. Requires sufficient rights for the chosen installation location. ```bash $ gzip -dc spark-pro---bin.tar.gz | tar xf - $ cd spark-pro---bin $ ./doinstall ``` -------------------------------- ### Install GNAT Studio from tarball on Linux/Mac Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Extract and install the GNAT Studio compressed tarball on Linux/Mac. Run these commands in sequence, then follow the displayed instructions. ```bash $ gzip -dc gnatstudio---bin.tar.gz | tar xf - $ cd gnatstudio---bin $ ./doinstall ``` -------------------------------- ### Install package to copy GNATprove summary files for My_Lib Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/how_to_run_gnatprove.rst Add this Install package to the library project file when using older versions of gprinstall that do not automatically install GNATprove summary files. The source pattern uses My_Lib'Library_ALI_Dir so summary files are picked up wherever GNATprove produced them. The destination defaults to lib/ under the installation prefix and must be adjusted to match the actual installation layout. ```Ada package Install is for Artifacts ("lib/my_lib/gnatprove") use (My_Lib'Library_ALI_Dir & "/gnatprove/*.ali"); end Install; ``` -------------------------------- ### Pragma SPARK_Mode (On) example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/spark_mode.rst Example of using the pragma with On argument. ```ada pragma SPARK_Mode (On); package P is ``` -------------------------------- ### T1Q3 package body with Swap, NandGate, NextDay_A, NextDay_B implementations Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst Package body for T1Q3. Provides implementations for Swap, NandGate, NextDay_A (using Day'Succ), and NextDay_B (using a case statement). GNATprove discharges all VCs for this example. ```ada package body T1Q3 is procedure Swap(X,Y: in out Integer) is Temp: Integer; begin Temp := X; X := Y; Y := Temp; end Swap; procedure NandGate(P,Q: in Boolean; R: out Boolean) is begin R := not (P and Q); -- simplest implementation end NandGate; procedure NextDay_A(Today: in Day; Tomorrow: out Day) -- This is implementation (a) of NextDay, in which Day'Succ is used is begin if Today = Sun then Tomorrow := Mon; else Tomorrow := Day'Succ(Today); end if; end NextDay_A; procedure NextDay_B(Today: in Day; Tomorrow: out Day) -- This is implementation (b) of NextDay, in which a case-statement is used is begin case Today is when Mon => Tomorrow := Tue; when Tue => Tomorrow := Wed; when Wed => Tomorrow := Thu; when Thu => Tomorrow := Fri; when Fri => Tomorrow := Sat; when Sat => Tomorrow := Sun; when Sun => Tomorrow := Mon; end case; end NextDay_B; end T1Q3; ``` -------------------------------- ### Selection sort specification (sort.ads) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Specification file for the selection sort example. Used together with sort.adb to verify the Selection_Sort procedure. ```ada .. literalinclude:: /examples/ug__sort/sort.ads :language: ada ``` -------------------------------- ### Add gnatprove dependency with alire Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Add the gnatprove dependency to an alire project. Alire will download gnatprove if necessary. ```bash alr with gnatprove ``` -------------------------------- ### GNATprove output for selection sort (test.out) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Output from GNATprove verifying the selection sort at level 2. ```none .. literalinclude:: /examples/ug__sort/test.out :language: none ``` -------------------------------- ### Prohibited example: package P with non-preelaborable initialization Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst This example is prohibited by the elaboration rules because the initialization of Flag calls F, which would fail elaboration checks. ```ada package P is function F return Boolean; Flag : Boolean := F; -- would fail elaboration checks end; ``` -------------------------------- ### GNATprove output with Alt-Ergo (test.out) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Output showing that Alt-Ergo does not prove the postcondition automatically, even with increased timeout. ```none .. literalinclude:: /examples/ug__nonlinear/test.out :language: none ``` -------------------------------- ### Aspect SPARK_Mode => On example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/spark_mode.rst Example of using the aspect with On argument. ```ada package P with SPARK_Mode => On is ``` -------------------------------- ### Example with pattern (f (g x)) and equality Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with a pattern on (f (g x)). Includes a negation and an equality. ```smt2 % (forall ((x Int)) (! (= (f (g x)) 0) :pattern ((f (g x))))) % (not (= (f c) 0)) % (= (g 0) c) % ``` -------------------------------- ### Example with pattern (f (+ x 1)) and arithmetic Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with a pattern on (f (+ x 1)). Includes arithmetic and a negation. ```smt2 % (forall ((x Int)) (! (= (f (+ x 1)) (+ (f x) 1)) % :pattern ((f (+ x 1))))) % (= (f 0) 0) % (not (= (f 1) 1)) % ``` -------------------------------- ### GNATprove output for lemma proof (test.out) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Output from GNATprove proving all checks on the final program at level 2. ```none .. literalinclude:: /examples/ug__perm/test.out :language: none ``` -------------------------------- ### GNATprove Output for Incr_All_Bad_Init Example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/subprogram_contracts.rst Output from GNATprove for the Incr_All_Bad_Init example. ```none -- content from /examples/ug__exceptions_bad_init/test.out ``` -------------------------------- ### Example with two variables and function g Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with two universally quantified variables and a function g. ```smt2 % (forall ((x Int) (y Int)) (= (g (f x) y) (g x y))) % ``` -------------------------------- ### SMTv2 task example with check-sat and get-model Source: https://github.com/adacore/spark2014/blob/master/docs/develguide/counterexamples.rst Example SMTv2 task that declares constants, asserts constraints, and checks satisfiability. The prover answers sat and provides a model. ```Ada (declare-const a Int) (declare-const b Int) (assert (= b 2)) (assert (= a (- 2 ))) (assert (in_range (* a b))) (check-sat) (get-model) ``` ```Ada sat (model (define-fun a () Int (- 2)) (define-fun b () Int 2) ) ``` -------------------------------- ### Coq proof script for postcondition Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Final proof script for the postcondition of a nonlinear example, using the intuition tactic to solve the goal. The script applies Z.quot_le_compat_l and Z.le_trans with m:=1%Z, and uses dynamic_invariant1 and in_range1 hypotheses. ```coq Theorem def'vc : forall (r1:Numbers.BinNums.Z) (r2:Numbers.BinNums.Z), dynamic_invariant1 x Init.Datatypes.true Init.Datatypes.false Init.Datatypes.true Init.Datatypes.true -> dynamic_invariant1 y Init.Datatypes.true Init.Datatypes.false Init.Datatypes.true Init.Datatypes.true -> dynamic_invariant1 z Init.Datatypes.true Init.Datatypes.false Init.Datatypes.true Init.Datatypes.true -> dynamic_invariant r1 Init.Datatypes.false Init.Datatypes.false Init.Datatypes.true Init.Datatypes.true -> dynamic_invariant r2 Init.Datatypes.false Init.Datatypes.false Init.Datatypes.true Init.Datatypes.true -> (z < y)%Z -> forall (r11:Numbers.BinNums.Z), (r11 = (ZArith.BinInt.Z.quot x y)) -> forall (r21:Numbers.BinNums.Z), (r21 = (ZArith.BinInt.Z.quot x z)) -> (r11 <= r21)%Z. Proof. intros r1 r2 h1 h2 h3 h4 h5 h6 r11 h7 r21 h8. subst. apply Z.quot_le_compat_l. apply Z.le_trans with (m:=1%Z). (* 0 <= x *) - apply Z.le_0_1. (* 1 <= x *) - unfold dynamic_invariant1, in_range1 in h1. apply h1. left. apply eq_refl. (* 0 < z <= y *) - unfold dynamic_invariant1, in_range1 in h3. intuition. Qed. ``` -------------------------------- ### Ada package with private type and expression function Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/translation_why3.md Example package P with SPARK_Mode showing a private type T, a deferred constant C, and an expression function Get. The proof context includes the record model, constant value, and function definition, allowing clients to prove P.Get (P.C) = 1 without user-written models. ```ada package P with SPARK_Mode is type T is private; C : constant T; function Get (X : T) return Integer; private type T is record F : Integer; end record; C : constant T := (F => 1); function Get (X : T) return Integer is (X.F); end P; ``` -------------------------------- ### Example Target Configuration File for PowerPC 750 Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/how_to_run_gnatprove.rst Example of a target.atp configuration file for a bare board PowerPC 750 processor configured as big-endian. This file is referenced by the '-gnateT' switch and contains target-dependent values such as endianness and sizes. ```text Bits_BE 1 Bits_Per_Unit 8 Bits_Per_Word 32 Bytes_BE 1 Char_Size 8 Double_Float_Alignment 0 Double_Scalar_Alignment 0 Double_Size 64 Float_Size 32 Float_Words_BE 1 Int_Size 32 Long_Double_Size 64 Long_Long_Size 64 Long_Size 32 Maximum_Alignment 16 Max_Unaligned_Field 64 Pointer_Size 32 Short_Enums 0 Short_Size 16 Strict_Alignment 1 System_Allocator_Alignment 8 Wchar_T_Size 32 Words_BE 1 float 6 I 32 32 double 15 I 64 64 long double 15 I 64 64 ``` -------------------------------- ### GNATprove Output for exceptions_post Example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/subprogram_contracts.rst Output from GNATprove for the exceptions_post example. ```none -- content from /examples/ug__exceptions_post/test.out ``` -------------------------------- ### GNATprove Output for Incr_All Example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/subprogram_contracts.rst Output from GNATprove verifying the Incr_All example and its callers. ```none -- content from /examples/ug__exceptions/test.out ``` -------------------------------- ### Example with pattern (g x) and disjunction Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with a pattern on (g x). The commented lines show a disjunction and a negation. ```smt2 % (or (= (g c) b) (= (f c) b)) % (not (= (f c) c)) % (forall ((x Int)) (! (= (f x) c) :pattern ((g x)))) % ``` -------------------------------- ### Example with pattern (g x) and negated quantifier Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with a pattern on (g x) and a negated quantifier. ```smt2 % (forall ((x Int)) (! (= (f x) c) :pattern ((g x)))) % (not (forall ((x Int)) (! (= (f x) c) :pattern ((g x))))) % ``` -------------------------------- ### FullStack function declaration Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst Function FullStack is referenced in the precondition to Push and currently has no postcondition. ```ada function FullStack(S : in Stack) return Boolean; ``` -------------------------------- ### Build and install SPARK 2014 with make Source: https://github.com/adacore/spark2014/blob/master/AGENTS.md Use `make` to build and `make install-all` to install. The gnatprove testsuite does not rebuild tools automatically, so re-run these commands before testing source changes. ```shell make ``` ```shell make install-all ``` -------------------------------- ### GNATprove invocation with --level=2 Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/how_to_view_gnatprove_output.rst Example command to run GNATprove on a project with level 2. This is the initial invocation that proves all checks. ```bash gnatprove -P --level=2 ``` -------------------------------- ### Example with conjunction and negated equality Source: https://github.com/adacore/spark2014/blob/master/docs/papers/triggers_SMT_2012/triggers_SMT_2012.tex SMT-LIB formula with a conjunction and a negated equality. ```smt2 % (and (= (f 1) (f 0)) (not (= (g 1 0) (g 0 0)))) % ``` -------------------------------- ### Simple SPARK procedure for manual proof (nonlinear.adb) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst A simple SPARK procedure used to demonstrate manual proof with Coq when GNATprove fails to prove the postcondition automatically. ```ada .. literalinclude:: /examples/ug__nonlinear/nonlinear.adb :language: ada :linenos: ``` -------------------------------- ### Load session and initialize controller Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/gnatwhy3.md Shows how to load a session from a directory and create a controller with the configuration and environment. ```Ocaml let session_dir = get_session_dir () in let (session, use_shapes) = Session_itp.load_session session_dir in ``` ```Ocaml let c = Controller_itp.create_controller Gnat_config.config Gnat_config.env session in ``` ```Ocaml if b then Controller_itp.add_file c Gnat_config.filename; if a then Controller_itp.reload_files c ~use_shapes; c ``` -------------------------------- ### Coq theorem Z.quot_le_compat_l Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Theorem found via SearchAbout Z.quot. Its conclusion matches the current goal, enabling the apply tactic. ```coq Z.quot_le_compat_l: forall p q r : int, (0 <= p)%Z -> (0 < q <= r)%Z -> (p ÷ r <= p ÷ q)%Z ``` -------------------------------- ### GNATprove output for Increment_Local (test.out) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/basic.rst Output from GNATprove proving the local Increment example. Shows that the assertion on line 15 holds. ```none .. literalinclude:: /examples/ug__increment_local/test.out :language: none ``` -------------------------------- ### Set environment for Alire project Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Set the environment for your Alire project so you can call gnatprove from the command line or in your editor. ```bash eval `alr printenv` ``` -------------------------------- ### Package Initialization_And_Elaboration with Initializes and Initial_Condition Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst Shows a library unit package that initializes a variable V from P.VP and declares a state abstraction with Initializes and Initial_Condition aspects. ```ada with P; pragma Elaborate_All (P); -- P.VP is used in initialization of V package Initialization_And_Elaboration with Abstract_State => State, Initializes => (State, V => P.VP), -- Initializing V depends on P.VP Initial_Condition => V = P.VP and Get_It = 0 is V : Integer := P.VP; procedure Do_It (I : in Integer) with Global => (In_Out => State); function Get_It return Integer with Global => State; end Initialization_And_Elaboration; ``` -------------------------------- ### Implement lemma with pragma Assume Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Implements the lemma procedure body with a single assumption using pragma Assume. This is an alternative to the null procedure implementation. ```ada procedure Number_Is_Prime (N : Positive) is begin pragma Assume (Is_Prime (N)); end Number_Is_Prime; ``` -------------------------------- ### Open GNAT Studio on the project Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/main.md Switch to GNAT Studio for easier interaction with GNATprove, including code navigation and error location. ```none > gnatstudio -P my_project.gpr ``` -------------------------------- ### Set GPR_PROJECT_PATH in C shell Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Use this command on Linux/Mac with C shell to set the GPR_PROJECT_PATH environment variable when GNATprove is installed in a different location. It adds the GNAT library and share/gpr directories and the SPARK lemma library project directory. ```csh setenv GPR_PROJECT_PATH /lib/gnat:/share/gpr:/lib/gnat:$GPR_PROJECT_PATH ``` -------------------------------- ### GNATprove command line for proving all Tokeneer files Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst Command generated by GPS's 'Prove > Prove All' option for analysing all Tokeneer files. It uses a 5-second timeout and reports all verification conditions. The output appears in GPS's locations pane and was copied to a file for counting VCs. ```ada gnatprove -Ptest.gpr --ide-progress-bar --show-tag -U -j4 --report=all --timeout=5 ``` -------------------------------- ### Coq proof state after apply Z.quot_le_compat_l Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst State after applying Z.quot_le_compat_l, generating two subgoals: (0 <= x)%Z and (0 < z <= y)%Z. ```coq 2 subgoals r1, r2 : int h1 : dynamic_invariant1 x true false true true h2 : dynamic_invariant1 y true false true true h3 : dynamic_invariant1 z true false true true h4 : dynamic_invariant r1 false false true true h5 : dynamic_invariant r2 false false true true h6 : (z < y)%Z ______________________________________(1/2) (0 <= x)%Z ______________________________________(2/2) (0 < z <= y)%Z ``` -------------------------------- ### Set GPR_PROJECT_PATH in Bourne shell Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/install.rst Use this command on Linux/Mac with Bourne shell to set the GPR_PROJECT_PATH environment variable when GNATprove is installed in a different location. It adds the GNAT library and share/gpr directories and the SPARK lemma library project directory. ```bash export GPR_PROJECT_PATH=/lib/gnat:/share/gpr:/lib/gnat:$GPR_PROJECT_PATH ``` -------------------------------- ### Initial Coq proof state after intros Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Shows the goal state after the automatically added intros tactic, with universally quantified variables and hypotheses in the environment. The goal is reduced to the inequality (r11 <= r21)%Z. ```coq 1 subgoal r1, r2 : int h1 : dynamic_invariant1 x true false true true h2 : dynamic_invariant1 y true false true true h3 : dynamic_invariant1 z true false true true h4 : dynamic_invariant r1 false false true true h5 : dynamic_invariant r2 false false true true h6 : (z < y)%Z r11 : int h7 : r11 = (x ÷ y)%Z r21 : int h8 : r21 = (x ÷ z)%Z ______________________________________(1/1) (r11 <= r21)%Z ``` -------------------------------- ### Linux: Set environment and build executable Source: https://github.com/adacore/spark2014/blob/master/testsuite/gnatprove/tests/ironsides/README.txt Sets environment variables and builds the executable with gnatmake. Paths need to be adjusted to your installation. ```shell cd ../ # you'll need to change the paths below to yours export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu export PATH=/usr/gnat/bin:$PATH gnatmake -gnat05 -O3 -gnatp -I/home/student/sparkgpl/lib/spark -I/home/student/sparkgpl/lib/spark/current spark_dns_main ``` -------------------------------- ### Windows: Build executable with gnatmake Source: https://github.com/adacore/spark2014/blob/master/testsuite/gnatprove/tests/ironsides/README.txt Creates an executable on Windows using GNAT and SPARK. Assumes SPARK is installed to c:\spark\2012. ```shell gnatmake -gnat05 -O3 -gnatp -Ic:\spark\2012\lib\spark -Ic:\spark\2012\lib\spark\current spark_dns_main ``` -------------------------------- ### GNATprove output confirming all checks proved Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Expected output from GNATprove after running the proof, showing that the postcondition, range checks, and division checks are all proved. ```text nonlinear.adb:4:11: info: postcondition proved nonlinear.adb:7:12: info: range check proved nonlinear.adb:7:12: info: division check proved nonlinear.adb:8:12: info: range check proved nonlinear.adb:8:12: info: division check proved ``` -------------------------------- ### GNATprove output for Silver Level example Source: https://github.com/adacore/spark2014/blob/master/docs/develguide/main.md GNATprove emits medium severity messages about possible precondition failures, with possible fixes suggesting postconditions. ```none 13:06: medium: precondition might fail, cannot prove not Full (This) 14:06: medium: precondition might fail, cannot prove not Full (This) [possible fix: call at line 13 should mention This (for argument S1) in a postcondition] 15:35: medium: precondition might fail, cannot prove not Empty (This) [possible fix: call at line 14 should mention This (for argument S1) in a postcondition] ``` -------------------------------- ### GNATprove Output for exceptions_bad Example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/subprogram_contracts.rst Output from GNATprove showing the error for the exceptions_bad example. ```none -- content from /examples/ug__exceptions_bad/test.out ``` -------------------------------- ### ClearLogEntries specification with moved pre/postconditions Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst Shows the package specification of ClearLogEntries after moving preconditions and postconditions from the body, referencing UsedLogFiles and its type LogFilesListT which were also moved to the specification. The precondition and postcondition both require UsedLogFiles.Length >= 1 to avoid underflow in the expression UsedLogFiles.Length - 1. ```ada procedure ClearLogEntries (User : in AuditTypes.UserTextT); --# global in ConfigData.State; --# in Clock.Now; --# in out FileState; --# in out State; --# derives FileState, --# State from FileState, --# State, --# ConfigData.State, --# User, --# Clock.Now; pragma Precondition (UsedLogFiles.Length >= 1 and then NumberLogEntries = LogEntryCountT(UsedLogFiles.Length -1)*MaxLogFileEntries + LogFileEntries(CurrentLogFile)); pragma Postcondition (UsedLogFiles.Length >= 1 and then NumberLogEntries = LogEntryCountT(UsedLogFiles.Length -1)*MaxLogFileEntries + LogFileEntries(CurrentLogFile)); ``` -------------------------------- ### Functional.Total_Maps: Get and Set primitives Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Declares the Get and Set primitives for SPARK.Containers.Functional.Total_Maps. In total maps every key is mapped to an element; keys not explicitly set are mapped to a default element supplied at instantiation. As a result, Get is total and there is no Has_Key primitive. ```ada function Get (M : Map; K : Key_Type) return Element_Type; function Set (M : Map; K : Key_Type; E : Element_Type) return Map; ``` -------------------------------- ### Run Why3 on generated .mlw file with SPARK libraries Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/counterexamples.md Use this command to launch Why3 directly on a generated .mlw file for debugging counterexamples. It requires the generated why3.conf file and SPARK libraries instead of standard ones. Replace and with your values. ```default why3 prove -C why3.conf -L ~/spark2014/install/share/spark/theories -L ~/spark2014/install/libexec/spark/share/why3/theories/ --no-stdlib -P cvc5_ce -a split_vc --check-ce --rac-prover=cvc5_ce --debug= ``` -------------------------------- ### T1Q3 package specification with Swap, NandGate, NextDay_A, NextDay_B Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst Package specification for the T1Q3 tutorial. Provides specifications for Swap, NandGate, and two versions of NextDay (NextDay_A and NextDay_B) with SPARK 2014 aspects and SPARK 2005 annotations. The NandGate postcondition shows equivalence between implication and 'if A then B'. ```ada package T1Q3 is type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); procedure Swap(X,Y: in out Integer) with Post => ((X = Y'Old) and (Y = X'Old)); --# derives X from Y & Y from X; --# post X = Y~ and Y = X~; procedure NandGate(P,Q: in Boolean; R: out Boolean) with Post => ((if ((not P) and (not Q)) then R) and (if ((not P) and Q) then R) and (if (P and (not Q)) then R) and (if (P and Q) then (not R))); --# derives R from P, Q; --# post ( ((not P) and (not Q)) -> R ) and --# ( ((not P) and Q ) -> R ) and --# ( ( P and (not Q)) -> R ) and --# ( ( P and Q ) -> not R); -- The above is the truth table for "A nand B" procedure NextDay_A(Today: in Day; Tomorrow: out Day) with Post => ((Today=Mon and Tomorrow=Tue) or (Today=Tue and Tomorrow=Wed) or (Today=Wed and Tomorrow=Thu) or (Today=Thu and Tomorrow=Fri) or (Today=Fri and Tomorrow=Sat) or (Today=Sat and Tomorrow=Sun) or (Today=Sun and Tomorrow=Mon)); --# derives Tomorrow from Today; --# post (Today=Mon and Tomorrow=Tue) or --# (Today=Tue and Tomorrow=Wed) or --# (Today=Wed and Tomorrow=Thu) or --# (Today=Thu and Tomorrow=Fri) or --# (Today=Fri and Tomorrow=Sat) or --# (Today=Sat and Tomorrow=Sun) or --# (Today=Sun and Tomorrow=Mon); procedure NextDay_B(Today: in Day; Tomorrow: out Day) with Post => ((Today=Mon and Tomorrow=Tue) or (Today=Tue and Tomorrow=Wed) or (Today=Wed and Tomorrow=Thu) or (Today=Thu and Tomorrow=Fri) or (Today=Fri and Tomorrow=Sat) or (Today=Sat and Tomorrow=Sun) or (Today=Sun and Tomorrow=Mon)); --# derives Tomorrow from Today; --# post (Today=Mon and Tomorrow=Tue) or --# (Today=Tue and Tomorrow=Wed) or --# (Today=Wed and Tomorrow=Thu) or --# (Today=Thu and Tomorrow=Fri) or --# (Today=Fri and Tomorrow=Sat) or --# (Today=Sat and Tomorrow=Sun) or --# (Today=Sun and Tomorrow=Mon); end T1Q3; ``` -------------------------------- ### Compile and run test_search.adb with gprbuild Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/tutorial.rst Compiles and runs the test program test_search.adb using the command-line interface. Expected output shows two OK messages confirming the test passes. ```bash $ gprbuild test_search.adb $ test_search > OK: Found existing value at first index > OK: Did not find non-existing value ``` -------------------------------- ### Functional.Maps: Has_Key and Get primitives Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Declares the Has_Key and Get primitives for SPARK.Containers.Functional.Maps. Functional maps offer a dictionary between any two types of elements. ```ada function Has_Key (M : Map; K : Key_Type) return Boolean; function Get (M : Map; K : Key_Type) return Element_Type; ``` -------------------------------- ### Functional.Infinite_Sequences: Length and Get primitives Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Declares the Length and Get primitives for SPARK.Containers.Functional.Infinite_Sequences. These sequences behave like Functional.Vectors but are indexed by mathematical integers. ```ada function Length (Container : Sequence) return Big_Natural; function Get (Container : Sequence; Position : Big_Integer) return Element_Type; ``` -------------------------------- ### Functional.Vectors: Length and Get primitives Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Declares the Length and Get primitives for SPARK.Containers.Functional.Vectors. Sequences are ordered collections of elements indexed by a user-chosen range. ```ada function Length (S : Sequence) return Count_Type; function Get (S : Sequence; N : Index_Type) return Element_Type; ``` -------------------------------- ### Run integration testsuite with SPARK and GNAT Studio Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/miscellaneous.md Command to launch the complete integration testsuite with SPARK and GNAT Studio tools. Requires Xvcd installed and SPARK/GNAT Studio available. ```bash ./testsuite --with-tools=spark,gps ``` -------------------------------- ### Linux: Extract and install GNAT and SPARK Source: https://github.com/adacore/spark2014/blob/master/testsuite/gnatprove/tests/ironsides/README.txt Downloads and installs GNAT and SPARK on Linux. Warns against using the Debian apt-get GNAT due to a bug. ```shell gzip -d *.gz ``` ```shell tar xpf *.tar ``` ```shell make sure you have a C compiler installed (e.g. on Ubuntu, apt-get install build-essential) ``` ```shell cd gnat-2012-x86_64-pc-linux-gnu-bin sudo ./doinstall ``` -------------------------------- ### Coq proof state after apply Z.le_trans with (m:=1%Z) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Shows the proof state after applying the transitivity lemma with m instantiated to 1. The goal splits into three subgoals: proving 0 <= 1, 1 <= x, and the original goal (0 < z <= y). ```coq 3 subgoals r1, r2 : int h1 : true = true \/ (1 <= 2147483647)%Z -> (1 <= x <= 2147483647)%Z h2 : dynamic_invariant1 y true false true true h3 : dynamic_invariant1 z true false true true h4 : dynamic_invariant r1 false false true true h5 : dynamic_invariant r2 false false true true h6 : (z < y)%Z ______________________________________(1/3) (0 <= 1)%Z ______________________________________(2/3) (1 <= x)%Z ______________________________________(3/3) (0 < z <= y)%Z ``` -------------------------------- ### Ada function declaration for access attribute example Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/translation_why3.md Example Ada function declaration used to illustrate the Why3 translation of the 'Access attribute on subprograms. ```ada function F (X1 : T1; ...) return T; ``` -------------------------------- ### Run gnatprove validation on bad example Source: https://github.com/adacore/spark2014/blob/master/skills/explain-code/SKILL.md Run gnatprove in check mode with oneline output against the bad example project to confirm at least one message lands on a -- error line. ```shell gnatprove -P ec-test/default.gpr --mode=check -j0 --output=oneline ``` -------------------------------- ### GNATprove command to check and save proof Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Command to run GNATprove with Coq prover, targeting the postcondition of nonlinear.adb at line 4, column 11, and reporting all checks. ```bash gnatprove -P .gpr --limit-line=nonlinear.adb:4:11:VC_POSTCONDITION --prover=Coq --report=all ``` -------------------------------- ### Ada package body Q and P with procedures for intra-package analysis Source: https://github.com/adacore/spark2014/blob/master/docs/flow/generated_globals_2016_redesign/gg_redesign.tex Provides the bodies of nested package Q and enclosing package P, showing procedure implementations that call each other and update globals. This is the second part of the intra-package analysis example, used to compute phase 1 closures. ```latex package body Q with Refined_State => (Nested => (G3, G4)) is G3 : Boolean := False; G4 : Boolean := G1; begin procedure P1 is begin P2; G3 := False; end P1; procedure P4 is begin P5; G3 := not G3; end P4; procedure P5 is begin G3 := G1; G4 := G2; end P5; end Q; procedure P0 is begin Q.P1; G1 := False; end P0; procedure P2 is begin P3; G0 := False; G2 := False; end P2; procedure P3 is begin Q.P4; end P3; end P; ``` -------------------------------- ### Exchange_With_Post_Uninitialized: Exchange with uninitialized variable and postcondition Source: https://github.com/adacore/spark2014/blob/master/docs/case_study/doc/case_study.rst A version with an uninitialized variable T and a postcondition. This is identical to the 'Exchange with uninitialized variable' example but with the addition of a postcondition. ```ada procedure Exchange_With_Post_Uninitialized (X, Y : in out Integer) with Post => (X = Y'Old and Y = X'Old); procedure Exchange_With_Post_Uninitialized (X, Y : in out Integer) is T : Integer; begin X := Y; Y := T; end Exchange_With_Post_Uninitialized; ``` -------------------------------- ### Package body Q with globals annotations Source: https://github.com/adacore/spark2014/blob/master/docs/flow/generated_globals_2016_redesign/gg_redesign.tex Implements package Q with local variable G3 and procedures P2 and P3, each annotated with the most precise globals possible (In_Out or Output) assuming the language extension to summarize hidden state. ```latex use P; package body Q is G3 : Boolean := False; procedure P2 is begin if G3 then P3; else G3 := True; end if; end P2; -- In_Out $\rightarrow$ (G3, P) procedure P3 is begin P.P4; end P3; -- Output $\rightarrow$ P end Q; ``` -------------------------------- ### Refined_Depends package specification example Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst Declares a package with SPARK_Mode and an abstract state, showing subprograms with Depends aspects that will be refined in the body. The package body example provides the corresponding Refined_State and constituent declarations. ```ada package Refined_Depends_Examples with SPARK_Mode, Abstract_State => State is procedure P1_1 (I : in Integer) with Global => (In_Out => State), Depends => (State =>+ I); procedure P1_2 (I : in Integer) with Global => (In_Out => State), Depends => (State =>+ I); procedure P1_3 (Result : out Integer) with Global => (Input => State), Depends => (Result => State); procedure P1_4 (I : in Integer) with Global => (Output => State), Depends => (State => I); end Refined_Depends_Examples; ``` ```ada package body Refined_Depends_Examples with SPARK_Mode, Refined_State => (State => (A, B)) is A : Integer; -- The constituents of State B : Integer; ``` -------------------------------- ### Loop_Entry rule example: declare block with loop and exit when not condition Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/statements.rst Shows the canonical form of a loop with an exit when not condition that the Loop_Entry rule permits. The rule prevents the following example from raising Constraint_Error. ```ada if then declare ... implicitly declared Loop_Entry constants begin loop sequence_of_statements; exit when not ; end loop; end; end if; ``` -------------------------------- ### GNATprove Inline_For_Proof annotation example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/appendix/additional_annotate_pragmas.rst This example shows a function with a postcondition that GNATprove would normally treat as an axiom. The Inline_For_Proof annotation can be used to inline the function's contract during proof. ```ada function Increment (X : Integer) return Integer with Post => Increment'Result >= X; ``` -------------------------------- ### Implement lemma subprogram with recursive calls (perm-lemma_subprograms.adb) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/manual_proof.rst Implements the lemma subprogram using recursive calls to assert the induction hypothesis. The proof is conditioned on termination, which GNATprove verifies using a Subprogram Variant. ```ada .. literalinclude:: /examples/ug__perm/perm-lemma_subprograms.adb :language: ada ``` -------------------------------- ### GNATprove Iterable_For_Proof with Constant_Reference example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/appendix/additional_annotate_pragmas.rst This example shows a container type that uses Constant_Reference for iteration, along with a Ghost Length function and a Constant_Access function. It illustrates how to define a model that avoids copying elements. ```ada type Sequence is private with Iterable => (... , Constant_Reference => Constant_Access); function Length (M : Sequence) return Natural with Ghost; function Constant_Access (M : Sequence; P : Positive) return not null access constant Element_Type with Pre => P <= Length (M); ``` -------------------------------- ### Access type declaration for pointer value example Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/access.rst Declares a not-null access-to-integer type. This is used in the example demonstrating that pointer values are not modeled by GNATprove; only the designated value matters. ```ada type Int_Acc is not null access Integer; ``` -------------------------------- ### Package body P with globals annotations Source: https://github.com/adacore/spark2014/blob/master/docs/flow/generated_globals_2016_redesign/gg_redesign.tex Implements package P with local variables G1 and G2 and procedures P0, P1, P4, and P5, each annotated with the most precise globals possible (In_Out or Output) assuming the language extension to summarize hidden state. ```latex use Q; package body P is G1 : Boolean; G2 : Boolean; procedure P0 is begin P1; G2 := False; end P0; -- In_Out $\rightarrow$ (G1, Q) -- Output $\rightarrow$ G2 procedure P1 is begin Q.P2; end P1; -- In_Out $\rightarrow$ (G1, G2, Q) procedure P4 is begin P5; end P4; -- Output $\rightarrow$ (G1, G2) procedure P5 is begin G1 := True; G2 := True; end P5; -- Output $\rightarrow$ (G1, G2) end P; ``` -------------------------------- ### Package P with Initializes and Elaborate_Body Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst Declares a package with an Initializes aspect for VP and uses pragma Elaborate_Body because VP is initialized in the body. ```ada package P with Initializes => VP is pragma Elaborate_Body; -- Needed because VP is VP : Integer; -- Initialized in the body end P; ``` -------------------------------- ### Loop invariant at start of loop with excluded range Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/how_to_write_loop_invariants.rst Place this pragma at the start of the loop to state that the searched value is not in the range from A'First up to Pos-1 (excluded). Use when the loop invariant is checked before the test at index Pos. ```ada pragma Loop_Invariant (for all K in A'First .. Pos - 1 => A (K) /= I); ``` -------------------------------- ### Package X with Initial_Condition and Initializes Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst Declares a package X with an abstract state A and a visible variable B, both initialized during elaboration. The Initial_Condition predicate A_Is_Ready and B = 0 holds after package elaboration. ```ada package X with Abstract_State => A, -- Declares an abstract state named A Initializes => (A, B), -- A and visible variable B are initialized -- during package initialization. Initial_Condition => A_Is_Ready and B = 0 -- The logical conditions that hold -- after package elaboration. is ... B : Integer; function A_Is_Ready return Boolean with Global => A; end X; ``` -------------------------------- ### Load project file in test.py Source: https://github.com/adacore/spark2014/blob/master/docs/booklet/miscellaneous.md First step in writing a test.py file: load the project using the yield command from the GNAT Studio plugin. ```python yield load_project_file(project_file) ``` -------------------------------- ### Loop_Entry rule example: procedure P with Loop_Invariant and empty string Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/statements.rst Example that the rule prevents from raising Constraint_Error. Declares procedure P with a loop that uses pragma Loop_Invariant referencing X(X'First)'Loop_Entry, then calls P with an empty string. ```ada declare procedure P (X : in out String) is begin for I in X'Range loop pragma Loop_Invariant (X(X'First)'Loop_Entry >= X(I)); X := F(X); -- modify X end loop; end P; Length_Is_Zero : String := ""; begin P (Length_Is_Zero); end; -- ...] ``` -------------------------------- ### Memory_Map query functions In_Memory and Get Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Functions to query the memory model of explicit reclamation pointers. In_Memory checks whether a cell exists for a pointer, and Get returns access to the value, with a non-null access constant. ```Ada function In_Memory (M : Memory_Map; P : Pointer) return Boolean; function Get (M : Memory_Map; P : Pointer) return not null access constant Object; ``` -------------------------------- ### Package Q with Initial_Condition and Abstract_State Source: https://github.com/adacore/spark2014/blob/master/docs/lrm/source/packages.rst Declares a package Q with an abstract state State, initializes it during elaboration, and specifies an Initial_Condition predicate Is_Ready. The function Is_Ready has a Global aspect referencing State. ```ada package Q with Abstract_State => State, -- Declaration of abstract state name State Initializes => State, -- State will be initialized during elaboration Initial_Condition => Is_Ready -- Predicate stating the logical state after -- initialization. is function Is_Ready return Boolean with Global => State; end Q; ``` -------------------------------- ### Functional.Trees: Is_Empty, Get, and Child primitives Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/spark_libraries.rst Declares the Is_Empty, Get, and Child primitives for SPARK.Containers.Functional.Trees. Functional trees are recursive mathematical data structures such that non-empty trees contain an element and a child tree per element of the Way_Type formal parameter type. ```ada function Is_Empty (Container : Tree) return Boolean; function Get (Container : Tree) return Element_Type; function Child (Container : Tree; W : Way_Type) return Tree; ``` -------------------------------- ### Ada package P with nested package Q for inception example Source: https://github.com/adacore/spark2014/blob/master/docs/flow/generated_globals_2016_redesign/gg_redesign.tex Declares package P with abstract state State and a body that refines State into G0 and G1. The body includes a local procedure P0 that declares a nested package Q with its own global G3 and procedure P2, illustrating deeper nesting for the inception scenario. ```latex package P with Abstract_State => State is procedure P0; procedure P1; -- When I get paid by the line, I always -- write my initialisation like this. end P; package body P with Refined_State => (State => G0, G1) is G0 : Boolean; G1 : Boolean; procedure P0 is G2 : Boolean; package Q is G3 : Boolean; procedure P2; end Q; package body Q is procedure P2 is begin G1 := G3; end P2; end Q; begin Q.G3 := False; G2 := False; Q.P2; end P0; ``` -------------------------------- ### Correct Swap procedure (swap.adb) Source: https://github.com/adacore/spark2014/blob/master/docs/ug/en/source/basic.rst Correct version of Swap using a temporary value. GNATprove proves both contracts and informs that the postcondition was proved. ```ada .. literalinclude:: /examples/ug__swap/swap.adb :language: ada :linenos: ```