Open imclerran opened 10 months ago
Oh, and my Roc version:
roc nightly pre-release, built from commit 4569770c82c on Fri Dec 29 09:12:45 UTC 2023
I cleaned up my code to resolve other compile blocking errors in my own code, and reduced my errors down to two:
1) CYCLIC ALIAS - Node : {val : I64, lhs : NodeOrNull, rhs : NodeOrNull}
2) TYPE MISMATCH - if (List.len lst) <= index then Null
- This index value is a: U32
But <= needs its 2nd argument to be: Int Natural
My understanding from this conversation is that the cyclic alias error is a long standing bug with recursive types #5119, so I am unable to resolve it.
The compiler error produced above continues to occur if I change the type of index from U32
to Int Natural
.
My updated code to reduce the errors in my code is below:
app "leafsimilar"
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" }
imports [pf.Stdout]
provides [main] to pf
## Type Aliases to define binary tree nodes
NodeOrNull: [Child Node, Null]
Node : {val : I64, lhs : NodeOrNull, rhs : NodeOrNull}
## Convert a boolean value to a string representation
boolToStr : Bool -> Str
boolToStr = \val ->
if val == Bool.true then "true"
else "false"
## Check if a node has a child on the right hand side
hasRhs : Node -> Bool
hasRhs = \ node ->
when node.rhs is
Child _ -> Bool.true
Null -> Bool.false
## Check if a node has a child on the left hand side
hasLhs : Node -> Bool
hasLhs = \ node ->
when node.lhs is
Child _ -> Bool.true
Null -> Bool.false
## Convert a list of string represented values (ordered by level) into a binary tree
## values may be a string represented integer, or "null"
listToTree : List Str -> NodeOrNull
listToTree = \lst -> listToTreeRecur lst 0
listToTreeRecur : List Str, Int Natural -> NodeOrNull
listToTreeRecur = \lst, index ->
if (List.len lst) <= index then Null
else
strVal =
when List.get lst index is
Ok str -> str
Err _ -> ""
if strVal == "null" then Null
else
val =
when Str.toI64 (strVal) is
Ok num -> num
Err InvalidNumStr -> 0
rhs = listToTreeRecur lst (index * 2 + 1)
lhs = listToTreeRecur lst (index * 2 + 2)
Child {val, rhs, lhs}
root = {val: 0, lhs: Null, rhs: Null}
treeList = ["0", "1", "null"]
tree = listToTree treeList
# root has no children, so hasRhs should return false
main = Stdout.line "\(boolToStr (hasRhs root))"
When building my roc code, (see below) I receive an error message as in the issue title:
My stack trace is as follows:
The roc code I am building is below. I know that it has some errors in the code, which previously prevented compiling. I made a one word change to the code to fix one of my errors, specifically changing the type of
listToTreeRecur
fromlistToTreeRecur : List Str, I32 -> NodeOrNull
tolistToTreeRecur : List Str, Natural -> NodeOrNull
, after which I received the error above. Here is my full code:Prior to changing the
I32
toNatural
, this was the output fromroc run LeafSimilar.roc
(note that thread main still panics):with the following backtrace: