Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
porcuquine committed Aug 8, 2023
1 parent 4d77e05 commit 5907ab7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/foil/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<T: PartialEq + Clone, F: LurkField, M: MetaData, R: Relation<F>, MR: MetaMa
let relation = {
self.mapper
.find(vertex.metadata())
.expect(&format!("relation missing for {:?}", vertex.metadata()))
.unwrap_or_else(|| panic!("relation missing for {:?}", vertex.metadata()))
};

dbg!(relation);
Expand Down
93 changes: 49 additions & 44 deletions src/foil/coil.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use log::info;
use std::collections::{HashMap, HashSet};
use std::convert::identity;
use std::marker::PhantomData;

use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
Expand Down Expand Up @@ -78,6 +77,44 @@ pub struct Context<F: LurkField> {
env: Ptr<F>,
}

/// Look up `var` in `env`, where `env` is a list of bindings from `Symbol` to `U64` representing bindings of variables to vertices
/// by id.
fn lookup_vertex_id<F: LurkField>(
store: &Store<F>,
env: &Ptr<F>,
var: &Ptr<F>,
) -> Result<Option<Id>> {
if let Some((binding, rest_env)) = store
.maybe_car_cdr(env)
.map_err(|_| anyhow!("env not a list"))?
{
if let Some((bound_var, id)) = store
.maybe_car_cdr(&binding)
.map_err(|_| anyhow!("binding not a pair"))?
{
if *var == bound_var {
match store.fetch_num(&id) {
Some(crate::num::Num::U64(n)) => {
info!("found {n}");
Ok(((*n) as Id).into())
}
_ => {
bail!("binding Id could not be fetched");
}
}
} else {
lookup_vertex_id(store, &rest_env, var)
}
} else {
info!("unbound");
Ok(None)
}
} else {
info!("unbound");
Ok(None)
}
}

