Comparing Objective Caml and Standard ML
6 hours ago
- #OCaml vs SML
- #ML family comparison
- #Programming language design
- Objective Caml (OCaml) and Standard ML (SML) are both members of the ML family but differ significantly in syntax and feature philosophy.
- OCaml uses special syntax for array and string accesses (e.g., arr.(1)), while SML uses function calls like Array.sub.
- Character literals in OCaml are written as 'c', similar to C, whereas SML uses #"c" to avoid confusion with type variables.
- OCaml enforces capitalization conventions for constructors and modules, preventing pattern-matching bugs; SML has no such rule.
- Let-binding syntax differs: OCaml separates top-level and local bindings, while SML uses a unified let..in..end construct.
- OCaml overloads the minus sign for negation and subtraction; SML uses tilde (~) for negation to maintain identifier consistency.
- Semicolon precedence rules vary: OCaml favors imperative sequencing, while SML requires parentheses for pure functional code.
- Infix operators in OCaml have fixed precedence and curried arguments; SML allows user-defined precedence with tupled arguments.
- Arithmetic operators are type-specific in OCaml (e.g., +. for floats), while SML overloads them, defaulting to int inference.
- Equality in OCaml works on any type but may raise runtime exceptions; SML uses equality types for compile-time safety.
- Standard libraries differ: OCaml has a single array type, while SML distinguishes between imperative arrays and functional vectors.
- OCaml standard library functions are curried; SML uses tupling for higher-order functions and elsewhere.
- OCaml uses exceptions for error conditions; SML often returns option types like NONE for clarity.
- OCaml includes generic comparison and hashing functions; SML limits generic functions to equality.
- Strings are mutable in OCaml but immutable in SML, defined as vectors for functional purity.
- Algebraic datatype constructors are second-class in OCaml, requiring wrappers; SML treats them as first-class functions.
- OCaml has built-in format strings (e.g., printf); SML avoids them to keep the language simple.
- OCaml supports labeled and optional function arguments; SML uses records or other workarounds.
- Record types in OCaml are generative with field name shadowing; SML uses anonymous record types.
- Recursive types in OCaml allow any mutual recursion; SML restricts recursion through datatype declarations.
- Pattern matching in OCaml includes guards and 'or' patterns; SML omits these to simplify the language.
- OCaml allows higher-order functors and signatures; SML restricts these, though SML/NJ has extensions.
- OCaml has a novel object system; SML implements OO features via core ML constructs without special syntax.
- OCaml permits 'open' in signatures for convenience; SML disallows it to simplify formal semantics.
- Separate compilation: OCaml uses filenames to imply module names; SML tools like SML/NJ's Compilation Manager handle it differently.
- Tools and ecosystems differ, with OCaml having a bytecode compiler, .NET integration (F#), and extensible parsing (camlp4), while SML emphasizes whole-program optimization (MLton) and formal semantics.
- Social factors include community contributions (more open in SML), learning materials, library availability (more in OCaml), and packaging options.
- Language design processes vary: OCaml evolves ad-hoc, while SML has a formal definition and semantics, though Successor ML aims for agility.