Skip to content

Commit

Permalink
Merge pull request #62 from metaborg/dwf-bounds-on-query-separate-traits
Browse files Browse the repository at this point in the history
Dwf bounds on query separate traits
  • Loading branch information
AZWN authored Oct 28, 2024
2 parents 6790225 + 65f2689 commit 3605d47
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 64 deletions.
2 changes: 1 addition & 1 deletion scopegraphs/src/completeness/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::Implicit;
/// Unlike [`ExplicitClose`](crate::completeness::ExplicitClose), this implementation will implicitly close edges once traversed.
/// This does not require special attention from the type checker writer.
///
/// Returns [`EdgeClosedError`](EdgeClosedError) when an edge is added to a scope in which the label is already
/// Returns [EdgeClosedError] when an edge is added to a scope in which the label is already
/// closed (because `get_edges(s, l, ...)` was called earlier.
///
/// When edges are retrieved (e.g. during query resolution) the `(src, label)` edge is closed.
Expand Down
30 changes: 26 additions & 4 deletions scopegraphs/src/containers/path.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::future_wrapper::FutureWrapper;
use crate::resolve::{Env, Path, ResolvedPath};
use futures::future::join_all;
use std::fmt::Debug;
use std::hash::Hash;

use super::EnvContainer;

/// Interface for path containers that support the operations required for query resolution.
pub trait PathContainer<'sg, 'rslv, LABEL: 'sg, DATA: 'sg>: 'rslv {
pub trait PathContainer<'sg, 'rslv, LABEL: 'sg, DATA: 'sg>: Debug + 'rslv {
/// Type returned by resolving a path to its sub-environment.
type EnvContainer;

Expand All @@ -15,7 +18,26 @@ pub trait PathContainer<'sg, 'rslv, LABEL: 'sg, DATA: 'sg>: 'rslv {
) -> Self::EnvContainer;
}

impl<'rslv, 'sg, LABEL: 'sg, DATA: 'sg> PathContainer<'sg, 'rslv, LABEL, DATA> for Vec<Path<LABEL>>
/// Trait that is auto-implemented for any [PathContainer] implementation that yields a valid [EnvContainer].
pub trait PathContainerWf<'sg, 'rslv, LABEL: 'sg, DATA: 'sg, DWFO>:
PathContainer<'sg, 'rslv, LABEL, DATA, EnvContainer = Self::EnvContainerWf>
{
/// Witness that ```Self::EnvContainer``` is a valid environment container.
type EnvContainerWf: EnvContainer<'sg, 'rslv, LABEL, DATA, DWFO>;
}

impl<'sg, 'rslv, LABEL, DATA, DWFO, T> PathContainerWf<'sg, 'rslv, LABEL, DATA, DWFO> for T
where
LABEL: Debug + 'sg,
DATA: 'sg,
T: PathContainer<'sg, 'rslv, LABEL, DATA>,
Self::EnvContainer: EnvContainer<'sg, 'rslv, LABEL, DATA, DWFO>,
{
type EnvContainerWf = Self::EnvContainer;
}

impl<'rslv, 'sg, LABEL: Debug + 'sg, DATA: 'sg> PathContainer<'sg, 'rslv, LABEL, DATA>
for Vec<Path<LABEL>>
where
Self: 'rslv,
LABEL: Clone + Hash + Eq,
Expand All @@ -30,8 +52,8 @@ where

// TODO: can this be generalized to arbitrary results of PathContainers?
// (challenge is converting between the different `::EnvContainer`s.)
impl<'rslv, 'sg, LABEL: 'sg, DATA: 'sg, E: 'rslv> PathContainer<'sg, 'rslv, LABEL, DATA>
for Result<Vec<Path<LABEL>>, E>
impl<'rslv, 'sg, LABEL: Debug + 'sg, DATA: 'sg, E: Debug + 'rslv>
PathContainer<'sg, 'rslv, LABEL, DATA> for Result<Vec<Path<LABEL>>, E>
where
Self: 'rslv,
LABEL: Clone + Hash,
Expand Down
120 changes: 114 additions & 6 deletions scopegraphs/src/containers/scope.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::fmt::Debug;
use std::hash::Hash;

use crate::future_wrapper::FutureWrapper;
use crate::resolve::Path;
use crate::Scope;

