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.
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.
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.
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.
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.
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...?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With