News for hackers. Stuff that matters.
Re:Parse, Don’t Validate by noobiemcfoob on Thursday November 07, 2019 @10:21PM (#21479733)
I like the concept of encoding properties, like a boolean of non-empty, in an object.

Beyond that, this article further convinced me type systems are for the pedantic. A given function signature is impossible? Seems like just another strength of a dynamic language.

Re:Parse, Don’t Validate by aidenn0 on Thursday November 07, 2019 @10:31PM (#21479765)
What does your dynamic language do when you take the first element of an empty list? There is no obvious "correct" thing to do. Furthermore, whatever you do return is unlikely to be the same sort of thing that is returned for a non-empty list.

A dynamic language will have a behavior that corresponds to some sort of type signature, and it's not possible to write behavior that corresponds with the type signatures given as examples of "impossible" in the article.

A type signature is merely a statement about behavior, so it's nonsensical to make a false statement about the behavior, and Haskell catches this.

Re:Parse, Don’t Validate by noobiemcfoob on Thursday November 07, 2019 @10:47PM (#21479817)
It throws an error. Errors are a type of behavior. Some of those error behaviors you can cope with. You catch those. Others you can't. You raise those and either the system can cope or it can't and you crash. What part is nonsensical?
Re:Parse, Don’t Validate by aidenn0 on Friday November 08, 2019 @12:05AM (#21480091)
Throwing an error should be part of the function signature. Otherwise the statement "This function takes a list of objects of type A, and returns an object of type A" is false; sometimes it will return an object of type A, other times it will signal an error.
Re:Parse, Don’t Validate by noobiemcfoob on Friday November 08, 2019 @08:24AM (#21481934)
It's better to assume all code could throw an error. No statement is safe. This becomes particularly important in distributed computing as resources may be offline at any given moment. It's from this perspective that type systems afford a false sense of security and appear to be scratching the itches of overactive Type As.
Re:Parse, Don’t Validate by aidenn0 on Friday November 08, 2019 @11:09AM (#21483534)
I'm not particularly an advocate for typed languages (my daily driver for personal projects is Common Lisp, which is mostly untyped), but I'm having trouble even understanding the point of view that "No statement is safe". If I had a system where the actual type of the expression "1+2" were "3 | Error" I would find a new system.

Code that doesn't read from unsafe memory locations, allocate memory, or perform I/O cannot throw an error. Code that does can throw an error. Some systems treat out-of-memory as fatal, removing one of those 3.

In some domains you don't want to know if the computation is happening locally or remotely, but IMO most of the time you do because pure computation cannot signal an error locally, but remotely it can. As you mention, distributed computing often runs in this mode, but distributed computing is an overkill for most problems; my phone is powerful enough to locally compute most daily tasks (even when it's done server-side for various reasons); my workstation is powerful enough to perform most daily tasks for 100s of people simultaneously.

Re:Parse, Don’t Validate by noobiemcfoob on Friday November 08, 2019 @12:20PM (#21484382)
It's hyperbole. A lot like systems that require hard type declarations for each and every function. The advantage of type systems is performance. Any correctness guarantees that come along with it are nice sugar but hardly the point and often misleading.
Re:Parse, Don’t Validate by lmm on Friday November 08, 2019 @11:34AM (#21483826)
Nonsense. A distributed scenario is precisely where an advanced type system becomes really valuable, because you can draw a distinction between local and remote calls while still treating both in a uniform way. Treating every single call as though it were remote is impractical and wasteful.
Re:Parse, Don’t Validate by scarface74 on Friday November 08, 2019 @08:10AM (#21481854)
Nice in theory but if you want to see how bad it is in practice, look no further than Java.
Re:Parse, Don’t Validate by aidenn0 on Friday November 08, 2019 @10:59AM (#21483437)
Java did a shitty job of checked exceptions. C++ did worse.
Re:Parse, Don’t Validate by millstone on Thursday November 07, 2019 @11:19PM (#21479933)
What would be the type signature of, say, Python's `pickle.load()`?
Re:Parse, Don’t Validate by aidenn0 on Friday November 08, 2019 @11:14AM (#21483586)
In the style TFA you should wrap pickle.load() with a function that will unpickle the specific type you are expecting. So you should write a unpickleArrayOfInts() or whatever.

The actual type would be a rather large union type, which would be unwieldy to use (but in an untyped system like python you still would need to deal with all of those corner cases to have a program that is correct in the face of arbitrary input).

Really the biggest annoyance of type systems is that they make you deal with corner cases that you don't think are practically possible. If you are right, then they are wasting your time. If you are wrong, then they are saving you from having bugs.

Re:Parse, Don’t Validate by tome on Friday November 08, 2019 @02:34AM (#21480546)
Re:Parse, Don’t Validate by millstone on Friday November 08, 2019 @03:00AM (#21480624)
I think that's different. `read` requires you to know what you're deserializing up-front, while `pickle` decodes the type dynamically from the data.

Dynamic languages really can have functions whose behavior cannot be expressed as some sort of type signature.

Re:Parse, Don’t Validate by mrgriffin on Friday November 08, 2019 @09:06AM (#21482202)
I'm pretty confident that you could write something that was equivalent to all the useful `pickle` calls. By that I mean you'll need to know which operations you'll want to do on your unpickled object:

  readAny :: forall c r. [TypeWithReadAnd c] -> String -> (forall a. c a => a -> r) -> Maybe r
  readAny types string handle 
I think it's fair to say "hey, pickle doesn't require me to list all my types explicitly", but on the other hand, it's not like pickle can conjure those types out of thin air--it considers only the types that are defined in your program.

Here's an example that uses Read as the serialization format and only deals with Int, Char and String; but hopefully you can imagine that I could replace the use of read with a per-type function that deserializes from a byte string or whatever.

https://repl.it/@mrgriffin/unpickle

Re:Parse, Don’t Validate by aidenn0 on Friday November 08, 2019 @11:10AM (#21483549)
IIRC the pickle format can define new classes, but I haven't looked at it in over a decade so I might be misremembering.
Re:Parse, Don’t Validate by mrgriffin on Friday November 08, 2019 @12:09PM (#21484261)
You might be right, it's been a long time for me too. But if it can define new classes, then I'd expect that the code for those classes' methods would also be in the pickled format, at which point there's no particular reason you couldn't deserialize it in Haskell too... with some caveats about either needing to know what types those functions should have, or needing to build a run-time representation of those types so that they can be checked when called, or (hopefully!) crashing if you're okay with just unsafeCoercing things around.
Re:Parse, Don’t Validate by youerbt on Friday November 08, 2019 @04:39AM (#21480966)
read :: String -> Maybe PlainPythonObject

read :: String -> Maybe Json

goes a long way.

Re:Parse, Don’t Validate by noobiemcfoob on Friday November 08, 2019 @08:30AM (#21481969)
But as soon as you're using "Maybe", you're impeding much of a type systems strengths, as the article outlined.
Re:Parse, Don’t Validate by dllthomas on Friday November 08, 2019 @12:05PM (#21484210)
Only if there is actually anything more you can say about String that would let you avoid the Maybe. If this is raw data and you don't even know that the JSON is well formatted (else why can a parse straight to a generic JSON object fail) then there's not any better way to type it.

You might want to return an error message on failure, or provide a mechanism for recovery, but those are somewhat specific to the use case and don't really address the key point of the article.

Re:Parse, Don’t Validate by youerbt on Friday November 08, 2019 @09:22AM (#21482368)
It's really a matter of what you care about to accomplish.

My point is that Python objects are certain data structure and if you bother to create type for it them then you can use it in function type.

Re:Parse, Don’t Validate by papln on Friday November 08, 2019 @12:31PM (#21484541)
Why would you want a language which extra support for bugs?

That's like saying Python is for the pedantic because it doesn't have a flag --crash_randomly, or won't let me write 0/0 and rely on the runtime to pick an abritrary value and keep going, even though I might want that.