News for hackers. Stuff that matters.
Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @11:39AM (#16459709)
This is a good rundown of the basics of ML modules, the syntactic aspects, if you will. But it doesn't really give you the important insight:

  Abstraction. is. powerful.
if you consider the IntMap module provided in the article, you notice that the type `t` is not defined. It is kept "abstract". One defining property of ML modules is that you can indeed keep the definition of types hidden and nothing can break abstraction. The rest of the program can only manipulate the type `t` through the functions provided by the module.

This allows, in practice, to encode pretty much any property you would like. In the case of maps, you can use BST and never have to worry about users breaking the invariants: the user can't even see it's a BST! You can provide any form of validation and be sure that the data you manipulate will stay validated (as long as the module is correct, of course).

Functors allows you to rely on this by giving you a way to abstract away a whole module. This gives you excellent guarantees: if two modules behaves exactly the same, swapping them is semantic-preserving. Said in another way: if I can prove that sets-as-lists and sets-as-bst are functionally equivalents, then I can swap one for the other, and the rest of the program will behave the same!

Refactoring becomes a breeze. Functors (and modules) makes all the Javaesque reflections and dependency injections completely redundant: modules are (in OCaml) first class objects that you can manipulate and apply as you wish.

It also gives you separate compilation! A lot of people say that the OCaml compiler is blazing fast: this is precisely because OCaml's module system ensure that each module can be typechecked and compiled incrementally.

It pains me that modules get so little appreciation. Many ML programmers get a sort of selective blindness: just like fish in the sea, they don't realize the power of what they're swimming in. Proper modules with actual abstraction are, in my opinion, the single most essential feature for "large scale" programming.

Re:A Crash Course on ML Modules (2015) by skybrian on Sunday February 25, 2018 @12:30PM (#16460022)
I agree this is great stuff, but let's not get carried away about types. They don't guarantee compatibility.

Two functions with the same type signature can compute different things, and code can advertently rely on this difference. For example, changing a hash function can cause code to break that relied on iteration order of a hashmap. (And with in a large enough codebase and a commonly used, rarely changed hash function, this is probably inevitable.)

Also, even if the values returned by the function are the same, you can still observe differences between different implementations by comparing performance, and code can inadvertently depend on performance differences. This can cause breakage or security holes.

Still, if you can avoid type errors, you're probably doing well.

Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @01:10PM (#16460252)
I didn't say anything about types. :) I said functionally equivalent.

What it means depends on the definition of "functionally equivalent" of course. The first version, which is types, is the most immediate and obvious, but as you pointed out, not that useful.

Abstractions, however also works at the semantics level: if A behaves like B trough the interface S, then you can confidently say that for any F (of type S -> S'), F(A) behaves like F(B). So you can just test that A and B are not distinguishable (property testing is very good at this) and feel confident about your refactoring!

The definition of behaves like depends on your language. Usually, this doesn't account for performances, as you point out, but only for the observable semantics. This is known as parametricity[1]. If you really want to get into the deep end, [2] demonstrates all that for a (very rich) ML module system.

[1]: https://en.wikipedia.org/wiki/Parametricity [2]: http://www.cs.cmu.edu/~crary/papers/2017/mapp.pdf

Re:A Crash Course on ML Modules (2015) by skybrian on Sunday February 25, 2018 @02:36PM (#16460712)
Good point! But leaving types aside, the same argument applies. "Observable semantics" seems to mean "observable according to our semantic model" which is an agreement to pretend that machine behavior outside the model isn't observable and avoid relying on it.

This agreement is normally a good thing since it's what allows for the same program to run on different machines, or to compile the same program with different optimizations, or swap in a newer version of a library with a better hash function and claim it doesn't break backward compatibility.

Nevertheless, it can be a blind spot as we saw with Meltdown and Spectre, so I wanted to emphasize that this is a useful myth but the world isn't obliged to go along with it. It's often important to observe program behavior that isn't specified by the language.

Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @02:46PM (#16460765)
Ah, yes, This is where safety and security differs! Abstraction is about safety: preventing errors and giving additional guarantees, not preventing attacks. :)

Nothing I said hold when people are being malicious: Even in OCaml, you have escape hooks that allows you to break past abstraction boundaries and do whatever you want.

Re:A Crash Course on ML Modules (2015) by nickpsecurity on Sunday February 25, 2018 @06:10PM (#16461768)
There's work to address that. Look into proof-carrying code and "fully-abstract compilation." There's also been proofs done for hardware and assembly against specs in ACL2 and Abstract, State Machines.
Re:A Crash Course on ML Modules (2015) by skybrian on Sunday February 25, 2018 @03:08PM (#16460893)
Agreed, but this is where our own jargon betrays us: in ordinary usage by non-programmers, "safety" includes safety from attacks. Perhaps especially from attacks?
Re:A Crash Course on ML Modules (2015) by Tomte on Sunday February 25, 2018 @03:20PM (#16460958)
Ordinary usage is often murky, but the usual distinction between security and safety is not whether it is about an attack, but about the direction of the threat.

Safety means the environment is not harmed by the system. Security means the system is not harmed by the environment.

Re:A Crash Course on ML Modules (2015) by nickpsecurity on Sunday February 25, 2018 @06:07PM (#16461759)
That's an interesting definition. Traditionally, we had several ways of looking at it:

1. Safety contends with accidents like components breaking. Security contends with malice where input is crafted and failures are set up intelligently to do damage unlikely to happen on accident. There's quite a bit of overlap, though.

2. If talking leaks, safety rarely requires confidentiality of data. It's usually integrity or availability that it shares with security. So, a security violation with covert/side channels might not be a safety violation.

Just two off top of my head to illustrate the difference.

Re:A Crash Course on ML Modules (2015) by lmm on Monday February 26, 2018 @09:09AM (#16465304)
I think of it the same way you'd think of it in a workshop: a "safe" drill or saw is one that can be used in a safe way, not one that's safe from deliberate attacks.
Re:A Crash Course on ML Modules (2015) by pjmlp on Monday February 26, 2018 @04:27AM (#16464141)
While ML has lots of cool features, that kind of abstraction capabilities and compilation speed is also possible in other languages with modules.

Even if their modules aren't exactly ML-like, they offer similar features.

For example CLU also explored hidden types and generic interface definitions.

Modula-3 does offer opaque types, generic module interface definitions with multiple implementations and class inheritance. However it does lack closures/lambdas, forcing the use of function pointers, which aren't so developer friendly,

Similarly the way traits work in Scala are quite close to ML modules.

So there are ways of achieving ML module capabilities, even if not 1:1 as they are done in Standard ML.

Re:A Crash Course on ML Modules (2015) by zvrba on Sunday February 25, 2018 @12:36PM (#16460052)
It seems to me that you can get the same thing in C++ with classes <> modules ; templated classes <> functors. What am I missing?
Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @12:53PM (#16460150)
There is no abstraction in C++, you can always poke around the memory layout of what you are given, and change your behavior depending on that.

For example, let us say you have a module satisfying this signature:

  module M : sig
    type sorted
    val import : int array -> sorted
  end = struct
    type sorted = int array
    let import = Array.sort
  end
In OCaml, if you have a value of type `sorted`, you know it's indeed sorted. In C++, as soon any code external to the module had an handle on it, you don't know! It could have modified the array behind your back, since it can look directly at the definition, or worse, poke in the memory layout.
Re:A Crash Course on ML Modules (2015) by rlp on Sunday February 25, 2018 @05:57PM (#16461713)
This is not accurate. C++ has statically enforced private variables that cannot be modified by outside functions without fairly painful workarounds and castings. But, if you want to say that the ability to do those castings would disqualify any compile-time guarantees, well, OCaml has an equivalent escape hatch. Module Obj contains a bunch of functions to manually manipulate any object fields and cast arbitrarily using the "magic" function.
Re:A Crash Course on ML Modules (2015) by rapala on Sunday February 25, 2018 @01:39PM (#16460399)
Yes, you can access raw memory and flip bits to modify a private member of an object (and it migth even be defined, not too sure about that thou). But I don't find that an valid argument for the statement that you can't do abstractions in c++. That's just not something people do.

The c++ version of your example would be, if I understood your code correctly, to take an std::vector as an constructor parameter, copy it to an private field and sort it.

Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @02:06PM (#16460548)
I don't know all that much about C++'s object system, so I can't give you a concrete example on how it breaks down.

However, you say that it is "not something people do" ... well maybe not in C++ (I highly doubt that), but it's very common in many languages. In C, it's common to look inside structs directly and change things. Javascript libraries do it all the time: They inspect their arguments, look at the types and change their behavior depending on it. It's a common programming practice to poke deep into the data-structures and do things. In Java, they made it an art with reflection and monstrosity such as Spring.

Abstraction is a bit like immutability: Sure, you can try to fake it in languages that don't have it, but then you are just praying that everyone plays by your rules. :)

Re:A Crash Course on ML Modules (2015) by rapala on Sunday February 25, 2018 @03:03PM (#16460870)
My comment "not something people do" was about directly accessing memory to circumvent the private data abstraction in c++ and I stand by that.

I admit that I kinda pushed you into it, but you are moving the goal post. People indeed use public fields in languages like C and Javascript.

In C it's often done for the sake of performance. Hiding data behind a pointer has a cost.

In Javascript I would say it's lazyness above all. Front end programs often aren't that big nor pinacles of code quality.

But it is possible to define abstract data types in both languages. ML makes it a bit easier and some times even more performant, but it doesn't "own" the idea.

Abstractions are quite like immutability. You can enforce both in many languages, some just give you better tools for it.

Re:A Crash Course on ML Modules (2015) by Drup on Sunday February 25, 2018 @03:21PM (#16460963)
The goal post, right from the start, is that by having proper abstraction (the kind that doesn't break when you blow lightly on it), you get many benefits.

You say that, even when the language doesn't enforce it, people don't break it ..... except when they do. It doesn't really matter why, it simply makes every thing else more brittle as a consequence and limits how you can reason about your code.

You seem to trust that programmers will play by the rules, even if the compiler doesn't enforce them. We will simply have to agree to disagree. :)

Re:A Crash Course on ML Modules (2015) by rapala on Monday February 26, 2018 @11:25AM (#16466435)
> You seem to trust that programmers will play by the rules, even if the compiler doesn't enforce them.

I've been trying to say the exact oppisite. C, C++, Javascript, all those languages provide ways to define abstract datatypes that cannot be circumvented (by "normal" code. Even Haskell has unsafePerformeIO). My latest argument was that people decide not to use those abstractions not because they are unavailable, but because it is more ergonomical or performant not to. The same happens even in ML, not all data is abstracted as an abstract data type.

Re:A Crash Course on ML Modules (2015) by zvrba on Wednesday February 28, 2018 @01:38AM (#16481148)
So, in C++

  class M
  {
    // This is private
    std::vector<int> _sorted;
  public:
    // This is your "import"
    M(const std::vector<int>& v) : _sorted(v) {
      std::sort(_sorted.begin(), _sorted.end());
    }
    // Add other ADT operations that preserve the invariant
  }

A more indirect way that more closely mimics the ML thing:

  struct M
  {
    // Everything private; but as M is "friend" it can access all members
    class Sorted {
      friend M;
      std::vector<int> value;
    }

    static Sorted import(std::vector<int> v) {
      std::sort(v.begin(), v.end());
      return Sorted{v};
    }
  }
Usage:

  auto s = M::import(std::vector<int>{4, 3, 2, 1});
  ++s.value[0]; // ERROR: value is private
Yes, you can use casts to access the private members. As others have pointed out, unsafe operations exist in Ocaml/Haskell/... too.
Re:A Crash Course on ML Modules (2015) by yen223 on Sunday February 25, 2018 @07:20PM (#16462081)
"In OCaml, if you have a value of type `sorted`, you know it's indeed sorted."

Could you expand on this? I'm pretty sure the OCaml compiler isn't sophisticated enough to make guarantees like this - i.e. I could pass in a function that satisfies the interface constraints, but doesn't actually do any sorting.

Re:A Crash Course on ML Modules (2015) by yorwba on Sunday February 25, 2018 @08:28PM (#16462353)
Since the function import in the signature is the only way to create a value of the abstract type sorted, and the implementation of import is given as Array.sort, all values of type sorted have indeed been sorted.
Re:A Crash Course on ML Modules (2015) by choeger on Sunday February 25, 2018 @12:52PM (#16460148)
An interface and safety. In C++ the template's arguments are directly copied into it's body. A module can only be accessed via its interface. This in turn allows for a typechecker to guarantee the absence of certain errors. It also enables separate compilation that you do not have for C++ template's. The downside is of course that a C++ compiler can apply more optimizations.

As a side note: the argument, I can do X with Y so why use Z is somewhat misleading, when Y and Z are both Turing complete ;).

Re:A Crash Course on ML Modules (2015) by l_dopa on Sunday February 25, 2018 @03:51PM (#16461091)
Type checking, type abstraction. If you're willing to throw those out, you can "get the same thing" with structs and void pointers.
Re:A Crash Course on ML Modules (2015) by pjmlp on Monday February 26, 2018 @01:46AM (#16463615)
Or with Modula-3 opaque types, for example.