miniML
A tiny ML with Hindley-Milner types that compiles to LLVM. It has no recursive let, so recursion is written as a function applied to itself.
feresum · Turing complete: yes · ●●○ moderate
Tested with 4 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
feresum wrote miniML as a compiler in Python. It parses the program, infers its types, and emits LLVM code. The language is pared down to almost nothing. There are functions, let, pairs, two-sided sums, if, and a dozen built-in operations on integers and characters. Everything else, lists and strings included, is defined in a small library that is put in front of every program.
Read the story
The library cannot use recursion directly, since a let cannot see its own name. Instead it writes self-apply (fun f -> fun n -> ... f f ...), where the function receives itself as an argument. That is the oldest trick in the lambda calculus, and in miniML it is the everyday way to loop.
The machine
| Code | Means |
|---|---|
fun x -> e fun () -> e | A function |
let x = e in body | A name, not recursive |
a; b if c then a else b | Sequence, condition |
a, b _fst(p) _snd(p) | A pair and its parts |
<e\| >e\| switch(s: x -> a \| y -> b) | A sum: left or right, and a match on it |
iadd imul idiv irem ilt ieq | Integer operations (curried) |
putchar getchar char ord | Characters |
map cat rev len int2str read-line | List functions from the library |
A list is a chain of left sums holding a pair, <(head, tail)|, ended by >()|. A string is a list of characters. Integers are 64 bits and wrap. idiv a b divides b by a, so that idiv 10 is "divide by ten". The program's own value is thrown away; only putchar prints.
Example
The last program is a factorial.
The last program is a factorial. f f is the recursive call, with f the function itself, handed over again so it can call itself once more.
Patterns it teaches
- Combinators: recursion by self-application, the way the Y combinator does it, but spelled out by hand at each use.
Try this
- Print the length of a line of input.
- Print the numbers 1 to 10 with
self-apply. - Hard: write
filterfor lists, keeping the even numbers of a list.
Further
This implementation runs programs without the type checker.
- The reference compiler, in Python, with its library
stdlib.miniml
This implementation runs programs without the type checker.
See also
Also in Functional and concatenative
Write your own in Run Convert a program from miniML
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: miniml.
Command line
shib run miniml -e 'map putchar "Hello, World!"'shib manual minimlSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/miniml.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language