Skip to content

Commit

Permalink
mutability dillema
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 4, 2023
1 parent c31b038 commit b15aadf
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 57 deletions.
16 changes: 9 additions & 7 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ fn reduce_cons<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
head.alloc_hash_equal(&mut cs.namespace(|| stringify!($var)), $name.value())?;
};
}
let c = store.get_constants();
let c = store.expect_constants();
// SOUNDNESS: All symbols with their own case clause must be represented in this list
def_head_val!(head_is_lambda0, c.lambda);
def_head_val!(head_is_let, c.let_);
Expand Down Expand Up @@ -3108,7 +3108,7 @@ fn apply_continuation<F: LurkField, CS: ConstraintSystem<F>>(
.and_then(|commit| store.open(commit))
.unwrap_or_else(|| {
// nil is dummy
(F::ZERO, store.get_nil())
(F::ZERO, store.nil_ptr())
});

let open_expr = AllocatedPtr::alloc(&mut cs.namespace(|| "open_expr"), || {
Expand Down Expand Up @@ -3617,7 +3617,7 @@ fn apply_continuation<F: LurkField, CS: ConstraintSystem<F>>(
let rest_is_nil = allocated_rest.is_nil(&mut cs.namespace(|| "rest_is_nil"), g)?;
let rest_not_nil = rest_is_nil.not();

let begin = store.get_begin();
let begin = store.begin_ptr();

let allocated_begin =
AllocatedPtr::alloc_ptr(&mut cs.namespace(|| "begin"), store, || Ok(&begin))?;
Expand Down Expand Up @@ -3934,7 +3934,7 @@ fn apply_continuation<F: LurkField, CS: ConstraintSystem<F>>(
b,
&diff,
op2.tag(),
store.get_constants(),
store.expect_constants(),
)?;

let field_arithmetic_result = AllocatedPtr::pick(
Expand Down Expand Up @@ -5160,10 +5160,12 @@ fn car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
.car_cdr(ptr)
.map_err(|_| SynthesisError::AssignmentMissing)?
} else {
(store.get_nil(), store.get_nil())
let nil_ptr = store.nil_ptr();
(nil_ptr, nil_ptr)
}
} else {
(store.get_nil(), store.get_nil())
let nil_ptr = store.nil_ptr();
(nil_ptr, nil_ptr)
};

let allocated_car = AllocatedPtr::alloc_ptr(&mut cs.namespace(|| "car"), store, || Ok(&car))?;
Expand Down Expand Up @@ -5700,7 +5702,7 @@ mod tests {
alloc_b.hash(),
&diff,
&g.op2_less_tag,
s.get_constants(),
s.expect_constants(),
)
.unwrap();
assert!(cs.is_satisfied());
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/gadgets/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<F: LurkField> GlobalAllocations<F> {
Ok(Op2::Equal.to_field())
})?;

let c = store.get_constants();
let c = store.expect_constants();

macro_rules! defsym {
($var:ident, $name:expr, $cname:ident) => {
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/gadgets/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl<F: LurkField> AllocatedPtr<F> {
store: &Store<F>,
boolean: &Boolean,
) -> Result<AllocatedPtr<F>, SynthesisError> {
let c = store.get_constants();
let c = store.expect_constants();
AllocatedPtr::pick_const(
cs.namespace(|| "allocated lurk bool"),
boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/eval/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<F: LurkField> Coprocessor<F> for DummyCoprocessor<F> {
/// but for now it exists as an exemplar demonstrating the intended shape of enums like the default, `Coproc`.
fn simple_evaluate(&self, s: &mut Store<F>, args: &[Ptr<F>]) -> Ptr<F> {
assert!(args.is_empty());
s.get_nil()
s.nil_ptr()
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,9 @@ where
}
}

#[inline]
pub fn empty_sym_env<F: LurkField>(store: &Store<F>) -> Ptr<F> {
store.get_nil()
store.nil_ptr()
}

// Convenience functions, mostly for use in tests.
Expand Down
4 changes: 2 additions & 2 deletions src/eval/reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn reduce<F: LurkField, C: Coprocessor<F>>(
store: &mut Store<F>,
lang: &Lang<F, C>,
) -> Result<(Ptr<F>, Ptr<F>, ContPtr<F>, Witness<F>), ReductionError> {
let c = *store.get_constants();
let c = *store.expect_constants();
let (ctrl, witness) = reduce_with_witness(expr, env, cont, store, &c, lang)?;
let (new_expr, new_env, new_cont) = ctrl.into_results(store);

Expand Down Expand Up @@ -1393,7 +1393,7 @@ pub(crate) fn lookup<F: LurkField>(
) -> Result<Ptr<F>, store::Error> {
assert!(matches!(var.tag, ExprTag::Sym));
match env.tag {
ExprTag::Nil => Ok(store.get_nil()),
ExprTag::Nil => Ok(store.nil_ptr()),
ExprTag::Cons => {
let (binding, smaller_env) = store.car_cdr(env)?;
let (v, val) = store.car_cdr(&binding)?;
Expand Down
4 changes: 2 additions & 2 deletions src/eval/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2317,8 +2317,8 @@ fn test_keyword() {
let expr2 = "(eq :asdf :asdf)";
let expr3 = "(eq :asdf 'asdf)";
let res = s.key("asdf");
let res2 = s.get_t();
let res3 = s.get_nil();
let res2 = s.t_ptr();
let res3 = s.nil_ptr();

let terminal = s.get_cont_terminal();

Expand Down
6 changes: 3 additions & 3 deletions src/proof/nova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3181,8 +3181,8 @@ pub mod tests {
let expr2 = "(eq :asdf :asdf)";
let expr3 = "(eq :asdf 'asdf)";
let res = s.key("asdf");
let res2 = s.get_t();
let res3 = s.get_nil();
let res2 = s.t_ptr();
let res3 = s.nil_ptr();

let terminal = s.get_cont_terminal();

Expand Down Expand Up @@ -3535,7 +3535,7 @@ pub mod tests {
#[test]
fn test_prove_call_literal_fun() {
let s = &mut Store::<Fr>::default();
let empty_env = s.get_nil();
let empty_env = s.nil_ptr();
let arg = s.sym("x");
let body = s.read("((+ x 1))").unwrap();
let fun = s.intern_fun(arg, body, empty_env);
Expand Down
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const LURK_PACKAGE_SYMBOLS_NAMES: [&str; 36] = [

#[cfg(test)]
pub mod test {
use super::{State, LURK_PACKAGE_SYMBOLS_NAMES, lurk_sym};
use super::{lurk_sym, State, LURK_PACKAGE_SYMBOLS_NAMES};
use crate::{
package::{Package, SymbolRef},
Symbol,
Expand Down
71 changes: 34 additions & 37 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::expr;
use crate::expr::{Expression, Thunk};
use crate::field::{FWrap, LurkField};
use crate::ptr::{ContPtr, Ptr, RawPtr};
use crate::state::{lurk_sym, State};
use crate::state::lurk_sym;
use crate::symbol::Symbol;
use crate::tag::{ContTag, ExprTag, Op1, Op2, Tag};
use crate::z_cont::ZCont;
Expand Down Expand Up @@ -84,7 +84,7 @@ pub struct Store<F: LurkField> {

impl<F: LurkField> Default for Store<F> {
fn default() -> Self {
Self {
let mut store = Self {
cons_store: Default::default(),
comm_store: Default::default(),
sym_store: Default::default(),
Expand Down Expand Up @@ -117,7 +117,9 @@ impl<F: LurkField> Default for Store<F> {
str_cache: Default::default(),
symbol_cache: Default::default(),
constants: Default::default(),
}
};
store.ensure_constants();
store
}
}

Expand All @@ -135,34 +137,38 @@ impl fmt::Display for Error {
/// Prefer these methods when constructing literal data or assembling program fragments in
/// tests or during evaluation, etc.
impl<F: LurkField> Store<F> {
#[inline]
pub fn nil_ptr(&mut self) -> Ptr<F> {
self.get_constants().nil.ptr()
pub fn expect_constants(&self) -> &NamedConstants<F> {
self.constants
.get()
.expect("Constants must have been set during instatiation")
}

#[inline]
pub fn t_ptr(&mut self) -> Ptr<F> {
self.get_constants().t.ptr()
pub fn nil_ptr(&self) -> Ptr<F> {
self.expect_constants().nil.ptr()
}

#[inline]
pub fn cons_ptr(&mut self) -> Ptr<F> {
self.get_constants().cons.ptr()
pub fn cons_ptr(&self) -> Ptr<F> {
self.expect_constants().cons.ptr()
}

#[inline]
pub fn eq_ptr(&mut self) -> Ptr<F> {
self.get_constants().equal.ptr()
pub fn begin_ptr(&self) -> Ptr<F> {
self.expect_constants().begin.ptr()
}

#[inline]
pub fn quote_ptr(&mut self) -> Ptr<F> {
self.get_constants().quote.ptr()
pub fn eq_ptr(&self) -> Ptr<F> {
self.expect_constants().equal.ptr()
}

#[inline]
pub fn dummy_ptr(&mut self) -> Ptr<F> {
self.get_constants().dummy.ptr()
pub fn quote_ptr(&self) -> Ptr<F> {
self.expect_constants().quote.ptr()
}

pub fn t_ptr(&self) -> Ptr<F> {
self.expect_constants().t.ptr()
}

pub fn dummy_ptr(&self) -> Ptr<F> {
self.expect_constants().dummy.ptr()
}

#[inline]
Expand Down Expand Up @@ -464,7 +470,7 @@ impl<F: LurkField> Store<F> {
let str_ptr = self.intern_string(s);
ptr = self.intern_symcons(str_ptr, ptr);
}
if sym == lurk_sym("nil") {
if *sym == lurk_sym("nil") {
Ptr {
tag: ExprTag::Nil,
raw: ptr.raw,
Expand Down Expand Up @@ -1410,10 +1416,6 @@ impl<F: LurkField> Store<F> {
let _ = self.constants.set(NamedConstants::new(self));
}

pub fn get_constants(&self) -> &NamedConstants<F> {
self.constants.get_or_init(|| NamedConstants::new(self))
}

/// The only places that `ZPtr`s for `Ptr`s should be created, to
/// ensure that they are cached properly
fn create_z_expr_ptr(&self, ptr: Ptr<F>, hash: F) -> ZExprPtr<F> {
Expand Down Expand Up @@ -1804,22 +1806,17 @@ pub struct NamedConstants<F: LurkField> {
}

impl<F: LurkField> NamedConstants<F> {
pub fn new(store: &Store<F>) -> Self {
let state = State::initial_lurk_state();
let resolve = |name: &str| {
state.resolve(name).expect("symbol must have been interned")
};
pub fn new(store: &mut Store<F>) -> Self {
let nil_ptr = store.intern_symbol(&lurk_sym("nil"));
let nil_z_ptr = Some(ZExpr::Nil.z_ptr(&store.poseidon_cache));

let mut hash_sym = |name: &str| {
let symbol = resolve(name);
let ptr = store.intern_symbol(&symbol);
let ptr = store.intern_symbol(&lurk_sym(name));
let maybe_z_ptr = store.hash_expr(&ptr);
ConstantPtrs(maybe_z_ptr, ptr)
};

let nil = ConstantPtrs(
Some(ZExpr::Nil.z_ptr(&store.poseidon_cache)),
store.intern_symbol(&resolve("nil")),
);
let nil = ConstantPtrs(nil_z_ptr, nil_ptr);
let t = hash_sym("t");
let lambda = hash_sym("lambda");
let quote = hash_sym("quote");
Expand Down
4 changes: 3 additions & 1 deletion src/z_data/z_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ impl<F: LurkField> ZStore<F> {

/// Stores a null symbol in the `ZStore` and returns the resulting pointer
pub fn nil_z_ptr(&mut self, poseidon_cache: &PoseidonCache<F>) -> ZExprPtr<F> {
let z_ptr = self.put_symbol(&crate::state::lurk_sym("nil"), poseidon_cache).0;
let z_ptr = self
.put_symbol(&crate::state::lurk_sym("nil"), poseidon_cache)
.0;
ZPtr(ExprTag::Nil, z_ptr.1)
}

Expand Down

0 comments on commit b15aadf

Please sign in to comment.