News for hackers. Stuff that matters.
Re:Dynamic type systems are not inherently more open by amelius on Sunday January 19, 2020 @08:54AM (#22091425)
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.
Re:Dynamic type systems are not inherently more open by chongli on Sunday January 19, 2020 @09:11AM (#22091502)
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).

Re:Dynamic type systems are not inherently more open by pron on Sunday January 19, 2020 @10:28AM (#22091892)
Type 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.
Re:Dynamic type systems are not inherently more open by PhineasRex on Sunday January 19, 2020 @01:01PM (#22092794)
If 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.

Re:Dynamic type systems are not inherently more open by pron on Sunday January 19, 2020 @03:51PM (#22093979)
> 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.

Re:Dynamic type systems are not inherently more open by firethief on Sunday January 19, 2020 @01:30PM (#22092984)
If 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 :)
Re:Dynamic type systems are not inherently more open by pron on Sunday January 19, 2020 @03:49PM (#22093967)
No, you can know -- and even prove -- that at a certain callsite, a Maybe would be a Just, and if you were using a dependent type system, you can prove that in the type system (although it probably won't be automatically inferred -- you may have to work hard for the proof), but Haskell's type system is too weak to express such properties and such proofs, at least in the general case.
Re:Dynamic type systems are not inherently more open by hopia on Sunday January 19, 2020 @10:00AM (#22091740)
On what editor setup you get this kind of functionality with Haskell? I mean the auto-insert of the inferred types.
Re:Dynamic type systems are not inherently more open by chongli on Sunday January 19, 2020 @10:10AM (#22091795)
I’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

[2] https://github.com/eagletmt/ghcmod-vim

[3] https://github.com/haskell/haskell-ide-engine

Re:Dynamic type systems are not inherently more open by hopia on Sunday January 19, 2020 @10:19AM (#22091842)
Thanks 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.

Re:Dynamic type systems are not inherently more open by jose_zap on Sunday January 19, 2020 @02:39PM (#22093469)
Yes, 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.
Re:Dynamic type systems are not inherently more open by danidiaz on Sunday January 19, 2020 @10:14AM (#22091817)
Re:Dynamic type systems are not inherently more open by hopia on Sunday January 19, 2020 @10:20AM (#22091851)
Template Haskell not working on ghcide has sadly been holding me back with it. I've heard great things about it otherwise
Re:Dynamic type systems are not inherently more open by jose_zap on Sunday January 19, 2020 @02:39PM (#22093474)
You can use HIE. It supports template Haskell
Re:Dynamic type systems are not inherently more open by pwm on Sunday January 19, 2020 @02:31PM (#22093415)
Recent ghcide can do it now as well as https://github.com/mpickering/apply-refact for hlint
Re:Dynamic type systems are not inherently more open by zitterbewegung on Sunday January 19, 2020 @08:58AM (#22091439)
Using Type inference[1] on a language could be able to figure out types in most situations.

[1] https://en.wikipedia.org/wiki/Type_inference

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @09:50AM (#22091688)
Yes, but "Type inference has usability problems".

http://web.eecs.utk.edu/~azh/blog/typeinference.html

Re:Dynamic type systems are not inherently more open by leshow on Sunday January 19, 2020 @12:46PM (#22092695)
I'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.

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @12:50PM (#22092730)
I 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.

Re:Dynamic type systems are not inherently more open by sweeneyrod on Sunday January 19, 2020 @04:40PM (#22094305)
If type inference damaged readability, we would expect companies/projects that use languages such as OCaml and Haskell where type inference is possible but not mandatory to have policies against it in their style guides. In my experience with OCaml, the widely agreed on style is to only annotate types of functions that are part of some public interface (although since this annotation is necessary to define the public interface of a library, it's not even necessarily true that type inference impairing readability is the primary reason for this).

Also, if lack of type annotations were a common problem, you would expect Java programmers who switch to Python to frequently want to annotate all their types. But as far as I know this is not the case: even now that mypy is becoming popular, people only annotate the types that it can't infer.

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @05:19PM (#22094527)
Companies do have policies against using type inference (e.g., the use of var in C#).

But how would companies/people know if it damaged readability? There has been little research on this (a few studies are currently in peer review!) and our intuition about such things is often inaccurate. There are so many other confounding factors that it is hard to isolate just type inference/annotations.

Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @06:36PM (#22094947)
> Companies do have policies against using type inference (e.g., the use of var in C#).

That's a language that historically has not had type inference. Any examples of companies mandating type annotations in historically type-inferred languages?

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @06:42PM (#22094987)
Yes! Any company that has adopted MyPy or TypeScript often have policies for this. One team I spoke to at Facebook said that any new code should have type annotations. I believe Dropbox had a nice blog post about new code should have type annotations too. It is hard to get these details from companies/teams (and be allowed to talk about it).
Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @07:32PM (#22095221)
That's because (a) these are not historically type-inferred languages–runtime typechecking doesn't count as type inference–and (b) the gradual typechecking approaches like Mypy and TypeScript often have a lot of type inference issues which force people to try to overcome them with manual annotations.

In languages with even reasonably good type inference you'll find that the idiom is not to annotate types at the very least inside function bodies. And in OCaml, which has principality of type inference i.e. the types it infers cannot be overridden and corrected by manual annotation, the idiom is to not annotate implementation files at all.

Re:Dynamic type systems are not inherently more open by sweeneyrod on Sunday January 19, 2020 @06:50PM (#22095029)
Do they mandate that only function types (and any ambiguous ones) should be annotated, or that all types (i.e. including those of local variables which mypy can infer) should be? I strongly suspect the former, which implies that they view type annotations more as a necessary evil to allow type checking rather than a benefit in their own right. After all, there was nothing stopping people putting "type annotations" in Python 2.5 code if you wanted, by writing `x = get_thing_count() # int`. But AFAIK no-one did that until annotations could actually be used to check type safety.
Re:Dynamic type systems are not inherently more open by leshow on Saturday January 25, 2020 @12:23PM (#22147306)
Typescript will infer the 'any' type by default in a lot of cases, that's the reason teams will mandate type annotations. It's really just not the same thing. These are bolt on type systems to dynamic languages where if you fail to put a type annotation you're actually losing information. That's not the case in Haskell, type inference causes no loss of precision for types.
Re:Dynamic type systems are not inherently more open by pwm on Sunday January 19, 2020 @02:44PM (#22093513)
I 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.
Re:Dynamic type systems are not inherently more open by leshow on Sunday January 19, 2020 @03:56PM (#22094021)
Agree 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.

Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @03:19PM (#22093746)
In practice, not really. A good type inference system like Hindley-Milner infers the most general type of any expression you give it. This usually makes your code more polymorphic i.e. generic than if you would have manually written down the types.

You talk about cognitive load of looking up type info but there's also a cognitive load to having to parse every type annotation over and over again whenever you read the code. And since code is read more often than it's written, that's really just optimizing for the wrong load. As you re-read a codebase over time you'll get familiar with its types. And then the annotations start to get in the way. This is in fact one of the reasons people prefer dynamic typing.

> My response? Go refactor your hideous code.

To what? Would be very interesting to see your suggested refactor of that code.

> Before every language designer blindly accepts type inference as a good thing, I propose that some research be conducted.

What would be the research question(s) specifically?

Re:Dynamic type systems are not inherently more open by amelius on Sunday January 19, 2020 @09:06AM (#22091480)
Yes, 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).
Re:Dynamic type systems are not inherently more open by yakshaving_jgt on Sunday January 19, 2020 @09:40AM (#22091640)
In my experience (currently writing Haskell professionally, previously writing Ruby professionally) prototyping is easier in Haskell.

How many systems have you prototyped in Haskell?

Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:04PM (#22093187)
> 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.

Re:Dynamic type systems are not inherently more open by rwmj on Sunday January 19, 2020 @09:18AM (#22091534)
When 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.
Re:Dynamic type systems are not inherently more open by hnra on Sunday January 19, 2020 @09:37AM (#22091622)
I Haskell you simply put a "_" in the annotation and the compiler suggests the correct type.
Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:07PM (#22093218)
You can also do this with values, asking "what goes here?" - it's great.
Re:Dynamic type systems are not inherently more open by andrepd on Sunday January 19, 2020 @10:51AM (#22092017)
Alternatively, merlin can tell you the type of the expression under your cursor, besides also flagging type mismatches like your situation.
Re:Dynamic type systems are not inherently more open by hopia on Sunday January 19, 2020 @10:13AM (#22091808)
Productivity 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.
Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:06PM (#22093204)
Right, 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.
Re:Dynamic type systems are not inherently more open by pwm on Sunday January 19, 2020 @02:51PM (#22093546)
It’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.
Re:Dynamic type systems are not inherently more open by dllthomas on Tuesday January 21, 2020 @05:04PM (#22112161)
Yeah, 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.

Re:Dynamic type systems are not inherently more open by zitterbewegung on Sunday January 19, 2020 @09:43AM (#22091661)
What 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.
Re:Dynamic type systems are not inherently more open by duckerude on Sunday January 19, 2020 @11:35AM (#22092264)
I don't have experience with it, but pytype can do that: 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

As far as I know it's less powerful since mypy isn't really built for unannotated code and pytype is.

Re:Dynamic type systems are not inherently more open by pron on Sunday January 19, 2020 @10:32AM (#22091915)
Unless 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.

Re:Dynamic type systems are not inherently more open by IggleSniggle on Sunday January 19, 2020 @02:27PM (#22093376)
TypeScript 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.
Re:Dynamic type systems are not inherently more open by ken on Sunday January 19, 2020 @09:31AM (#22091587)
You mean a compiler?