Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for a language to be statically typed?

My understanding is that it means that one can potentially write a program to formally prove that a program written in a statically typed language will be free of a certain (small) subset of defects.

My problem with this is as follows:

Assume that we have two turing complete languages, A and B. A is presumed to be 'type safe' and 'B' is presumed not to be. Suppose I am given a program L to check the correctness of any program written in A. What is to stop me from translating any program written in B to A, applying L. If P translates from A to B then why isn't PL a valid type checker for any program written in B?

I'm trained in Algebra and am only just starting to study CS so there might be some obvious reason that this doesn't work but I would very much like to know. This whole 'type safety' thing has smelt fishy to me for a while.

like image 600
aaronasterling Avatar asked Sep 02 '10 01:09

aaronasterling


People also ask

What does dynamically-typed language means?

Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable's value at the time.

What does it mean that C++ is statically typed?

Static means values are attached to types ('compiled') at compile time. Dynamic means they are attached ('interpreted') at runtime. Since C++ attaches values to types at compile, it follows that C++ is a statically typed language.

Why Java is called statically typed language?

Statically Typed Languages This means that before source code is compiled, the type associated with each and every single variable must be known. Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.

What is statically typed and dynamically-typed language?

If a script written in a statically-typed language (such as Java) contains errors, it will fail to compile until the errors have been fixed. Second, statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.


1 Answers

If you can translate every B' (a program written in B) into an equivalent A' (which is correct if B' is), then language B enjoys just as much "type-safety" as language A (in a theoretical sense, of course;-) -- basically this would mean that B is such that you can do perfect type inferencing. But that's extremely limited for a dynamic language -- e.g., consider:

if userinput() = 'bah':
    thefun(23)
else:
    thefun('gotcha')

where thefun (let's assume) is typesafe for int argument, but not for str argument. Now -- how do you translate this to language A in the first place...?

like image 148
Alex Martelli Avatar answered Oct 11 '22 23:10

Alex Martelli