SML 和 OCaml 有什么不同?

是什么将两种 ML 方言区别开来?

19210 次浏览

OCaml adds object-orientation features and has some minor syntax differences.

There are lots of differences, some technical, some sociopolitical. I've tried to put more important differences first.

  • SML is a language with a definition and a standard. It is stable (and in fact has been frozen so it cannot evolve). Objective Caml is an implementation controlled by a small group at INRIA. It continues to evolve. (IMO the evolution is managed well.)

  • SML has many implementations; Caml has just one.

  • Objective Caml has a number of additional features, among which the most prominent are probably objects and polymorphic variants.

  • The two languages have dramatically different models of record types. Briefly, in Caml, names of record fields must be unique, where in SML, two different record types in the same scope may have field names in common. This quirk can make porting from SML to Caml a bit tricky.

  • There are quite a few syntactic differences.

  • The libraries and standard functions are dramatically different. The Caml library is very imperative, whereas the SML Standard Basis Library is more functional. For example, function composition is a top-level primitive in SML; it's not part of the Caml library. The Caml string library doesn't provide a fold function (at least not as of version 3.08). Implementations of many of the Caml List functions are unsafe for very long lists; they blow the stack.

  • The type systems are subtly different: In Caml, a type annotation on an expression e : ty is accepted if the type ty unifies with the type of e. In SML, e : ty is accepted only if the type ty is an instance of the type of e. This distinction renders the annotation in Caml much less useful in practice, because it is impossible to use a type annotation to insist that an expression is polymorphic.

  • Caml has a much more sane and sensible relationship between interfaces (called module types or signatures) and (concrete) implementations (called modules or structures) than SML. In SML pretty much anything goes and you have to rely on the programmer to establish good conventions. In Caml, good conventions are established and enforced by the compiler.

  • In SML, arithmetic operators are overloaded to apply to both floating-point and integer data. In Caml, operators are not overloaded; floating-point operators are notated with an extra dot.

  • In SML, the programmer can control the precedence and associtivity of infix operators. In Caml, these are determined by the first character of the operator's name. This restriction limits the benefits of being able to define your own infix notation.

For a more detailed analysis complete with editorial comment, you could also look at Adam Chlipala's comparison page.