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

Joachim Durchholz jo at durchholz.org
Mon Dec 18 20:31:50 UTC 2017


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).


More information about the Haskell-Cafe mailing list