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

[BUG]: rebind doesn't work #859

Closed
cheburakshu opened this issue Sep 20, 2023 · 8 comments
Closed

[BUG]: rebind doesn't work #859

cheburakshu opened this issue Sep 20, 2023 · 8 comments
Assignees
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label

Comments

@cheburakshu
Copy link

Bug description

Hosted Juypter instance:

fn b(num: Int32) -> Float32:
    return rebind[Float32, Int32](num)


print(b(1))
error: Expression [47]:9:33: failed to legalize operation 'kgen.rebind' that was explicitly marked illegal
    let s = rebind[Float32, Int](num)
                                ^

expression failed to parse (no further compiler diagnostics)

Same with Int -> StringLiteral too.

fn c(num: Int32) -> StringLiteral:
    return rebind[StringLiteral, Int32](num)


print(c(1))
error: Expression [53]:6:40: failed to legalize operation 'kgen.rebind' that was explicitly marked illegal
    return rebind[StringLiteral, Int32](num)
                                       ^

expression failed to parse (no further compiler diagnostics)

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.
  • If applicable, add screenshots to help explain the problem.
  • If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.
  • Include anything else that might help us debug the issue.

System information

- What OS did you do install Mojo on ?
- Provide version information for Mojo by pasting the output of `mojo -v`
- Provide Modular CLI version by pasting the output of `modular -v`
@cheburakshu cheburakshu added bug Something isn't working mojo Issues that are related to mojo labels Sep 20, 2023
@rd4com
Copy link
Contributor

rd4com commented Sep 21, 2023

I dont have the answer but i'll participate

https://docs.modular.com/mojo/roadmap.html#the-rebind-builtin

it says: "Mojo cannot always figure out whether some parametric types are equal".." it type-checks the function without instantiation"

so it seems that the rebind function was created for that reason, and not for bitcast conversions

here are some examples:

fn print_int(x:Int):
    print(x)

fn myfunc[T:AnyType](x:T):
    if T == Int: 
        #print_int(x)                #cannot do that because x is still of type T:AnyType
        print_int(rebind[Int](x))   
        
fn main() :
    myfunc[Int](1)
fn myfunc2(x:AnyType):
    print(rebind[Int](x) 1)

fn main() :
    myfunc2(rebind[AnyType](1))
fn print_int(x:Int):
    print(x)

fn myfunc3[T:String](x:AnyType):
    @parameter                       #compile time if statement
    if T == "int_please":
        print_int(rebind[Int](x))
    else:
        print("error..")
    
fn main():
    var x=0
    myfunc3["int_please"](rebind[AnyType](x))
    myfunc3["nothing should happen"](rebind[AnyType](1.4))

parameters cannot be runtime values:
https://docs.modular.com/mojo/programming-manual.html#parameterization-compile-time-metaprogramming

from the page: " We decided to reclaim “parameter” and “parameter expression” to represent a compile-time value in Mojo"

I think that rebind is something for compile time metaprogramming,
maybe your function would work if you return AnyType and accept AnyType, if it is what you try to do,
not to convert a Float to an Int, in the sense of Int(Float(1.1))

@cheburakshu
Copy link
Author

Not sure if parameters play a role here. For example, the below code will run, but when you print, it will cause an error.

fn b(num: Int32) -> Float32:
    return rebind[Float32, Int32](num)


b(1)

@rd4com
Copy link
Contributor

rd4com commented Sep 21, 2023

If i understand the rebind function correctly, it is relabeling the type, not casting

@cheburakshu
Copy link
Author

Don't think I am doing anything wrong in my code, I will wait to hear from the Mojo team.

@rd4com
Copy link
Contributor

rd4com commented Sep 21, 2023

sure, i just felt the need to participate in your issue, you can also come and join the community on discord if you like and have not already 🖖

@ematejska ematejska added the mojo-lang Tag for all issues related to language. label Sep 21, 2023
@cheburakshu
Copy link
Author

cheburakshu commented Sep 21, 2023

sure, i just felt the need to participate in your issue

Sure, thanks for your ideas and insights, appreciate your willingness to help 👍

@Mogball
Copy link
Collaborator

Mogball commented Sep 21, 2023

This isn't the intended use of rebind. The rebind builtin doesn't actually do anything -- it asserts that two parametric types are already equal. In your case, you just want a regular cast to Float32 like Float32(num).

I'll improve the error message and update the doc to be clear about its intended usage. Thanks for filing this report!

@Mogball Mogball self-assigned this Sep 21, 2023
@cheburakshu
Copy link
Author

cheburakshu commented Sep 21, 2023

Ok, sounds good. Probably would be better to rename it too, so as to avoid confusion, as rebind kind of means there is some action happening here. For example in Python, isinstance does a similar thing and is intuitive. Thanks for replying and acting on the report 👍

@Mogball Mogball closed this as completed Sep 25, 2023
@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

4 participants