Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Racket (and Typed Racket) strongly or softly typed?

Tags:

racket

I realize the definitions of "strongly typed" and "softly typed" are loose and open to interpretation, but I have yet to find a clear definition in relation to untyped Racket (which from my understanding means dynamically typed) and Typed Racket on this.

Again, I'm sure its not so cut and dry, but at least I'd like to learn more about which direction leans in. The more research I've done of this the more confused I've gotten, so thank you in advance for the help!

like image 858
no_parachute44 Avatar asked Nov 17 '16 20:11

no_parachute44


3 Answers

One problem in answering questions like this is that people disagree about the meanings of nearly all of these terms. So... what follows is my opinion (though it is a fairly well-informed one, if I do say so myself).

All languages operate on some set of values, and have some runtime behavior. Trying to add a number to a function fails in nearly all languages. You can call this a "type system," but it's probably not the right term.

So what is a type system? These days, I claim that the term generally refers to a system that examines a program and statically[*] deduces properties of the program. Typically, if it's called a type system, this means attaching a "type" to each expression that constrains the set/class of values that the expression can evaluate to. Note that this definition basically makes the term "dynamically typed" meaningless.

Note the giant loophole: there's a "trivial type system", which simply assigns the "type" containing all program values to every expression. So, if you want to, you can consider literally any language to be statically typed. Or, if you prefer, "unityped" (note the "i" in there).

Okay, down to brass tacks.

Racket is not typed. Or, if you prefer, "dynamically typed," or "unityped," or even "untyped".

Typed Racket is typed. It has a static type system that assigns to every expression a single type. Its type system is "sound", which means that evaluation of the program will conform to the claims made by the type system: if Typed Racket (henceforth TR) type-checks your program and assigns the type 'Natural' to an expression, then it will definitely evaluate to a natural number (assuming no bugs in the TR type checker or the Racket runtime system).

Typed Racket has many unusual characteristics that allow code written in TR to interoperate with code written in Racket. The most well-known of these is "occurrence typing", which allows a TR program to deal with types like (U Number String) (that is, a value that's either a number or a string) without exploding, as earlier similar type systems did.

That's kind of beside the point, though: your question was about Racket and TR, and the simple answer is that the basic Racket language does not have a static type system, and TR does.

[*] defining the term 'static' is outside the scope of this post :).

like image 121
John Clements Avatar answered Nov 05 '22 20:11

John Clements


Strongly typed and weakly typed has nothing to do with static or dynamic typing. you can have a combination of them so that you have 4 variations. (strong/static, weak/static, strong/dynamic, weak/dynamic). Scheme (and thus #lang racket) are dynamicaly and stronged typed.

> (string-append "test" 5)
string-append: contract violation
  expected: string?
  given: 5
  argument position: 2nd
  other arguments...:

All it's values have a type and the functions can demand a type. If you were to append a string to a number you get a type error. you need to explicitly cast the number to a string using number->string to satisfy the contract of all arguments being strings. With weakly typed languages, like JavaScript, it would just cast the number to a string so satisfy the function. Less code, but possibly more runtime bugs.

Since Scheme is strongly typed #lang typed/racket is definitely too.

While Scheme/#lang racket is dynamicly typed I'm not entirely sure if #lang typed/racket is completely static. The Guide calls it a gradually-typed language.

like image 2
Sylwester Avatar answered Nov 05 '22 19:11

Sylwester


One of the definitions of "weakly typed" is that when there is a type mismatch between operands instead of giving an error the language will try its best to continue, by coercing the operands from one type to the other or giving a default result.

For example, in Perl a string containing digits will be coerced into a number if you use it in an arithmetic operation:

# This Perl program prints 6.
print 3 * "2a"

Under this definition, Racket would be categorized a dynamically typed (type errors occur at runtime) and strongly typed (it doesn't automatically convert values from one type to the other).

Since Typed Racket doesn't change the runtime semantics of Racket (except by introducing some extra contract checking) it would be just as strongly typed as regular Racket.


By the way, the usual words people use are weak and strong typing. Soft typing might refer to one specific kind of type system that was created during the 90s. It didn't turn out all that well, which is one of the reasons that people come up with the Gradual Typing system that is used in languages such as Typed Racket and Typescript.

like image 1
hugomg Avatar answered Nov 05 '22 20:11

hugomg