Re: How to implement dynamic typing?

torbenm@diku.dk (Torben Ęgidius Mogensen)
Wed, 07 Apr 2010 12:00:34 +0200

          From comp.compilers

Related articles
[2 earlier articles]
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)
Re: How to implement dynamic typing? ctalk@ctalklang.org (Ctalk Project) (2010-04-07)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-07)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-10)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-11)
Re: How to implement dynamic typing? harold.aptroot@gmail.com (Harold Aptroot) (2010-04-11)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-11)
[16 later articles]
| List of all articles for this month |

From: torbenm@diku.dk (Torben Ęgidius Mogensen)
Newsgroups: comp.compilers
Date: Wed, 07 Apr 2010 12:00:34 +0200
Organization: UNI-C
References: 10-04-009
Keywords: types
Posted-Date: 07 Apr 2010 12:27:27 EDT

David Belier <davidbleier@hotmail.com> writes:


> Recently I finished chapter 7 of the dragon book (2nd edition) but I
> still don't understand how to implement dynamic typing. I don't get
> how you are supposed to store the type information with each variable
> and check it every time using an acceptable amount of resources and
> time. Can someone please name papers or websites to read?


Combining each value (not variable) with a type tag is the most common
way to implement dynamic types. If the number of possible types is
fixed at compile time, the tags can be small integers (one for each
possible type) and you would typically test the tag before each
operation (where an operation would only allow a small number of
different types). If you can dynamically create types or add operations
to types, the tag can be a pointer to a descriptor, which itself can be
a table of the possible operations. This is typical for OO languages,
which have dynamic types to some degree (due to dynamic subtyping) even
when they are nominally statically typed (such as Java).


> Note: I have a strong C++ background and know x86 assembler. I can
> imagine a way to implement it doubling memory requirements and making
> the program dead slow in the process so there must be better ways. In
> the past I have also used Java and Python for small programs (CS
> course and writing file converters). They seemed to show reasonable
> performance.


Most Python imlementations are fairly slow, but you will only notice
this if you do intensive calculations.


If you want to make a dynamically-typed language fast, you should
exploit the fact that most programs use only single types for most
variables, so you can do type inference before compilation and add/test
type tags only where the inference can't statically infer a type. This
is called "soft typing" and can also be used as a bug-finding tool
(since the inferencer can find instances where it is certain that a
dynamic type check will fail).


Torben



Post a followup to this message

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