#![allow(unused_variables)]
use crate::{
ghost::{FnGhost, Plain},
invariant::{GuardRef, Guarded},
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]
fn public(self) -> Self::Public;
#[logic(prophetic)]
fn protocol(self) -> bool;
}
#[cfg(feature = "sc-drf")]
#[opaque]
pub struct AtomicInvariantSC<T>(PhantomData<*mut T>);
#[cfg(feature = "sc-drf")]
#[trusted]
unsafe impl<T: Send> Sync for AtomicInvariantSC<T> {}
#[cfg(feature = "sc-drf")]
#[trusted]
unsafe impl<T: Send> Send for AtomicInvariantSC<T> {}
#[cfg(feature = "sc-drf")]
impl<T: Protocol> AtomicInvariantSC<T> {
#[trusted]
#[requires(value.protocol())]
#[ensures(result.public() == value.public())]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(value: Ghost<T>, 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.public() == self.public() && result.protocol())]
#[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.public() == self.public() && t.protocol() && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).public() == self.public() && (^t).protocol()))]
#[ensures(exists<t: &mut T> t.public() == self.public() && t.protocol() && inv(t) &&
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")
}
#[trusted]
#[check(ghost)]
#[ensures(result.inner.public() == self.public())]
#[ensures((^result.inner).public() == (^self).public())]
#[ensures((^self).namespace() == (*self).namespace())]
pub fn open_mut(&mut self) -> Guarded<&mut T, OpenMutGuard> {
unreachable!("ghost code only")
}
}
#[opaque]
pub struct AtomicInvariant<T>(PhantomData<*mut T>);
#[cfg(creusot)]
#[trusted]
unsafe impl<T: Send + Objective> Sync for AtomicInvariant<T> {}
#[cfg(not(creusot))]
unsafe impl<T: Send> Sync for AtomicInvariant<T> {}
#[trusted]
unsafe impl<T: Send> Send for AtomicInvariant<T> {}
impl<T: Protocol> AtomicInvariant<T> {
#[trusted]
#[requires(value.protocol())]
#[ensures(result.public() == value.public())]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(value: Ghost<T>, 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.public() == self.public() && result.protocol())]
#[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.public() == self.public() && t.protocol() && inv(t) ==>
f.precondition((t,)) &&
// f must restore the invariant
(forall<res: A> f.postcondition_once((t,), res) ==> (^t).public() == self.public() && (^t).protocol()))]
#[ensures(exists<t: &mut T> t.public() == self.public() && t.protocol() && inv(t) &&
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")
}
#[trusted]
#[check(ghost)]
#[ensures(result.inner.public() == self.public())]
#[ensures((^result.inner).public() == (^self).public())]
#[ensures((^self).namespace() == (*self).namespace())]
pub fn open_mut(&mut self) -> Guarded<&mut T, OpenMutGuard> {
unreachable!("ghost code only")
}
}
#[opaque]
pub struct NonAtomicInvariant<T: Protocol>(PhantomData<*mut T>);
#[trusted]
unsafe impl<T: Protocol> Send for NonAtomicInvariant<T> {}
pub struct OpenGuard<T: Protocol>(pub T::Public);
impl<T: Protocol> GuardRef<T> for OpenGuard<T> {
#[logic(open, prophetic, inline)]
fn guards_ref(self, b: T) -> bool {
b.public() == self.0 && b.protocol()
}
}
pub struct OpenMutGuard;
impl<T: Protocol> GuardRef<T> for OpenMutGuard {
#[logic(open, prophetic, inline)]
fn guards_ref(self, x: T) -> bool {
x.protocol()
}
}
impl<T: Protocol> NonAtomicInvariant<T> {
#[trusted]
#[requires(value.protocol())]
#[ensures(result.public() == value.public())]
#[ensures(result.namespace() == *namespace)]
#[check(ghost)]
pub fn new(value: Ghost<T>, namespace: Snapshot<Namespace>) -> Ghost<Self> {
Ghost::conjure()
}
#[trusted]
#[ensures(result.public() == self.public() && result.protocol())]
#[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(self.namespace()))]
#[ensures(result.guard().0 == self.public())]
#[check(ghost)]
pub fn open<'a>(&'a self, tokens: Tokens<'a>) -> Guarded<&'a mut T, OpenGuard<T>> {
panic!("Should not be called outside ghost code")
}
#[trusted]
#[requires(tokens.contains(self.namespace()))]
#[ensures(result.public() == self.public() && result.protocol())]
#[check(ghost)]
pub fn open_const<'a>(&'a self, tokens: &'a Tokens) -> &'a T {
panic!("Should not be called outside ghost code")
}
#[trusted]
#[check(ghost)]
#[ensures(result.inner.public() == self.public())]
#[ensures((^result.inner).public() == (^self).public())]
#[ensures((^self).namespace() == (*self).namespace())]
pub fn open_mut(&mut self) -> Guarded<&mut T, OpenMutGuard> {
unreachable!("ghost code only")
}
}