Stream: beginners

Topic: Help with very crash-y lisp interpreter


view this post on Zulip Hannes (Aug 01 2024 at 12:36):

I saw @Ray Myers shared a lisp interpreter recently, which reminded me of a half finished lisp interpreter I started a while ago. It's currently a one-to-one translation of this Gleam example repo to try and have a fair performance comparison, but I would like to translate it to more idiomatic Roc once it's working.

The repo is here, and running roc check src/lib/main.roc results in

thread '<unknown>' has overflowed its stack
fatal runtime error: stack overflow

I know I had a compiling project a while ago, but now that I've come back to it I've forgotten what I was working on when it broke :sweat_smile: I think the type definitions broke it, replacing type aliases with their defintions seems to help. e.g. replacing every occurence of Procedure with its definition List Expr, State -> Evaluated helps reduce the crashes.

I did a binary search commenting out half of the code from the bottom up and this line was the one that changed from a crash to an error:

Scope : Dict Str Expr

view this post on Zulip Hannes (Aug 02 2024 at 05:00):

I have a MWE now, this code has a recursive alias error, but the compiler crashes before it can show the error

Expr : [
    # ListExpr (List Expr),
    # BoolExpr Bool,
    # IntExpr I64,
    ProcedureExpr Procedure,
]

Scope : Dict Str Expr

State : { globalScope : Scope, localScope : Scope }

Procedure : List Expr, State-> Evaluated

Replacing Procedure with it's definition like this:

Procedure : List Expr, { globalScope : Dict Str Expr, localScope : Dict Str Expr } -> Evaluated

fixes the compiler crash and shows the error

view this post on Zulip Luke Boswell (Aug 02 2024 at 05:28):

I think there is a known bug where recursive unions have to be defined in one type.

view this post on Zulip Luke Boswell (Aug 02 2024 at 05:29):

I don't properly understand it, but maybe this help you find a solution?

view this post on Zulip Ray Myers (Aug 02 2024 at 05:42):

I also ran into that, and what Luke suggested was how I got around it - I re-organized my types to make it only recursive not mutually recursive. I think this is the tracked issue for it if you want to upvote or add info.

view this post on Zulip Hannes (Aug 02 2024 at 05:53):

Ah thank you both, I'll see if i can get it to compile now


Last updated: Jul 05 2025 at 12:14 UTC