r/Clojure • • Oct 27 '17

Clojure vs. The Static Typing World

http://www.lispcast.com/clojure-and-types
77 Upvotes

24 comments sorted by

13

u/Savo4ka Oct 27 '17

Very constructive article by Eric Normand on all the Clojure vs Static Languages debate that ensued after the latest Conj keynote by Rich Hickey.

10

u/IvanMalison Oct 27 '17

Not a huge deal, but the author claims that ADT stands for Abstract Data Type, when it actually stands for Algebraic Data Type.

7

u/SimonGray Oct 27 '17

ADT stands for a bunch of different things in the computer world.

11

u/IvanMalison Oct 27 '17

Haha, okay, sure, but he is clearly making reference to Algebraic Data Types in the article.

3

u/SimonGray Oct 27 '17

Ah yeah, I see what you mean.

6

u/[deleted] Oct 27 '17

Re: "Maybe String":

I'm kind of surprised this doesn't get mentioned in any of the static typing articles I've read:

Nil makes no distinction between "we know that it doesn't have a value", and "it may have a value but we don't know what it is", and "it has a value but we don't know what it is".

For example: {:first-name "Joe", :last-name "Blow", :middle-name nil}

Are we saying "We know Joe has no middle name"? Or are we saying "We don't know Joe's middle name, or even if he has one", or "We know Joe has a middle name but we don't know what it is".

This seems important. If you have the above record in your program, and later Joe fills out a form and updates his middle name to nil, then you've got new information, but the record is exactly the same! Joe has told the system "I have no middle name" but the system can't record it anywhere. You can kludge it with an empty string or some constant, of course. But the point is that "nil" is ambiguous.

8

u/[deleted] Oct 27 '17

It depends on your conventions, but to me, this means that Joe has no middle name. I think of maps in terms of assertions of knowledge; {:first-name "joe"} is an assertion that :first-name has the value "joe" (in whatever context, at whatever point in time). The absence of :middle-name means that we know nothing, and {:middle-name nil} means we know it's nil.

2

u/[deleted] Oct 27 '17

Ok that helps a bit, but it still doesn't let you assert knowledge that joe has a middle name (but not what it is).

I suppose you could do {:middle-name true} or something.

This example is kind of contrived, but imagine you're querying your db for "count of people that have middle names". You want to include Joe in the count if you know he has a middle name, even if you don't know what his middle name is.

(edit: my point with all this is that I haven't seen any language or database deal with this distinction very well, whether static typed or not. And that's kind of surprising given that real systems have to deal with incomplete data all the time).

8

u/Psetmaj Oct 27 '17

I haven't been in very many situations where the distinction between "we don't know it" and "we don't know it, but we know it exists" matters. Most of the time, we either always try to find it when it's missing or we carry on without it.

In a system where it really matters, I'd expect something along the lines of {:middle-name {:exists true :value X}} with the value key missing when unknown.

3

u/Borkdude Oct 27 '17

How is Nothing less ambiguous than nil?

7

u/trex-eaterofcadrs Oct 27 '17

I'm not aiming at quite the same level of abstraction but Maybe has well-defined behavior and obeys a rigorous set of laws (Monoid, Functor, Applicative, etc) and nil is a poorly defined state with context-sensitive semantics.

2

u/vagif Oct 27 '17

The point of Maybe type is not to be less ambiguous than nil. It is to force programmer to check for nil at compile time.

6

u/[deleted] Oct 27 '17

Yes, but in practice (I have worked with both Haskell and Clojure) you find situations where you have no idea of the meaning of the nil you receive, and worst, nil is an inhabitant of every Clojure type, so basically all your values are implicitly Maybes, so how can I tell the Maybe specified by the programmer from the implicit Maybe of the type system? For example, in suations such as working with maps, you can't rely on nil and you need to fallback to contains? because doing something like (:something a-map) might return nil if a-map doesn't have a :something key, or a-map does have a :something key just that it is nil.

2

u/[deleted] Oct 28 '17

nil is an inhabitant of every Clojure type, so basically all your values are implicitly Maybes, so how can I tell the Maybe specified by the programmer from the implicit Maybe of the type system?

Very well put.

Thinking as of Clojure, how would one deal with the different "meanings" nil may have?

3

u/[deleted] Oct 28 '17

What I do is try to use another value, for example, a map with :result and :error keys, which is very similar to using an Either type in expressive typed languages. This situation is very common in dynamic languages and in JavaScript I wonder if this is why there you have both, null and undefined. 🤔

2

u/trex-eaterofcadrs Oct 28 '17

I ended up adopting cats for my non-nil needs.

4

u/[deleted] Oct 28 '17 edited Oct 28 '17

I worked with Alejandro and Andrey, the authors of cats. Actually Alejandro was the one who introduced me to Haskell. Problem I had with using cats was the error messages due to my code returning an unexpected nil that propagated and exploded as an exception in the guts of the macros and structures they use to emulate do-notation and the like, that made me realize of the importance a type system having your back when you are working at such high level of abstraction, imagine with things like transformers or free algebras that are even more abstract. Today I just try to program the Clojure way where in Clojure, and the Haskell way where in Haskell (or any language with a higher kinded type system such as Scala)

3

u/[deleted] Oct 27 '17

It's not.

1

u/gzmask Oct 29 '17 edited Oct 29 '17

Maybe is a monadic value. Each time Nothing is presented, the monadic "bind" will handle the situation and rest of the computations just have to be pure functions and nothing more.

Where nullables will easily encounter a computation that isn't expecting them and give an unexpected result: that makes the system unreliable.

1

u/maxiepoo_ Oct 27 '17

You don't have to use Maybe, though. I agree without context you don't know what Nothing means any more than you know what nil means without looking at documentation, so I don't see why types are hurting you here? You can of course encode that these are different possibilities, maybe with a type like this:

data Received a = Known a | Unknown | NotApplicable

2

u/[deleted] Oct 27 '17

Yeah it's not that types hurt you, it was more of a lament on the failure to distinguish these things across all of computing. Databases generally do a terrible job of handling that sort of thing too.

3

u/ForgetTheHammer Oct 28 '17

There is a lot of good discussion on r/haskell about this.

2

u/namesandfaces Oct 27 '17 edited Oct 27 '17

Is the author essentially saying that types are nice when library authors design them for you, thereby removing the type design cost? Definitely an interesting idea, that typed languages when ideal will shift burden to library authors, while programmers reap the benefits.

I'm also curious to see how Racket will develop over time, since it has both a typed and untyped variant.

9

u/inushi Oct 27 '17

Trying to compress the whole article into one sentence will miss a lot, and I would choose a different sentence to sum it up: "Clojure is designed to tackle particular kinds of problems, and Clojure's flexible information model suits those problems somewhat better than static typing does."