From a quick scan, this has been discussed and problems are somewhat known, but not in the view of a pure lazy stream type, although the root cause is likely the same as the Simple Linked List thread as the error messages are similar. Back when the Roc compiler was still implemented in Rust, I opened an issue on this that wasn't resolved and was closed in view of the many changes to the language and the new Zig compiler; however, it seems to me that problem is still there (unless I am doing something wrong; please show me):
Unlike examples of Simple linked lists this stream type is a Co-Inductive Stream (CIS) with the type containing a head value and a thunk function to produce the next stream element recursively. the short example then tries to manipulate an instance of this stream to produce two and all the odd numbers following, finally creating a Str of a number of these and printing it. The short code is as follows:
app [main!] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.27.0/9yjxqdGTP4xSpSEmDazwTmRsehfRZzA19pwEKkR6fj1g.tar.zst",
}
import cli.Stdout
# lazy CIS stream type definition...
CIS(a) := [Cons(a, {} -> CIS(a))]
# show the expanded CIS Stream for given `n` elements...
showCISFor : CIS(a), I32 -> Str where [a.to_str : a -> Str]
showCISFor = |cis, n| {
loopi = |icis, i, lst| {
if (i <= 0) { Str.join_with(List.map(lst, |e| e.to_str()), ", ") }
else match icis {
Cons(hd, tlf) => loopi(tlf({}), i - 1, lst |> List.append(hd))
}
}
loopi(cis, n, [])
}
cisTest : {} -> CIS(I32)
cisTest = |{}| {
next = |i| Cons(i, |{}| next(i + 2))
Cons(2.I32, |{}| next(3))
}
main! = |_| {
# Stdout.line!("What the...")
cisTest({}) |> showCISFor(20) |> Stdout.line!()
}
Typical errors such as for the Cons(2... line is as follows:
── ✗ anonymous recursion ────────────────────────────────────────────────────────────────────── RecursiveTypes.roc:25:20
I am inferring a recursive type that has no name.
Cons(2.I32, |{}| next(3))
^^^^^^^
Here is the type I'm inferring. You will see <RecursiveType> for parts of the type that repeat.
{} -> [Cons(c, <RecursiveType>)]
where [
c.from_numeral : Numeral -> Try(c, [InvalidNumeral(Str)]),
c.plus : c, d -> c,
d.from_numeral : Numeral -> Try(d, [InvalidNumeral(Str)]),
]
Hint: Recursive types are only allowed through nominal types. If you need a recursive data structure, define a nominal type using :=.
NOTE THAT THE CIS TYPE IS A NOMINAL TYPE!!!
The equivalent code in Elm is as follows with no type problems; top level type signatures are used but aren't needed:
module Main exposing (main)
import Html exposing (Html, text)
-- recursive stream type...
type CIS a = CIS a (() -> CIS a)
cisStringTo : Int -> CIS Int -> String
cisStringTo n cis =
let
loop i (CIS hd tlf) lst =
if i <= 0 then
String.join ", "
<| List.map String.fromInt
<| List.reverse lst
else loop (i - 1) (tlf()) (hd :: lst)
in loop n cis []
-- generate stream of two and all odds following...
cisTest : () -> CIS Int
cisTest () =
let next i = CIS i <| \() -> next (i + 2)
in CIS 2 <| \() -> next 3
main : Html Never
main = cisTest () |> cisStringTo 20 |> text
EDITED: Sorry, the Elm code was more complex than it had to be and not in line with the Roc code, now the same...
Written by GPT on behalf of Luke.
Yes—I ran it successfully with nightly-2026-11-12-ce942d6, the latest published nightly at the time of testing (13 November 2026).
Your CIS declaration is nominal. The problem is inference inside the local recursive functions: bare Cons allows an anonymous structural type to form. Using CIS.Cons explicitly in both construction and pattern matching resolves it. This distinction is described under qualified tags.
Here is the working program:
app [main!] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.28.0/2yhxoR7cZPSYbyUzPWULFw8AA6QEcdsgFNRmzu7pZbst.tar.zst",
}
import cli.Stdout
CIS(a) := [Cons(a, {} -> CIS(a))]
showCISFor : CIS(a), I32 -> Str where [a.to_str : a -> Str]
showCISFor = |cis, n| {
loopi = |icis, i, lst| {
if i <= 0 {
Str.join_with(List.map(lst, |e| e.to_str()), ", ")
} else match icis {
CIS.Cons(hd, tlf) =>
loopi(tlf({}), i - 1, List.append(lst, hd))
}
}
loopi(cis, n, [])
}
cisTest : {} -> CIS(I32)
cisTest = |{}| {
next = |i| CIS.Cons(i, |{}| next(i + 2))
CIS.Cons(2.I32, |{}| next(3))
}
main! = |_| {
cisTest({}) |> showCISFor(20) |> Stdout.line!()
}
Running roc run --no-cache prints:
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39
Basically the bare Cons return tag isn't being lifted to a CIS -- though I feel like the compiler should pick that up. So maybe a clear bug here?
Like I would have thought Cons(2.I32, ...) unifies with the tag in CIS being the return from cisTest : {} -> CIS(I32)
@Jared Ramirez is this a clear bug?
i don’t think a bug! the inner fn loopi defines Cons as a anon tag union, and nothing before it generalizes gives the compiler information that the Cons is part of CIS. the correct fix here is the qualified tag or an explicit annotation of loopi
this is a known limitation of coercing anon tag unions into nominals (that it is not always possible to do without explicit types like an anno or qualifies ref)
Thank you Jared, that makes sense
Luke Boswell said:
Thank you Jared, that makes sense
@Luke Boswell and @Jared Ramirez thank the both of you for this; I am not used to using the contruction qualifier and the need for it.
yeah, most of the time the compiler can figure it out! but not always :grinning:
Last updated: Nov 26 2026 at 20:35 UTC