Skip to content

Commit 358a1a8

Browse files
Add source for subprogram contracts lab
1 parent 8f6b04d commit 358a1a8

8 files changed

Lines changed: 202 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pragma SPARK_Mode (On);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
project Lab is
2+
3+
for Object_Dir use "obj";
4+
5+
package Compiler is
6+
for Default_Switches ("Ada") use ("-g", "-O0", "-gnato13", "-gnata", "-gnatyr", "-gnatwa");
7+
end Compiler;
8+
9+
package Builder is
10+
for Global_Configuration_Pragmas use "config.adc";
11+
end Builder;
12+
13+
package Prove is
14+
for Proof_Switches ("Ada") use ("-j0");
15+
end Prove;
16+
17+
end Lab;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
with Ada.Characters.Handling; use Ada.Characters.Handling;
2+
with Ada.Text_IO;
3+
4+
package body Math with
5+
SPARK_Mode
6+
is
7+
8+
package IO is new Ada.Text_IO.Integer_IO (Integer);
9+
10+
function Saturated_Add
11+
(X, Y : Integer)
12+
return Integer is
13+
begin
14+
if X < 0 and Y < 0 then -- both negative
15+
if X < Integer'First - Y then
16+
return Integer'First;
17+
else
18+
return X + Y;
19+
end if;
20+
21+
elsif X > 0 and Y > 0 then -- both positive
22+
if X > Integer'Last - Y then
23+
return Integer'Last;
24+
else
25+
return X + Y;
26+
end if;
27+
28+
else -- one positive or null, one negative or null, adding them is safe
29+
return X + Y;
30+
end if;
31+
end Saturated_Add;
32+
33+
procedure Add
34+
(X, Y : Integer;
35+
Z : out Integer) is
36+
begin
37+
Z := Saturated_Add (X, Y);
38+
end Add;
39+
40+
procedure Convert
41+
(S : String;
42+
Value : out Integer) is
43+
Unused : Positive;
44+
begin
45+
if
46+
(for some C of S =>
47+
not
48+
(Is_Digit (C) or C = 'E' or C = 'e' or C = '+' or C = '-' or
49+
C = '_'))
50+
then
51+
raise Invalid_String;
52+
else
53+
IO.Get (S, Value, Unused);
54+
end if;
55+
exception
56+
when Ada.Text_IO.Data_Error =>
57+
raise Constraint_Error;
58+
end Convert;
59+
60+
end Math;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Math with
2+
SPARK_Mode
3+
is
4+
5+
Invalid_String : exception;
6+
7+
function Saturated_Add
8+
(X, Y : Integer)
9+
return Integer with
10+
SPARK_Mode,
11+
Contract_Cases =>
12+
((X + Y in Integer) => Saturated_Add'Result = X + Y,
13+
X + Y < Integer'First => Saturated_Add'Result = Integer'First,
14+
X + Y > Integer'Last => Saturated_Add'Result = Integer'Last);
15+
16+
procedure Add
17+
(X, Y : Integer;
18+
Z : out Integer) with
19+
Post => Z = Integer'First or Z = Integer'Last or Z = X + Y;
20+
21+
procedure Convert
22+
(S : String;
23+
Value : out Integer) with
24+
Exceptional_Cases => (Invalid_String | Constraint_Error => True);
25+
26+
end Math;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pragma SPARK_Mode (On);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
project Lab is
2+
3+
for Object_Dir use "obj";
4+
5+
package Compiler is
6+
for Default_Switches ("Ada") use ("-g", "-O0", "-gnato13", "-gnata", "-gnatyr", "-gnatwa");
7+
end Compiler;
8+
9+
package Builder is
10+
for Global_Configuration_Pragmas use "config.adc";
11+
end Builder;
12+
13+
package Prove is
14+
for Proof_Switches ("Ada") use ("-j0");
15+
end Prove;
16+
17+
end Lab;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
with Ada.Characters.Handling; use Ada.Characters.Handling;
2+
with Ada.Text_IO;
3+
4+
package body Math with
5+
SPARK_Mode
6+
is
7+
8+
package IO is new Ada.Text_IO.Integer_IO (Integer);
9+
10+
function Saturated_Add
11+
(X, Y : Integer)
12+
return Integer is
13+
begin
14+
if X < 0 and Y < 0 then -- both negative
15+
if X < Integer'First - Y then
16+
return Integer'First;
17+
else
18+
return X + Y;
19+
end if;
20+
21+
elsif X > 0 and Y > 0 then -- both positive
22+
if X > Integer'Last - Y then
23+
return Integer'Last;
24+
else
25+
return X + Y;
26+
end if;
27+
28+
else -- one positive or null, one negative or null, adding them is safe
29+
return X + Y;
30+
end if;
31+
end Saturated_Add;
32+
33+
procedure Add
34+
(X, Y : Integer;
35+
Z : out Integer) is
36+
begin
37+
Z := Saturated_Add (X, Y);
38+
end Add;
39+
40+
procedure Convert
41+
(S : String;
42+
Value : out Integer) is
43+
Unused : Positive;
44+
begin
45+
if
46+
(for some C of S =>
47+
not
48+
(Is_Digit (C) or C = 'E' or C = 'e' or C = '+' or C = '-' or
49+
C = '_'))
50+
then
51+
raise Invalid_String;
52+
else
53+
IO.Get (S, Value, Unused);
54+
end if;
55+
exception
56+
when Ada.Text_IO.Data_Error =>
57+
raise Constraint_Error;
58+
end Convert;
59+
60+
end Math;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Math with
2+
SPARK_Mode
3+
is
4+
5+
Invalid_String : exception;
6+
7+
function Saturated_Add
8+
(X, Y : Integer)
9+
return Integer;
10+
11+
procedure Add
12+
(X, Y : Integer;
13+
Z : out Integer) with
14+
Post => Z = Integer'First or Z = Integer'Last or Z = X + Y;
15+
16+
procedure Convert
17+
(S : String;
18+
Value : out Integer);
19+
20+
end Math;

0 commit comments

Comments
 (0)