Skip to content

ilist dialect should use list as a sugar #277

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

Open
Roger-luo opened this issue Feb 19, 2025 · 7 comments
Open

ilist dialect should use list as a sugar #277

Roger-luo opened this issue Feb 19, 2025 · 7 comments
Labels
breaking breaking changes or proposed changes that would break existing APIs dialect:ilist triage request for triage. A decision will be made on the issue or PR.

Comments

@Roger-luo
Copy link
Member

currently we use IList | list in the statement wrappers to make linter happy, however, when working with larger codebase, one may declare a function as following

def foo(xs: IList): ...

however, when calling this method using a syntax sugar [..]

def main():
    foo([1, 2, 3])

the linter will not be happy about it. I think there are two options here:

  1. we just also accept list in type annotation but treat it as IList[T, Any]
  2. ban all list syntax sugar

cc: @kaihsin @weinbe58

@Roger-luo Roger-luo added breaking breaking changes or proposed changes that would break existing APIs dialect:ilist triage request for triage. A decision will be made on the issue or PR. labels Feb 19, 2025
@kaihsin
Copy link
Contributor

kaihsin commented Feb 20, 2025

We could do something like this:

  1. rename IList class to IListRuntime, since user does not directly using this runtime class anyway.
  2. make IList as TypeAlias.
from typing import TypeVar
from typing import Any
from kirin.dialects.ilist import IListRuntime  
ElemT = TypeVar("ElemT")
N = TypeVar("N")

IList = IListRuntime[ElemT,N] | list[ElemT]

def myfoo(vars: IList[float, Any]):
    return vars[0]


myfoo([1.,2.,3.]) # linter will be happy

@Roger-luo
Copy link
Member Author

I thought @kaihsin was against having list in type annotation :-)

@kaihsin
Copy link
Contributor

kaihsin commented Feb 20, 2025

This approach user will annotate as the same as IList in their the code, and linter will be happy too

@Roger-luo
Copy link
Member Author

how do you construct an IList outside of the kernel tho? Or we are accepting list and convert it into IList implicitly?

@kaihsin
Copy link
Contributor

kaihsin commented Feb 20, 2025 via email

@Roger-luo
Copy link
Member Author

I think we should implicitly convert to IList implicitly, depending on the methods' dialect group. If a given method has IList or list

how do you construct an IList then? always via IListRuntime or just treat list as IList if the dialect group contains ilist dialect?

Another question is if we are actually using IList under the hood, what do we return as a runtime value? IListRuntime?

This is now also in conflict with the explicitly annotated element type in an IList - when one converts the syntax [...] into IList there is no such knowledge of element type and thus we have mark it as Any.


I feel supporting this as a syntax sugar is problematic in Python, we should just use the plain constructors instead of trying to support another syntax sugar - it's just not possible in Python.

@Roger-luo
Copy link
Member Author

Roger-luo commented Apr 21, 2025

I think another way of doing it is just treat all list as IList implicitly (e.g during type inference). As long as we don't provide methods to mutate the object the semantics here is still consistent with a standard Python list. So basically always annoate the wrapper with list instead of IList - we can always do a conversion to IList at the entry point anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking breaking changes or proposed changes that would break existing APIs dialect:ilist triage request for triage. A decision will be made on the issue or PR.
Projects
None yet
Development

No branches or pull requests

2 participants