我正在学习 Haskell,并试图掌握 Haskell 类型系统的工作原理,重新确定事物的类型:动态、静态、手动设置、推断?
我懂一点的语言:
int i;
, 强类型(从字符串中减去整数是编译错误)。典型的静态类型系统。 强类型(从
int
中减去 str
会引发异常)。 Haskell 的类型系统真的值得描述为“静态的”吗?我的意思是自动类型推断不是(经典)静态类型。
最佳答案
Does Haskell's type system really deserve description "static"? I mean automatic type inference is not (classic) static typing.
类型推断是在编译时完成的。在编译时检查所有类型。 Haskell 实现可以在运行时删除类型,因为它们具有类型安全的编译时证明。
所以说 Haskell 有一个“静态”类型系统是正确的。 “静态”是指编译时和运行时之间阶段区别的一方面。
引用罗伯特哈珀的话:
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.
来自 Practical Foundations for Programming Languages , 2014 年。
在这种描述下,Haskell 是一种具有静态类型系统的安全语言。
作为旁注,我强烈推荐那些有兴趣学习理解编程语言及其特性的基本技能的人看上面的书。
关于haskell - Haskell 类型系统的性质 : static/dynamic, 手动/推断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035893/