7 ms·
Yes. Perhaps this is an indication that we actually need automated type annotation. E.g. you initially write/prototype your program in a dynamically typed form,
by amelius 7y ago
Yes. Perhaps this is an indication that we actually need automated type annotation. E.g. you initially write/prototype your program in a dynamically typed form, then you click a "magic" button, and a tool converts your program into statically typed style.
- zitterbewegung 7y agoUsing Type inference[1] on a language could be able to figure out types in most situations. [1] https://en.wikipedia.org/wiki/Type_inference https://en.wikipedia.org/wiki/Type_inference
- amelius 7y agoYes, some programming languages take this approach but I don't think that prototyping is as easy in such languages as it is in a dynamically typed language (e.g. Haskell vs Python).
- rwmj 7y agoWhen writing OCaml, if I'm unsure of the type I set the type to some impossible value, compile it, and the compiler tells me what the type really is.
- hnra 7y agoI Haskell you simply put a "_" in the annotation and the compiler suggests the correct type.
- dllthomas 7y agoYou can also do this with values, asking "what goes here?" - it's great.
- andrepd 7y agoAlternatively, merlin can tell you the type of the expression under your cursor, besides also flagging type mismatches like your situation.
- yakshaving_jgt 7y agoIn my experience (currently writing Haskell professionally, previously writing Ruby professionally) prototyping is easier in Haskell. How many systems have you prototyped in Haskell?
- dllthomas 7y ago> prototyping is easier in Haskell That's very much my experience, as well. Assuming - and it's a very big assumption - that your use case has equivalent library support.
- zitterbewegung 7y agoWhat would be neat is if you could take a Python program and run type inference on the program and it would output a type annotated version of the code.
- duckerude 7y agoI don't have experience with it, but pytype can do that: https://github.com/google/pytype https://github.com/google/pytype After typechecking it leaves provisional stubs in .pytype/pyi/, and it has a tool to merge them into the original code. mypy has stubgen: https://mypy.readthedocs.io/en/latest/stubgen.html https://mypy.readthedocs.io/en/latest/stubgen.html As far as I know it's less powerful since mypy isn't really built for unannotated code and pytype is.
- hopia 7y agoProductivity can be really subjective. Those that are used to modeling the problem domain with the type system find that their most productive approach, even with prototyping.
- dllthomas 7y agoRight, and the type system is such a big help in refactoring - especially in small projects - that you don't have to be all that right in your first N cuts at the model. You can write code and find out where you're wrong, and fix it fast.
- pwm 7y agoIt’s even more help in larger projects :) The dividends of type-checher aid refactoring really start paying off once the project is above a a certain size/age.
- dllthomas 7y agoYeah, I think I was unclear. Given a need to refactor, it's a bigger help on a bigger refactor. But a large refactor on a large project is still a pain. By the time you get there, you still want your model stable and/or flexible enough that you don't need to do that much. On a small enough project, every refactor is easy enough that you can lean into it. Make a bunch of assumptions you're not confident about, setting up the types so you're sure you'll know what needs fixing when they're wrong. Then you test those assumptions as you write - and run - more code.
- azhenley 7y agoYes, but "Type inference has usability problems". http://web.eecs.utk.edu/~azh/blog/typeinference.html http://web.eecs.utk.edu/~azh/blog/typeinference.html
- leshow 7y agoI'm curious why you quote this like it's a fact when it's an opinion. The understanding of type inference used as the basis for making the argument in that blog post is superficial. Type inference doesn't just allow you to leave off type information from variables. A good inference engine will infer the most generic type for variables (and functions!), it's a discovery that you can query the compiler for in order to do type driven development. A full rebuttal of this blog post would take much longer, but suffice to say inference does the exact opposite of what they are claiming; it increases readability and usability.
- azhenley 7y agoI quoted the title of my blog post. Do you have any evidence for your claim that it increases readability or usability? A few human-subjects studies from different labs have recently been ran on this and the findings mostly agree with my opinions. Papers forthcoming.
- pwm 7y agoI don’t agree with readibility (there are pros and cons, ie. terser code vs. implicit types) but, at least in Haskell, it improves usability by inferring the most generic type to your expression.
- leshow 7y agoAgree on what? "Type inference" is a broad feature, feel free to post the resources. edit: In Haskell it goes beyond just locally inferring variables, it enables a new type of development with typed holes that feels more like a back and forth conversation with the compiler. I don't see how "I can leave the types off my C# variables" has anything to do with that.
- 7y ago
- chongli 7y agoThis 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.
- ken 7y agoYou mean a compiler?
- pron 7y agoUnless your type system is very weak -- and in effect, an untyped language can be considered to already be doing what you said only in a type system that has a single type -- this is generally impossible (halting problem and its extensions). So you have the choice of something that's not very useful or something that's impossible -- take your pick. Type inference, as others have suggested, does not do what you want. If your program is well-typed according to a particular type system which you must have in mind while writing the program, they'll save you from writing explicit type annotations, but they cannot come up with "correct" type for a correct program that's not written for that particular type system.
- IggleSniggle 7y agoTypeScript can come close to this, especially with the latest release with Assertion support. You make assertions about your code, and the downstream types are inferred based on your assertions. Thus, the runtime behavior of your code gives you progressively enhanced type-checking. If you do these assertions at the borders, your "correctness" is dependent on the strength of your runtime assertions.