[Haskell-cafe] Writing an interpreter for a language with mutability
tikhon at jelv.is
Tue Dec 19 17:55:37 UTC 2017
"Write Yourself a Scheme in 48 Hours" 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.
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>
>> Am 17.12.2017 um 22:03 schrieb Jeremy Mikkola:
>>> How does one go about interpreting a language with mutable objects in
>>> 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:
>> 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:
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe