finos / morphir-elm

Tools to work with the Morphir IR in Elm.
https://package.elm-lang.org/packages/finos/morphir-elm/latest
Apache License 2.0
46 stars 65 forks source link

Compiler improperly detects cycles if mutually recursive functions are defined #1144

Open edwardpeters opened 9 months ago

edwardpeters commented 9 months ago

Describe the bug morphir-elm make will report value cycles when none exist.

To Reproduce The following code compiles in native elm but not in morphir-elm:

countA : Int -> Int
countA x = 
    if x == 0
        then 0
        else x + countB (x-1)

countB : Int -> Int
countB x = 
    if x == 0
        then 0
        else x + countA (x-1)

The following example demonstrates the same issue, but better isolates that it is specifically mutually recursive functions that cause this issue:

type IntCons = 
    Nil
    | HeadTail Int IntCons

sum : IntCons -> Int
sum c = 
    case c of
        Nil -> 0
        HeadTail i tail -> i + redirect tail

redirect : IntCons -> Int
redirect x = sum x

Note that without the `redirect` function this is accepted.

Expected behavior As this is not a true value cycle, the compiler should not reject it.

Desktop (please complete the following information):

Additional context While the example above is pointless, this is causing us an actual issue where we need a "dispatch" function that may send certain variants of a recursive type to particular handlers based on what else exists in the tree.