by recursivedoubts · 6y ago · 0 comments
When I first came across it in the gosu code base (https://gosu-lang.github.io) I said:
"Wait. That's all you do? You just take the type from the right hand side and then put it on the left hand side?"
"Yep."
"That's type inference?"
"Yep."
It called into question many aspects of my computer science education.
Even in Haskell where global type inference is supported, people end up putting type signatures on all top-level declarations because doing otherwise makes code less readable and makes type error messages way too confusing. Global type inference means a single type error in a single function can be propagated by the compiler to arbitrarily faraway at the call site, or even several layers deep in a call site. Solving such errors resulting from global type inference simply isn't worth the time.
a = b
otherwise I'd always do a : Some -> Type -> MoreType
a = f b
Readability is a reason why. Types are extremely pedagogic tools. I believe when I think about my code, I just think about types. I don't think about computation, just the topology of types. Can I go to Int from X. If not why? This is very different than when I write Python code. So seeing types in code helps me a lot understand what's going on.But the real reason is: errors. It's so much more easier to understand errors when types are explicit.
When I used to write haskell, I'd very often write a function, iterate on it until it did what I want, then infer the type of the function and insert that into the program. Now, I was relatively new to haskell, so was probably less comfortable writing types than reading them (so I assume it was nicer to have the compiler do that for me, eyeball it to check it's what I meant to do, and put it in).
I'm wondering if someone more familiar would think "alright, what type do I want my function to be", fill that in, and then write the type out?
I definitely agree that a program is more readable with types, which is why I always filled them in (I just did it afterwards!)
Usually, the functions with more imperative flavour get their implementation first, and the more functionally flavoured bits get their type first. But that's not always the case.
Sometimes I even write the QuickCheck properties first.
In agda you usually have 2 goals creating a function
* Proving a property of some other object. In this case you have to write the type first since type is the theorem you want to prove, and implementation is the proof.
* Implement a transformation. In this case, as you described you might get away first writing the impl, and then the type, but I personally never even attempt to do that since writing out the type gives me a lot to think about the implementation of the transformation. Once types are explicit, implementation usually ends up being a lot more obvious.
In Haskell, OCaml and eg Rust, the compiler can infer the type of a new variable depending on how you are using it later.
Herb Sutter's Almost Always Auto advice was not good, in my opinion. I still find I actually need to find out the actual type so when I use it it's only typically when the RHS of the assigment mentions the type, static_cast, make_unqiue etc.
I find it saves on typing but not so much for readability. There are different rules if you're writing an STL algorithm versus a final concrete program.
That's what they mean by local variable inference
let map = HashMap::new(); // <K,V> unknown
map.insert("hello", 1);A pure HM type-system is pretty restrictive, so most ML family languages don't have global type inference anyway.
I recently implemented the bidirectional system in Rust from the paper you linked, it was fun (and a challenge!)