[Haskell-cafe] Writing an interpreter for a language with mutability

Tikhon Jelvis tikhon at jelv.is
Tue Dec 19 17:55:37 UTC 2017


"Write Yourself a Scheme in 48 Hours"[1] is a tutorial about implementing
Scheme in Haskell. It takes the approach of modeling mutable variables in
Scheme using IORefs. It's a good place to start learning about these
ideas—in fact, it's pretty much how I learned Haskell.

Looking back at it now I'm not sure I would make the same decisions as the
author any more, but it worked and the tutorial does a good job of guiding
you through what the code does. After you follow that, you should have a
good feeling for how IORefs work in Haskell and how they fit into an
interpreter for a mutable language.

[1]: https://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours

On Tue, Dec 19, 2017 at 9:37 AM, Jeremy Mikkola <jeremy128 at gmail.com> wrote:

> Hi all, thank you for the replies!
>
> The language this will interpret does not expose pointers (so it acts like
> the more modern languages).
>
> I have started to try to wrap my head around IORef and that monad (anyone
> know of a "for dummies" guide to those?). I think that they will likely be
> exactly what I need.
>
> Thanks again,
>
> - Jeremy Mikkola
>
> On Mon, Dec 18, 2017 at 12:31 PM, Joachim Durchholz <jo at durchholz.org>
> wrote:
>
>> Am 17.12.2017 um 22:03 schrieb Jeremy Mikkola:
>>
>>> 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.
>>>
>>
>> It depends on how data is addressed in the language.
>> If you wanto interpret a C-style language where every address can be cast
>> to an int, then that's the most straightforward (though not necessarily
>> best) approach.
>> If it is just references to objects as in most, erm, "more modern"
>> languages, the Id can be any type. E.g. something based on the language's
>> data model - Int | Real | Record | Array, with type parameters as
>> appropriate. Upside is that you leverage the Haskell GC that way, downside
>> is that you'll need a recursive type (Array is really Array RefId, and my
>> Haskell-fu is hilariously inadequate to properly flesh out that recursion).
>>
>> _______________________________________________
>> 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.
>>
>
>
> _______________________________________________
> 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/20171219/f1e0d24f/attachment.html>


More information about the Haskell-Cafe mailing list