Sml — Maths

fun size Leaf = 0 | size (Node (l, _, r)) = 1 + size l + size r Here, the definition of size is a recursive structural definition—pure, mathematical, and executable. A deep fact of logic, the Curry-Howard correspondence, says that programs are proofs and types are logical formulas . SML is one of the purest practical examples of this. Writing an SML function of type 'a -> 'a (the identity function) is proving that the proposition “if A then A” is true in intuitionistic logic. This isn’t just philosophy; it influences how SML programmers think about code. 5. No Side Effects (Mostly) SML is not purely functional (it has references), but idiomatic SML avoids mutation. Mathematical functions map inputs to outputs without changing global state. An SML function f : real -> real behaves exactly like a mathematical function—same input always gives the same output. This reduces cognitive load: you reason algebraically, not operationally. Limitations: Not for Heavy Numerics SML is not for large-scale matrix multiplication or statistical modeling. It lacks built-in vectorized operations and its numeric performance (without extensions) lags behind C or Fortran. Its math niche is discrete mathematics , formal verification , and type theory , not crunching floating-point arrays. In Summary SML won’t replace your calculator or Jupyter notebook. But for teaching or practicing mathematical thinking —induction, recursion, algebraic data types, and proof-like reasoning—SML is an underappreciated gem. It’s a language where functions are functions in the mathematical sense, and types are theorems you accidentally prove while coding.

fun compose (f, g) = fn x => f (g x) The type of compose is inferred as: ('a -> 'b) * ('c -> 'a) -> ('c -> 'b) sml maths

datatype 'a tree = Leaf | Node of 'a tree * 'a * 'a tree This is a free algebra . You can then write functions that pattern-match on the structure, much like defining a function by cases in mathematics. fun size Leaf = 0 | size (Node

When people think of programming languages for math, Python (with NumPy) or MATLAB usually come to mind. But there’s a quieter, older language that offers a uniquely elegant mathematical experience: Standard ML (SML) . Writing an SML function of type 'a ->

SML is a functional programming language with a strong static type system. While it’s not a computer algebra system (like Mathematica) or a numerical computing environment, its core philosophy aligns beautifully with mathematical reasoning. In SML, types aren’t just machine-checked labels—they can feel like mathematical assertions.

Take the factorial function:

That is a precise, abstract statement about function composition, independent of concrete numbers. This is reminiscent of category theory or function composition in set theory. When you write SML, the compiler proves that your implementation matches that abstract contract. SML has no for loops. Instead, you write recursive functions. This directly mirrors mathematical induction.