tags:

views:

135

answers:

7

This question arises from this question.

One saying is that if a language has the type string or float, etc, then it is typed. But what if it also fits Wikipedia's explanation of untyped:

http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages

an untyped language, such as most assembly languages, allows any operation to be performed on any data, which are generally considered to be sequences of bits of various lengths. High-level languages which are untyped include BCPL and some varieties of Forth.

so what if the language allows operations on any data, but just return "undefined" or "NaN", can we say it is untyped even though it has string and float, etc?

Are string, array, and object not considered to be "sequences of bits of various lengths"? Does the "sequences of bits of various lengths" actually means "sequences of bits of some pre-defined lengths on the system (such as 32 bit, 64 bit, etc)"?

A: 

I would probably have a hard time saying that a language is untyped if it returned "undefined" or "NaN" for operations on certain types. To me, that would mean the specified operation is not applicable to that data type, which clearly would mean that the language is not supporting the operation on that data type.

In contrast, most assembly languages are considered untyped, because at that level, the language really deals with everything you would consider a "data type" as just data. All the operations done in assembly give no regard to what type of data it's actually dealing with. For example, MOV or MOVE just moves data from one location to another, ADD is a bitwise operation, so even there you're just adding two registers' binary data together. Everything at this point is considered bitwise arithmetic, so everything is treated as such.

Therefore, any language that calls itself "untyped" would have to comply to this level of operation. By returning "undefined" or "NaN" the language is really stating "I don't do that".

Joseph
+2  A: 

If the languages has types like "string" and "float" then it is typed. Whether it is statically typed or dynamically typed is another issue. Untyped languages work with memory directly whereas typed languages have an abstraction layer over memory (the types themselves).

so what if the language allows operations on any data, but just return "undefined" or "NaN", can we say it is untyped even though it has string and float, etc?

This sounds like a dynamic, weakly-typed language that relies on duck typing to operate on types, not directly on memory. What does "NaN" mean in a language that has no types? If the interpreter/compiler of the language you are using is capable of determining that a value found at a memory address is "not a number" then that language is type-aware. An untyped language doesn't care about types at all - it simply manipulates memory directly.

Andrew Hare
You are using a definition of "typed" ("has types") different from the one in Wikipedia ("the specification of every operation defines types of data to which the operation is applicable, with the implication that it is not applicable to other types.") This is just a comment, I personally haven't used the concept "typed" in my life.
Daniel Daranas
@Daniel Actually I don't think Andrew's answer is in contrast to the wiki. When you deal with untyped languages, the data you work with is never assigned a type, because there isn't one. I think that's the point he's trying to get at. Any typed language, therefore, has to have an abstraction layer in order to define what the "data types" are.
Joseph
A: 

Some would call that "loosely typed". For floats and ints, most compilers/languages will upcast the int to a float in order to do the calculation (since the real numbers contain the integers).

HVS
A: 

I would say it's weakly typed, in that each value has a type but there may be implicit conversion for most (or all) types.

Niki Yoshiuchi
A: 

An untyped language treats any given chunk of data as bits that are given meaning by the operation. For example, integer addition would treat these bits as an integer, and print would treat them as a character string. In these languages, making sure the operations are meaningful is the programmer's responsibility. (In Forth, for example, while there are usually data types in memory, what's on the stack, which means what's actually being manipulated, is stack entries rather than integers or characters or whatever.)

Therefore, if every operation will just take the bit patterns and run with them, the language is untyped. If the operations do different things to the bits (adding 32-bit integers and 32-bit floats, for example, do much different things) or if not all operations are defined on all data, then it would be typed. If it's typed, but it's easy to treat data as a different data type, then we'd say it's weakly typed.

You don't give enough context to know. A floating-point operation might well return "NaN" on arbitrary bit patterns, and a print operation might well return "undefined" if it can't parse its input as valid Unicode characters, and so this could happen in an untyped language. If there's circumstances on which an operation will return "NaN" or "undefined" no matter what the bit pattern of the data, the language is typed.

David Thornley
A: 

Consider casting has to be done. What if it an operation that receives a typed variable and returns binary (which is also a type?), and vice versa? You can especially see it in C#, which is a long way from C's (void*) (pointer to anything). Also, consider polymorphism - is an operation on Object considered untyped?

All in all, it's pretty semantic. I doubt there are more than a few languages that would not allow casting to bytes, because it is very useful at times. However, using any operation on any data would be mostly logically wrong.

Also, NaN is part of the float standard, it is represented by a number bigger than INF if I'm not mistaken.

Nefzen
A: 

I think that the difference is that wikipedia is giving an OPERATIONAL definition whereas most people are giving a SEMANTIC definition.

By those terms I mean that an operational definition is dependent entirely on how the system behaves as a black box, but a semantic definition can depend on some internal state.

Javascript, for example, HAS types. You can pretty much always get around them, so OPERATIONALLY, it may appear that it's an untyped language, but SEMANTICALLY, the concept of types is part of the language.

I'm sure that there are better terms than the ones I just used, but I think you get the idea.

Brian Postow