5 ms·
This already exists in Haskell. Firstly, Haskell has type inference so you can write entire programs without any type annotations. Second of all, since type ann
by chongli 7y ago
This already exists in Haskell. Firstly, Haskell has type inference so you can write entire programs without any type annotations. Second of all, since type annotations actually help to document your code, it’s recommended as good Haskell style to annotate all top-level definitions.
To aid you in the latter task, you can press a key in your editor to fetch an automatically-inferred type for the name under the cursor and insert it into your code, then refine it as necessary if you find it to be too general (inference always gives the most general type possible).
- hopia 7y agoOn what editor setup you get this kind of functionality with Haskell? I mean the auto-insert of the inferred types.
- chongli 7y agoI’ve been out of the game for a number of years but I used something called ghc-mod [1] with its associated vim plugin [2] to get this functionality. Moving forward, it seems that all of the effort has moved over to Haskell IDE Engine [3]. It looks like this exact feature hasn’t been brought over yet but it is planned. In the mean time you could still use ghc-mod though. [1] https://github.com/DanielG/ghc-mod https://github.com/DanielG/ghc-mod [2] https://github.com/eagletmt/ghcmod-vim https://github.com/eagletmt/ghcmod-vim [3] https://github.com/haskell/haskell-ide-engine https://github.com/haskell/haskell-ide-engine
- hopia 7y agoThanks for the tip! Haskell IDE engine is exactly what I've been using til now and would've been very pleased to find out it had such a convenient feature. Ghcide is on my list to try next as it's gotten a lot of attention lately.
- jose_zap 7y agoYes, it already has a code command to automatically add top level type annotations. The caveat I that it can only work if you add -Wall to your compilations flags.
- danidiaz 7y agoIt seems that "ghcide" can do this: https://twitter.com/chris__martin/status/1218331415869190144 https://twitter.com/chris__martin/status/1218331415869190144 https://github.com/digital-asset/ghcide https://github.com/digital-asset/ghcide
- pwm 7y agoRecent ghcide can do it now as well as https://github.com/mpickering/apply-refact https://github.com/mpickering/apply-refact for hlint
- pron 7y agoType inference does not automatically "typify" your code. The type system -- whether the type annotations are explicit or the types are inferred -- determines which expressions are "well-formed" (i.e. syntactically correct), and so you can't write arbitrary code. E.g., in Haskell, you can know for a fact that at one call site a Maybe instance is Just, but whether you annotate your types or rely on inference, the type system will still force you to match on the constructor even though you don't really have to from a correctness perspective.
- PhineasRex 7y agoIf you're talking about the fact that you have to unbox the Maybe, that's not really related to type inference or static types but how "null" values are represented in the language. If you're talking about exhaustiveness checks, you can disable those and use `-fdefer-type-errors` to compile programs which don't type-check. Although I will say that most of the time when you have a Maybe instance that you know is Just, it means you're branching in the wrong part of your program.
- pron 7y ago> If you're talking about exhaustiveness checks, you can disable those and use `-fdefer-type-errors` to compile programs which don't type-check. Right, but if you were using, say, Idris, the program would type check. It's just that Haskell's type system is too weak. Disabling type checking is not what the question was about. The program is type-correct, yet not in Haskell's particular system. > Although I will say that most of the time when you have a Maybe instance that you know is Just, it means you're branching in the wrong part of your program. Perhaps, but that doesn't change the fact that if you write it in some way that is perfectly correct, Haskell's particular type system will still not accept your program.
- firethief 7y agoIf the reason you know your Maybe T is a Just _ is that you tested it locally: in any language that favors the Maybe pattern, the idiomatic way of performing that test makes the result available to the type system (destructuring / combinators / 'nads). On the other hand, if you know your Maybe is a Just because of non-local information, you will eventually turn out to be wrong :)