#![allow(unused_variables)]
use crate::{
ghost::{FnGhost, Plain},
logic::Set,
prelude::*,
};
use core::marker::PhantomData;
#[cfg(creusot)]
use crate::ghost::Objective;
pub use base_macros::declare_namespace;
#[intrinsic("namespace")]
pub struct Namespace(());
impl Clone for Namespace {
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
*self
}
}
impl Copy for Namespace {}
impl Plain for Namespace {
#[trusted]
#[ensures(*result == *snap)]
#[check(ghost)]
#[allow(unused_variables)]
fn into_ghost(snap: Snapshot<Self>) -> Ghost<Self> {
Ghost::conjure()
}
}
#[opaque]
pub struct Tokens<'a>(PhantomData<&'a ()>, PhantomData<*mut ()>);
impl<'a> Tokens<'a> {
#[logic(opaque)]
pub fn namespaces(self) -> Set<Namespace> {
dead
}
#[trusted]
#[ensures(forall<ns: Namespace> result.contains(ns))]
#[intrinsic("tokens_new")]
#[check(ghost)]
pub fn new() -> Ghost<Self> {
Ghost::conjure()
}
#[trusted]
#[ensures(result == *self && ^self == *self)]
#[check(ghost)]
pub fn reborrow<'b>(&'b mut self) -> Tokens<'b> {
Tokens(PhantomData, PhantomData)
}
#[trusted]
#[requires(self.contains(*ns))]
#[ensures(result.0.contains(*ns))]
#[ensures(result.1.namespaces() == self.namespaces().remove(*ns))]
#[check(ghost)]
pub fn split(self, ns: Snapshot<Namespace>) -> (Tokens<'a>, Tokens<'a>) {
(Tokens(PhantomData, PhantomData), Tokens(PhantomData, PhantomData))
}
#[logic(open)]
pub fn contains(self, namespace: Namespace) -> bool {
self.namespaces().contains(namespace)
}
}
impl View for Tokens<'_> {
type ViewTy = Set<Namespace>;
#[logic(open, inline)]
fn view(self) -> Set<Namespace> {
self.namespaces()
}
}
pub trait Protocol {
type Public;
#[logic(prophetic)]
fn protocol(self, data: Self::Public) -> bool;
}
#[opaque]
pub struct AtomicInvariantSC<T>(PhantomData<*mut T>);
unsafe impl<T: Send> Sync for AtomicInvariantSC<T> {}
impl<T: Protocol> AtomicInvariantSC<T> {
#[trusted]
#[requires(value.protocol(*public))]
#[ensures(result.public() == *public)]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(
value: Ghost<T>,
public: Snapshot<T::Public>,
namespace: Snapshot<Namespace>,
) -> Ghost<Self> {
Ghost::conjure()
}
#[logic(opaque)]
pub fn namespace(self) -> Namespace {
dead
}
#[logic(opaque)]
pub fn public(self) -> T::Public {
dead
}
#[trusted]
#[ensures(result.protocol(self.public()))]
#[check(ghost)]
pub fn into_inner(self) -> T {
panic!("Should not be called outside ghost code")
}
#[trusted]
#[requires(tokens.contains(self.namespace()))]
#[requires(forall<t: &mut T> t.protocol(self.public()) && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).protocol(self.public())))]
#[ensures(exists<t: &mut T> inv(t) && t.protocol(self.public()) && f.postcondition_once((t,), result))]
#[check(ghost)]
pub fn open<A>(&self, tokens: Tokens, f: impl FnGhost + for<'a> FnOnce(&'a mut T) -> A) -> A {
panic!("Should not be called outside ghost code")
}
}
#[opaque]
pub struct AtomicInvariant<T>(PhantomData<*mut T>);
#[cfg(creusot)]
unsafe impl<T: Send + Objective> Sync for AtomicInvariant<T> {}
#[cfg(not(creusot))]
unsafe impl<T: Send> Sync for AtomicInvariant<T> {}
impl<T: Protocol> AtomicInvariant<T> {
#[trusted]
#[requires(value.protocol(*public))]
#[ensures(result.public() == *public)]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(
value: Ghost<T>,
public: Snapshot<T::Public>,
namespace: Snapshot<Namespace>,
) -> Ghost<Self> {
Ghost::conjure()
}
#[logic(opaque)]
pub fn namespace(self) -> Namespace {
dead
}
#[logic(opaque)]
pub fn public(self) -> T::Public {
dead
}
#[trusted]
#[ensures(result.protocol(self.public()))]
#[check(ghost)]
pub fn into_inner(self) -> T {
panic!("Should not be called outside ghost code")
}
#[trusted]
#[requires(tokens.contains(self.namespace()))]
#[requires(forall<t: &mut T> t.protocol(self.public()) && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).protocol(self.public())))]
#[ensures(exists<t: &mut T> inv(t) && t.protocol(self.public()) && f.postcondition_once((t,), result))]
#[check(ghost)]
pub fn open<A>(&self, tokens: Tokens, f: impl FnGhost + for<'a> FnOnce(&'a mut T) -> A) -> A {
panic!("Should not be called outside ghost code")
}
}
#[opaque]
pub struct NonAtomicInvariant<T: Protocol>(PhantomData<*mut T>);
unsafe impl<T: Protocol> Send for NonAtomicInvariant<T> {}
pub trait NonAtomicInvariantExt<'a> {
type Inner: 'a;
#[requires(false)]
fn open<A, F>(self, tokens: Ghost<Tokens<'a>>, f: F) -> A
where
F: FnOnce(Ghost<&'a mut Self::Inner>) -> A;
}
impl<'a, T: Protocol> NonAtomicInvariantExt<'a> for Ghost<&'a NonAtomicInvariant<T>> {
type Inner = T;
#[requires(tokens.contains(self.namespace()))]
#[requires(forall<t: Ghost<&mut T>> (**t).protocol(self.public()) && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).protocol(self.public())))]
#[ensures(exists<t: Ghost<&mut T>> t.protocol(self.public()) && f.postcondition_once((t,), result))]
fn open<A, F>(self, tokens: Ghost<Tokens<'a>>, f: F) -> A
where
F: FnOnce(Ghost<&'a mut Self::Inner>) -> A,
{
NonAtomicInvariant::open(self, tokens, f)
}
}
impl<'a, T> NonAtomicInvariantExt<'a> for Ghost<&'a T>
where
T: core::ops::Deref,
Ghost<&'a T::Target>: NonAtomicInvariantExt<'a>,
{
type Inner = <Ghost<&'a T::Target> as NonAtomicInvariantExt<'a>>::Inner;
#[requires(T::deref.precondition((*self,)))]
#[requires(forall<this> T::deref.postcondition((*self,), this) ==>
<Ghost<&'a T::Target> as NonAtomicInvariantExt<'a>>::open.precondition((Ghost::new_logic(this), tokens, f))
)]
#[ensures(exists<this> T::deref.postcondition((*self,), this) &&
<Ghost<&'a T::Target> as NonAtomicInvariantExt<'a>>::open.postcondition((Ghost::new_logic(this), tokens, f), result)
)]
fn open<A, F>(self, tokens: Ghost<Tokens<'a>>, f: F) -> A
where
F: FnOnce(Ghost<&'a mut Self::Inner>) -> A,
{
let this: Ghost<&T::Target> = ghost!(&self);
this.open(tokens, f)
}
}
impl<'a, L> NonAtomicInvariantExt<'a> for &'a Ghost<L>
where
Ghost<&'a L>: NonAtomicInvariantExt<'a>,
{
type Inner = <Ghost<&'a L> as NonAtomicInvariantExt<'a>>::Inner;
#[requires(<Ghost<&'a L> as NonAtomicInvariantExt<'a>>::open.precondition((Ghost::new_logic(&**self), tokens, f)))]
#[ensures(<Ghost<&'a L> as NonAtomicInvariantExt<'a>>::open.postcondition((Ghost::new_logic(&**self), tokens, f), result))]
fn open<A, F>(self, tokens: Ghost<Tokens<'a>>, f: F) -> A
where
F: FnOnce(Ghost<&'a mut Self::Inner>) -> A,
{
self.borrow().open(tokens, f)
}
}
impl<T: Protocol> NonAtomicInvariant<T> {
#[trusted]
#[requires(value.protocol(*public))]
#[ensures(result.public() == *public)]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(
value: Ghost<T>,
public: Snapshot<T::Public>,
namespace: Snapshot<Namespace>,
) -> Ghost<Self> {
Ghost::conjure()
}
#[trusted]
#[ensures(result.protocol(self.public()))]
#[check(ghost)]
pub fn into_inner(self) -> T {
panic!("Should not be called outside ghost code")
}
#[logic(opaque)]
pub fn namespace(self) -> Namespace {
dead
}
#[logic(opaque)]
pub fn public(self) -> T::Public {
dead
}
#[trusted]
#[requires(tokens.contains(this.namespace()))]
#[requires(forall<t: Ghost<&mut T>> t.protocol(this.public()) && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).protocol(this.public())))]
#[ensures(exists<t: Ghost<&mut T>> inv(t) && t.protocol(this.public()) && f.postcondition_once((t,), result))]
pub fn open<'a, A>(
this: Ghost<&'a Self>,
tokens: Ghost<Tokens<'a>>,
f: impl FnOnce(Ghost<&'a mut T>) -> A,
) -> A {
f(Ghost::conjure())
}
#[trusted]
#[requires(tokens.contains(self.namespace()))]
#[ensures(result.protocol(self.public()))]
#[check(ghost)]
pub fn open_const<'a>(&'a self, tokens: &'a Tokens) -> &'a T {
panic!("Should not be called outside ghost code")
}
}