Skip to content

Commit

Permalink
Thunks now implement the full Cache interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dpl0a committed Apr 3, 2023
1 parent 55f9425 commit df97144
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
17 changes: 8 additions & 9 deletions src/eval/cache/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl IncCache {
let mut current_node = self.store.get_mut(i).unwrap();
current_node.cached = None;
current_node.state = IncNodeState::Suspended;
println!("IDX: {:?} BLs: {:?}", i, current_node.backlinks);
stack.extend(
current_node
.backlinks
Expand Down Expand Up @@ -189,15 +190,13 @@ impl IncCache {
for i in new_indices.values() {
let current_node = self.store.get_mut(*i).unwrap();

current_node.backlinks = std::mem::take(&mut current_node.backlinks)
.into_iter()
.filter_map(|dep| {
Some(DependencyLink {
idx: *new_indices.get(&dep.idx)?,
..dep
})
})
.collect();
for dep in current_node.backlinks.iter_mut() {
dep.idx = if let Some(idx) = new_indices.get(&dep.idx) {
*idx
} else {
dep.idx
}
}

let mut to_be_updated = vec![];

Expand Down
33 changes: 31 additions & 2 deletions src/eval/cache/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use crate::{
identifier::Ident,
term::{record::FieldDeps, BindingType, RichTerm, Term},
};
use std::cell::{Ref, RefCell, RefMut};
use std::rc::{Rc, Weak};
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashMap,
};

/// The state of a thunk.
///
Expand Down Expand Up @@ -355,7 +358,7 @@ impl ThunkData {
/// inside a record may be invalidated by merging, and thus need to store the unaltered original
/// expression. Those aspects are handled and discussed in more detail in
/// [InnerThunkData].
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
pub struct Thunk {
data: Rc<RefCell<ThunkData>>,
ident_kind: IdentKind,
Expand Down Expand Up @@ -560,6 +563,21 @@ impl Thunk {
self.data.borrow().deps()
}
}

impl PartialEq for Thunk {
fn eq(&self, other: &Self) -> bool {
self.data.as_ptr() == other.data.as_ptr() && self.ident_kind == other.ident_kind
}
}

impl Eq for Thunk {}

impl std::hash::Hash for Thunk {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let raw_ptr = self.data.as_ptr();
(self.ident_kind, raw_ptr).hash(state)
}
}
/// A thunk update frame.
///
/// A thunk update frame is put on the stack whenever a variable is entered, such that once this
Expand Down Expand Up @@ -692,4 +710,15 @@ impl Cache for CBNCache {
) -> Result<Self::UpdateIndex, BlackholedError> {
idx.mk_update_frame()
}

fn smart_clone(
&mut self,
v: Vec<CacheIndex>,
) -> std::collections::HashMap<CacheIndex, CacheIndex> {
v.into_iter()
.map(|idx| (idx.clone(), self.revert(&idx)))
.collect()
}

fn propagate_dirty(&mut self, indices: Vec<CacheIndex>) {}
}
12 changes: 6 additions & 6 deletions src/eval/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ use crate::{
term::{record::FieldDeps, BindingType, RichTerm},
};

// pub mod lazy;
pub mod incremental;
pub mod lazy;
// pub mod incremental;

/// An index to a specific item stored in the cache
// pub type CacheIndex = lazy::Thunk;
pub type CacheIndex = usize;
pub type CacheIndex = lazy::Thunk;
// pub type CacheIndex = usize;

/// The current Cache implementation
// pub type CacheImpl = lazy::CBNCache;
pub type CacheImpl = incremental::IncCache;
pub type CacheImpl = lazy::CBNCache;
// pub type CacheImpl = incremental::IncCache;

/// A black-holed node was accessed, which would lead to infinite recursion.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion src/eval/fixpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn rec_env<'a, I: Iterator<Item = (&'a Ident, &'a Field)>, C: Cache>(
// so we start from in the environment of the original record.
let mut final_env = env.clone();
let id_value = Ident::fresh();
final_env.insert(id_value, idx);
final_env.insert(id_value, idx.clone());

let with_ctr_applied = PendingContract::apply_all(
RichTerm::new(Term::Var(id_value), value.pos),
Expand Down
8 changes: 4 additions & 4 deletions src/eval/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,15 @@ fn smart_clone_closurize<C: Cache>(
.flat_map(|(field_idx, contract_idxs)| {
std::iter::once(field_idx).chain(contract_idxs.iter())
})
.filter_map(|x| x.as_ref().copied())
.filter_map(|x| x.as_ref().cloned())
.collect();
let new_indices = cache.smart_clone(to_smart_clone);

for (id, (old_idx, old_contract_indices)) in index_map {
if let Some(idx) = old_idx {
let new_idx = new_indices.get(&idx).unwrap();
let fresh_ident = Ident::fresh();
env.insert(fresh_ident, *new_idx);
env.insert(fresh_ident, new_idx.clone());
let mut field = fields.get_mut(&id).unwrap();
let pos = field.value.as_ref().unwrap().pos;
field.value = Some(RichTerm::new(Term::Var(fresh_ident), pos));
Expand All @@ -599,7 +599,7 @@ fn smart_clone_closurize<C: Cache>(
{
let new_idx = new_indices.get(&idx).unwrap();
let fresh_ident = Ident::fresh();
env.insert(fresh_ident, *new_idx);
env.insert(fresh_ident, new_idx.clone());
let mut pending_contract = fields
.get_mut(&id)
.unwrap()
Expand Down Expand Up @@ -627,7 +627,7 @@ impl GetCacheIndex for Field {
impl GetCacheIndex for RichTerm {
fn get_idx(&self, env: &Environment) -> Option<CacheIndex> {
if let Term::Var(id) = self.as_ref() {
env.get(id).copied()
env.get(id).cloned()
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ impl<C: Cache> VirtualMachine<ImportCache, C> {
}

/// Kind of an identifier.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum IdentKind {
Let,
Lambda,
Expand Down

0 comments on commit df97144

Please sign in to comment.