I'm learning Haskell and trying to grasp how exactly Haskell type system works re working out what is the type of the thing: dynamic, static, set manually, inferred?
Languages I know a bit:
C, Java: set manually by a programmer, verified at compile time, like int i;
, strong typing (subtracting integer from a string is a compile error). Typical static type system.
Python: types inferred automatically by runtime (dynamic typing),
strong typing (subtracting int
from a str
raises exception).
Perl, PHP: types inferred automatically at runtime (dynamic typing), weak typing.
Haskell: types often inferred automatically at compile time (either this or type is set explicitly by a programmer before compile time), strong typing.
Does Haskell's type system really deserve description "static"? I mean automatic type inference is not (classic) static typing.
Does Haskell's type system really deserve description "static"? I mean automatic type inference is not (classic) static typing.
Type inference is done at compile time. All types are checked at compile time. Haskell implementations may erase types at runtime, as they have a compile-time proof of type safety.
So it is correct to say that Haskell has a "static" type system. "Static" refers to one side of the phase distinction between compile-time and runtime.
To quote Robert Harper:
Most programming languages exhibit a phase distinction between the static and dynamic phases of processing. The static phase consists of parsing and type checking to ensure that the program is well-formed; the dynamic phase consists of execution of well-formed programs. A language is said to be safe exactly when well-formed programs are well behaved when executed.
From Practical Foundations for Programming Languages, 2014.
Under this description Haskell is a safe language with a static type system.
As a side note, I'd strongly recommend the above book for those interested in learning the essential skills for understanding about programming languages and their features.