by naasking · 7y ago · 0 comments
Unityped, as in, they have only one type. You can assign any type to any variable, so all values have a single type.
> Also, division by zero is an runtime operation error, it is not really a type error. The division operation doesn't fail because it violated the type system, it fails because you can't mathematically divide by zero.
That is a type error in some type systems. The general notion of a type is a logical proposition. "Non-zero number" is a proposition and so you can represent this with types. The type of division is then `Num -> (Num != 0) -> Num`.
> Dynamic languages are untyped languages like javascript and racket.
This is not true. All languages are typed, either with rich types, or they are unityped (like the lambda calculus or JS).
Machine language is typed, and the fundamental types are (roughly) word and double. These are the registers of machine language, which you can roughly think of as variables in programming languages.
Furthermore, variables potentially holding any value doesn't mean they have no type. It is just a potential. A variable effectively has that type which is actually put into it.
If a given variable is always either a string or nil, then that's what it's type is; the sum type of null (the type that contains nil) and the string type.
It may be the case that in a given language implementation, this fact about the variable is not calculated; but it's still a fact.
Neglecting to analyze the types of variables (and other nodes in the program) is an implementation quality/depth choice, not a kind of type system design.
I said they have one type, I didn't say they have no type.
(If they have one type, what is that type? And can values be of that type?)
So no, JS is still not untyped, merely dynamically typed. An example of a language that also has only one data type for value would be something like Tcl, where the only type is a string - and everything else is just an optimized way to store certain kinds of strings, that could be removed without affecting semantics of any program.
It may be the case that no such thing is actually analyzed and the implementation always retains the potential to reassign that variable; that doesn't change the mathematical fact that the set of values which that variable is fixed.
That the implementation is not interested in examining the type of a variable (let alone checking for consistency with the rest of the program) shouldn't be mistaken for the absence of type. There is a mathematical fact about the program that isn't constrained by the quality and depth of its implementation.
data JSObj
= Num Double
| Str String
| Obj (Hashtable String JSObj)
| True
| False
| Nil
| UndefinedBehaviour is based on values, not types. 1+2 will print out a different value than 1+1, but these expressions have the same type (typically).
> So no, JS is still not untyped, merely dynamically typed.
I said JS was unityped, not untyped.
If there are no distinguishable properties ("one type") then untyped vs unityped seems a distinction without a difference.
Now contrast with something like Scheme or JS. Clearly there is richer underlying structure than mere lambdas, but since every value is still interchangeable (a global sum type) I think this suggests a "unityped" label as a meaningful distinction from the untyped case.
So I would amend my original comment to move the lambda calculus out of unityped and into untyped, and possibly Tcl as well since everything is conceptually a string there.
'The major difference between BCPL and ALGOL is that all ALGOL variables are declared with data-types (integer, real, boolean, string, array, procedure, label, pointer, etc.), whereas all BCPL variables have the same data-type: a 16-bit number.
In ALGOL, the meaning of an expression is dependent both on its context and on the data-types of the entities involved, and only expressions with certain data-types may appear in a given context.
In BCPL, any expression may be used in any context; the context alone determines how the 16-bit value of the expression is interpreted. BCPL never checks that a value is "appropriate" for use in a given way.'
http://www.bitsavers.org/pdf/xerox/alto/bcpl/BCPL_Reference_...
( I haven't been able to figure out what happens on divide-by-zero:
The division operator / yields the correct result if E1 is divisible by E2; it is otherwise implementation dependent but the rounding error is never greater than 1.
Almost all languages. There is one language that can be typed without a type, the empty language.
Since the empty language contains no programs, it follows that "Untyped Programs Don't Exist".