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

Parameterize TypeRow by element type #256

Merged
merged 55 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
ee60c72
In ConstValue::predicate, assert that the unit value is expected by t…
acl-cqc Jul 7, 2023
900ffce
Remove one TypeRow::from, inlining into the other
acl-cqc Jul 7, 2023
5455cd9
Linear containers can have classic values too
acl-cqc Jul 7, 2023
de40c36
Flatten LinearType into SimpleType
acl-cqc Jul 7, 2023
4d585f8
Inline single use of impl_from_into_simple_type macro
acl-cqc Jul 7, 2023
079dd6f
Merge remote-tracking branch 'origin/main' into refactor/linear_classic
acl-cqc Jul 10, 2023
6ccc5fa
WIP
acl-cqc Jul 7, 2023
68772ba
Masses of ToOwned/'static weirdness
acl-cqc Jul 7, 2023
757426f
Require Debug everywhere
acl-cqc Jul 7, 2023
ccfdb13
More ToOwned - everything <Owned=Vec<T>>
acl-cqc Jul 7, 2023
76616a0
TypeRow<ClassicType> has predicate_variant_rows as predicates are alw…
acl-cqc Jul 7, 2023
edbbdf8
try_convert_elems for new_sum/new_tuple
acl-cqc Jul 7, 2023
b5b3975
Driveby use SimpleType::is_linear rather than lambda
acl-cqc Jul 7, 2023
c48e4e7
TEMP turn off types/simple/serialize.rs
acl-cqc Jul 10, 2023
5b080a2
Constant Sums must be all-classic; other fixes
acl-cqc Jul 10, 2023
83b9736
Add TypeRow::into_owned; Remove ConstTypeError::LinearTypeDisallowed;…
acl-cqc Jul 10, 2023
93b030a
Fix tests, except serialization of course!
acl-cqc Jul 10, 2023
4c8141a
T: Clone+'static avoids the crazy [T]: ToOwned<Owned=Vec<T>>
acl-cqc Jul 10, 2023
a2b4f2c
Use PrimType constraint for all TypeRow's
acl-cqc Jul 10, 2023
8ef8399
Revert "TEMP turn off types/simple/serialize.rs"
acl-cqc Jul 10, 2023
d6a7452
Fix serialization by making SerSimpleType contain only SerSimpleTypes
acl-cqc Jul 10, 2023
6c01233
Drop a now-unnecessary into
acl-cqc Jul 10, 2023
9e43ddb
Remove now-unnecessary hoop-jumping in Signature::get for 'other' ports
acl-cqc Jul 10, 2023
dd6c705
Merge remote-tracking branch 'origin/main' into refactor/linear_classic
acl-cqc Jul 12, 2023
2e4a0bb
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Jul 13, 2023
11ca1e1
Drop unused purely_linear methods.
acl-cqc Jul 12, 2023
638cd34
PrimType::LINEAR -> CLASSIC
acl-cqc Jul 12, 2023
267da4e
Remove LeafOp::linear_count and Signature::linear
acl-cqc Jul 14, 2023
ff85434
Remove SimpleType::is_linear, keeping only is_classical
acl-cqc Jul 14, 2023
ea7da61
Alias{ID,Defn,Decl}: change linear -> classic
acl-cqc Jul 14, 2023
972c4ba
EdgeKind.is_linear: clarify comment
acl-cqc Jul 12, 2023
2ee2369
Some spec updates (esp type constraints TODO) - dropping const-able
acl-cqc Jul 14, 2023
f23d301
Remove LinearType from spec ???
acl-cqc Jul 14, 2023
21a8fd9
Comment out pyo3 of TypeRow w/TODO; fix doc link
acl-cqc Jul 14, 2023
48e4e39
Use unicode superset
acl-cqc Jul 17, 2023
230adf4
typo: anynum -> any
acl-cqc Jul 17, 2023
a469f3b
Merge remote-tracking branch 'origin/main' into refactor/linear_classic
acl-cqc Jul 17, 2023
e852511
Merge remote-tracking branch 'origin/main' into refactor/linear_classic
acl-cqc Jul 17, 2023
40cfd79
Merge branch 'refactor/linear_classic' into refactor/parameterize_typ…
acl-cqc Jul 17, 2023
5e90a5c
Use classic_row!
acl-cqc Jul 17, 2023
3a6ec73
SerializableType trait w/CLASSIC, unimplemented for SerSimpleType
acl-cqc Jul 17, 2023
3061a5f
A couple more classic_row!s
acl-cqc Jul 17, 2023
fdf88ba
Drop some unnecessary type annotations (add comments)
acl-cqc Jul 17, 2023
9a11e07
SimpleRow + ClassicRow
acl-cqc Jul 17, 2023
8696dad
fix doc links in macros.rs
acl-cqc Jul 17, 2023
e04294c
Properly doc classic_row!
acl-cqc Jul 17, 2023
d8023f9
And more macro doc fixes
acl-cqc Jul 17, 2023
ae63688
Merge remote-tracking branch 'origin/main' into refactor/parameterize…
acl-cqc Jul 23, 2023
c71f6fe
fmt
acl-cqc Jul 23, 2023
fecce95
fix doc
acl-cqc Jul 23, 2023
c32bd45
Test new_tuple and new_sum
acl-cqc Jul 24, 2023
b6f023c
Combine impl blocks
acl-cqc Jul 24, 2023
ea976ba
types: export PrimType, TypeRow
acl-cqc Jul 24, 2023
f4dcd89
tidy some imports
acl-cqc Jul 24, 2023
f74a968
Merge remote-tracking branch 'origin/main' into refactor/parameterize…
acl-cqc Jul 25, 2023
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
24 changes: 12 additions & 12 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
types::EdgeKind,
};

