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

Nested recursive fn compilation error #46

Closed
diyessi opened this issue May 7, 2023 · 2 comments
Closed

Nested recursive fn compilation error #46

diyessi opened this issue May 7, 2023 · 2 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@diyessi
Copy link

diyessi commented May 7, 2023

Bug Description

I defined:

fn fact(n : Int) -> Int:
    fn factint(p : Int, m : Int) -> Int:
        if m == 0:
            return p
        return factint(p*m, m-1)
    return factint(1, n)

and got the compilation error:

error: Expression [6]:5:1: cyclic reference between expressions defining and using parameters
fn fact(n : Int) -> Int:
^

Expression [6]:6:5: parameter "factint($Int::Int,$Int::Int)" is defined here, which references itself
    fn factint(p : Int, m : Int) -> Int:
    ^

A simpler recursive fact without the tail-recursive helper does compile, and a top-level factint compiles, so the problem seems to be related to factint being internal to fact.

Context

11:blkio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
10:devices:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
9:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
8:freezer:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
7:cpu,cpuacct:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
6:memory:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
5:hugetlb:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
4:pids:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
3:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
2:net_cls,net_prio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podfeaac2d5_a246_41df_9300_b56906cac38e.slice/cri-containerd-c1fa9d6cdf96a39b653a771aca2c6190bba02cf664ccb2525e5543afa25f80c2.scope
0::/
@diyessi diyessi added bug Something isn't working mojo-external labels May 7, 2023
@hoxha-saber
Copy link

I believe it is related to this defined in the Mojo docs:

Nested functions (any function that is not a top-level function) cannot be recursive in any way. Nested functions are considered “parameters”, and although parameter values do not have to obey lexical order, their uses and definitions cannot form a cycle. Current limitations in Mojo mean that nested functions, which are considered parameter values, cannot be cyclic.

@diyessi
Copy link
Author

diyessi commented May 8, 2023

That would do it.

@diyessi diyessi closed this as completed May 8, 2023
@ematejska ematejska added the mojo-repo Tag all issues with this label label May 7, 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-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants