[Haskell-cafe] Writing an interpreter for a language with mutability
Chris Wong
lambda.fairy at gmail.com
Mon Dec 18 10:25:15 UTC 2017
Hi Jeremy,
I'm on my phone right now so I can't link, but try searching for "IORef"
and the "ST monad".
Chris
On Dec 18, 2017 10:06, "Jeremy Mikkola" <jeremy128 at gmail.com> wrote:
> I am quite certain I am not the first to try to do this, but my google-fu
> is failing me today.
>
> How does one go about interpreting a language with mutable objects in
> Haskell?
>
>
> The best approach I can think of is to represent the language's memory as
> a `Data.Map.Map RefID LanguageObject` where RefID is some type (probably
> Int) used as a reference. The LanguageObject structure might contain some
> values of type RefID to refer to other objects. Mutating an object involves
> simply replacing the value in the map at a given RefID.
>
>
> I don't like this approach for two reasons:
>
> 1. Map lookups aren't very efficient compared with actual references to
> the value.
>
> 2. I have to re-invent garbage collection, removing objects from the map
> when they no longer have incoming references. (Unlike simple interpreters
> for languages with immutable values, I can't rely on Haskell's garbage
> collector to do the work for me.)
>
>
> Is there a better approach?
>
>
> - Jeremy
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171218/4f5a94b9/attachment.html>
More information about the Haskell-Cafe
mailing list