1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
use crate::completeness::private::Sealed;
use crate::completeness::{
Completeness, CriticalEdgeBasedCompleteness, CriticalEdgeSet, Delay, EdgeClosedError,
EdgesOrDelay, FutureCompleteness,
};
use crate::scopegraph::{InnerScopeGraph, Scope, ScopeGraph};
use crate::Label;
use std::{collections::HashSet, hash::Hash};
/// Critical-edge based [`Completeness`] implementation.
///
/// Unlike [`ImplicitClose`](super::ImplicitClose), this implementation shifts responsibility of closing edges to the
/// _type checker writer_. I.e., they have to insert `sg.close(scope, label)` statements at the
/// appropriate positions in the code.
///
/// Returns [`EdgeClosedError`] when an edge is added to a scope in which the label is already
/// closed (by an explicit close of the type checker writer).
///
/// Returns [`Delay`] when edges are retrieved (e.g. during query resolution) for an edge that is
/// not yet closed.
#[derive(Debug)]
pub struct ExplicitClose<LABEL> {
critical_edges: CriticalEdgeSet<LABEL>,
}
impl<LABEL> Default for ExplicitClose<LABEL> {
fn default() -> Self {
ExplicitClose {
critical_edges: CriticalEdgeSet::default(),
}
}
}
impl<LABEL> Sealed for ExplicitClose<LABEL> {}
impl<LABEL: Hash + Eq + Label, DATA> Completeness<LABEL, DATA> for ExplicitClose<LABEL> {
fn cmpl_new_scope(&self, _: &InnerScopeGraph<LABEL, DATA>, _: Scope) {
<ExplicitClose<LABEL> as CriticalEdgeBasedCompleteness<LABEL, DATA>>::init_scope_with(
self,
LABEL::iter().collect(), // init with all labels: programmer is responsible for closing edges
)
}
fn cmpl_new_complete_scope(&self, _: &InnerScopeGraph<LABEL, DATA>, _: Scope) {
<ExplicitClose<LABEL> as CriticalEdgeBasedCompleteness<LABEL, DATA>>::init_scope_with(
self,
HashSet::new(), // init with empty label set to prevent extension
)
}
type NewEdgeResult = Result<(), EdgeClosedError<LABEL>>;
fn cmpl_new_edge(
&self,
inner_scope_graph: &InnerScopeGraph<LABEL, DATA>,
src: Scope,
lbl: LABEL,
dst: Scope,
) -> Self::NewEdgeResult {
if self.critical_edges.is_open(src, &lbl) {
inner_scope_graph.add_edge(src, lbl, dst);
Ok(())
} else {
Err(EdgeClosedError {
scope: src,
label: lbl,
})
}
}
type GetEdgesResult<'rslv> = EdgesOrDelay<Vec<Scope>, LABEL>
where
Self: 'rslv, LABEL: 'rslv, DATA: 'rslv;
fn cmpl_get_edges<'rslv>(
&self,
inner_scope_graph: &InnerScopeGraph<LABEL, DATA>,
src: Scope,
lbl: LABEL,
) -> Self::GetEdgesResult<'rslv>
where
LABEL: 'rslv,
DATA: 'rslv,
{
if self.critical_edges.is_open(src, &lbl) {
Err(Delay {
scope: src,
label: lbl,
})
} else {
Ok(inner_scope_graph.get_edges(src, lbl))
}
}
}
impl<LABEL: Hash + Eq + Label, DATA> CriticalEdgeBasedCompleteness<LABEL, DATA>
for ExplicitClose<LABEL>
{
fn init_scope_with(&self, open_labels: HashSet<LABEL>) {
self.critical_edges.init_scope(open_labels)
}
}
impl<LABEL: Hash + Eq> ExplicitClose<LABEL> {
/// Close a scope for a certain label
/// // TODO: link to "closing" in concepts
pub fn close(&self, scope: Scope, label: &LABEL) {
self.critical_edges.close(scope, label);
}
}
impl<'sg, LABEL: Hash + Eq, DATA> ScopeGraph<'sg, LABEL, DATA, ExplicitClose<LABEL>> {
// TODO: fix this sentence
/// Closes an edge, (i.e., prohibit future new
///
/// For example, the following program will return an error.
/// ```
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::Label;
/// # use scopegraphs::Storage;
/// # use scopegraphs::ScopeGraph;
///
/// # #[derive(Eq, Hash, PartialEq, Label)] enum Lbl { Def }
/// # use Lbl::*;
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// sg.close(s1, &Def);
/// sg.add_edge(s1, Def, s2).expect_err("cannot add edge after closing edge");
/// ```
///
/// Closing is required to permit queries to traverse these edges:
/// ```
///
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::ScopeGraph;
/// # use scopegraphs::resolve::{DefaultDataEquivalence, DefaultLabelOrder, EdgeOrData, Resolve};
/// # use scopegraphs_macros::{compile_regex, Label};
/// # use scopegraphs::Storage;
/// #
/// # #[derive(Eq, Hash, PartialEq, Label, Debug, Copy, Clone)]
/// # enum Lbl { Def }
/// # use Lbl::*;
/// # type LblD = EdgeOrData<Lbl>;
/// #
/// # compile_regex!(type Regex<Lbl> = Def);
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// // Note: not calling `sg.close(s1, &Def)`
///
/// let query_result = sg.query()
/// .with_path_wellformedness(Regex::new()) // regex: `Def`
/// .with_data_wellformedness(|x: &usize| *x == 42) // match `42`
/// .resolve(s1);
///
/// query_result.expect_err("require s1/Def to be closed");
/// ```
///
/// Closing allows queries to resolve:
/// ```
///
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::ScopeGraph;
/// # use scopegraphs::resolve::{DefaultDataEquivalence, DefaultLabelOrder, EdgeOrData, Resolve};
/// # use scopegraphs_macros::{compile_regex, Label};
/// # use scopegraphs::Storage;
/// #
/// # #[derive(Eq, Hash, PartialEq, Label, Debug, Copy, Clone)]
/// # enum Lbl { Def }
/// # use Lbl::*;
/// # type LblD = EdgeOrData<Lbl>;
/// #
/// # compile_regex!(type Regex<Lbl> = Def);
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// // Note: closing the edge *after* creating all edges, *before* doing the query
/// sg.close(s1, &Def);
///
/// let query_result = sg.query()
/// .with_path_wellformedness(Regex::new()) // regex: `Def`
/// .with_data_wellformedness(|x: &usize| *x == 42) // match `42`
/// .resolve(s1);
///
/// query_result.expect("query should return result");
/// ```
pub fn close(&self, scope: Scope, label: &LABEL) {
self.completeness.close(scope, label)
}
}
impl<'sg, LABEL: Hash + Eq + Copy, DATA> ScopeGraph<'sg, LABEL, DATA, FutureCompleteness<LABEL>> {
// TODO: update this example to use futures
// TODO: fix this sentence
/// Closes an edge, (i.e., prohibit future new
///
/// For example, the following program will return an error.
/// ```
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::ScopeGraph;
/// # use scopegraphs_macros::Label;
/// # use scopegraphs::Storage;
/// # #[derive(Eq, Hash, PartialEq, Label)] enum Lbl { Def }
/// # use Lbl::*;
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// sg.close(s1, &Def);
/// sg.add_edge(s1, Def, s2).expect_err("cannot add edge after closing edge");
/// ```
///
/// Closing is required to permit queries to traverse these edges:
/// ```
///
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::ScopeGraph;
/// # use scopegraphs::resolve::{DefaultDataEquivalence, DefaultLabelOrder, EdgeOrData, Resolve};
/// # use scopegraphs_macros::{compile_regex, Label};
/// # use scopegraphs::Storage;
/// #
/// # #[derive(Eq, Hash, PartialEq, Label, Debug, Copy, Clone)]
/// # enum Lbl { Def }
/// # use Lbl::*;
/// # type LblD = EdgeOrData<Lbl>;
/// #
/// # compile_regex!(type Regex<Lbl> = Def);
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// // Note: not calling `sg.close(s1, &Def)`
///
/// let query_result = sg.query()
/// .with_path_wellformedness(Regex::new()) // regex: `Def`
/// .with_data_wellformedness(|x: &usize| *x == 42) // match `42`
/// .resolve(s1);
///
/// query_result.expect_err("require s1/Def to be closed");
/// ```
///
/// Closing allows queries to resolve:
/// ```
///
/// # use scopegraphs::completeness::ExplicitClose;
/// # use scopegraphs::ScopeGraph;
/// # use scopegraphs::resolve::{DefaultDataEquivalence, DefaultLabelOrder, EdgeOrData, Resolve};
/// # use scopegraphs_macros::{compile_regex, Label};
/// # use scopegraphs::Storage;
/// #
/// # #[derive(Eq, Hash, PartialEq, Label, Debug, Copy, Clone)]
/// # enum Lbl { Def }
/// # use Lbl::*;
/// # type LblD = EdgeOrData<Lbl>;
/// #
/// # compile_regex!(type Regex<Lbl> = Def);
/// let storage = Storage::new();
/// let mut sg = ScopeGraph::<Lbl, usize, _>::new(&storage, ExplicitClose::default());
///
/// let s1 = sg.add_scope_with(0, [Def]);
/// let s2 = sg.add_scope_closed(42);
///
/// // Note: closing the edge *after* creating all edges, *before* doing the query
/// sg.close(s1, &Def);
///
/// let query_result = sg.query()
/// .with_path_wellformedness(Regex::new()) // regex: `Def`
/// .with_data_wellformedness(|x: &usize| *x == 42) // match `42`
/// .resolve(s1);
///
/// query_result.expect("query should return result");
/// ```
pub fn close(&self, scope: Scope, label: &LABEL) {
self.completeness.close(scope, label)
}
}