ohua-dev / ohua-core

Core Haskell library for the compiler
https://ohua-dev.github.io
Eclipse Public License 1.0
5 stars 0 forks source link

Tail recursion rewrite only works in specific case #31

Open JustusAdam opened 5 years ago

JustusAdam commented 5 years ago

This whole rewriteCond and rewriteBranch algorithm is not correct. That is to say it only works in the specific case where a single if is the last expression in a recursive function. While the reason for this assumption is obvious we must consider that also nested if's technically are valid tail recursive functions so long as there is at least one branch that recurses and one branch that does not. I feel implementing this correctly however is going to require some effort, thus I think we should do so later.

JustusAdam commented 5 years ago

Also currently the branches of that singular if only allow values and a recursive call. Not even let's are allowed.

Feliix42 commented 5 years ago

The following tail recursion algorithm does not work yet, although it sounds from your description like it should work:

ns some_ns;
use sf to::implement::{find_path, update_grid, is_empty};

fn main(maze: Maze, to_map: Vec<(Point, Point)>) -> Maze {
    let paths = for pair in to_map {
        find_path(maze, to_map)
    };

    let (remap_paths, new_maze) = update_grid(maze, paths);

    if (is_empty(remap_paths)) {
        new_maze
    } else {
        main(new_maze, remap_paths)
    }
}

The error says:

ohuac: Unsortable! (Probably due to a cycle)
CallStack (from HasCallStack):
  error, called at src/Universum/Debug.hs:60:11 in universum-1.2.0-6ylCZDYYoS898SMIz2iM8j:Universum.Debug
  error, called at src/hs/Ohua/Standalone.hs:301:22 in ohuac-0.3.0-8NKnqdddZODJ0p3a3pKPB3:Ohua.Standalone

Unfortunately, I am unable to provide ALang/DFLang dumps as the error is raised before these stages are processed.

JustusAdam commented 5 years ago

This was tracked in ohua-dev/ohuac#17 and has been fixed