Just for fun, I was trying to create iterators. I know they are not planned to be in the language and I don't think they should be. I was just trying some stuff out. I was going with ocaml's Seq
design to create possibly infinite iterators when the compiler crashed. I don't know if this is a know issue or not.
I have the following file:
interface IterMinimal
exposes [
Iter,
Node,
fromList,
map,
toList,
]
imports []
Iter a : [Iter ({} -> Node a)] # Needs to be a tag because of recursive types
Node a : [
Nil,
Cons (a, Iter a),
]
# ======= Sequence Generation =======
fromList : List a -> Iter a
fromList = \list ->
when list is
[] -> Iter \{} -> Nil
[x, .. as rest] -> Iter \{} -> Cons (x, fromList rest)
expect toList (fromList [1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]
# ======= Sequence Transformation =======
map : Iter a, (a -> b) -> Iter b
map = \Iter iter, func ->
Iter \{} ->
when iter {} is
Nil -> Nil
Cons (x, rest) -> Cons (func x, map rest func)
expect toList (map (fromList [1, 2, 3, 4, 5]) \x -> x * 2) == [2, 4, 6, 8, 10]
# ======= Sequence Consumption =======
toList : Iter a -> List a
toList = \iter ->
helper : Iter a, List a -> List a
helper = \Iter it, acc ->
when it {} is
Nil -> acc
Cons (x, rest) -> helper rest (List.append acc x)
helper iter []
When I do roc check
on it, nothing seems wrong. When I do roc test
, the compiler crashed with the following message:
thread 'main' panicked at 'Error in alias analysis: duplicate function name FuncName("\x11\x00\x00\x00\x03\x00\x00\x00\x97\xf1\xf2\xd5\xc5]C:") in module', crates/compiler/gen_llvm/src/llvm/build.rs:5744:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Try merging iter and node into one declaration
Currently only self recursive types work, not mutually recursive
Literally pasting Node
's definition into Iter
does not work:
Iter a : [
Iter (
{} -> [
Nil,
Cons (a, Iter a),
]
)
]
Brendan Hansknecht said:
Currently only self recursive types work, not mutually recursive
Is there a plan to make mutually recursive types work?
Just one of those long standing bugs that someone needs to track down and fix, though if this didn't fix your crash must be from something different
Also, does roc check
say anything useful?
It checks just fine
Hmm... I'm not sure currently
Also, interesting, due to the closure, the types work here even though they are technically mutually recursive, no need to merge definitions
Some reason, there is an issue when using the map function...not sure why
Not sure if this is useful, but probably need to file a bug.
This is what happens when removing the type from map and letting the compiler try to solve for it:
thread '<unnamed>' panicked at 'structures ( Opaque(
4.IdentId(0)
, [313], <313>RangedNumber( NumAtLeastEitherSign(I8))), Recursion(3394, None), )<3393> and ( Opaque(4.IdentId(0)
, [313], <313>RangedNumber( NumAtLeastEitherSign(I8))), ['Iter' <3356>Func([<3359>EmptyRecord,], <3358=3358>LambdaSet([], ^<3356>), <3357>Recursion(3360, None)) , ]<Any(3283)>, )<3362> do not unify; they should never have been involved in fixing!', crates/compiler/unify/src/fix.rs:346:22
note: run withRUST_BACKTRACE=1
environment variable to display a backtrace
Brendan Hansknecht said:
Some reason, there is an issue when using the map function...not sure why
Yeah, I had a few tests before adding map which ran fine, maybe should have said that :sweat_smile:
Brendan Hansknecht said:
Not sure if this is useful, but probably need to file a bug.
This is what happens when removing the type from map and letting the compiler try to solve for it:
thread '<unnamed>' panicked at 'structures ( Opaque(
4.IdentId(0)
, [313], <313>RangedNumber( NumAtLeastEitherSign(I8))), Recursion(3394, None), )<3393> and ( Opaque(4.IdentId(0)
, [313], <313>RangedNumber( NumAtLeastEitherSign(I8))), ['Iter' <3356>Func([<3359>EmptyRecord,], <3358=3358>LambdaSet([], ^<3356>), <3357>Recursion(3360, None)) , ]<Any(3283)>, )<3362> do not unify; they should never have been involved in fixing!', crates/compiler/unify/src/fix.rs:346:22
note: run withRUST_BACKTRACE=1
environment variable to display a backtrace
I will do it later tonight
Last updated: Jul 05 2025 at 12:14 UTC