Skip to content

Implement ilist any and all methods #429

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

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions src/kirin/dialects/ilist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .runtime import IList as IList
from ._dialect import dialect as dialect
from ._wrapper import ( # careful this is not the builtin range
all as all,
any as any,
map as map,
scan as scan,
foldl as foldl,
Expand Down
8 changes: 8 additions & 0 deletions src/kirin/dialects/ilist/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ def for_each(
fn: typing.Callable[[ElemT], typing.Any],
collection: IList[ElemT, LenT] | list[ElemT],
) -> None: ...


@lowering.wraps(stmts.Any)
def any(collection: IList[bool, LenT] | list[bool]) -> bool: ...


@lowering.wraps(stmts.All)
def all(collection: IList[bool, LenT] | list[bool]) -> bool: ...
12 changes: 11 additions & 1 deletion src/kirin/dialects/ilist/interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from kirin.dialects.py.len import Len
from kirin.dialects.py.binop import Add

from .stmts import Map, New, Push, Scan, Foldl, Foldr, Range, ForEach
from .stmts import All, Any, Map, New, Push, Scan, Foldl, Foldr, Range, ForEach
from .runtime import IList
from ._dialect import dialect

Expand Down Expand Up @@ -96,3 +96,13 @@ def for_each(self, interp: Interpreter, frame: Frame, stmt: ForEach):
# NOTE: assume fn has been type checked
interp.call(fn.code, fn, elem)
return

@impl(Any)
def any(self, interp: Interpreter, frame: Frame, stmt: Any):
coll: IList = frame.get(stmt.collection)
return (any(coll),)

@impl(All)
def all(self, interp: Interpreter, frame: Frame, stmt: All):
coll: IList = frame.get(stmt.collection)
return (all(coll),)
14 changes: 14 additions & 0 deletions src/kirin/dialects/ilist/stmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ class ForEach(ir.Statement):
purity: bool = info.attribute(default=False)
fn: ir.SSAValue = info.argument(types.Generic(ir.Method, [ElemT], types.NoneType))
collection: ir.SSAValue = info.argument(IListType[ElemT])


@statement(dialect=dialect)
class Any(ir.Statement):
traits = frozenset({ir.Pure(), lowering.FromPythonCall()})
collection: ir.SSAValue = info.argument(IListType[types.Bool, ListLen])
result: ir.ResultValue = info.result(types.Bool)


@statement(dialect=dialect)
class All(ir.Statement):
traits = frozenset({ir.Pure(), lowering.FromPythonCall()})
collection: ir.SSAValue = info.argument(IListType[types.Bool, ListLen])
result: ir.ResultValue = info.result(types.Bool)
35 changes: 35 additions & 0 deletions test/dialects/test_ilist_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,38 @@ def scan_wrap():
10 + 1 + 1 + 1 + 3,
10 + 1 + 1 + 1 + 1 + 4,
]


def test_any_all_wrapper():

@basic
def test_any_all():
ls = [True, False, False]
return ls, ilist.any(ls), ilist.all(ls)

test_any_all.print()

ls, any_val, all_val = test_any_all()

assert isinstance(ls, ilist.IList)
assert ls.data == [True, False, False]
assert any_val
assert not all_val

@basic
def test_any_all2():
ls = [False, False]
return ilist.any(ls), ilist.all(ls)

any_val, all_val = test_any_all2()
assert not any_val
assert not all_val

@basic
def test_any_all3():
ls = [True, True, True, True, True]
return ilist.any(ls), ilist.all(ls)

any_val, all_val = test_any_all3()
assert any_val
assert all_val
Loading