Stream: beginners

Topic: ✔ implements multiple


view this post on Zulip Karakatiza (Apr 05 2024 at 22:50):

I am trying to write a simple toEncoder generator for enum values based on the mapping list, which requires Eq ability.
roc check doesn't find errors or warnings, but I get the following error on roc run:

thread '<unnamed>' panicked at 'Cannot find a partial proc for LambdaName { name: `17.IdentId(9)`, niche: Niche(Captures([InLayout(54)])) }', crates/compiler/mono/src/ir.rs:3193:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

main.roc :

app "simple-enum"
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
        json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.6.3/_2Dh4Eju2v_tFtZeMq8aZ9qw2outG04NbkmKpFhXS_4.tar.br",
    }
    imports [
        pf.Stdout,
        json.Core.{ json },
    ]
    provides [main] to pf

main =
    Encode.toBytes (@PipelineStatus Running) json |> Str.fromUtf8 |> Result.withDefault "Failed to encode" |> Stdout.line

encodeEnum : List (a, Str) -> (a -> Encoder fmt) where a implements Eq, fmt implements EncoderFormatting
encodeEnum = \values -> \enum ->
        Encode.custom
            (\bytes, _fmt -> List.concat
                    bytes
                    (
                        when
                            List.findFirst values \pair -> enum == pair.0
                        is
                            Ok pair -> Str.toUtf8 "\"$(pair.1)\""
                            Err err -> crash (Inspect.toStr err)
                    )
            )

PipelineStatus := [
    Shutdown,
    Running,
]
    implements [
        Encoding { toEncoder: encodePipelineStatus },
        Inspect, # auto derive
        Eq, # auto derive
    ]
valuesPipelineStatus = [
    (@PipelineStatus Shutdown, "Shutdown"),
    (@PipelineStatus Running, "Running"),
]
encodePipelineStatus = encodeEnum valuesPipelineStatus

The same error occurs without type declaration for encodeEnum.

It looks like I might need to specify type definition something like

encodeEnum : List (a, Str) -> (a -> Encoder fmt) where a implements [Encoding, Eq], fmt implements EncoderFormatting

but it looks like this syntax isn't supported yet.
Did anyone had a similar issue?

view this post on Zulip Karakatiza (Apr 05 2024 at 22:51):

Type declaration

encodeEnum : List (a, Str) -> (a -> Encoder fmt) where a implements Encoding, fmt implements EncoderFormatting

on roc run yields

thread '<unnamed>' panicked at '[Abilities] not yet implemented. Tracking issue: https://github.com/roc-lang/roc/issues/2463', crates/compiler/solve/src/specialize.rs:874:34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Brendan Hansknecht (Apr 05 2024 at 23:04):

The syntax is with an &. So should be something like:

encodeEnum : List (a, Str) -> (a -> Encoder fmt) where a implements Encoding & Eq, fmt implements EncoderFormatting

view this post on Zulip Brendan Hansknecht (Apr 05 2024 at 23:05):

Not sure that is the actual bug here specifically, but that would be the type

view this post on Zulip Karakatiza (Apr 05 2024 at 23:07):

Thanks, this seems to be the case! But this indeed is not the source of the error, I am working on a simpler reproduction

view this post on Zulip Brendan Hansknecht (Apr 05 2024 at 23:08):

I know a workaround. This is the first time I have seen this bug in an app. Generally it is only seen on the app platform boundary.

@Ayaz Hafiz would probably have context as to why the issue exist

I have no idea, just have a workround:
Change the last line to:

encodePipelineStatus = \e -> (encodeEnum valuesPipelineStatus) e

view this post on Zulip Karakatiza (Apr 05 2024 at 23:10):

Wow, this works!! Thank you, Brendan!

view this post on Zulip Karakatiza (Apr 05 2024 at 23:11):

It is a fun game I am having with Roc - when something errors out in runtime - try to shuffle equivalent expressions / add explicit types until the code works :sweat_smile:

view this post on Zulip Notification Bot (Apr 05 2024 at 23:11):

Karakatiza has marked this topic as resolved.

view this post on Zulip Notification Bot (Apr 05 2024 at 23:19):

Karakatiza has marked this topic as unresolved.

view this post on Zulip Karakatiza (Apr 05 2024 at 23:21):

I have a minimal reproduction (requires multiple files):
Show.roc

interface Show
    exposes [Show, show]
    imports [
    ]

Show implements
    show : a -> Str where a implements Show

Types.roc

interface Types
    exposes [PipelineStatus, pipelineStatus]
    imports [
        Show.{ Show, show },
        Enum.{ showEnum },
    ]

PipelineStatus := [
    Shutdown,
    Running,
]
    implements [
        Eq,
        Show { show: showPipelineStatus },
    ]
valuesPipelineStatus = [
    (@PipelineStatus Shutdown, "Shutdown"),
    (@PipelineStatus Running, "Running"),
]
showPipelineStatus = showEnum valuesPipelineStatus
pipelineStatus = \x -> @PipelineStatus x

expect
    show (pipelineStatus Running) == "Running"

Enum.roc

interface Enum
    exposes [
        showEnum,
    ]
    imports [
    ]

showEnum = \values -> \enum ->
        when
            List.findFirst values \pair -> enum == pair.0
        is
            Ok pair -> pair.1
            Err err -> crash (Inspect.toStr err)

main.roc

app "simple-enum"
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
    }
    imports [
        pf.Stdout,
        Show.{ Show, show, pipelineStatus },
        Types.{ pipelineStatus },
    ]
    provides [main] to pf

main =
    show (pipelineStatus Running) |> Stdout.line

roc run yields

thread 'main' panicked at 'Error in alias analysis: error in module ModName("UserApp"), function definition FuncName("\x11\x00\x00\x00\x00\x00\x00\x00t\xebz\x14\xc2\x8d\x90\xb5"), definition of value binding ValueId(4): expected type '((heap_cell,),)', found type '((heap_cell, bag<((heap_cell,), ())>),)'', crates/compiler/gen_llvm/src/llvm/build.rs:5759:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

roc test yields

thread '<unnamed>' panicked at 'Cannot find a partial proc for LambdaName { name: `40.IdentId(2)`, niche: Niche(Captures([InLayout(27)])) }', crates/compiler/mono/src/ir.rs:3193:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Brendan Hansknecht (Apr 05 2024 at 23:23):

Yeah, though roc's core is pretty stable, it is easy to hit bugs especially, with more complex types and features (like abilities)

view this post on Zulip Luke Boswell (Apr 06 2024 at 01:09):

@Karakatiza I think this would be helpful as an Issue if you dont mind.

view this post on Zulip Karakatiza (Apr 06 2024 at 02:31):

Now tracking in https://github.com/roc-lang/roc/issues/6629

view this post on Zulip Notification Bot (Apr 06 2024 at 02:31):

Karakatiza has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC