Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing of FMap in logic #1254

Merged
merged 4 commits into from
Nov 27, 2024
Merged
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
34 changes: 25 additions & 9 deletions creusot-contracts/src/logic/fmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{logic::Mapping, util::*, *};
use crate::{
logic::{ops::IndexLogic, Mapping},
util::*,
*,
};

#[cfg_attr(not(creusot), allow(dead_code))]
type PMap<K, V> = Mapping<K, Option<SizedW<V>>>;
Expand Down Expand Up @@ -26,6 +30,15 @@ pub struct FMap<K, V: ?Sized>(std::marker::PhantomData<K>, std::marker::PhantomD

/// Logical definitions
impl<K, V: ?Sized> FMap<K, V> {
/// Returns the empty map.
#[trusted]
#[logic]
#[ensures(result.len() == 0)]
#[ensures(result.view() == Mapping::cst(None))]
pub fn empty() -> Self {
dead
}

#[trusted]
#[logic]
#[ensures(result >= 0)]
Expand Down Expand Up @@ -107,14 +120,6 @@ impl<K, V: ?Sized> FMap<K, V> {
self.get_unsized(k) != None
}

#[trusted]
#[logic]
#[ensures(result.len() == 0)]
#[ensures(result.view() == Mapping::cst(None))]
pub fn empty() -> Self {
dead
}

#[logic]
#[open]
pub fn is_empty(self) -> bool {
Expand Down Expand Up @@ -184,6 +189,17 @@ impl<K, V: ?Sized> FMap<K, V> {
}
}

impl<K, V> IndexLogic<K> for FMap<K, V> {
type Item = V;

#[logic]
#[open]
#[why3::attr = "inline:trivial"]
fn index_logic(self, key: K) -> Self::Item {
self.lookup(key)
}
}

/// Ghost definitions
impl<K, V: ?Sized> FMap<K, V> {
/// Create a new, empty map on the ghost heap.
Expand Down
9 changes: 8 additions & 1 deletion creusot-contracts/src/logic/fset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ impl<T: ?Sized> FSet<T> {
#[creusot::builtins = "set.Fset.empty"]
pub const EMPTY: Self = { FSet(std::marker::PhantomData) };

/// Returns the empty set.
#[logic]
#[open]
pub fn empty() -> Self {
Self::EMPTY
}

#[open]
#[predicate]
#[why3::attr = "inline:trivial"]
Expand Down Expand Up @@ -141,7 +148,7 @@ impl<T: ?Sized> FSet<T> {
#[ensures(result.is_empty())]
#[allow(unreachable_code)]
pub fn new() -> GhostBox<Self> {
ghost!(loop {})
ghost!(panic!())
}

/// Returns the number of elements in the set.
Expand Down
9 changes: 8 additions & 1 deletion creusot-contracts/src/logic/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl<T: ?Sized> Seq<T> {
#[creusot::builtins = "seq.Seq.empty"]
pub const EMPTY: Self = { Seq(std::marker::PhantomData) };

/// Returns the empty sequence.
#[logic]
#[open]
pub fn empty() -> Self {
Self::EMPTY
}

/// Create a new sequence in pearlite.
///
/// The new sequence will be of length `n`, and will contain `mapping[i]` at index `i`.
Expand Down Expand Up @@ -252,7 +259,7 @@ impl<T> Seq<T> {
#[ensures(*result == Self::EMPTY)]
#[allow(unreachable_code)]
pub fn new() -> GhostBox<Self> {
ghost!(loop {})
ghost!(panic!())
}

/// Returns the number of elements in the sequence, also referred to as its 'length'.
Expand Down
412 changes: 206 additions & 206 deletions creusot/tests/creusot-contracts/creusot-contracts.coma

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions creusot/tests/should_fail/bug/878.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions creusot/tests/should_fail/bug/specialize.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion creusot/tests/should_succeed/bug/217.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions creusot/tests/should_succeed/bug/949.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions creusot/tests/should_succeed/bug/final_borrows.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions creusot/tests/should_succeed/bug/vcgen.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions creusot/tests/should_succeed/fmap_indexing.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions creusot/tests/should_succeed/fmap_indexing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extern crate creusot_contracts;
use creusot_contracts::{logic::FMap, *};

pub fn foo() {
let mut map = snapshot!(FMap::empty());
map = snapshot!(map.insert(1, 3));
proof_assert!(map[1] == 3);
map = snapshot!(map.insert(2, 42));
proof_assert!(map[1] == 3 && map[2] == 42);
map = snapshot!(map.insert(1, 4));
proof_assert!(map[1] == 4 && map[2] == 42);
}
14 changes: 14 additions & 0 deletions creusot/tests/should_succeed/fmap_indexing/why3session.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Loading
Loading