Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jcb/nom and syntax #383

Merged
merged 3 commits into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -39,6 +39,8 @@ num-bigint = "0.4.3"
num-integer = "0.1.45"
num-traits = "0.2.15"
nom = "7.1.3"
nom_locate = "4.1.0"
base-x = "0.2.11"
clap = "4.1.8"
tap = "1.0.1"
stable_deref_trait = "1.1.1"
4 changes: 2 additions & 2 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
@@ -1413,7 +1413,7 @@ fn reduce_cons<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let mut head_is_coprocessor_bools = Vec::with_capacity(lang.coprocessors().len());

for (sym, (_coproc, scalar_ptr)) in lang.coprocessors().iter() {
let cs = &mut cs.namespace(|| format!("head is {}", sym.full_name()));
let cs = &mut cs.namespace(|| format!("head is {}", sym));

let allocated_boolean = head.alloc_hash_equal(cs, *scalar_ptr.value())?;

@@ -2636,7 +2636,7 @@ fn reduce_cons<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
)?;

for (sym, (coproc, scalar_ptr)) in lang.coprocessors().iter() {
let cs = &mut cs.namespace(|| format!("{} coprocessor", sym.full_name()));
let cs = &mut cs.namespace(|| format!("{} coprocessor", sym));

let arity = coproc.arity();

10 changes: 5 additions & 5 deletions src/eval/lang.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use crate::coprocessor::{CoCircuit, Coprocessor};
use crate::field::LurkField;
use crate::ptr::Ptr;
use crate::store::Store;
use crate::sym::Sym;
use crate::symbol::Symbol;
use crate::z_ptr::ZExprPtr;

/// `DummyCoprocessor` is a concrete implementation of the [`crate::coprocessor::Coprocessor`] trait.
@@ -114,7 +114,7 @@ impl<F: LurkField> CoCircuit<F> for Coproc<F> {
#[derive(Debug, Default, Clone)]
pub struct Lang<F: LurkField, C: Coprocessor<F>> {
// A HashMap that stores coprocessors with their associated `Sym` keys.
coprocessors: HashMap<Sym, (C, ZExprPtr<F>)>,
coprocessors: HashMap<Symbol, (C, ZExprPtr<F>)>,
}

impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {
@@ -124,14 +124,14 @@ impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {
}
}

pub fn add_coprocessor(&mut self, name: Sym, cproc: C, store: &mut Store<F>) {
let ptr = store.intern_sym(name.clone());
pub fn add_coprocessor(&mut self, name: Symbol, cproc: C, store: &mut Store<F>) {
let ptr = store.intern_symbol(name.clone());
let scalar_ptr = store.hash_expr(&ptr).unwrap();

self.coprocessors.insert(name, (cproc, scalar_ptr));
}

pub fn coprocessors(&self) -> &HashMap<Sym, (C, ZExprPtr<F>)> {
pub fn coprocessors(&self) -> &HashMap<Symbol, (C, ZExprPtr<F>)> {
&self.coprocessors
}

18 changes: 9 additions & 9 deletions src/eval/reduction.rs
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ use crate::expr::{Expression, Thunk};
use crate::field::LurkField;
use crate::hash_witness::{ConsName, ConsWitness, ContName, ContWitness};
use crate::num::Num;
use crate::ptr::{ContPtr, Ptr};
use crate::ptr::{ContPtr, Ptr, TypePredicates};
use crate::store;
use crate::store::{NamedConstants, Store, TypePredicates};
use crate::store::{NamedConstants, Store};
use crate::tag::{ContTag, ExprTag, Op1, Op2};
use crate::writer::Write;

@@ -916,7 +916,7 @@ fn apply_continuation<F: LurkField>(
.hash_expr(&result)
.ok_or_else(|| store::Error("expr hash missing".into()))?;

store.get_u64(scalar_ptr.value().to_u64_unchecked())
store.intern_u64(scalar_ptr.value().to_u64_unchecked())
}
ExprTag::U64 => result,
_ => return Ok(Control::Error(result, env)),
@@ -935,7 +935,7 @@ fn apply_continuation<F: LurkField>(
let scalar_ptr = store
.hash_expr(&result)
.ok_or_else(|| store::Error("expr hash missing".into()))?;
store.get_char_from_u32(scalar_ptr.value().to_u32_unchecked())
store.intern_char(scalar_ptr.value().to_char().unwrap())
}
_ => return Ok(Control::Error(result, env)),
},
@@ -1062,9 +1062,9 @@ fn apply_continuation<F: LurkField>(
}
(Expression::UInt(a), Expression::UInt(b)) if operator.is_numeric() => {
match operator {
Op2::Sum => store.get_u64((a + b).into()),
Op2::Diff => store.get_u64((a - b).into()),
Op2::Product => store.get_u64((a * b).into()),
Op2::Sum => store.intern_u64((a + b).into()),
Op2::Diff => store.intern_u64((a - b).into()),
Op2::Product => store.intern_u64((a * b).into()),
Op2::Quotient => {
if b.is_zero() {
return Ok(Control::Return(
@@ -1073,7 +1073,7 @@ fn apply_continuation<F: LurkField>(
store.intern_cont_error(),
));
} else {
store.get_u64((a / b).into())
store.intern_u64((a / b).into())
}
}
Op2::Modulo => {
@@ -1084,7 +1084,7 @@ fn apply_continuation<F: LurkField>(
store.intern_cont_error(),
));
} else {
store.get_u64((a % b).into())
store.intern_u64((a % b).into())
}
}
Op2::Equal | Op2::NumEqual => store.as_lurk_boolean(a == b),
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -14,13 +14,14 @@ pub mod expr;
pub mod field;
pub mod hash;
pub mod hash_witness;
pub mod package;
//pub mod package;
pub mod parser;
pub mod proof;
pub mod ptr;
pub mod repl;
pub mod store;
pub mod sym;
pub mod symbol;
pub mod syntax;
pub mod tag;
pub mod uint;
pub mod writer;
@@ -33,7 +34,7 @@ pub mod z_store;
pub mod error;
mod num;
pub use num::Num;
pub use sym::{Sym, Symbol};
pub use symbol::Symbol;
pub use uint::UInt;

pub const TEST_SEED: [u8; 16] = [
31 changes: 18 additions & 13 deletions src/num.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,9 @@ use std::{
ops::{AddAssign, DivAssign, MulAssign, SubAssign},
};

#[cfg(not(target_arch = "wasm32"))]
use proptest::prelude::*;

use crate::field::LurkField;
use crate::uint::UInt;

@@ -24,6 +27,21 @@ pub enum Num<F: LurkField> {
U64(u64),
}

#[cfg(not(target_arch = "wasm32"))]
impl<Fr: LurkField> Arbitrary for Num<Fr> {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
any::<FWrap<Fr>>()
.prop_map(|f| match f.0.to_u64() {
Some(x) => Self::U64(x),
None => Self::Scalar(f.0),
})
.boxed()
}
}

impl<F: LurkField> Copy for Num<F> {}

impl<F: LurkField> Display for Num<F> {
@@ -281,19 +299,6 @@ mod tests {
use blstrs::Scalar as Fr;
use ff::Field;

impl<Fr: LurkField> Arbitrary for Num<Fr> {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
prop_oneof!(
any::<u64>().prop_map(Num::U64),
any::<FWrap<Fr>>().prop_map(|f| Num::Scalar(f.0)),
)
.boxed()
}
}

//proptest! {
// #[test]
// fn prop_num_ipld(x in any::<Num<Fr>>()) {
28 changes: 14 additions & 14 deletions src/package.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashSet;

use crate::sym::Sym;
use crate::symbol::Symbol;

#[derive(Clone, Debug)]
pub struct Package {
pub name: Sym,
pub external_symbols: HashSet<Sym>,
pub name: Symbol,
pub external_symbols: HashSet<Symbol>,
}

pub const LURK_EXTERNAL_SYMBOL_NAMES: &[&str] = &[
@@ -48,18 +48,18 @@ pub const LURK_EXTERNAL_SYMBOL_NAMES: &[&str] = &[
];

impl Package {
pub fn new(name: Sym) -> Self {
pub fn new(name: Symbol) -> Self {
Self {
name,
external_symbols: Default::default(),
}
}

pub fn root() -> Self {
Self::new(Sym::root())
Self::new(Symbol::root())
}

pub fn name(&self) -> &Sym {
pub fn name(&self) -> &Symbol {
&self.name
}

@@ -79,44 +79,44 @@ impl Package {
}
}

pub fn remove_external_symbol(&mut self, sym: &Sym) {
pub fn remove_external_symbol(&mut self, sym: &Symbol) {
self.external_symbols.remove(sym);
}

pub fn remove_external_symbols(&mut self, syms: &[Sym]) {
pub fn remove_external_symbols(&mut self, syms: &[Symbol]) {
for sym in syms.iter() {
self.external_symbols.remove(sym);
}
}

pub fn local_symbol(&self, sym: &Sym) -> Option<&Sym> {
pub fn local_symbol(&self, sym: &Symbol) -> Option<&Symbol> {
self.external_symbols.get(sym)
}

pub fn relative_abbreviation(&self, sym: &Sym) -> Sym {
pub fn relative_abbreviation(&self, sym: &Symbol) -> Symbol {
let name = &self.name;
let name_path = name.path();
let sym_path = sym.path();

if sym.is_keyword() != name.is_keyword() {
if sym.is_key() != name.is_key() {
return sym.clone();
}

if sym_path.len() < name_path.len() {
return sym.clone();
}

let name_is_prefix = sym_path.iter().zip(name.path()).all(|(a, b)| a == b);
let name_is_prefix = sym_path.iter().zip(name.path()).all(|(a, b)| *a == b);

if name_is_prefix && sym_path != name_path {
Sym::new_from_path(false, sym_path[name_path.len()..].to_vec())
Symbol::Sym(sym_path[name_path.len()..].to_vec())
} else {
sym.clone()
}
}

pub fn lurk() -> Self {
let lurk_sym = Sym::new_from_path(false, vec!["".into(), "LURK".into()]);
let lurk_sym = Symbol::new(vec!["LURK"]);
let mut package = Package::new(lurk_sym);

package.add_external_symbols(LURK_EXTERNAL_SYMBOL_NAMES);
Loading