Stream: beginners

Topic: Open Tag unions on parsers


view this post on Zulip Oskar Hahn (Nov 21 2026 at 08:49):

Hi,

to prepare myself for AoC, I try to understand the parser package again. I have to relearn the concept every year, so I want to give me a bit of a head start.

I have this code:

parse_line : Parser(List(U8), [Left(U64), Right(U64)], _)
parse_line = {
    Parser.one_of([
        parse_left,
        parse_right,
    ])
}

parse_left : Parser(List(U8), [Left(U64)], _)
parse_left = {
    Parser.const(|n| Left(n)).skip(Parser.Utf8.codeunit('L')).keep(Parser.Utf8.digits({}))
}

parse_right : Parser(List(U8), [Right(U64)], _)
parse_right = {
    Parser.const(|n| Right(n)).skip(Parser.Utf8.codeunit('R')).keep(Parser.Utf8.digits({}))
}

It fails with the message:

── ✗ type mismatch ──────────────────────────────────────────────────────────────────────────────────────

The two elements in this list have incompatible types.

parse_right,
^^^^^^^^^^^

The first element has this type:

    Parser(List(U8), [Left(U64)], [ExpectedByte(U8), ExpectedBytes(List(U8)), ExpectedDigit, ExpectedPredicate, IntegerOverflow(U64),
    InvalidUtf8, UnexpectedEnd])

However, the second element has this type:

    Parser(List(U8), [Right(U64)], [ExpectedByte(U8), ExpectedBytes(List(U8)), ExpectedDigit, ExpectedPredicate, IntegerOverflow(U64),
    InvalidUtf8, UnexpectedEnd])

All elements in a list must have compatible types.
Note: You can wrap each element in a tag to make them compatible.
To learn about tags, see <https://www.roc-lang.org/tutorial#tags>

It works, if I remove the type annotation of parse_left and parse_right, since then the type gets inferred to have [Left(U64) ,Right(U64)]

I tried to "open" the type by adding ..: parse_left : Parser(List(U8), [Left(U64), ..], _). But this gets the error:

── ✗ value is not polymorphic ───────────────────────────────────────────────────────────────────────────

The type annotation on parse_left says it can be used at many types, but parse_left isn't defined as a function (like |x| ...), so it can only
have one type.

parse_left : Parser(List(U8), [Left(U64), ..], _)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you want me to infer its type, remove each .., or write a concrete type.

If you want to use it at many types, make it a function that takes {}:
    parse_left : {} -> Parser(List(U8), [Left(U64), ..], _)
    parse_left = |{}| ...
Then call it as parse_left({}) wherever you use it.

The hint works:

parse_line : Parser(List(U8), [Left(U64), Right(U64)], _)
parse_line = {
    Parser.one_of([
        parse_left(),
        parse_right(),
    ])
}

parse_left : () -> Parser(List(U8), [Left(U64)], _)
parse_left = || {
    Parser.const(|n| Left(n)).skip(Parser.Utf8.codeunit('L')).keep(Parser.Utf8.digits({}))
}

parse_right : () -> Parser(List(U8), [Right(U64)], _)
parse_right = || {
    Parser.const(|n| Right(n)).skip(Parser.Utf8.codeunit('R')).keep(Parser.Utf8.digits({}))
}

I don't like this solution/workaround. Most of the time, I add the types after the fact, when I debug the code. It is not so nice, that I have to rewrite the parsers as functions, to add types.

Is there a way to open the type of a parser without writing it as a function?

view this post on Zulip Anton (Nov 21 2026 at 15:47):

Good to see you again @Oskar Hahn :)
I will reply to your question later tonight if no one gets to it before then.

view this post on Zulip notramo (Nov 21 2026 at 16:45):

Have you tried to make the unions open?

view this post on Zulip notramo (Nov 21 2026 at 16:47):

I don't really understand how tag unions work, because most of the time, it just works, so I haven't looked it up yet (and probably there are no docs about it yet), but when I get errors like this, I always try with open unions.


Last updated: Nov 26 2026 at 20:35 UTC