Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions examples/adder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
//!
use open_hypergraphs::lax::var;
use open_hypergraphs::lax::*;
use std::sync::{Arc, Mutex};

type Term = OpenHypergraph<Bit, Gate>;
type Builder = Arc<Mutex<Term>>;

// There is a single generating object in the category: the bit.
#[derive(PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -53,14 +57,10 @@
impl var::HasNot<Bit, Gate> for Gate {
fn not(_: Bit) -> (Bit, Gate) {
(Bit, Gate::Not)
}

Check warning on line 60 in examples/adder.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/open-hypergraphs/open-hypergraphs/examples/adder.rs
}

use std::cell::RefCell;
use std::rc::Rc;

type Term = OpenHypergraph<Bit, Gate>;
type Builder = Rc<RefCell<Term>>;
type Var = var::Var<Bit, Gate>;

fn zero(state: Builder) -> Var {
Expand All @@ -78,14 +78,14 @@
(sum, cout)
}

fn ripple_carry_adder(state: Builder, a: &[Var], b: &[Var]) -> (Vec<Var>, Var) {
fn ripple_carry_adder(state: Arc<Mutex<Term>>, a: &[Var], b: &[Var]) -> (Vec<Var>, Var) {
let n = a.len();
assert_eq!(n, b.len(), "Input bit arrays must have the same length");

let mut sum = Vec::with_capacity(n);

// Start with carry_in = 0
let mut carry = zero(state);
let mut carry = zero(state.clone());

// Process each bit position
for i in 0..n {
Expand Down
9 changes: 4 additions & 5 deletions src/lax/var/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use crate::lax::open_hypergraph::OpenHypergraph;

use super::var::*;

use std::cell::RefCell;
use std::ops::*;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

/// A general helper for constructing `m → n` maps
pub fn operation<O: Clone, A: HasVar>(
// TODO: generalise to something which borrows a mutable OpenHypergraph?
builder: &Rc<RefCell<OpenHypergraph<O, A>>>,
builder: &Arc<Mutex<OpenHypergraph<O, A>>>,
vars: &[Var<O, A>],
result_types: Vec<O>, // types of output vars
op: A,
Expand All @@ -26,7 +25,7 @@ pub fn operation<O: Clone, A: HasVar>(
.collect();
let result_nodes = result_vars.iter().map(|v| v.new_source()).collect();

let mut term = builder.borrow_mut();
let mut term = builder.lock().unwrap();
let _ = term.new_edge(
op,
Hyperedge {
Expand All @@ -41,7 +40,7 @@ pub fn operation<O: Clone, A: HasVar>(
/// An `n → 1` operation, returning its sole target `Var`.
pub fn fn_operation<O: Clone, A: HasVar>(
// TODO: generalise to something which borrows a mutable OpenHypergraph?
builder: &Rc<RefCell<OpenHypergraph<O, A>>>,
builder: &Arc<Mutex<OpenHypergraph<O, A>>>,
vars: &[Var<O, A>],
result_type: O,
op: A,
Expand Down
27 changes: 14 additions & 13 deletions src/lax/var/var.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::lax::hypergraph::*;
use crate::lax::open_hypergraph::*;

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

// Given a set of operation labels (i.e., edge labels),
// distinguish one as the type of "Vars".
Expand All @@ -15,15 +14,15 @@

#[derive(Clone, Debug)]
pub struct Var<O, A> {
pub state: Rc<RefCell<OpenHypergraph<O, A>>>,
pub state: Arc<Mutex<OpenHypergraph<O, A>>>,
pub edge_id: EdgeId,
pub label: O,
}

/// Vars can be created when the set of edge labels has a 'Copy' operation.

Check warning on line 22 in src/lax/var/var.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/open-hypergraphs/open-hypergraphs/src/lax/var/var.rs
impl<O: Clone, A: HasVar> Var<O, A> {
pub fn new(state: Rc<RefCell<OpenHypergraph<O, A>>>, default_node_label: O) -> Self {
let (edge_id, _) = state.borrow_mut().new_operation(A::var(), vec![], vec![]);
pub fn new(state: Arc<Mutex<OpenHypergraph<O, A>>>, default_node_label: O) -> Self {
let (edge_id, _) = state.lock().unwrap().new_operation(A::var(), vec![], vec![]);
Var {
state,
edge_id,
Expand All @@ -33,30 +32,32 @@

pub fn new_source(&self) -> NodeId {
self.state
.borrow_mut()
.lock()
.unwrap()
.add_edge_source(self.edge_id, self.label.clone())
}

pub fn new_target(&self) -> NodeId {
self.state
.borrow_mut()
.lock()
.unwrap()
.add_edge_target(self.edge_id, self.label.clone())
}
}

pub type BuildResult<O, A> = Result<OpenHypergraph<O, A>, Rc<RefCell<OpenHypergraph<O, A>>>>;
pub type BuildResult<O, A> = Result<OpenHypergraph<O, A>, Arc<Mutex<OpenHypergraph<O, A>>>>;

/// Construct an [`OpenHypergraph`] from a function taking an empty OpenHypergraph,
/// and returning two lists of [`Var`]s corresponding to *sources* and *targets*.
pub fn build<F, O: Clone, A: HasVar>(f: F) -> BuildResult<O, A>
where
F: Fn(&Rc<RefCell<OpenHypergraph<O, A>>>) -> (Vec<Var<O, A>>, Vec<Var<O, A>>),
F: Fn(&Arc<Mutex<OpenHypergraph<O, A>>>) -> (Vec<Var<O, A>>, Vec<Var<O, A>>),
{
let state = Rc::new(RefCell::new(OpenHypergraph::<O, A>::empty()));
let state = Arc::new(Mutex::new(OpenHypergraph::<O, A>::empty()));
{
let (s, t) = f(&state);
state.borrow_mut().sources = s.iter().map(|x| x.new_source()).collect();
state.borrow_mut().targets = t.iter().map(|x| x.new_target()).collect();
state.lock().unwrap().sources = s.iter().map(|x| x.new_source()).collect();
state.lock().unwrap().targets = t.iter().map(|x| x.new_target()).collect();
}
Rc::try_unwrap(state).map(|f| f.into_inner())
Arc::try_unwrap(state).map(|f| f.into_inner().unwrap())
}
Loading