Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 71 additions & 8 deletions rocq-brick-libstdcpp/test/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,84 @@
)
(subdir geeks_for_geeks_examples
(rule
(targets N0_hello_world_cpp.v)
(targets N12_area_cpp.v)
(alias test_ast)
(deps (:input N0_hello_world.cpp) (glob_files_rec ../*.hpp) (universe))
(deps (:input N12_area.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N0_hello_world_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N0_hello_world.cpp))
(run cpp2v -v %{input} -o N12_area_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N12_area.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N2_input_cpp.v)
(targets N1_hello_world_cpp.v)
(alias test_ast)
(deps (:input N2_input.cpp) (glob_files_rec ../*.hpp) (universe))
(deps (:input N1_hello_world.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N2_input_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N2_input.cpp))
(run cpp2v -v %{input} -o N1_hello_world_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N1_hello_world.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N3_input_cpp.v)
(alias test_ast)
(deps (:input N3_input.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N3_input_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N3_input.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N4_sum_cpp.v)
(alias test_ast)
(deps (:input N4_sum.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N4_sum_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N4_sum.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N4_sum_a_cpp.v)
(alias test_ast)
(deps (:input N4_sum_a.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N4_sum_a_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N4_sum_a.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N5_swap_cpp.v)
(alias test_ast)
(deps (:input N5_swap.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N5_swap_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N5_swap.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N6_print_sizeof_cpp.v)
(alias test_ast)
(deps (:input N6_print_sizeof.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N6_print_sizeof_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N6_print_sizeof.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N8_ascii_value_cast_cpp.v)
(alias test_ast)
(deps (:input N8_ascii_value_cast.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N8_ascii_value_cast_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N8_ascii_value_cast.cpp))
)
(subdir geeks_for_geeks_examples
(rule
(targets N8_ascii_value_printf_cpp.v)
(alias test_ast)
(deps (:input N8_ascii_value_printf.cpp) (glob_files_rec ../*.hpp) (universe))
(action
(run cpp2v -v %{input} -o N8_ascii_value_printf_cpp.v --no-elaborate -- -std=c++20 -stdlib=libstdc++ )))
(alias (name srcs) (deps N8_ascii_value_printf.cpp))
)
(subdir geeks_for_geeks_examples
(rule
Expand Down
30 changes: 30 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N12_area.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// C++ program to find area
// and perimeter of rectangle
#include<iostream>
using namespace std;

// Utility function
int areaRectangle(int a, int b)
{
int area = a * b;
return area;
}

int perimeterRectangle(int a, int b)
{
int perimeter = 2*(a + b);
return perimeter;
}

// Driver code
int main()
{
int a = 5;
int b = 6;
cout << "Area = " <<
areaRectangle(a, b) <<
endl;
cout << "Perimeter = " <<
perimeterRectangle(a, b);
return 0;
}
37 changes: 37 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N12_area.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N12_area_cpp.

Import linearity.

Section with_cpp.
Context `{Σ : cpp_logic, σ : genv}.

Definition area_of_rectangle (side1 side2 : Z) := side1 * side2.
Definition perimeter_of_rectangle (side1 side2 : Z) := 2 * (side1 + side2).
Definition side1 := 5.
Definition side2 := 6.

cpp.spec "main()" from source as main_spec with (
\prepost{osM} _global "std::cout" |-> ostreamR 1$m osM
\pre{str} _global "std::cout" |-> ostream_contentR 1$m str
\post[Vint 0]
_global "std::cout" |-> ostream_contentR 1$m
(str ++ "Area = " ++ Z_to_string (area_of_rectangle side1 side2) ++ "\n" ++ "Perimeter = " ++ Z_to_string (perimeter_of_rectangle side1 side2))
).

cpp.spec "areaRectangle(int, int)" from source inline.
cpp.spec "perimeterRectangle(int, int)" from source inline.

Lemma main_ok : verify?[source] main_spec.
Proof.
verify_shift; go.
iExists (_ : ostreamT → ostreamT), (_ : cstring.t → cstring.t); work with br_erefl; go.

banish_string_literals.
iModIntro.
work.
by rewrite -!(assoc_L BS.append).
Qed.
End with_cpp.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N0_hello_world_cpp.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N1_hello_world_cpp.

Section with_cpp.
Context `{Σ : cpp_logic, σ : genv}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N2_input_cpp.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N3_input_cpp.

Import linearity.

Expand Down
13 changes: 13 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N4_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://www.geeksforgeeks.org/cpp/cpp-add-numbers/

#include <iostream>
using namespace std;

int main() {
int a = 11, b = 9;

// Adding the two numbers and printing
// their sum
cout << a + b;
return 0;
}
20 changes: 20 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N4_sum.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N4_sum_cpp.

Import linearity.

Section with_cpp.
Context `{Σ : cpp_logic, σ : genv}.

cpp.spec "main()" from source as main_spec with (
\prepost{osM} _global "std::cout" |-> ostreamR 1$m osM
\pre{str} _global "std::cout" |-> ostream_contentR 1$m str
\post[Vint 0]
_global "std::cout" |-> ostream_contentR 1$m (str ++ Z_to_string 20)
).

Lemma main_ok : verify?[source] main_spec.
Proof. verify_spec; go. Qed.
End with_cpp.
20 changes: 20 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N4_sum_a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://www.geeksforgeeks.org/cpp/cpp-add-numbers/, 2nd version

#include <iostream>
using namespace std;

int main() {
int a = 11, b = 9;

// If b is positive, increment a to b times
for (int i = 0; i < b; i++)
a++;

// If b is negative, decrement a to |b| times
for (int i = 0; i > b; i--)
a--;

cout << a;

return 0;
}
37 changes: 37 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N4_sum_a.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N4_sum_a_cpp.

Import linearity.

Section with_cpp.
Context `{Σ : cpp_logic, σ : genv}.

cpp.spec "main()" from source as main_spec with (
\prepost{osM} _global "std::cout" |-> ostreamR 1$m osM
\pre{str} _global "std::cout" |-> ostream_contentR 1$m str
\post[Vint 0]
_global "std::cout" |-> ostream_contentR 1$m (str ++ Z_to_string 20)
).

#[global] Declare Instance Z_to_string_inj : Inj eq eq Z_to_string.

Lemma main_ok : verify?[source] main_spec.
Proof.
verify_spec; go.

wp_for (fun ρ =>
\pre{i1} _local ρ "i" |-> intR 1$m i1
\pre _local ρ "a" |-> intR 1$m (11 + i1)
\require 0 <= i1 <= 9
\post* _local ρ "i" |-> anyR "int" 1$m
\post* _local ρ "a" |-> intR 1$m 20
\post emp
).

go.
wp_if; go.
wp_for (fun ρ => emp); go.
Qed.
End with_cpp.
18 changes: 18 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N5_swap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://www.geeksforgeeks.org/cpp/cpp-program-to-swap-two-numbers/

#include <iostream>
using namespace std;

int main(){
int a = 2, b = 3;

cout << "Before swapping a = " << a << " , b = " << b << endl;

int temp;

temp = a;
a = b;
b = temp;
cout << "After swapping a = " << a << " , b = " << b << endl;
return 0;
}
44 changes: 44 additions & 0 deletions rocq-brick-libstdcpp/test/geeks_for_geeks_examples/N5_swap.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Require Import skylabs.auto.cpp.prelude.proof.
Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.spec.

Require Import skylabs.brick.libstdcpp.test.geeks_for_geeks_examples.N5_swap_cpp.

Import linearity.

Section with_cpp.
Context `{Σ : cpp_logic, σ : genv}.

cpp.spec "main()" from source as main_spec with (
\prepost{osM} _global "std::cout" |-> ostreamR 1$m osM
\pre{str} _global "std::cout" |-> ostream_contentR 1$m str
\post[Vint 0]
_global "std::cout" |-> ostream_contentR 1$m
(str ++
"Before swapping a = " ++
Z_to_string 2 ++ " , b = " ++ Z_to_string 3 ++ "\n" ++
"After swapping a = " ++ Z_to_string 3 ++ " , b = " ++ Z_to_string 2 ++ "\n"
)
).

#[program]
Definition hint_stream_F :=
\cancelx@{mpredI}
\bound_existential (stream_f : cstring.t → cstring.t)
\bound_existential (state_f : ostreamT → ostreamT)
\instantiate stream_f := fun str => (str ++ "\n")%bs
\instantiate state_f := fun osM => osM
\end.
Next Obligation. work. Qed.

Lemma main_ok : verify?[source] main_spec.
Proof.
verify_shift; go.
(* iExists id, (fun str => str ++ "\n")%bs; go. *)
iExists (_ : ostreamT → ostreamT), (_ : cstring.t → cstring.t); work with br_erefl; go.
iExists (_ : ostreamT → ostreamT), (_ : cstring.t → cstring.t); work with br_erefl; go.
banish_string_literals.
iModIntro.
work.
by rewrite -!(assoc_L BS.append).
Qed.
End with_cpp.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;

int main()
{
int integerType;
char charType;
float floatType;
double doubleType;

// Calculate and Print
// the size of integer type
cout << "Size of int is: " << sizeof(integerType)
<< "\n";

// Calculate and Print
// the size of doubleType
cout << "Size of char is: " << sizeof(charType) << "\n";

// Calculate and Print
// the size of charType
cout << "Size of float is: " << sizeof(floatType)
<< "\n";

// Calculate and Print
// the size of floatType
cout << "Size of double is: " << sizeof(doubleType)
<< "\n";

return 0;
}
Loading