by kazinator · 12y ago · 0 comments
These people who stay away from typed languages, who are they and what are they using?
Shell? Awk? Assembler?
When is a language untyped: is it when you can misuse an operand by applying the wrong operation, with no diagnosis and strange, non-portable consequences? Or is when the character string "1.3" can be used as an arithmetic operand, automatically becoming a floating-point approximation of "1.3"? (If that is "untyped", how is it that "1.3" is identified as a character string, and how is the context identified as requiring an arithmetic operand?)
Or is it untyped when wrong type operands do something unpredictable, but dumb?
Bash:
$ string=abcd
echo $(( string + 3 ))
3
$ number=4
$ echo $(( number + 3 ))
7
That must be untyped. No?There's the formal definition of type and there's the colloquial definition of type. A type in the context of the article means a formal type, it is an syntactic classifier that is part of the static semantics of the language. The properties you describe ( string/number exceptions ) are part of the dynamics of the language, which is a separate concept. Dynamically typed languages quite often use boxed values with an attached runtime tags which are colloquially referred to as "types" even though they have nothing to do with static types.
If you read the article in the context of "type" meaning "static type" it will make more sense.
Are you sure dynamic types have nothing to do with static types? What about compilers for dynamic languages which reason about type, like that if the program calls (symbol-name x), it is inferred that x must hold a string, so that a subsequent (cos x) is inconsistent? Is such a compiler is reasoning about something which has nothing to do with static type?
That's irrelevant; if the program containing (symbol-name x) and (cos x) works in an interpreter but not in the compiler then the compiler is broken. Note that throwing-exceptions/triggering-errors/etc. is "working", since it's a valid form of control flow which the program could intercept and carry on. Aborting immediately (like a segfault) isn't "working". Triggering undefined behaviour also isn't "working", but since most of these languages lack any real defined behaviour, it's assumed that "whatever the official interpreter does" is correct.
Usually, these compilers completely acknowledge that their behaviour is incorrect WRT the interpreter. They embrace this and define a new, static language which just-so-happens to share a lot of syntax and semantics with that dynamic language. For example, HipHop for PHP could not compile PHP; it could only compile a static language which looks like PHP. Likewise for ShedSkin and Python. PyPy even gives its static language a name: RPython.
Hence, these compilers don't show that runtime tags and static types are the same/similar. Instead, they show that sometimes programmers without static types will use tags to do some things that static types are often used for.
The compiler isn't broken.
The interpreter throws an exception, which meets your own definition of "works".
The compiler gives an opinion "x has an inconsistent type". In this manner, the code also "works" in the compiler.
You can still run the program if you like; then that opinion becomes an exception if an input case exercises the code, proving the compiler's opinion right.
Compilers for dynamic languages do not preserve all interpreted behaviors. This is usually explicitly rejected as a goal: users must accept certain conventions if they want code to behave the same in all situations.
For instance, some Common Lisp implementations re-expand the same macros during evaluation, which is friendly toward developers when they are modifying macros. But in compilation, macros are expanded once.
Usually these compilation-interpretation discrepancies are obscure; we are not talking about obscure features here. It's a basic tenet of type checking in a compiler that it will flag things that will throw type-related exceptions at run time. You cannot say that type checking compilers for dynamic languages are simply not allowed because type mismatches are well-defined behavior; that's simply outlandish.
Since dynamic languages only have one static type, static type inference is trivial. So is type-safety: there's no way to cast a value of one type to an incompatible type, since there are no other types.
Inferring tags doesn't work in general: when I say `print x` it doesn't mean that `x` must be a string; I might want Python to raise a "TypeError" exception, so that I can jump to the handler where all the real work takes place. Of course that's bad practice, but exceptions are only "errors" by convention; in the same way that False is only a failure by convention.
"Static type" does not really mean "class of values." That is a common use of static types, but they can also be used for things like tracking what resources are available/released or describing what exceptions a function might raise.
So the proof and the tag are related in that one is about the other, but that does not mean they are the same thing.
It may certainly be a static type in another language; that language which is used to implement an interpreter or virtual machine for the dynamic language. E.g. a Lisp interpreter in C has some "value" typedef which can hold a number, string, symbol, ...
That type is a C type, though, not a Lisp type.
A language can't be confused with the one it compiles into, or which is used to write an interpreter for it.
Utimately, everything is interpreted by a processor, through some machine language representation which uses typeless words.
Just because we have stuffed some knowledge into tags that exist at run time (and chosen suitable machine representations to make that possible) doesn't mean it's a different class of knowledge.