impl<F: LurkField> Context<F> {
fn new(store: &mut Store<F>) -> Self {
Self {
Expand All @@ -91,39 +128,7 @@ impl<F: LurkField> Context<F> {
var.fmt_to_string(store),
self.env.fmt_to_string(store)
);
self.lookup_aux(store, &self.env, var)
}

fn lookup_aux(&self, store: &Store<F>, env: &Ptr<F>, var: &Ptr<F>) -> Result<Option<Id>> {
if let Some((binding, rest_env)) = store
.maybe_car_cdr(env)
.map_err(|_| anyhow!("env not a list"))?
{
if let Some((bound_var, id)) = store
.maybe_car_cdr(&binding)
.map_err(|_| anyhow!("binding not a pair"))?
{
if *var == bound_var {
match store.fetch_num(&id) {
Some(crate::num::Num::U64(n)) => {
info!("found {n}");
Ok(((*n) as Id).into())
}
_ => {
bail!("binding Id could not be fetched");
}
}
} else {
self.lookup_aux(store, &rest_env, var)
}
} else {
info!("unbound");
Ok(None)
}
} else {
info!("unbound");
Ok(None)
}
lookup_vertex_id(store, &self.env, var)
}

fn push_binding(&mut self, store: &mut Store<F>, var: Ptr<F>, id: Id) {
Expand All @@ -137,7 +142,7 @@ impl<F: LurkField> Context<F> {
.car_cdr(&self.env)
.map_err(|_| anyhow!("failed to destructure env"))?;
self.env = cdr;
let (var, id) = store
let (_var, id) = store
.car_cdr(&car)
.map_err(|_| anyhow!("failed to destrcture binding"))?;

Expand Down Expand Up @@ -283,9 +288,9 @@ impl<F: LurkField, R: Relation<F>> CoilDef<F, R, CoilSyntax> {

let let_sym = sym!("lurk", "let");
let bind = sym!("coil", "bind");
let var = sym!("coil", "var");
let _var = sym!("coil", "var");

def.register_equivalence(bind.clone());
def.register_equivalence(bind);
def.register_syntax(let_sym, CoilSyntax::Let(Let {}));

def
Expand Down Expand Up @@ -328,8 +333,8 @@ impl<F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {

let mut constructors: Vec<Func<CoilMeta>> = self
.constructors
.iter()
.map(|(sym, syms)| self.symbol_func(sym.clone()))
.keys()
.map(|sym| self.symbol_func(sym.clone()))
.collect();
constructors.sort();

Expand All @@ -354,7 +359,7 @@ impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> MetaMapper<CoilMeta, S>
}
}

impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {
impl<F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {
fn add_to_foil(
&self,
foil: &mut Foil<F, CoilMeta>,
Expand Down Expand Up @@ -421,7 +426,7 @@ impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {

Ok(Some(foil.add_with_meta(
func,
successor_verts.into_iter().flat_map(identity).collect(),
successor_verts.into_iter().flatten().collect(),
meta,
)))
}
Expand Down Expand Up @@ -475,7 +480,7 @@ impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {
context: &mut Context<F>,
args: &[Ptr<F>],
) -> Result<Option<Vert>> {
if args.len() != 0 {
if !args.is_empty() {
bail!("pop-binding needs exactly zero args")
};

Expand All @@ -487,12 +492,12 @@ impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> CoilDef<F, R, S> {
}
}

impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> From<CoilDef<F, R, S>> for Schema<CoilMeta> {
impl<F: LurkField, R: Relation<F>, S: Syntax<F>> From<CoilDef<F, R, S>> for Schema<CoilMeta> {
fn from(coil_def: CoilDef<F, R, S>) -> Self {
coil_def.schema()
}
}
impl<'a, F: LurkField, R: Relation<F>, S: Syntax<F>> From<&CoilDef<F, R, S>> for Schema<CoilMeta> {
impl<F: LurkField, R: Relation<F>, S: Syntax<F>> From<&CoilDef<F, R, S>> for Schema<CoilMeta> {
fn from(coil_def: &CoilDef<F, R, S>) -> Self {
coil_def.schema()
}
Expand Down
28 changes: 15 additions & 13 deletions src/foil/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

//! FOIL
//! Flat Optimization Intermediate Language
use log::{info, warn};
Expand Down Expand Up @@ -39,46 +41,46 @@ pub struct Schema<T: MetaData> {
}

impl<T: MetaData> Schema<T> {
fn equivalences(&self) -> Vec<Func<T>> {
pub fn equivalences(&self) -> Vec<Func<T>> {
self.equivalences.clone()
}
fn constructors(&self) -> Vec<Func<T>> {
pub fn constructors(&self) -> Vec<Func<T>> {
self.constructors.clone()
}
fn add_constructor(&mut self, constructor: Func<T>, _metadata: T) {
pub fn add_constructor(&mut self, constructor: Func<T>, _metadata: T) {
self.constructors.push(constructor)
}
}

impl<T: MetaData> Func<T> {
fn new<S: Into<String>>(name: S) -> Self {
pub fn new<S: Into<String>>(name: S) -> Self {
Self {
name: name.into(),
projectors: None,
metadata: T::default(),
}
}
fn new_with_metadata<S: Into<String>>(name: S, metadata: T) -> Self {
pub fn new_with_metadata<S: Into<String>>(name: S, metadata: T) -> Self {
Self {
name: name.into(),
projectors: None,
metadata,
}
}
fn constructor<S: Into<String>>(name: S, projectors: Vec<Self>, metadata: T) -> Self {
pub fn constructor<S: Into<String>>(name: S, projectors: Vec<Self>, metadata: T) -> Self {
Self {
name: name.into(),
projectors: Some(projectors),
metadata,
}
}
fn name(&self) -> &String {
pub fn name(&self) -> &String {
&self.name
}
fn metadata(&self) -> &T {
pub fn metadata(&self) -> &T {
&self.metadata
}
fn projectors(&self) -> Option<&Vec<Self>> {
pub fn projectors(&self) -> Option<&Vec<Self>> {
self.projectors.as_ref()
}
}
Expand Down Expand Up @@ -687,16 +689,16 @@ enum State {
}

impl State {
fn is_initial(&self) -> bool {
pub(crate) fn is_initial(&self) -> bool {
matches!(self, Self::Initial)
}
fn is_finalized(&self) -> bool {
pub(crate) fn is_finalized(&self) -> bool {
matches!(self, Self::Finalized)
}
fn is_closed(&self) -> bool {
pub(crate) fn is_closed(&self) -> bool {
matches!(self, Self::Closed)
}
fn is_minimized(&self) -> bool {
pub(crate) fn is_minimized(&self) -> bool {
matches!(self, Self::Minimized)
}
}
Expand Down

0 comments on commit 5907ab7

Please sign in to comment.