Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
package Bubble
|
||||
is
|
||||
|
||||
type Arr is array(Integer range <>) of Integer;
|
||||
|
||||
procedure Sort (A : in out Arr);
|
||||
--# derives A from *;
|
||||
|
||||
end Bubble;
|
||||
|
||||
|
||||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
Temp : Integer;
|
||||
begin
|
||||
if A'Last /= A'First then
|
||||
loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. A'Last - 1 loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Temp := A (J + 1);
|
||||
A (J + 1) := A (J);
|
||||
A (J) := Temp;
|
||||
end if;
|
||||
end loop;
|
||||
--# assert A'Last /= A'First;
|
||||
exit when Finished;
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package Bubble
|
||||
is
|
||||
|
||||
type Arr is array(Integer range <>) of Integer;
|
||||
|
||||
-- Sorted is a proof function with the definition:
|
||||
-- Sorted(A, From_I, To_I)
|
||||
-- <->
|
||||
-- (for all I in Integer range From_I .. To_I - 1 =>
|
||||
-- (A(I) <= A(I + 1))) .
|
||||
--
|
||||
--# function Sorted (A : Arr;
|
||||
--# From_I, To_I : Integer) return Boolean;
|
||||
|
||||
procedure Sort (A : in out Arr);
|
||||
--# derives A from *;
|
||||
--# post Sorted(A, A'First, A'Last);
|
||||
|
||||
end Bubble;
|
||||
|
||||
|
||||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
Temp : Integer;
|
||||
begin
|
||||
if A'Last > A'First then
|
||||
loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. A'Last - 1
|
||||
--# assert Finished -> Sorted(A, A'First, J);
|
||||
loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Temp := A (J + 1);
|
||||
A (J + 1) := A (J);
|
||||
A (J) := Temp;
|
||||
end if;
|
||||
end loop;
|
||||
--# assert A'Last /= A'First
|
||||
--# and (Finished -> Sorted(A, A'First, A'Last));
|
||||
exit when Finished;
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
|
||||
-- In_Position is a proof function with the definition:
|
||||
-- In_Position(A, A_Start, A_I, A_End)
|
||||
-- <->
|
||||
-- ((for all K in Integer range A_Start .. A_I - 1 =>
|
||||
-- (A(K) <= A(A_I)))
|
||||
-- and
|
||||
-- Sorted(A, A_I, A_End) .
|
||||
--
|
||||
--# function In_Position (A : Arr;
|
||||
--# A_Start, A_I, A_End : Integer) return Boolean;
|
||||
|
||||
-- Swapped is a proof function with the definition:
|
||||
-- Swapped(A_In, A_Out, I1, I2)
|
||||
-- <->
|
||||
-- (A_Out = A_In[I1 => A_In(I2); I2 => A_In(I1)]).
|
||||
--
|
||||
--# function Swapped (A_In, A_Out : Arr;
|
||||
--# I1, I2 : Integer) return Boolean;
|
||||
|
||||
procedure Swap (A : in out Arr;
|
||||
I1 : in Integer;
|
||||
I2 : in Integer)
|
||||
--# derives A from *, I1, I2;
|
||||
--# pre I1 in A'First .. A'Last
|
||||
--# and I2 in A'First .. A'Last;
|
||||
--# post Swapped(A~, A, I1, I2);
|
||||
is
|
||||
Temp : Integer;
|
||||
begin
|
||||
Temp := A(I2);
|
||||
A(I2) := A(I1);
|
||||
A(I1) := Temp;
|
||||
end Swap;
|
||||
pragma Inline (Swap);
|
||||
|
||||
begin
|
||||
if A'Last > A'First then
|
||||
for I in reverse Integer range A'First + 1 .. A'Last loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. I - 1 loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Swap (A, J, J + 1);
|
||||
end if;
|
||||
--# assert I% = I -- I is unchanged by execution of the loop
|
||||
--# and (for all K in Integer range A'First .. J =>
|
||||
--# (A(K) <= A(J + 1)))
|
||||
--# and (I < A'Last -> In_Position(A, A'First, I + 1, A'Last))
|
||||
--# and (Finished -> Sorted(A, A'First, J + 1));
|
||||
end loop;
|
||||
exit when Finished;
|
||||
--# assert In_Position(A, A'First, I, A'Last);
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package Bubble with SPARK_Mode is
|
||||
|
||||
type Arr is array (Integer range <>) of Integer;
|
||||
|
||||
function Sorted (A : Arr) return Boolean is
|
||||
(for all I in A'First .. A'Last - 1 => A(I) <= A(I + 1))
|
||||
with
|
||||
Ghost,
|
||||
Pre => A'Last > Integer'First;
|
||||
|
||||
function Bubbled (A : Arr) return Boolean is
|
||||
(for all I in A'First .. A'Last - 1 => A(I) <= A(A'Last))
|
||||
with
|
||||
Ghost,
|
||||
Pre => A'Last > Integer'First;
|
||||
|
||||
procedure Sort (A : in out Arr)
|
||||
with
|
||||
Pre => A'Last > Integer'First and A'Last < Integer'Last,
|
||||
Post => Sorted (A);
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package body Bubble with SPARK_Mode is
|
||||
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Prev : Arr (A'Range) with Ghost;
|
||||
Done : Boolean;
|
||||
begin
|
||||
for I in reverse A'First .. A'Last - 1 loop
|
||||
Prev := A;
|
||||
Done := True;
|
||||
for J in A'First .. I loop
|
||||
if A(J) > A(J + 1) then
|
||||
declare
|
||||
TMP : Integer := A(J);
|
||||
begin
|
||||
A(J) := A(J + 1);
|
||||
A(J + 1) := TMP;
|
||||
Done := False;
|
||||
end;
|
||||
end if;
|
||||
pragma Loop_Invariant (if Done then Sorted (A(A'First .. J + 1)));
|
||||
pragma Loop_Invariant (Bubbled (A(A'First .. J + 1)));
|
||||
pragma Loop_Invariant (A(J + 2 .. A'Last) = Prev(J + 2 .. A'Last));
|
||||
pragma Loop_Invariant (for some K in A'First .. J + 1 =>
|
||||
A(J + 1) = Prev(K));
|
||||
end loop;
|
||||
exit when Done;
|
||||
pragma Loop_Invariant (if Done then Sorted (A));
|
||||
pragma Loop_Invariant (Bubbled (A(A'First .. I + 1)));
|
||||
pragma Loop_Invariant (Sorted (A(I + 1 .. A'Last)));
|
||||
end loop;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
with Ada.Integer_Text_IO;
|
||||
with Bubble;
|
||||
|
||||
procedure Main is
|
||||
A : Bubble.Arr := (5,4,6,3,7,2,8,1,9);
|
||||
begin
|
||||
Bubble.Sort (A);
|
||||
for I in A'Range loop
|
||||
Ada.Integer_Text_IO.Put (A(I));
|
||||
end loop;
|
||||
end Main;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
project Bubble is
|
||||
|
||||
for Main use ("main.adb");
|
||||
|
||||
end Bubble;
|
||||
Loading…
Add table
Add a link
Reference in a new issue