5 ms·
I smiled when I read this: Haskell's type system literally allows the compiler to infer the correct code to run based on its type context It literally has ove
by barrkel 17y ago
I smiled when I read this:
Haskell's type system literally allows the compiler to infer the correct code to run based on its type context
It literally has overloading! Imagine that!
- ekiru 17y agoHow many other programming languages have a type system that supports overloading on both parameter types and the desired return type?
- barrkel 17y agoLanguages with operator overloads, including typecast ops.
- silentOpen 17y agoMost languages with operator overloads don't have inferred static type-checking woven through them, though.
- barrkel 17y agoOn the contrary, almost all statically typed languages with overloaded operators use type inference to type subexpressions; it's how they do type checking and produce type errors. Type inference for named values, or locations, or functions is one thing; but type inference of expressions is everywhere. If you consider '+' as overloaded on ints, floats etc., even C uses type inference to infer a type for '4 + 0.5'.
- silentOpen 16y agoI don't know if I'd call that "inference" so much as coercion, precedence and propagation. The exciting thing about Haskell type classes is precisely the integration with the type system and its modified HM type inference -- if you don't think the Haskell type system is cool, you probably won't be impressed much by its fairly elegant support for an abstraction that lets you overload operators, perform multidispatch, enforce contracts, and define the very laws of mathematics. Perhaps if you don't know Haskell, you should learn Haskell to understand what the article is saying. Edit: This might help: http://www.haskell.org/tutorial/classes.html http://www.haskell.org/tutorial/classes.html
- barrkel 16y agoI already know Hindley-Milner type inference, and I do know Haskell well enough to understand typeclasses, monads, etc. I also believe that type inference is of limited practical application at scale. On a local level, internal to a module or function, it can make a lot of sense, but it's at risk of underspecification at the module interface level. For example, when you modify function bodies, you may inadvertently add more constraints to an inferred type (e.g. use an operator or function defined on a typeclass not previously brought in), and consequently break clients of the module.
- silentOpen 16y agoThis is definitely something that any interface author must deal with and is very closely related to the more OO-specific skill of "good interface design". It is important to include in an interface those types and type constraints that effectively and efficiently describe your problem domain. OCaml (and potentially Haskell, though I haven't used this feature in Haskell) ameliorates this problem by giving the programmer the facility to specify (or generate and edit based on inference) module signatures that are then checked against. With this approach, I would argue that type inference actually makes your large-scale code more robust and easier to modify than without it due to the automatic verification of module signature. If you inadvertently introduce extra type constraints to your module's interface, the type-checker will tell you and save you from releasing a module that is incompatible with previous versions of the same module. Without type inference, you're either annotating types everywhere (looking at you Java...) or waiting until runtime (or production!) to find out you've made an error (e.g. perl, python, ruby and on and on). I really don't see how type inference and its associated program analysis and verification is anything but good.
- chc 17y agoTo give a concrete example: The printf function in Haskell takes on the responsibilities of both the traditional printf (printing to the screen) and sprintf (generating a formatted string). Haskell knows which it's supposed to do based on whether the return value is used in an IO context or as a string.
- jmillikin 17y agoWhich is, when you think about it, not a good idea. If 'sprintf' is defined, then 'printf' ought to be simply 'putStr . sprintf'.
- chc 17y agoBut sprintf is not defined in Haskell, nor is it really needed. printf returns a typeclass that includes both 'String' and 'IO a'.
- jmillikin 17y agoMy point is that there should be no typeclass; there's no reason to combine IO and formatting in the same function, and doing so makes it more difficult to read and modify code.
- loewenskind 17y agoAgreed. This is something I dislike about Haskell. After seeing the regex class and it tons of contexts I was just thinking "didn't they learn anything from perl?".