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

feat: add op for creating new/empty list #1729

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions hugr-core/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ impl ExtensionSet {
},
}))
}

pub(crate) fn contains_vars(&self) -> bool {
self.0.iter().any(|id| as_typevar(id).is_some())
}
}

impl From<ExtensionId> for ExtensionSet {
Expand Down
6 changes: 6 additions & 0 deletions hugr-core/src/std_extensions/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ impl ListValue {
}

/// Create a new [CustomConst] for an empty list of values of type `typ`.
/// Note this will be valid only if `typ` is concrete (does not contain type variables).
pub fn new_empty(typ: Type) -> Self {
debug_assert!(!typ.contains_vars());
Self(vec![], typ)
}

Expand Down Expand Up @@ -123,6 +125,8 @@ impl CustomConst for ListValue {
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum ListOp {
/// Create a new empty list
new,
/// Pop from the end of list. Return an optional value.
pop,
/// Push to end of list. Return the new list.
Expand Down Expand Up @@ -160,6 +164,7 @@ impl ListOp {
let e = Type::new_var_use(0, TypeBound::Any);
let l = self.list_type(list_type_def, 0);
match self {
new => self.list_polytype(Vec::<Type>::new(), l).into(),
pop => self
.list_polytype(vec![l.clone()], vec![l, Type::from(option_type(e))])
.into(),
Expand Down Expand Up @@ -237,6 +242,7 @@ impl MakeOpDef for ListOp {
use ListOp::*;

match self {
new => "Create a new empty list",
pop => "Pop from the back of list. Returns an optional value.",
push => "Push to the back of list",
get => "Lookup an element in a list by index. Panics if the index is out of bounds.",
Expand Down
19 changes: 19 additions & 0 deletions hugr-core/src/std_extensions/collections/list_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{ListOp, ListValue};

pub(super) fn set_fold(op: &ListOp, def: &mut OpDef) {
match op {
ListOp::new => def.set_constant_folder(NewFold),
ListOp::pop => def.set_constant_folder(PopFold),
ListOp::push => def.set_constant_folder(PushFold),
ListOp::get => def.set_constant_folder(GetFold),
Expand All @@ -23,6 +24,24 @@ pub(super) fn set_fold(op: &ListOp, def: &mut OpDef) {
}
}

pub struct NewFold;

impl ConstFold for NewFold {
fn fold(
&self,
type_args: &[TypeArg],
_consts: &[(crate::IncomingPort, crate::ops::Value)],
) -> ConstFoldResult {
let [TypeArg::Type { ty }] = type_args else {
panic!("Should have just element type")
};
if ty.contains_vars() {
None
} else {
Some(vec![(0.into(), ListValue::new_empty(ty.clone()).into())])
}
}
}
pub struct PopFold;

impl ConstFold for PopFold {
Expand Down
22 changes: 22 additions & 0 deletions hugr-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,28 @@ impl<RV: MaybeRV> TypeBase<RV> {
}
}
}

pub(crate) fn contains_vars(&self) -> bool {
match self.as_type_enum() {
TypeEnum::Extension(custom_type) => {
custom_type.args().iter().any(TypeArg::contains_vars)
}
TypeEnum::Alias(_) => false, // Any unresolved/undefined AliasDecl must be at top level in Module so no vars in scope
TypeEnum::Function(ft) => {
ft.input.iter().any(TypeRV::contains_vars)
|| ft.output.iter().any(TypeRV::contains_vars)
|| ft.extension_reqs.contains_vars()
}
TypeEnum::Variable(_, _) | TypeEnum::RowVar(_) => true,
TypeEnum::Sum(sum_type) => (0..sum_type.num_variants()).any(|i| {
sum_type
.get_variant(i)
.unwrap()
.iter()
.any(TypeRV::contains_vars)
}),
}
}
}

impl Type {
Expand Down
10 changes: 10 additions & 0 deletions hugr-core/src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ impl TypeArg {
} => t.apply_var(*idx, cached_decl),
}
}

pub(crate) fn contains_vars(&self) -> bool {
match self {
TypeArg::Type { ty } => ty.contains_vars(),
TypeArg::BoundedNat { .. } | TypeArg::String { .. } => false,
TypeArg::Sequence { elems } => elems.iter().any(TypeArg::contains_vars),
TypeArg::Extensions { es } => es.contains_vars(),
TypeArg::Variable { .. } => true,
}
}
}

impl TypeArgVariable {
Expand Down
Loading