use crate::types::{Signature, SimpleType, TypeRow};
use crate::types::{ClassicRow, ClassicType, Signature, SimpleRow, SimpleType};

use itertools::Itertools;

Expand Down Expand Up @@ -277,11 +277,11 @@ pub trait Dataflow: Container {
fn cfg_builder(
&mut self,
inputs: impl IntoIterator<Item = (SimpleType, Wire)>,
output_types: TypeRow,
output_types: SimpleRow,
) -> Result<CFGBuilder<&mut Hugr>, BuildError> {
let (input_types, input_wires): (Vec<SimpleType>, Vec<Wire>) = inputs.into_iter().unzip();

let inputs: TypeRow = input_types.into();
let inputs: SimpleRow = input_types.into();

let (cfg_node, _) = add_op_with_wires(
self,
Expand Down Expand Up @@ -334,11 +334,11 @@ pub trait Dataflow: Container {
/// the [`ops::TailLoop`] node.
fn tail_loop_builder(
&mut self,
just_inputs: impl IntoIterator<Item = (SimpleType, Wire)>,
just_inputs: impl IntoIterator<Item = (ClassicType, Wire)>,
inputs_outputs: impl IntoIterator<Item = (SimpleType, Wire)>,
just_out_types: TypeRow,
just_out_types: ClassicRow,
) -> Result<TailLoopBuilder<&mut Hugr>, BuildError> {
let (input_types, mut input_wires): (Vec<SimpleType>, Vec<Wire>) =
let (input_types, mut input_wires): (Vec<ClassicType>, Vec<Wire>) =
just_inputs.into_iter().unzip();
let (rest_types, rest_input_wires): (Vec<SimpleType>, Vec<Wire>) =
inputs_outputs.into_iter().unzip();
Expand Down Expand Up @@ -368,16 +368,16 @@ pub trait Dataflow: Container {
/// the Conditional node.
fn conditional_builder(
&mut self,
(predicate_inputs, predicate_wire): (impl IntoIterator<Item = TypeRow>, Wire),
(predicate_inputs, predicate_wire): (impl IntoIterator<Item = ClassicRow>, Wire),
other_inputs: impl IntoIterator<Item = (SimpleType, Wire)>,
output_types: TypeRow,
output_types: SimpleRow,
) -> Result<ConditionalBuilder<&mut Hugr>, BuildError> {
let mut input_wires = vec![predicate_wire];
let (input_types, rest_input_wires): (Vec<SimpleType>, Vec<Wire>) =
other_inputs.into_iter().unzip();

input_wires.extend(rest_input_wires);
let inputs: TypeRow = input_types.into();
let inputs: SimpleRow = input_types.into();
let predicate_inputs: Vec<_> = predicate_inputs.into_iter().collect();
let n_cases = predicate_inputs.len();
let n_out_wires = output_types.len();
Expand Down Expand Up @@ -452,7 +452,7 @@ pub trait Dataflow: Container {
fn make_tag(
&mut self,
tag: usize,
variants: impl Into<TypeRow>,
variants: impl Into<SimpleRow>,
value: Wire,
) -> Result<Wire, BuildError> {
let make_op = self.add_dataflow_op(
Expand All @@ -470,11 +470,11 @@ pub trait Dataflow: Container {
fn make_predicate(
&mut self,
tag: usize,
predicate_variants: impl IntoIterator<Item = TypeRow>,
predicate_variants: impl IntoIterator<Item = ClassicRow>,
values: impl IntoIterator<Item = Wire>,
) -> Result<Wire, BuildError> {
let tuple = self.make_tuple(values)?;
let variants = TypeRow::predicate_variants_row(predicate_variants);
let variants = ClassicRow::predicate_variants_row(predicate_variants).map_into();
let make_op = self.add_dataflow_op(LeafOp::Tag { tag, variants }, vec![tuple])?;
Ok(make_op.out_wire(0))
}
Expand Down
67 changes: 36 additions & 31 deletions src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ use super::{
BasicBlockID, BuildError, CfgID, Container, Dataflow, HugrBuilder, Wire,
};

use crate::{hugr::view::HugrView, type_row, types::SimpleType};

use crate::hugr::view::HugrView;
use crate::hugr::HugrMut;
use crate::ops::handle::NodeHandle;
use crate::ops::{self, BasicBlock, OpType};
use crate::types::Signature;

use crate::Node;
use crate::{hugr::HugrMut, types::TypeRow, Hugr};
use crate::types::{ClassicRow, Signature, SimpleRow, SimpleType};
use crate::{type_row, Hugr, Node};

/// Builder for a [`crate::ops::CFG`] child control
/// flow graph
#[derive(Debug, PartialEq)]
pub struct CFGBuilder<T> {
pub(super) base: T,
pub(super) cfg_node: Node,
pub(super) inputs: Option<TypeRow>,
pub(super) inputs: Option<SimpleRow>,
pub(super) exit_node: Node,
pub(super) n_out_wires: usize,
}
Expand Down Expand Up @@ -54,7 +52,10 @@ impl<H: AsMut<Hugr> + AsRef<Hugr>> SubContainer for CFGBuilder<H> {

impl CFGBuilder<Hugr> {
/// New CFG rooted HUGR builder
pub fn new(input: impl Into<TypeRow>, output: impl Into<TypeRow>) -> Result<Self, BuildError> {
pub fn new(
input: impl Into<SimpleRow>,
output: impl Into<SimpleRow>,
) -> Result<Self, BuildError> {
let input = input.into();
let output = output.into();
let cfg_op = ops::CFG {
Expand All @@ -79,8 +80,8 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
pub(super) fn create(
mut base: B,
cfg_node: Node,
input: TypeRow,
output: TypeRow,
input: SimpleRow,
output: SimpleRow,
) -> Result<Self, BuildError> {
let n_out_wires = output.len();
let exit_block_type = OpType::BasicBlock(BasicBlock::Exit {
Expand Down Expand Up @@ -122,18 +123,18 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
/// This function will return an error if there is an error adding the node.
pub fn block_builder(
&mut self,
inputs: TypeRow,
predicate_variants: Vec<TypeRow>,
other_outputs: TypeRow,
inputs: SimpleRow,
predicate_variants: Vec<ClassicRow>,
other_outputs: SimpleRow,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
self.any_block_builder(inputs, predicate_variants, other_outputs, false)
}

fn any_block_builder(
&mut self,
inputs: TypeRow,
predicate_variants: Vec<TypeRow>,
other_outputs: TypeRow,
inputs: SimpleRow,
predicate_variants: Vec<ClassicRow>,
other_outputs: SimpleRow,
entry: bool,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
let op = OpType::BasicBlock(BasicBlock::DFB {
Expand Down Expand Up @@ -166,8 +167,8 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
/// This function will return an error if there is an error adding the node.
pub fn simple_block_builder(
&mut self,
inputs: TypeRow,
outputs: TypeRow,
inputs: SimpleRow,
outputs: SimpleRow,
n_cases: usize,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
self.block_builder(inputs, vec![type_row![]; n_cases], outputs)
Expand All @@ -182,8 +183,8 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
/// This function will return an error if an entry block has already been built.
pub fn entry_builder(
&mut self,
predicate_variants: Vec<TypeRow>,
other_outputs: TypeRow,
predicate_variants: Vec<ClassicRow>,
other_outputs: SimpleRow,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
let inputs = self
.inputs
Expand All @@ -200,7 +201,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
/// This function will return an error if there is an error adding the node.
pub fn simple_entry_builder(
&mut self,
outputs: TypeRow,
outputs: SimpleRow,
n_cases: usize,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
self.entry_builder(vec![type_row![]; n_cases], outputs)
Expand Down Expand Up @@ -244,15 +245,15 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
fn create(
base: B,
block_n: Node,
predicate_variants: Vec<TypeRow>,
other_outputs: TypeRow,
inputs: TypeRow,
predicate_variants: Vec<ClassicRow>,
other_outputs: SimpleRow,
inputs: SimpleRow,
) -> Result<Self, BuildError> {
// The node outputs a predicate before the data outputs of the block node
let predicate_type = SimpleType::new_predicate(predicate_variants);
let mut node_outputs = vec![predicate_type];
node_outputs.extend_from_slice(&other_outputs);
let signature = Signature::new_df(inputs, TypeRow::from(node_outputs));
let signature = Signature::new_df(inputs, SimpleRow::from(node_outputs));
let db = DFGBuilder::create_with_io(base, block_n, signature)?;
Ok(BlockBuilder::from_dfg_builder(db))
}
Expand All @@ -275,9 +276,9 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
impl BlockBuilder<Hugr> {
/// Initialize a [`BasicBlock::DFB`] rooted HUGR builder
pub fn new(
inputs: impl Into<TypeRow>,
predicate_variants: impl IntoIterator<Item = TypeRow>,
other_outputs: impl Into<TypeRow>,
inputs: impl Into<SimpleRow>,
predicate_variants: impl IntoIterator<Item = ClassicRow>,
other_outputs: impl Into<SimpleRow>,
) -> Result<Self, BuildError> {
let inputs = inputs.into();
let predicate_variants: Vec<_> = predicate_variants.into_iter().collect();
Expand All @@ -298,11 +299,12 @@ impl BlockBuilder<Hugr> {
mod test {
use std::collections::HashSet;

use cool_asserts::assert_matches;

use crate::builder::build_traits::HugrBuilder;
use crate::builder::{DataflowSubContainer, ModuleBuilder};
use crate::macros::classic_row;
use crate::types::ClassicType;
use crate::{builder::test::NAT, ops::ConstValue, type_row, types::Signature};
use cool_asserts::assert_matches;

use super::*;
#[test]
Expand Down Expand Up @@ -372,7 +374,10 @@ mod test {
fn build_basic_cfg<T: AsMut<Hugr> + AsRef<Hugr>>(
cfg_builder: &mut CFGBuilder<T>,
) -> Result<(), BuildError> {
let sum2_variants = vec![type_row![NAT], type_row![NAT]];
let sum2_variants = vec![
classic_row![ClassicType::i64()],
classic_row![ClassicType::i64()],
];
let mut entry_b = cfg_builder.entry_builder(sum2_variants.clone(), type_row![])?;
let entry = {
let [inw] = entry_b.input_wires_arr();
Expand Down
13 changes: 8 additions & 5 deletions src/builder/conditional.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hugr::view::HugrView;
use crate::types::{Signature, TypeRow};
use crate::types::{ClassicRow, Signature, SimpleRow};

use crate::ops;
use crate::ops::handle::CaseID;
Expand Down Expand Up @@ -146,9 +146,9 @@ impl HugrBuilder for ConditionalBuilder<Hugr> {
impl ConditionalBuilder<Hugr> {
/// Initialize a Conditional rooted HUGR builder
pub fn new(
predicate_inputs: impl IntoIterator<Item = TypeRow>,
other_inputs: impl Into<TypeRow>,
outputs: impl Into<TypeRow>,
predicate_inputs: impl IntoIterator<Item = ClassicRow>,
other_inputs: impl Into<SimpleRow>,
outputs: impl Into<SimpleRow>,
) -> Result<Self, BuildError> {
let predicate_inputs: Vec<_> = predicate_inputs.into_iter().collect();
let other_inputs = other_inputs.into();
Expand Down Expand Up @@ -176,7 +176,10 @@ impl ConditionalBuilder<Hugr> {

impl CaseBuilder<Hugr> {
/// Initialize a Case rooted HUGR
pub fn new(input: impl Into<TypeRow>, output: impl Into<TypeRow>) -> Result<Self, BuildError> {
pub fn new(
input: impl Into<SimpleRow>,
output: impl Into<SimpleRow>,
) -> Result<Self, BuildError> {
let input = input.into();
let output = output.into();
let signature = Signature::new_df(input, output);
Expand Down
6 changes: 3 additions & 3 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::marker::PhantomData;
use crate::hugr::{HugrView, ValidationError};
use crate::ops;

use crate::types::{Signature, TypeRow};
use crate::types::{Signature, SimpleRow};

use crate::Node;
use crate::{hugr::HugrMut, Hugr};
Expand Down Expand Up @@ -60,8 +60,8 @@ impl DFGBuilder<Hugr> {
///
/// Error in adding DFG child nodes.
pub fn new(
input: impl Into<TypeRow>,
output: impl Into<TypeRow>,
input: impl Into<SimpleRow>,
output: impl Into<SimpleRow>,
) -> Result<DFGBuilder<Hugr>, BuildError> {
let input = input.into();
let output = output.into();
Expand Down
22 changes: 13 additions & 9 deletions src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ops::{self, OpType};

use crate::hugr::view::HugrView;
use crate::types::{Signature, TypeRow};
use crate::types::{ClassicRow, Signature, SimpleRow};
use crate::{Hugr, Node};

use super::build_traits::SubContainer;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> TailLoopBuilder<B> {
}

/// The output types of the child graph, including the predicate as the first.
pub fn internal_output_row(&self) -> Result<TypeRow, BuildError> {
pub fn internal_output_row(&self) -> Result<SimpleRow, BuildError> {
self.loop_signature().map(ops::TailLoop::body_output_row)
}
}
Expand All @@ -72,9 +72,9 @@ impl<H: AsMut<Hugr> + AsRef<Hugr>> TailLoopBuilder<H> {
impl TailLoopBuilder<Hugr> {
/// Initialize new builder for a [`ops::TailLoop`] rooted HUGR
pub fn new(
just_inputs: impl Into<TypeRow>,
inputs_outputs: impl Into<TypeRow>,
just_outputs: impl Into<TypeRow>,
just_inputs: impl Into<ClassicRow>,
inputs_outputs: impl Into<SimpleRow>,
just_outputs: impl Into<ClassicRow>,
) -> Result<Self, BuildError> {
let tail_loop = ops::TailLoop {
just_inputs: just_inputs.into(),
Expand All @@ -96,18 +96,19 @@ mod test {
test::{BIT, NAT},
DataflowSubContainer, HugrBuilder, ModuleBuilder,
},
classic_row,
hugr::ValidationError,
ops::ConstValue,
type_row,
types::Signature,
types::{ClassicType, Signature},
Hugr,
};

use super::*;
#[test]
fn basic_loop() -> Result<(), BuildError> {
let build_result: Result<Hugr, ValidationError> = {
let mut loop_b = TailLoopBuilder::new(vec![], vec![BIT], type_row![NAT])?;
let mut loop_b = TailLoopBuilder::new(vec![], vec![BIT], vec![ClassicType::i64()])?;
let [i1] = loop_b.input_wires_arr();
let const_wire = loop_b.add_load_const(ConstValue::i64(1))?;

Expand All @@ -129,8 +130,11 @@ mod test {
let _fdef = {
let [b1] = fbuild.input_wires_arr();
let loop_id = {
let mut loop_b =
fbuild.tail_loop_builder(vec![(BIT, b1)], vec![], type_row![NAT])?;
let mut loop_b = fbuild.tail_loop_builder(
vec![(ClassicType::bit(), b1)],
vec![],
classic_row![ClassicType::i64()],
)?;
let signature = loop_b.loop_signature()?.clone();
let const_wire = loop_b.add_load_const(ConstValue::true_val())?;
let [b1] = loop_b.input_wires_arr();
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use pyo3::prelude::*;
use crate::ops::constant::CustomConst;
use crate::resource::{OpDef, ResourceSet, TypeDef};
use crate::types::type_param::TypeArg;
use crate::types::{CustomType, SimpleType, TypeRow};
use crate::types::{CustomType, SimpleRow, SimpleType};
use crate::Resource;

pub const fn resource_id() -> SmolStr {
Expand All @@ -34,7 +34,7 @@ pub fn resource() -> Resource {
vec![],
HashMap::default(),
|_arg_values: &[TypeArg]| {
let t: TypeRow = vec![SimpleType::Classic(Type::Angle.custom_type().into())].into();
let t: SimpleRow = vec![SimpleType::Classic(Type::Angle.custom_type().into())].into();
Ok((t.clone(), t, ResourceSet::default()))
},
);
Expand Down
Loading