Names are not type safety (2020)

(lexi-lambda.github.io)

43 points | by azhenley 8 hours ago ago

16 comments

  • lmm 6 hours ago

    IME this is exactly backwards: type safety is mostly about names, everything else is a nice-to-have. Yes, you can bypass your name checks if you want to, but you can bypass any type check if you want to. Most relevant type relationships in most programming are business relationships that would be prohibitively expensive to express in a full formalism if that was even possible. But putting names on them is cheap, easy, and effective. The biggest win from typed languages comes from using these basic techniques.

    • b_e_n_t_o_n 5 hours ago

      Hmm, IME the preferred type systems are structural - a function shouldn't care what the name is of the struct passed to it, it should just work if it has the correct fields.

  • 5pl1n73r 6 hours ago

    My peers and I work on a language centered around "constructive data modeling" (first time I hear it called that). We implement integers, and indeed, things like non empty lists using algebraic data types, for example. You can both have a theory of values that doesn't rely on trapdoors like "int32" or "string", as well as encode invariants, as this article covers.

    As I understand it, the primary purpose of newtypes is actually just to work around typeclass issues like in the examples mentioned at the end of the article. They are specifically designed to be zero cost, because you want to not pay when you work around the type class instance already being taken for the type you want to make an instance for. When you make an abstract data type by not exporting the data constructors, that can be done with or without newtype.

    • eru 6 hours ago

      The alternative to newtypes is probably to go the same route as OCaml and have people explicitly bring their own instances for typeclasses, instead of allowing each type only one instance?

      I think OCaml calls these things modules or so. But the concepts are similar. For most cases, when there's one obvious instance that you want, having Haskell pick the instance is less of a hassle.

  • nixpulvis 6 hours ago

    In Rust I find myself gaining a good bit of type safety without losing ergonomics by wrapping types in a newtype then implementing Deref for them. At first it might seem like a waste, but it prevents accidentally passing the wrong type of thing to a function (e.g. a user UUID as a post UUID).

    • weinzierl 2 hours ago

      This and for the use case from the article we will hopefully gain pattern types in Rust soon.

      They do not solve every problem that constructive data modeling does but in my opinion a large portion of what actually occurs in everyday programs. Since they are zero-cost I'd say their cost-benefit ratio is pretty good.

      Ada and Pascal also had handled the "encode the range in the type" nicely for decades.

    • chiffaa an hour ago

      I want to point out that, technically, using Deref for this is an anti-pattern, as Deref is intended exclusively for smart pointers. Nothing really wrong with doing this outside of some loss in opacity (and unexpected behaviour if you're writing a library), but it's worth pointing out

  • skybrian 5 hours ago

    The author seems concerned about compile-time range checking: did you handle the full range of inputs?

    Range checking can be very annoying to deal with if you take it too seriously. This comes up when writing a property testing framework. It's easy to generate test data that will cause out of memory errors - just pass in maximum-length strings everywhere. Your code accepts any string, right? That's what type signature says!

    In practice, setting compile-time limits on string sizes for the inputs to every internal function would be unreasonable. When using dynamically allocated memory, the maximum input size is really a system property: how much memory does the system have? Limits on input sizes need to be set at system boundaries.

  • andyferris 4 hours ago

    These are possibly situations where I’d resort to a panic on the extra branch rather than complicate the return type.

    Providing a proof of program correctness is pretty challenging even in languages that support it. In most cases careful checking of invariants at runtime (where not possible at compile time) and crashing loudly and early is sufficient for reliable-enough software.

  • kazinator 6 hours ago

    What if I want a type called MinusIntMaxToPlusIntMax?

    In other words the full range of Int?

    Is newtype still bad?

    In other words how much of this criticism has to do with newtype not providing sub-ranging for enumerable types?

    It seems that it could be extended to do that.

  • auggierose 2 hours ago

    These kinds of types are just a waste of time. It is going to be OneToSix or OneToSeven very soon...

    It's just an example! Well, if you cannot come up with a good example, maybe you don't have a point.

    • ashton314 an hour ago

      I take it you didn’t read the whole thing, as the next example is NonEmptyList, which is a good compelling example. It’s also not hard to think of other examples from my own work: I can imagine a URL type that only exposes constructors to create well-formed URLs. Etc etc.

      Really good examples will be rather domain-specific, so it’s perfectly understandable why Alexis would trust her readers to be able to imagine uses that suit their needs.

  • valenterry 4 hours ago

    Title should been "names are not ENOUGH for type-safety" but then no one would have read it I guess...

  • b_e_n_t_o_n 5 hours ago

    Perhaps it's because I'm not a haskeller but I'm not sure if I'm sold on encoding this into the type system. In go (and other languages for example), you would simply use a struct with a hidden Int, and receiver methods for construction/modification/access. I'm not sure I see the benefit of the type ceremony around it.

    • cryptonector 3 hours ago

      > you would simply use a struct with a hidden

      In such languages that's the equivalent of a newtype in Haskell.

    • the_af 4 hours ago

      Isn't the whole article a discussion of the kind of guarantees such an approach (which can also be done in Haskell) cannot provide?