esolang

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

Expected: Hello, World!

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

CodeMeans
fun x -> e fun () -> eA function
let x = e in bodyA name, not recursive
a; b if c then a else bSequence, 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 ieqInteger operations (curried)
putchar getchar char ordCharacters
map cat rev len int2str read-lineList 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.
Expected: 1099511627776
Expected: desserts
Expected: 2432902008176640000

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

  1. Print the length of a line of input.
  2. Print the numbers 1 to 10 with self-apply.
  3. Hard: write filter for lists, keeping the even numbers of a list.

Further

This implementation runs programs without the type checker.

This implementation runs programs without the type checker.

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 miniml

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/miniml.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language