Skip to content

Commit

Permalink
Utility macros
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jan 3, 2024
1 parent 2f5e387 commit bfc78a0
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 122 deletions.
3 changes: 2 additions & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
eval::evaluate_with_env_and_cont,
multiframe::MultiFrame,
pointers::{Ptr, RawPtr, ZPtr},
store::expect_ptrs,
Tag,
},
package::{Package, SymbolRef},
Expand Down Expand Up @@ -945,7 +946,7 @@ impl MetaCmd<F> {
io[0].fmt_to_string(&repl.store, &repl.state.borrow())
)
};
let [pre_verify, post_verify] = &repl.store.expect_2_ptrs(*idx);
let [pre_verify, post_verify] = &expect_ptrs!(repl.store, 2, *idx);

if pre_verify.is_nil() {
bail!("Pre-verification predicate rejected the input")
Expand Down
35 changes: 21 additions & 14 deletions src/cli/zstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
field::{FWrap, LurkField},
lem::{
pointers::{Ptr, RawPtr, ZPtr},
store::Store,
store::{expect_ptrs, intern_ptrs_hydrated, Store},
},
};

Expand Down Expand Up @@ -53,7 +53,7 @@ impl<F: LurkField> ZDag<F> {
unreachable!()
}
RawPtr::Hash4(idx) => {
let [a, b] = store.expect_2_ptrs(*idx);
let [a, b] = expect_ptrs!(store, 2, *idx);
let a = self.populate_with(&a, store, cache);
let b = self.populate_with(&b, store, cache);
let z_ptr = ZPtr::from_parts(
Expand All @@ -69,7 +69,7 @@ impl<F: LurkField> ZDag<F> {
z_ptr
}
RawPtr::Hash6(idx) => {
let [a, b, c] = store.expect_3_ptrs(*idx);
let [a, b, c] = expect_ptrs!(store, 3, *idx);
let a = self.populate_with(&a, store, cache);
let b = self.populate_with(&b, store, cache);
let c = self.populate_with(&c, store, cache);
Expand All @@ -88,7 +88,7 @@ impl<F: LurkField> ZDag<F> {
z_ptr
}
RawPtr::Hash8(idx) => {
let [a, b, c, d] = store.expect_4_ptrs(*idx);
let [a, b, c, d] = expect_ptrs!(store, 4, *idx);
let a = self.populate_with(&a, store, cache);
let b = self.populate_with(&b, store, cache);
let c = self.populate_with(&c, store, cache);
Expand Down Expand Up @@ -137,20 +137,20 @@ impl<F: LurkField> ZDag<F> {
Some(ZPtrType::Tuple2(z1, z2)) => {
let ptr1 = self.populate_store(z1, store, cache)?;
let ptr2 = self.populate_store(z2, store, cache)?;
store.intern_2_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, *z_ptr)
intern_ptrs_hydrated!(store, *z_ptr.tag(), *z_ptr, ptr1, ptr2)
}
Some(ZPtrType::Tuple3(z1, z2, z3)) => {
let ptr1 = self.populate_store(z1, store, cache)?;
let ptr2 = self.populate_store(z2, store, cache)?;
let ptr3 = self.populate_store(z3, store, cache)?;
store.intern_3_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, ptr3, *z_ptr)
intern_ptrs_hydrated!(store, *z_ptr.tag(), *z_ptr, ptr1, ptr2, ptr3)
}
Some(ZPtrType::Tuple4(z1, z2, z3, z4)) => {
let ptr1 = self.populate_store(z1, store, cache)?;
let ptr2 = self.populate_store(z2, store, cache)?;
let ptr3 = self.populate_store(z3, store, cache)?;
let ptr4 = self.populate_store(z4, store, cache)?;
store.intern_4_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, ptr3, ptr4, *z_ptr)
intern_ptrs_hydrated!(store, *z_ptr.tag(), *z_ptr, ptr1, ptr2, ptr3, ptr4)
}
};
cache.insert(*z_ptr, ptr);
Expand Down Expand Up @@ -281,7 +281,11 @@ mod tests {

use crate::{
field::LurkField,
lem::{pointers::Ptr, store::Store, Tag},
lem::{
pointers::Ptr,
store::{intern_ptrs, Store},
Tag,
},
tag::{ContTag, ExprTag, Op1, Op2},
};

Expand All @@ -302,23 +306,26 @@ mod tests {
} else {
match rnd % 4 {
0 => store.intern_atom(tag, Fp::from_u64(rnd)),
1 => store.intern_2_ptrs(
1 => intern_ptrs!(
store,
tag,
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store)
),
2 => store.intern_3_ptrs(
2 => intern_ptrs!(
store,
tag,
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store)
),
3 => store.intern_4_ptrs(
3 => intern_ptrs!(
store,
tag,
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store),
rng_interner(rng, max_depth - 1, store)
),
_ => unreachable!(),
}
Expand Down
25 changes: 14 additions & 11 deletions src/coprocessor/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
lem::{
circuit::GlobalAllocator,
pointers::{Ptr, ZPtr},
store::Store,
store::{expect_ptrs, Store},
},
tag::{ExprTag, Tag},
};
Expand Down Expand Up @@ -178,7 +178,7 @@ pub(crate) fn deconstruct_tuple2<F: LurkField, CS: ConstraintSystem<F>>(
) -> Result<(AllocatedPtr<F>, AllocatedPtr<F>), SynthesisError> {
let (a, b) = if not_dummy.get_value() == Some(true) {
let idx = get_ptr(tuple, store)?.get_index2().expect("invalid Ptr");
let [a, b] = &store.expect_2_ptrs(idx);
let [a, b] = &expect_ptrs!(store, 2, idx);
(store.hash_ptr(a), store.hash_ptr(b))
} else {
(ZPtr::dummy(), ZPtr::dummy())
Expand Down Expand Up @@ -221,7 +221,7 @@ pub(crate) fn deconstruct_tuple3<F: LurkField, CS: ConstraintSystem<F>>(
) -> Result<(AllocatedPtr<F>, AllocatedPtr<F>, AllocatedPtr<F>), SynthesisError> {
let (a, b, c) = if not_dummy.get_value() == Some(true) {
let idx = get_ptr(tuple, store)?.get_index3().expect("invalid Ptr");
let [a, b, c] = &store.expect_3_ptrs(idx);
let [a, b, c] = &expect_ptrs!(store, 3, idx);
(store.hash_ptr(a), store.hash_ptr(b), store.hash_ptr(c))
} else {
(ZPtr::dummy(), ZPtr::dummy(), ZPtr::dummy())
Expand Down Expand Up @@ -275,7 +275,7 @@ pub(crate) fn deconstruct_tuple4<F: LurkField, CS: ConstraintSystem<F>>(
> {
let (a, b, c, d) = if not_dummy.get_value() == Some(true) {
let idx = get_ptr(tuple, store)?.get_index4().expect("invalid Ptr");
let [a, b, c, d] = &store.expect_4_ptrs(idx);
let [a, b, c, d] = &expect_ptrs!(store, 4, idx);
(
store.hash_ptr(a),
store.hash_ptr(b),
Expand Down Expand Up @@ -538,7 +538,10 @@ mod test {
deconstruct_tuple4,
},
field::LurkField,
lem::{circuit::GlobalAllocator, store::Store},
lem::{
circuit::GlobalAllocator,
store::{intern_ptrs, Store},
},
};

use super::{a_ptr_as_z_ptr, chain_car_cdr, construct_list, deconstruct_tuple2};
Expand All @@ -561,7 +564,7 @@ mod test {
&a_nil,
)
.unwrap();
let nil2_ptr = store.intern_2_ptrs(*nil_tag, nil, nil);
let nil2_ptr = intern_ptrs!(store, *nil_tag, nil, nil);
let z_nil2_ptr = store.hash_ptr(&nil2_ptr);
assert_eq!(a_ptr_as_z_ptr(&nil2), Some(z_nil2_ptr));

Expand All @@ -575,7 +578,7 @@ mod test {
&a_nil,
)
.unwrap();
let nil3_ptr = store.intern_3_ptrs(*nil_tag, nil, nil, nil);
let nil3_ptr = intern_ptrs!(store, *nil_tag, nil, nil, nil);
let z_nil3_ptr = store.hash_ptr(&nil3_ptr);
assert_eq!(a_ptr_as_z_ptr(&nil3), Some(z_nil3_ptr));

Expand All @@ -590,7 +593,7 @@ mod test {
&a_nil,
)
.unwrap();
let nil4_ptr = store.intern_4_ptrs(*nil_tag, nil, nil, nil, nil);
let nil4_ptr = intern_ptrs!(store, *nil_tag, nil, nil, nil, nil);
let z_nil4_ptr = store.hash_ptr(&nil4_ptr);
assert_eq!(a_ptr_as_z_ptr(&nil4), Some(z_nil4_ptr));
}
Expand Down Expand Up @@ -624,7 +627,7 @@ mod test {
let nil_tag = *nil.tag();
let not_dummy = Boolean::Constant(true);

let tuple2 = store.intern_2_ptrs(nil_tag, nil, nil);
let tuple2 = intern_ptrs!(store, nil_tag, nil, nil);
let z_tuple2 = store.hash_ptr(&tuple2);
let a_tuple2 = AllocatedPtr::alloc_infallible(&mut cs.namespace(|| "tuple2"), || z_tuple2);
let (a, b) = deconstruct_tuple2(
Expand All @@ -637,7 +640,7 @@ mod test {
assert_eq!(a_ptr_as_z_ptr(&a), Some(z_nil));
assert_eq!(a_ptr_as_z_ptr(&b), Some(z_nil));

let tuple3 = store.intern_3_ptrs(nil_tag, nil, nil, nil);
let tuple3 = intern_ptrs!(store, nil_tag, nil, nil, nil);
let z_tuple3 = store.hash_ptr(&tuple3);
let a_tuple3 = AllocatedPtr::alloc_infallible(&mut cs.namespace(|| "tuple3"), || z_tuple3);
let (a, b, c) = deconstruct_tuple3(
Expand All @@ -651,7 +654,7 @@ mod test {
assert_eq!(a_ptr_as_z_ptr(&b), Some(z_nil));
assert_eq!(a_ptr_as_z_ptr(&c), Some(z_nil));

let tuple4 = store.intern_4_ptrs(nil_tag, nil, nil, nil, nil);
let tuple4 = intern_ptrs!(store, nil_tag, nil, nil, nil, nil);
let z_tuple4 = store.hash_ptr(&tuple4);
let a_tuple4 = AllocatedPtr::alloc_infallible(&mut cs.namespace(|| "tuple4"), || z_tuple4);
let (a, b, c, d) = deconstruct_tuple4(
Expand Down
7 changes: 3 additions & 4 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
use super::{
interpreter::{Frame, Hints},
pointers::{Ptr, RawPtr},
store::Store,
store::{fetch_ptrs, Store},
Ctrl, Func, Op, Tag, Var,
};

Expand All @@ -42,9 +42,8 @@ fn get_pc<F: LurkField, C: Coprocessor<F>>(
) -> usize {
match expr.parts() {
(Tag::Expr(Cproc), RawPtr::Hash4(idx)) => {
let [cproc, _] = &store
.fetch_2_ptrs(*idx)
.expect("Coprocessor expression is not interned");
let [cproc, _] =
&fetch_ptrs!(store, 2, *idx).expect("Coprocessor expression is not interned");
let cproc_sym = store
.fetch_symbol(cproc)
.expect("Coprocessor expression is not interned");
Expand Down
17 changes: 9 additions & 8 deletions src/lem/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
path::Path,
pointers::{Ptr, RawPtr},
slot::{SlotData, Val},
store::Store,
store::{fetch_ptrs, intern_ptrs, Store},
var_map::VarMap,
Block, Ctrl, Func, Op, Tag, Var,
};
Expand Down Expand Up @@ -341,27 +341,28 @@ impl Block {
}
Op::Cons2(img, tag, preimg) => {
let preimg_ptrs = bindings.get_many_ptr(preimg)?;
let tgt_ptr = store.intern_2_ptrs(*tag, preimg_ptrs[0], preimg_ptrs[1]);
let tgt_ptr = intern_ptrs!(store, *tag, preimg_ptrs[0], preimg_ptrs[1]);
bindings.insert_ptr(img.clone(), tgt_ptr);
let vals = preimg_ptrs.into_iter().map(Val::Pointer).collect();
hints.hash4.push(Some(SlotData { vals }));
}
Op::Cons3(img, tag, preimg) => {
let preimg_ptrs = bindings.get_many_ptr(preimg)?;
let tgt_ptr =
store.intern_3_ptrs(*tag, preimg_ptrs[0], preimg_ptrs[1], preimg_ptrs[2]);
intern_ptrs!(store, *tag, preimg_ptrs[0], preimg_ptrs[1], preimg_ptrs[2]);
bindings.insert_ptr(img.clone(), tgt_ptr);
let vals = preimg_ptrs.into_iter().map(Val::Pointer).collect();
hints.hash6.push(Some(SlotData { vals }));
}
Op::Cons4(img, tag, preimg) => {
let preimg_ptrs = bindings.get_many_ptr(preimg)?;
let tgt_ptr = store.intern_4_ptrs(
let tgt_ptr = intern_ptrs!(
store,
*tag,
preimg_ptrs[0],
preimg_ptrs[1],
preimg_ptrs[2],
preimg_ptrs[3],
preimg_ptrs[3]
);
bindings.insert_ptr(img.clone(), tgt_ptr);
let vals = preimg_ptrs.into_iter().map(Val::Pointer).collect();
Expand All @@ -372,7 +373,7 @@ impl Block {
let Some(idx) = img_ptr.get_index2() else {
bail!("{img} isn't a Tree2 pointer");
};
let Some(preimg_ptrs) = store.fetch_2_ptrs(idx) else {
let Some(preimg_ptrs) = fetch_ptrs!(store, 2, idx) else {
bail!("Couldn't fetch {img}'s children")
};
for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) {
Expand All @@ -386,7 +387,7 @@ impl Block {
let Some(idx) = img_ptr.get_index3() else {
bail!("{img} isn't a Tree3 pointer");
};
let Some(preimg_ptrs) = store.fetch_3_ptrs(idx) else {
let Some(preimg_ptrs) = fetch_ptrs!(store, 3, idx) else {
bail!("Couldn't fetch {img}'s children")
};
for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) {
Expand All @@ -400,7 +401,7 @@ impl Block {
let Some(idx) = img_ptr.get_index4() else {
bail!("{img} isn't a Tree4 pointer");
};
let Some(preimg_ptrs) = store.fetch_4_ptrs(idx) else {
let Some(preimg_ptrs) = fetch_ptrs!(store, 4, idx) else {
bail!("Couldn't fetch {img}'s children")
};
for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) {
Expand Down
Loading

0 comments on commit bfc78a0

Please sign in to comment.