3 ms·
It depends on the implementation, but even if you have proper type inference, I don't understand how it would help with a heterogenous list, since it's the hete
by justin_vanw 11y ago
It depends on the implementation, but even if you have proper type inference, I don't understand how it would help with a heterogenous list, since it's the heterogenous nature that will prevent optimization anyway, even if you knew exactly what the types were.
Most lisps, and certainly common lisp, do not depend on type inference for correct behavior in any way, as they are fundamentally strongly but dynamically typed (like Python). The reason for type inference in Lisps is generally for optimization, where knowing that an array is of type X lets you compile X-specific code for functions that take arrays of type X, and to allow efficient memory layout, where your array is made up of contiguous X objects, rather than storing an array of pointers to X objects, and all the cache missing and so on that this causes (as well as overhead of allocating space for the pointers). Just for completeness, I am saying that in the absence of type information, arrays are always actually arrays of pointers.
So lists are always made up of cons cells which contain pointers, so one optimization is out regardless (it is possible that primative types like int or float would be packed into the cons cell itself, but most implementations would achieve this by some kind of bit tagging rather than type information being 'known'). The other optimization is out because it would likely be equivalent to do dispatch via the usual method for untyped objects rather than trying to do somehow use the type information.
- urs2102 11y agoThanks man, this is great. > since it's the heterogeneous nature that will prevent optimization anyway That was the part I was wondering about. I wanted to see if it was possible to do compile time type checking on a list where I don't know what element would be inside. It was less for optimization and more for experimentation. Like I was trying to see if I approached building a Lisp with OCaml type checking - how much of the "Lisp" would I have to trade to get proper HM type inference. No purpose other than for play. Then, although I assumed it was naturally impossible, figured I'd ask. So car can work for type cons, and then that returns the element regardless of type since the first part of the cons that is pulled becomes just the element?