roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.14k stars 291 forks source link

Rosetree causes type and layout problems #934

Open folkertdev opened 3 years ago

folkertdev commented 3 years ago
app "rosetree"
    packages { base: "platform" }
    imports [base.Task]
    provides [ main ] to base

main : Task.Task {} []
main =
    myTree : Tree Str
    myTree = tree "foo" [ singleton "bar", singleton "baz" ]

    myTree
        |> count
        |> Str.fromInt
        |> Task.putLine

Tree a : [ Tree a (List (Tree a)) ]

tree : a, List (Tree a) -> Tree a
tree = \a, t -> Tree a t

singleton : a -> Tree a
singleton = \x -> Tree x []

count : Tree Str -> I64
count = \t -> foldl (\_, x -> x + 1) 0 t

foldl : (Str, I64 -> I64), I64, Tree Str -> I64
foldl = \f, acc, (Tree d xs) -> listFoldl xs (foldlHelper f) (f d acc)

listFoldl : List (Tree Str), (Tree Str, I64 -> I64), I64  -> I64
listFoldl = \a,b,c -> List.walk a b c

foldlHelper : (Str, I64 -> I64) -> (Tree Str, I64 -> I64)
foldlHelper = \f -> \subtree, subacc ->
    foldl f subacc subtree

# foldl : (a, b -> b), b, Tree a -> b
# foldl = \f, acc, (Tree d xs) -> List.walk xs (foldlHelper f) (f d acc)
# 
# foldlHelper : (a, b -> b) -> (Tree a, b -> b)
# foldlHelper = \f -> \subtree, subacc ->
#     foldl f subacc subtree

This runs into all kinds of layout and recursion problems.

JanCVanB commented 2 years ago

FWIW, I get this output today:

[jan@framey roc]$ cat test.roc
app "rosetree"
    packages { pf: "examples/hello-world/platform/main.roc" }
    imports []
    provides [main] to pf

main =
    myTree : Tree Str
    myTree = tree "foo" [ singleton "bar", singleton "baz" ]

    myTree
        |> count
        |> Str.fromInt

Tree a : [ Tree a (List (Tree a)) ]

tree : a, List (Tree a) -> Tree a
tree = \a, t -> Tree a t

singleton : a -> Tree a
singleton = \x -> Tree x []

count : Tree Str -> I64
count = \t -> foldl (\_, x -> x + 1) 0 t

foldl : (Str, I64 -> I64), I64, Tree Str -> I64
foldl = \f, acc, (Tree d xs) -> listFoldl xs (foldlHelper f) (f d acc)

listFoldl : List (Tree Str), (Tree Str, I64 -> I64), I64  -> I64
listFoldl = \a,b,c -> List.walk a b c

foldlHelper : (Str, I64 -> I64) -> (Tree Str, I64 -> I64)
foldlHelper = \f -> \subtree, subacc ->
    foldl f subacc subtree

# foldl : (a, b -> b), b, Tree a -> b
# foldl = \f, acc, (Tree d xs) -> List.walk xs (foldlHelper f) (f d acc)
# 
# foldlHelper : (a, b -> b) -> (Tree a, b -> b)
# foldlHelper = \f -> \subtree, subacc ->
#     foldl f subacc subtree
[jan@framey roc]$ roc test.roc

thread '<unknown>' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)
[jan@framey roc]$