Re: How to implement dynamic typing?

andy johnson <sleepdev@gmail.com>
Tue, 6 Apr 2010 00:31:25 -0700 (PDT)

          From comp.compilers

Related articles
How to implement dynamic typing? davidbleier@hotmail.com (David Belier) (2010-04-02)
Re: How to implement dynamic typing? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-04-06)
Re: How to implement dynamic typing? sleepdev@gmail.com (andy johnson) (2010-04-06)
Re: How to implement dynamic typing? rpw3@rpw3.org (2010-04-06)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-06)
Re: How to implement dynamic typing? paul.biggar@gmail.com (Paul Biggar) (2010-04-06)
Re: How to implement dynamic typing? iant@google.com (Ian Lance Taylor) (2010-04-06)
Re: How to implement dynamic typing? barry.j.kelly@gmail.com (Barry Kelly) (2010-04-07)
Re: How to implement dynamic typing? torbenm@diku.dk (2010-04-07)
[22 later articles]
| List of all articles for this month |

From: andy johnson <sleepdev@gmail.com>
Newsgroups: comp.compilers
Date: Tue, 6 Apr 2010 00:31:25 -0700 (PDT)
Organization: Compilers Central
References: 10-04-009
Keywords: types
Posted-Date: 07 Apr 2010 01:42:13 EDT

Dynamic typing is all about separating language semantics from
implementation semantics by throwing compile-time/run-time checks out
the window entirely. This can be accomplished trivially by unifying
all data types into a single one-size-fits-all structure. Python
accomplishes this by equating an object to a namespace which can be
implemented as a dictionary. Lisp goes more in the direction of lists,
but the reasoning is the same: when everything is a duck there is no
need to check if it quacks.


For primitive data types you can make special exceptions to this rule
and do pattern matching on those types whenever the lower level "raw"
value is needed. So at some level, dynamic languages usually will
generate runtime type errors when these basic data types are misused,
but for anything above that the programmer is on their own. There is
lots of recent research into breaking this categorization of how
dynamic languages work, but for now this seems to be the most
prevalent way to implement dynamic typing.


A single typedef might be clearer if you are familiar with haskell or
similar:


data Term = T_Object (Map Term Term) |
    T_Function ([Term] -> Term) |
    T_Bool Bool | T_Int Integer | T_Char Char |
    T_Tuple [Term] | T_Map (Map Term Term)


Or in plain english, a term in the language can be
    1) an arbitrary object
    2) a function (takes zero or more terms and returns a term)
    3) a boolean, integer, or character
    4) a tuple, or map


Notice the similarity between objects and maps. This language would
work similar to python with all user-defined objects being
structurally equivalent to a dictionary.



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.