use super::{PathContainer, PathContainerWf};

/// Interface for scope containers that support the operations required for query resolution.
pub trait ScopeContainer<LABEL> {
pub trait ScopeContainer<'sg, 'rslv, LABEL: Debug + 'sg, DATA: 'sg>: Debug {
/// The type containing paths obtained after stepping to this scope.
type PathContainer;

Expand All @@ -14,7 +19,95 @@ pub trait ScopeContainer<LABEL> {
fn lift_step(self, lbl: LABEL, prefix: Path<LABEL>) -> Self::PathContainer;
}

impl<LABEL: Copy> ScopeContainer<LABEL> for Vec<Scope> {
/// Trait that is auto-implemented for any [ScopeContainer] implementation that yields a valid [PathContainer].
///
/// This trait is implemented for:
/// - `Vec<Scope>`,
/// - [Result] of scope containers, and
/// - [FutureWrapper] of scope containers.
/// ```
/// # use scopegraphs::containers::ScopeContainerWf;
/// # use scopegraphs::future_wrapper::FutureWrapper;
/// # use scopegraphs::Scope;
/// # use std::fmt::Debug;
/// # use std::hash::Hash;
///
/// # trait LBound<'sg>: Copy + Hash + Eq + Debug + 'sg {}
/// # trait DBound<'sg>: Hash + Eq + 'sg {}
///
/// fn test<'sg, 'rslv, LABEL: LBound<'sg>, DATA: DBound<'sg>, DWFO>(
/// cont: impl ScopeContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>
/// ) { }
///
/// # fn scope_vec<'sg, 'rslv, LABEL: LBound<'sg>, DATA: DBound<'sg>>() {
/// let vec: Vec<Scope> = todo!();
/// test::<'_, '_, LABEL, DATA, bool>(vec);
/// # }
///
/// # fn result<'sg, 'rslv, LABEL: LBound<'sg>, DATA: DBound<'sg>, E: Debug + Clone>() {
/// let result: Result<Vec<Scope>, E> = todo!();
/// test::<'_, '_, LABEL, DATA, bool>(result);
/// test::<'_, '_, LABEL, DATA, Result<bool, E>>(result);
/// # }
///
/// # fn future<'sg, 'rslv, LABEL: LBound<'sg>, DATA: DBound<'sg>>() {
/// let future: FutureWrapper<Vec<Scope>> = todo!();
/// test::<'_, '_, LABEL, DATA, bool>(future);
/// test::<'_, '_, LABEL, DATA, FutureWrapper<'_, bool>>(future);
/// # }
/// ```
///
/// ```no_run
/// # use scopegraphs::containers::ScopeContainerWf;
/// # use scopegraphs::Scope;
/// # use std::fmt::Debug;
/// # use std::hash::Hash;
///
///
/// fn test<'sg, 'rslv, LABEL: Hash + Eq + Debug + 'sg, DATA: Hash + Eq + 'sg, DWFO>(cont: impl ScopeContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>) {
///
/// }
/// ```
///
/// ```no_run
/// # use scopegraphs::containers::ScopeContainerWf;
/// # use scopegraphs::Scope;
/// # use std::fmt::Debug;
/// # use std::hash::Hash;
///
/// test::<'_, '_, (), (), bool>(Result::<_, ()>::Ok(Vec::<Scope>::new()));
/// test::<'_, '_, (), (), Result<bool, ()>>(Result::<_, ()>::Ok(Vec::<Scope>::new()));
///
/// fn test<'sg, 'rslv, LABEL: Hash + Eq + Debug + 'sg, DATA: Hash + Eq + 'sg, DWFO>(cont: impl ScopeContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>) {
///
/// }
/// ```
///
pub trait ScopeContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>:
ScopeContainer<'sg, 'rslv, LABEL, DATA, PathContainer = Self::PathContainerWf>
where
LABEL: Debug + 'sg,
DATA: 'sg,
{
/// Refinement of `Self::PathContainer`, carrying proof that this scope container resolves to valid path containers.
type PathContainerWf: PathContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>;
}

impl<'sg, 'rslv, LABEL, DATA, DWFO, T> ScopeContainerWf<'sg, 'rslv, LABEL, DATA, DWFO> for T
where
LABEL: Debug + 'sg,
DATA: 'sg,
T: ScopeContainer<'sg, 'rslv, LABEL, DATA>,
Self::PathContainer: PathContainerWf<'sg, 'rslv, LABEL, DATA, DWFO>,
{
type PathContainerWf = Self::PathContainer;
}

impl<'sg: 'rslv, 'rslv, LABEL: Debug + Copy + Eq + Hash + 'sg, DATA: Eq + Hash + 'sg>
ScopeContainer<'sg, 'rslv, LABEL, DATA> for Vec<Scope>
where
Vec<Path<LABEL>>: PathContainer<'sg, 'rslv, LABEL, DATA>,
{
type PathContainer = Vec<Path<LABEL>>;

fn lift_step(self, lbl: LABEL, prefix: Path<LABEL>) -> Self::PathContainer {
Expand All @@ -24,21 +117,36 @@ impl<LABEL: Copy> ScopeContainer<LABEL> for Vec<Scope> {
}
}

impl<LABEL, SC: ScopeContainer<LABEL>, E> ScopeContainer<LABEL> for Result<SC, E> {
impl<
'sg: 'rslv,
'rslv,
LABEL: Debug + Copy + Eq + Hash + 'sg,
DATA: Eq + Hash + 'sg,
SC: ScopeContainer<'sg, 'rslv, LABEL, DATA>,
E: Debug,
> ScopeContainer<'sg, 'rslv, LABEL, DATA> for Result<SC, E>
where
Result<SC::PathContainer, E>: PathContainer<'sg, 'rslv, LABEL, DATA>,
{
type PathContainer = Result<SC::PathContainer, E>;

fn lift_step(self, lbl: LABEL, prefix: Path<LABEL>) -> Self::PathContainer {
self.map(|sc| sc.lift_step(lbl, prefix))
}
}

impl<'rslv, LABEL, SC: ScopeContainer<LABEL> + Clone> ScopeContainer<LABEL>
for FutureWrapper<'rslv, SC>
impl<
'sg: 'rslv,
'rslv,
LABEL: Debug + Copy + Eq + Hash + 'sg,
DATA: Eq + Hash + 'sg,
SC: ScopeContainer<'sg, 'rslv, LABEL, DATA> + Clone,
> ScopeContainer<'sg, 'rslv, LABEL, DATA> for FutureWrapper<'rslv, SC>
where
LABEL: Copy,
SC::PathContainer: Clone,
Self: 'rslv,
LABEL: 'rslv,
SC::PathContainer: Clone,
{
type PathContainer = FutureWrapper<'rslv, SC::PathContainer>;

Expand Down
7 changes: 5 additions & 2 deletions scopegraphs/src/future_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Defines a cloneable pointer to a future.
use futures::future::Shared;
use futures::FutureExt;
use std::fmt::{Debug, Formatter};
Expand All @@ -7,8 +9,8 @@ use std::{
task::{Context, Poll},
};

// TODO: fork futures and create our own shared future that can have a
// ?Sized inner type (this should be possible)
// FIXME: only expose futures on tests
/// Shared pointer to a future, useful to make functions parametric over the type of future they are invoked with.
pub struct FutureWrapper<'fut, T>(pub Shared<Pin<Box<dyn Future<Output = T> + 'fut>>>);

impl<T> Clone for FutureWrapper<'_, T> {
Expand All @@ -18,6 +20,7 @@ impl<T> Clone for FutureWrapper<'_, T> {
}

impl<'fut, T: Clone> FutureWrapper<'fut, T> {
/// Creates shared pointer to `f`.
pub fn new(f: impl Future<Output = T> + 'fut) -> Self {
let f: Pin<Box<dyn Future<Output = T> + 'fut>> = Box::pin(f);
Self(f.shared())
Expand Down
5 changes: 4 additions & 1 deletion scopegraphs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ pub use scopegraphs_regular_expressions::*;

pub mod completeness;
pub mod containers;
mod future_wrapper;
// #[cfg(test)]
pub mod future_wrapper;
// #[cfg(not(test))]
// mod future_wrapper;

pub mod resolve;

Expand Down
Loading

0 comments on commit 3605d47

Please sign in to comment.