Re: How to implement dynamic typing?

"bartc" <bartc@freeuk.com>
Tue, 6 Apr 2010 16:38:56 -0000

          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)
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)
[20 later articles]
| List of all articles for this month |

From: "bartc" <bartc@freeuk.com>
Newsgroups: comp.compilers
Date: Tue, 6 Apr 2010 16:38:56 -0000
Organization: virginmedia.com
References: 10-04-009
Keywords: types
Posted-Date: 07 Apr 2010 01:53:13 EDT

"David Belier" <davidbleier@hotmail.com> wrote in message
> 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.


What would be acceptable?


>Can someone please name papers or websites to read?


I quite liked a paper called The Implementation of Lua (I think here:


http://www.jucs.org/jucs_11_7/the_implementation_of_lua/jucs_11_7_1159_1176_defigueiredo.html )


(Although it claims Lua to be fast, I didn't find it that fast.)


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


It sounds like you already have a good idea of what's involved. Often
dynamic typing is linked to interpreted languages, and you would expect the
combination to be a magnitude at least slower than statically typed and
fully compiled code.


The way I do it, dynamic typing involves adding an extra tag to every value.
This could be a pointer to a table (of function pointers; indexed by
whatever operation you want to do on it). Binary operations then need yet
more testing. However I tend to just use an index or handle to the type
represented.


I don't see that a tag will double memory, assuming that you include
homogeneous arrays in your type system (you don't need to tag every
element), strings, structures, and so on. The total overhead then should be
small.


It is true that you frequently have to check these tags, but there are ways
to speed that up (tables of jumps and pointers as mentioned). And if the
operations you're performing are high-level (on a whole array, string, or
structure), then again the overhead should be small.


Dynamic typing can work well (which is why I'm switching to static typing
:-) (but mixed with dynamic types)


> the past I have also used Java and Python for small programs (CS
> course and writing file converters). They seemed to show reasonable
> performance.


If you think that Python is fast enough, then you're not going to have any
problem...


--
Bartc



Post a followup to this message

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