Writing an interpreter for a dynamically typed language usually involves creating an Environment, a data structure which stores mappings of variable names to their values. Values are boxed in some kind of wrapper type, which might look like this in C:
typedef struct value {
type_t type;
void * datum;
} value_t;
where type_t
explicitly stores the type of the given object. A dictionary from strings to value_t
could be used as the environment for the language interpreter.
OCaml and Haskell are both strongly-typed languages, for which there are both compilers and interpreters. How do interpreters for statically typed languages differ from those of dynamically typed languages, if at all?
My first thought was that there could be one environment which maps variables to a descriptor of their types (type_t
in the example above), followed by a separate environment for each type seen so far. However, I can't imagine how one would actually go about implementing such a thing. Also, after skimming the source code for the OCaml interpreter, it looks like the same kind of boxing/unboxing of values familiar from dynamic languages is used there too.