Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crash when implementing state monad #7432

Open
jkuebart opened this issue Dec 29, 2024 · 2 comments
Open

Compiler crash when implementing state monad #7432

jkuebart opened this issue Dec 29, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jkuebart
Copy link

Hi,

I've tried to implement a state monad to solve one of the advent of code tasks. Below is a very pared-down version that doesn't make sense in itself, but still reproduces the compiler crash.

I haven't been able to simplify it further while reproducing the crash. This crash happens on macOS-Intel.

The crash is the following with both roc_nightly-macos_x86_64-2024-12-18-1285a24 and roc_nightly-macos_x86_64-2024-12-28-2e2be45.

An internal compiler expectation was broken.
This is definitely a compiler bug.
Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>
no lambda set found for (`#UserApp.IdentId(40)`, [
    InLayout(
        94,
    ),
    InLayout(
        118,
    ),
]): LambdaSet {
    set: [
        ( Test.40, [InLayout(94), InLayout(109)]),
    ],
    args: [
        InLayout(U64),
    ],
    ret: InLayout(
        83,
    ),
    representation: InLayout(
        100,
    ),
    full_layout: InLayout(
        105,
    ),
}
Location: crates/compiler/mono/src/layout.rs:1590:17

Here is the code:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br",
}

import pf.Stdout

main =
    step 0 |> eval 0 |> Inspect.toStr |> Stdout.line

step : U64 -> Checksum
step = \i ->
    if
        0 == i
    then
        checksum i
    else
        checksum i
        |> bind \s0 ->
            step (i - 1)
            |> map \s1 -> s0 + s1

Checksum : State U64 U64

checksum : U64 -> Checksum
checksum = \len ->
    get
    |> bind \pos ->
        put (pos + len)
        |> map \_ -> len * (2 * pos + len - 1) // 2

State s a := s -> (a, s)

run : State s a, s -> (a, s)
run = \@State s, t -> s t

eval : State s a, s -> a
eval = \s, t -> (run s t).0

put : s -> State s {}
put = \s -> @State \_ -> ({}, s)

get : State s s
get = @State \s -> (s, s)

map : State s a, (a -> b) -> State s b
map = \s, f -> @State \s0 ->
    (x, s1) = run s s0
    (f x, s1)

bind : State s a, (a -> State s b) -> State s b
bind = \s, f -> @State \s0 ->
    (x, s1) = run s s0
    run (f x) s1
@Anton-4 Anton-4 added the bug Something isn't working label Dec 30, 2024
@Anton-4
Copy link
Collaborator

Anton-4 commented Dec 30, 2024

Hi @jkuebart,
This may take a while to get fixed because it's very complicated but you can follow progress here.

@jkuebart
Copy link
Author

no worries, it's possible to work around it by using bind followed by pure instead of map, I just filed the issue because the compiler asked me to ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants