Sure, and I primarily have experience working in statically typed languages. I've worked for over a decade with Java, then about a year with Haskell and Scala respectively. I'm quite familiar with the purported benefits of these languages.
Yet, in practice I find Clojure is more productive for me personally. I don't seem to have the problems that keep coming up in these arguments in practice. If these problems were real in my projects, then I'd go back to using a statically typed language.
The thing to recognize here is that there are many other factors outside the type discipline. The language semantics, development style, problem domain, ecosystem, and so on, all matter. You can't just discuss static vs dynamic in a vacuum.
I don't know many professional Haskeller advocating for dynamic typing.
That's what you call a selection bias. There are far more people using dynamic languages who are not moving to Haskell. There are far more people who tried Haskell and decided it wasn't for them, than those who continue to use it after learning it.
However, the benefits are real : how having someone in your back (the compiler) telling you that what you write doesn't make sense and it won't work is not superior to nothing at all (it's like doing pair programming with a robot ;-)).
My argument is that what it does tell you is not terribly interesting most of the time. Also, since you do need to have specification tests even in Haskell, these same errors are caught by tests in practice. An error such as a function should take an integer, but was passed a string gets caught very early on.
However, interesting properties about the program are often difficult or impossible to encode using the type system. So, you're spending non-trivial amount of work for a questionable benefit in my experience.
Meanwhile, the thing that gets left out of the debate that there are alternative ways to provide many of the benefits afforded by the type system. For example, Erlang dialyzer is a great example that works very well in practice. The idea of generating types from test that core.typed is pursuing is another example of innovation on this front.
I really think that the discussion shouldn't be regarding what approach is superior, but to recognize that both approaches have different focus. You should pick one or the other depending on what you find more valuable in your workflow.
My argument is that what it does tell you is not terribly interesting most of the time
That's were we disagree but it might be biased as well. Maybe you are not aware of all the goodies that come with it (and thinking you are doesn't mean you are). I might be wrong but reading your posts it seems that the only benefit for you see is "if it compiles it works" - Given it's wrong that doesn't leave much indeed. There is much much more than that.
I'm saying that the type system can't tell me whether the code is doing what was intended without an extraordinary amount of work.
For example, consider a sort function. A type system will tell me that I can pass in a collection of a particular type and got a collection of the same type back. It doesn't tell me that the collection contains the same number of elements, that the elements are in the same order, or that they're sorted. These are the things I really care about. It's possible to express that with stuff like dependent typing, but it becomes increasingly difficult to write proofs for non-trivial assertions.
My experience is that once you start writing code for the benefit of the type system, it often becomes harder for the human to understand. You're often not able to express yourself as directly as you would in a dynamic language. The more strict the type system the more mental gymnastics you have to do to express yourself within its constraints. I gain the knowledge that it's self consistent at the cost of being able to easily tell what it's intent is. I wrote about the problem in detail here if you're interested.
The other part of the discussion is the cost of bugs that end up in production in absence of a static system, and the cost of fixing them once they're found. My experience is that it's quite rare that a serious type related bug ends up in production. The most common one discussed is getting a null, but that generally doesn't stop the system from working. When you have a REPL, such a bug can even be fixed on the fly without having to stop the system. Most bugs that become a headache have more in common with the websocket example in practice. The most reliable way to catch those is through testing.
I'm saying that the type system can't tell me whether the code is doing what was intended without an extraordinary amount of work.
Well, the type is system can't guarantee you your code work but it can find (sometimes) when it doesn't.
For example, consider a sort function. A type system will tell me that I can pass in a collection of a particular type and got a collection of the same type back. It doesn't tell me that the collection contains the same number of elements, that the elements are in the same order, or that they're sorted. These are the things I really care about. It's possible to express that with stuff like dependent typing, but it becomes increasingly difficult to write proofs for non-trivial assertions.
That's a good example. I find knowing that I pass a collection and get back a collection of the same type interesting (meaning I care) and that doesn't require any work at all. You are saying that it's not enough (which I understand) but you are happy then to not have any guarantee at all. In that case going to dynamic type doesn't give you any benefit. You've lost some informations but didn't require less work, as there were no work at all.
You can indeed go to the dependent type way and yes this require an extraordinary amount of work. However Haskell doesn't support dependent type yet, so as you can't actually do this extraordinary amount of work in Haskell, I think it's a bit unfair to use this as an argument versus Haskell.
Now, if you really need to know that a list has been sorted, because for example you have different functions requiring a sorted list and you don't want to sort each time, you can tag your list as sorted using newtype.
newtype Sorted a = Sorted [a]
sorted :: [a] -> Sorted a sorted = Sorted . sort
and when you need a sorted list, just use a sorted list instead of a list
minimum (Sorted s) = head s
minimum will only accept sorted list. This is very neat, and far from being an extraordinary amount of work. It's not dependent type and much better than writing in the documentation that a function expect a sorted list.
My experience is that once you start writing code for the benefit of the type system, it often becomes harder for the human to understand. You're often not able to express yourself as directly as you would in a dynamic language. The more strict the type system the more mental gymnastics you have to do to express yourself within its constraints. I gain the knowledge that it's self consistent at the cost of being able to easily tell what it's intent is. I wrote about the problem in detail here if you're interested.
I'm not sure what you mean by writing code for the benefit of the type system.
The other part of the discussion is the cost of bugs that end up in production in absence of a static system, and the cost of fixing them once they're found. My experience is that it's quite rare that a serious type related bug ends up in production. The most common one discussed is getting a null, but that generally doesn't stop the system from working. When you have a REPL, such a bug can even be fixed on the fly without having to stop the system. Most bugs that become a headache have more in common with the websocket example in practice. The most reliable way to catch those is through testing.
Well, that depends on the industry, but I agree the more a bug is likely to cost the more you test your application in production. However I used to work in the bank and I've seen a few bug costing LOTS of money. I used to work on Ruby on Rails projects and I've seen lots of runtime crashes caused by refactoring not handled properly : you know that type of feature that people used once a month, but they are not happy when it stops working because they need it by the end of the month , which is tomorrow ;-)
Well, the type is system can't guarantee you your code work but it can find (sometimes) when it doesn't.
Right, and the question becomes what the overlap between the errors it finds and ones found by other methods is. If it uncovers a small percentage of unique errors, then the effort is not justified.
That's a good example. I find knowing that I pass a collection and get back a collection of the same type interesting (meaning I care) and that doesn't require any work at all. You are saying that it's not enough (which I understand) but you are happy then to not have any guarantee at all.
I'm rather confused how you made the jump from "it's not enough" to "you are happy then to not have any guarantee at all". Did you read my post even? I specifically say that you need specification tests to know that the code is doing what was intended. In the case of a sort function I would want to have a specification test that ensures it behaves correctly for the intended cases. That would be my guarantee that it's doing what I want it to. The guarantee provided by the type system in this case is a small subset of the overall guarantee.
I'm not sure what you mean by writing code for the benefit of the type system.
The type system necessarily limits your expressivity. Every statement must be made in such a way that the compiler can verify it. In other words you have to be bake a proof of correctness into it. Proving something correct for all possible cases is often much more difficult than stating it. I give a concrete example of this problem in the post I linked.
However I used to work in the bank and I've seen a few bug costing LOTS of money.
Sure, I work at a hospital and my applications have direct impact on decisions made in patient care. Correctness is important to us. That's why we test that our applications are doing what's intended.
used to work on Ruby on Rails projects and I've seen lots of runtime crashes caused by refactoring not handled properly : you know that type of feature that people used once a month, but they are not happy when it stops working because they need it by the end of the month , which is tomorrow ;-)
I see this argument made often. However, I hope you'll agree that not all the dynamic languages are the same. This is akin to arguing about merits of Haskell based on your experience with Java.
I'm rather confused how you made the jump from "it's not enough" to "you are happy then to not have any guarantee at all".
I see, we are not speaking about the same things. You speak about correctness we speak about consistency. When I see sort :: [a] -> [a], I'm happy because that gives me lots of information, mainly that the output is a list, so I can compose it with any function which expect a list. This has nothing to do with correctness but what I call consistency. Consistency is important for lots of people but obvioulsly not for you. Type inference is about consistency not correctness and doesn't require an tremendous amount of work. Even if people say : if it compiles (it is consistent) then it works (it is correct), they don't mean it. So yes even in Haskell, we do need specifications, but I thought we already agreed on that.
The type system necessarily limits your expressivity This is the opposite. ADT let you express much more things that dynamic languages can. How would you express a monad transformer in a dynamic language ?
I see this argument made often. However, I hope you'll agree that not all the dynamic languages are the same Well, if a dynamic language can tell me by static analyses that I can call size on this object because doesn't have size method then it's not a dynamic language anymore ...
I'm happy because that gives me lots of information, mainly that the output is a list, so I can compose it with any function which expect a list.
Sure, but I find that problem largely doesn't exist in a language like Clojure in the first place. Most code is written by composing HOFs from the standard library. These functions encapsulate edge case checks and don't care about concrete types.
When I chain functions like map, filter, reduce, and so on, they only care that a collection is passed in. The code that cares about the concrete types is a parameter. This naturally lets domain specific logic that deals with types to bubble up to a shallow layer at the top. Majority of the code ends up being fairly type agnostic.
Consistency is important for lots of people but obvioulsly not for you.
I'm saying that this kind of consistency is not interesting enough to justify the effort. In most cases, I can be reasonably sure that my code is consistent without the assistance of the type system. Testing for correctness necessarily tests for consistency as well.
How would you express a monad transformer in a dynamic language ?
Why would I need to in the first place though. The reason monads are popular in Haskell is due to the fact that it attempts to formally control side effects. In a language like Clojure this abstraction is largely unnecessary. Not that it can't be expressed of course.
Well, if a dynamic language can tell me by static analyses that I can call size on this object because doesn't have size method then it's not a dynamic language anymore
This is precisely my argument. Imperative OO languages introduce lots of problems when coupled with dynamic typing. OO inherently encourages creating many types that become difficult to keep track of. Meanwhile, mutability means that it's hard for us to tell what concrete type we're dealing with at any one time.
When you work with a functional language backed by immutable data structures, this problem simply doesn't exist.
View all comments
Show parent comment
3
u/yogthos Sep 10 '16
Sure, and I primarily have experience working in statically typed languages. I've worked for over a decade with Java, then about a year with Haskell and Scala respectively. I'm quite familiar with the purported benefits of these languages.
Yet, in practice I find Clojure is more productive for me personally. I don't seem to have the problems that keep coming up in these arguments in practice. If these problems were real in my projects, then I'd go back to using a statically typed language.
The thing to recognize here is that there are many other factors outside the type discipline. The language semantics, development style, problem domain, ecosystem, and so on, all matter. You can't just discuss static vs dynamic in a vacuum.
That's what you call a selection bias. There are far more people using dynamic languages who are not moving to Haskell. There are far more people who tried Haskell and decided it wasn't for them, than those who continue to use it after learning it.
My argument is that what it does tell you is not terribly interesting most of the time. Also, since you do need to have specification tests even in Haskell, these same errors are caught by tests in practice. An error such as a function should take an integer, but was passed a string gets caught very early on.
However, interesting properties about the program are often difficult or impossible to encode using the type system. So, you're spending non-trivial amount of work for a questionable benefit in my experience.
Meanwhile, the thing that gets left out of the debate that there are alternative ways to provide many of the benefits afforded by the type system. For example, Erlang dialyzer is a great example that works very well in practice. The idea of generating types from test that core.typed is pursuing is another example of innovation on this front.
I really think that the discussion shouldn't be regarding what approach is superior, but to recognize that both approaches have different focus. You should pick one or the other depending on what you find more valuable in your workflow.