-
Notifications
You must be signed in to change notification settings - Fork 5
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
Parameterize TypeRow by element type #256
Conversation
… cargo build compiles
This reverts commit c48e4e7.
/// given the rows of each tuple | ||
pub fn predicate_variants_row(variant_rows: impl IntoIterator<Item = TypeRow>) -> Self { | ||
variant_rows | ||
fn try_convert_elems<D: PrimType + TryFrom<T>>(self) -> Result<TypeRow<D>, D::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to just have
impl <F: PrimType, T: PrimType+TryFrom<F>> TryFrom<TypeRow<F>> for TypeRow<T>
but get a conflicting instance declaration...
} | ||
|
||
/// Converts the elements of this TypeRow into some other type that they can `.into()` | ||
pub fn map_into<T2: PrimType + From<T>>(self) -> TypeRow<T2> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, it'd be good for this just to be an Into/From impl, but again that seems to raise conflicting-instance problems
…erow_classic_predicate
…_typerow_classic_predicate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite a bit of renaming noise, but LGTM
src/types.rs
Outdated
pub use simple::{ClassicRow, ClassicType, Container, SimpleRow, SimpleType}; | ||
|
||
use smol_str::SmolStr; | ||
|
||
use crate::hugr::{Direction, Port}; | ||
use crate::utils::display_list; | ||
use crate::{resource::ResourceSet, type_row}; | ||
|
||
use self::simple::{PrimType, TypeRow}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Export PrimType
and TypeRow
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is something I debate. Obviously Rust requires that these types have to be public, but I'd kinda like to encourage users to use the aliases. The case in which a user should be using TypeRow....almost certainly is in a function parameterized <T: PrimType> ...TypeRow<T>....
Which is not the common case, but it is a reasonable case ;-). So ok, exported (within types
but no further, as nothing is exported out of types
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However @aborgna-q one thing we might do is make PrimType sealed (i.e. can only be implemented inside the crate)....?
/// let repeated_row: TypeRow = type_row![B; 2]; | ||
/// assert_eq!(repeated_row, static_row); | ||
/// let repeated_row: SimpleRow = type_row![B; 3]; | ||
/// assert_eq!(repeated_row, sig.output); | ||
/// ``` | ||
#[allow(unused_macros)] | ||
#[macro_export] | ||
macro_rules! type_row { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the renaming... in a separate PR
It's mostly useful when dealing with Signatures of leaf ops. It may be useless complexity, but we don't have any benchmarks at the moment so I'd wait for that before making any refactoring. |
Just to clarify - I did not mean to suggest removal of |
…_typerow_classic_predicate
There is a mass of churn here but in the end few major changes. There are two main benefits:
ClassicType::Container
of a tuple including aQubit
is now statically ruled outSignature::get
for "other" ports no longer needs an error caseNote this does not follow #253; we could do #253 before or after this, and doing this first will involve significantly more churn, but also be more explicit and probably find more bugs...so I'm doing this first ;)
We retain that there are "kind of" two ways to declare the SimpleType tuple-of-two-ints:
SimpleType::Classic
ofClassicType::Container
ofContainer::Tuple
of TypeRow of twoClassicType::i64()
sSimpleType::Qontainer
ofContainer::Tuple
of TypeRow of twoSimpleType::Classic(ClassicType::i64())
s...the idea being that factory methods like
new_predicate
,new_tuple
andnew_sum
will pick the correct one.Drawbacks:
src/types/simple/serialize.rs
) so that, rather than SerSimpleType containing TypeRows containing SimpleTypes, SerSimpleType now contains only (TypeRows of) SerSimpleTypes; flattening is thus recursively and once only rather than iteratively one stage at a time.pyo3
for the time being. We might bring it back via wrapperSimpleRow
andClassicRow
, say.Secondly there is a new macro -
type_row!
buildsTypeRow<SimpleType>
only, there is now alsoclassic_row!
. However, both of these are used (almost) only in tests, so I have to wonder whether we need such control over efficient static allocation for test code. There is a possibility of renamingtype_row!
tosimple_row!
(another mass of churn!) but I'm holding off on that for now because we might rename SimpleType to Type or AnyType in which casetype_row
may be a reasonable name to keep. Renaming can follow in a later PR.