CyborgPL

Examples

Real programs from the repo, each with the output it really prints.

fn add ['a': int32, 'b': int32] --> int32 {
    return 'a' plus 'b';
}
MAIN {
    let 'sum' := add[10, 20];
    std::print.useful.stdout["sum: ", 'sum', \n];
Add8 lines
fn count [&'text': str.utf8] --> int32 { return str.graphemes[lend 'text']; }       # `&` is `lent`
fn shout [&mut 'text': str.utf8] { change 'text' = copy 'text' ++ "!"; }                  # `&mut` is `lent.mut`
fn apply ['f': fn[&str.utf8] --> int32, &'s': str.utf8] --> int32 { return 'f'[&'s']; }
MAIN {
    let 's' := "hello";
    std::print.useful.stdout[count[&'s'], \n];            # `&'s'` is `lend 's'`
Alternatives14 lines
fn first [&'a': str.utf8] --> &str.utf8 { return &'a'; }
fn longer @x [&@x 'a': str.utf8, &@x 'b': str.utf8] --> &@x str.utf8 {
    if str.bytes[&'a'] >/== str.bytes[&'b'] { return &'a'; }
    return &'b';
}
fn grow @y [&mut@y 'xs': list[int32]] --> &mut@y list[int32] { list.append[&mut 'xs', 9]; return &mut 'xs'; }
Ampersand everywhere16 lines
fn loud ['name': str.utf8, 'answer': bool] --> bool {
    std::print.useful.stdout["checked ", 'name', \n];
    return 'answer';
}
MAIN {
    let 'ok' := loud["first", false] and loud["second", true];    # both print
and runs both sides18 lines
fn loud ['name': str.utf8, 'answer': bool] --> bool {
    std::print.useful.stdout["checked ", 'name', \n];
    return 'answer';
}
MAIN {
    let 'xs' := [3, 5, 8];
and.then / or.else19 lines
MAIN {
    let 'sample': !std_int24 = -8_000_000;
    let 'flags': !std_u4 = 9;
    let 'big': !std_i100 = 1_000_000_000_000_000_000;
    std::print.useful.stdout['sample', " ", 'flags', " ", 'big', " ", ('flags' plus.wrap 9), \n];
}
Any width8 lines
MAIN ['argc': int32, 'argv': ptr[ptr[uint8]]] {
    std::print.stored.stdout["argc = ", 'argc', \n];
    let mut 'i': int32 = 1;
    while 'i' < 'argc' {
        nocheck let 'word' := ptr.read[ptr.at['argv', 'i']];
        let mut 'k': int32 = 0;
Args24 lines
fn show ['b': uint8] { std::print.useful.stdout['b', \n]; }
fn wide ['w': uint32] --> uint32 { return 'w'; }
MAIN {
    show[0 minus.wrap 1];
    let 'v': uint64 = to.uint64[wide[2 shl 3]];
    std::print.useful.stdout['v', " ", wide[not.bit 0], \n];
Argument context12 lines
MAIN {
    let 'f': bin32 = 0.1;
    let 'h': bin16 = 0.1;
    let 'd': bin64 = 0.1;
    std::print.stored.stdout['f', \n];     # 0.100000001490116119384765625
    std::print.stored.stdout['h', \n];     # 0.0999755859375
Bin widths31 lines
MAIN {
    let 'a': uint8 = 12;                       # 0000 1100
    let 'b': uint8 = 10;                       # 0000 1010
    std::print.useful.stdout['a' and.bit 'b', \n];   # 0000 1000 = 8
    std::print.useful.stdout['a' or.bit 'b', \n];    # 0000 1110 = 14
    std::print.useful.stdout['a' xor 'b', \n];       # 0000 0110 = 6
Bitwise14 lines
MAIN {
    let 'correct?': bool = true;
    let 'wrong?' := not 'correct?';
    if 'correct?' and (not 'wrong?') {
        std::print.stored.stdout["fine", \n];
    }
Bools10 lines
fn fill ['count': int32] --> list[int32][0, 5] or fail[too_many] {
    if 'count' > 5 { fail too_many; }
    let mut 'out' := list.empty;
    for 'i' in range[1, 'count'] { list.append[lend.mut 'out', 'i' times 'i']; }
    return give 'out';
}
Bounded21 lines
MAIN {
    let mut 'small': map[str.utf8[1, 3 bytes], int32[0, 10]][0, 2] = map.empty;
    map.set[lend.mut 'small', "ab", 5];
    map.set[lend.mut 'small', "cd", 7];
    let 'n' := map.length[lend 'small'];         # uint64[0, 2]
    let 'tiny': int8 = 'n';                      # proven by the bound
Bounded map12 lines
MAIN {
    let 'a': brain16 = 0.1;                     # a literal rounds to the nearest: 0.10009765625
    let 'b': bfloat16 = 0.2;
    let 'c': bf16 = 'a' plus 'b';                  # all three names are one type
    std::print.stored.stdout['a', " ", 'c', \n];       # what is stored
    std::print.useful.stdout['a', " ", 'c', \n];  # the shortest text that rounds back to it
Brain1616 lines
MAIN {
    let 'good': list[uint8] = [104, 195, 169, 108, 108, 111];
    let 'bad': list[uint8] = [104, 255, 105];
    let 'a' := to.str.utf8['good'] otherwise "?";
    let 'b' := to.str.utf8['bad'] otherwise "not UTF-8";
    std::print.stored.stdout['a', \n, 'b', \n];
Bytes to text10 lines
layout.C enum Key { a = 65  b = 66  escape = 256 }
extern.C fn IsKeyDown ['key': Key] --> bool;
fn name ['k': Key] --> str.utf8 {
    match 'k' {
        a { return "A"; }
        b { return "B"; }
C enums19 lines
layout.C enum Key { a = 65  b = 66  escape = 256 }
fn on_key ['code': int32] --> int32 {
    let 'k' := to.Key['code'] otherwise { out_of_range { return 0; } };
    match 'k' {
        a { return 1; }
        b { return 2; }
C enum convert16 lines
layout.C struct Rect { w: int32[0, 4096]  h: int32[0, 4096] }
extern.C fn DrawRect ['r': Rect];
MAIN {
    let 'r' := Rect[w = 640, h = 480];
    std::print.useful.stdout['r'.w, "x", 'r'.h, " ", size.of[Rect], \n];
}
C struct ranges9 lines
layout.C enum Mode { emacs = 0  vi = 1 }
extern.C fn malloc ['bytes': uint64] --> ptr.mut[Mode];
extern.C mut 'edit_mode': int32;
fn mode_at ['p': ptr[Mode]] --> int32 {
    nocheck let 'm' := ptr.read['p'] otherwise Mode.emacs;
    match 'm' {
C variables25 lines
extern.C fn qsort ['base': ptr.mut[int32], 'n': uint64, 'size': uint64,
                 'cmp': fn[ptr[int32], ptr[int32]] --> int32];
fn ascending ['a': ptr[int32], 'b': ptr[int32]] --> int32 {
    nocheck return ptr.read['a'] minus ptr.read['b'];        # C hands pointers to two elements
}
fn descending ['a': ptr[int32], 'b': ptr[int32]] --> int32 {
Callbacks19 lines
MAIN {
    let 'a' := `A`;
    std::print.useful.stdout[char.code['a'], \n];                        # 65 — the number under it
    let 'seven' := to.int32[to.str.utf8[`7`]] otherwise -1;        # 7 — a digit's value is a text parse
    std::print.useful.stdout['seven', \n];
    let 'b' := char.point.from_code[66] otherwise `?`;
Chars19 lines
fn draw ['y': bin16] { std::print.useful.stdout["drawing ", 'y', \n]; }
MAIN {
    let 'x': bin32 = 70000.0;
    if 'x' > 65_504.0 {
        std::print.stored.stdout["too big", \n];
    } else {
Check tiers25 lines
MAIN {
    let 'a' := 1;          # trailing: from here to the end of the line
    let 'b' := 2; #wl this whole line is a comment, including the code before the #
    let 'c' := 3;
    let 'd' := 4;
    let 'e' := 5; #2ld this line and two more down
Comments12 lines
fn fetch [lent 'url': str.utf8] --> str.utf8 { return "page of " ++ 'url'; }
MAIN {
    let 'results' := channel[str.utf8];
    let 'a' := "a.com";
    concur with [lend 'results', give 'a'] { channel.send[lend 'results', fetch[lend 'a']]; }
    concur with [lend 'results'] { channel.send[lend 'results', fetch["b.com"]]; }
Concurrency15 lines
pub const 'max' := 8;
const 'pi': bin64 = 3.141_592_653_589_793;
const 'twice_max' := 'max' times 2;
const 'greeting' := "hello " ++ "world";
pub struct P { x: int32 }
pub fn area ['r': bin64] --> bin64 { return 'pi' times 'r' times 'r'; }
Consts13 lines
fn g ['p': bool] --> uint8 { return 0; }
MAIN {
    let 'x': int64 = 0;
    let 'v': uint8 = g[0 > (-1) minus 'x'];       # the (-1) minus 'x' is int64 arithmetic, not uint8
    let 'w': uint32 = 2 shl 3;                # but a bare literal still takes its width from here
    std::print.useful.stdout['v', " ", 'w', \n];
Context stops at a call9 lines
MAIN {
    let 'w': uint32 = 3;
    let 'b': bool = (5 minus.wrap 'w') > 1;          # a uint32 subtraction, 'w' says so, not a bool one
    let 'c': bool = 1 < (0 minus.wrap 'w');          # wraps to 4294967293
    let 'n': uint64 = 5;
    let 'd': bool = 'n' > (0 minus.wrap 1);          # but the other side sizes it: 0 minus.wrap 1 wraps in uint64
Context stops at a comparison10 lines
MAIN {
    for 'i': uint64 in range[3, 1, -1] { std::print.useful.stdout['i', " "]; }
    std::print.stored.stdout[\n];
    for 'j': uint64 in range[3, 0, -1] { std::print.useful.stdout['j', " "]; }   # ends at 0: must not underflow
    std::print.stored.stdout[\n];
    let 'xs' := [10, 20, 30];
Countdown11 lines
MAIN {
    let 'a': deci64 = 0.1;
    let 'b': deci64 = 'a' plus 0.2;
    std::print.stored.stdout['b', " ", 'b' == 0.3, \n];
    let 'price': deci64 = 19.99;
    let 'sub': deci64 = 'price' times 3;
deci: exact decimals37 lines
MAIN {
    let 'a': deci64 = 1.00;
    std::print.stored.stdout['a' plus (1.5 times 2), " ", 'a' plus 1.5 times 2, \n];
    let 'f': bin32 = 0.1;
    std::print.stored.stdout[to.useful.deci64['f'], " ", to.stored.deci64['f'], \n];
    let mut 'm': map[deci64, int32] = map.empty;
deci beside other numbers12 lines
MAIN {
    let 't': deci64 = 7.50;
    let 'm': deci64 = -7.50;
    std::print.stored.stdout['t' mod.floor 2, " ", 'm' mod.floor 2, " ", 't' mod.floor -2, \n];
    let 'r': deci64 = 1.05;
    std::print.stored.stdout['r' pow 10, " ", 'r' pow 0, " ", 'r' pow -1, \n];
deci, continued22 lines
MAIN {
    let 'x': bin64 = 0.00000000000000000001;
    let 'n': bin64 = -0.00000000000000000001;
    let 'two': bin64 = 2.0;
    let 'turn': bin64 = 360.0;
    std::print.useful.stdout[('x' div.ceil 'two'), " ", ('x' mod.ceil 'two'), " ", ('n' div.floor 'turn'), " ", ('n' mod.floor 'turn'), \n];
Div edges17 lines
MAIN {
    let 'a': int8 = -128;
    std::print.useful.stdout[('a' div.trunc -1), " ", ('a' div.floor -1), " ", ('a' mod.floor -1), \n];
    let 'b': int16 = 'a' div.near -1;     # lands in an int16: fits
    std::print.useful.stdout['b', \n];
}
Div lands8 lines
MAIN {
    std::print.useful.stdout[(7 div.floor 2), " ", (7 div.ceil 2), " ", (7 div.trunc 2), " ", (7 div.near 2), \n];
    std::print.useful.stdout[(-7 div.floor 2), " ", (-7 div.ceil 2), " ", (-7 div.trunc 2), " ", (-7 div.near 2), \n];
    std::print.useful.stdout[(7 mod.floor 2), " ", (7 mod.ceil 2), " ", (7 mod.trunc 2), " ", (7 mod.near 2), \n];
    std::print.useful.stdout[(-7 mod.floor 2), " ", (-7 mod.ceil 2), " ", (-7 mod.trunc 2), " ", (-7 mod.near 2), \n];
    std::print.useful.stdout[(5 div.near 2), " ", (5 mod.near 2), \n];        # 2.5 is a tie: to the even 2, 1 left
Div rounding20 lines
MAIN {
    let 'a': int8 = -128;
    std::print.useful.stdout[('a' div.trunc.wrap -1), " ", ('a' div.floor.wrap -1), " ", ('a' div.trunc -1), \n];
    let 'm': int128 = -170141183460469231731687303715884105728;
    std::print.useful.stdout[('m' div.trunc.wrap -1), " ", ('m' div.near.wrap 2), \n];
}
Div wrap9 lines
MAIN {
    std::print.useful.stdout[7 div.floor 2, " ", 7 mod.floor 2, \n];
    std::print.useful.stdout[-7 div.floor 2, " ", -7 mod.floor 2, \n];        # -4 and 1: the clock never goes negative
    std::print.useful.stdout[7 div.floor -2, " ", 7 mod.floor -2, \n];        # -4 and -1: the result has the divisor's sign
    let 'hour' := -1;
    std::print.useful.stdout[('hour' mod.floor 12) plus 1, \n];         # an hour before 1 o'clock is 12
Division10 lines
MAIN {
    let 'xs': list[int32] = [];
    list.append[lend.mut 'xs', 7];
    list.append[lend.mut 'xs', 8];
    std::print.useful.stdout['xs', " ", list.length[lend 'xs'], \n];
    let 'ys' := list.empty;
Empty10 lines
enum Status {
    empty
    claimed: str.utf8
    locked
}
struct Locker {
Enums & match33 lines
MAIN {
    let 'price' := *19.99;
    let 'tenth' := *0.1;
    let 'total' := 'tenth' plus *0.2;              # exactly 0.3
    let 'half': deci32 = *0.5;                  # a typed deci takes it at its width
    let 'speed' := 0.1;                         # a bin
Exact literal10 lines
module text {
    extern.C {
        pub fn strlen ['s': ptr[uint8]] --> uint64;
        fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] --> int64;
    }
    pub fn say [lent 'msg': str.utf8] {
Extern block pub20 lines
extern.C 'lib_version': int32;              # C: extern int lib_version;
extern.C mut 'lib_verbose': int32;          # C: extern int lib_verbose;  (we may write it)
MAIN {
    nocheck let 'v' := 'lib_version';
    std::print.useful.stdout["version ", 'v', \n];
    nocheck change 'lib_verbose' = 1;
Extern data11 lines
extern.C fn strlen ['s': ptr[uint8]] --> uint64[0, 10];      # C promises at most 10; checked at the door
MAIN {
    let 'short' := "hello";
    let 'long' := "this is longer than ten";
    nocheck let 'a' := strlen[lend 'short'] otherwise 99;
    nocheck let 'b' := strlen[lend 'long'] otherwise 99;    # 23 is outside 0..10: fails out_of_range
Extern range12 lines
struct Locker {
    owner: str.utf8
    items: list[str.utf8]
}
fn stash [lent.mut 'l': Locker, 'item': str.utf8] --> bool or fail[full, bad_item: str.utf8] {
    if list.length[lend 'l'.items] >/== 2 { fail full; }
Failures43 lines
MAIN {
    let 'x': deci64 = 3.141592653589793;
    let 'y' := to.deci32['x'] otherwise 0;
    let 'z' := to.int64['y' times 1000000] otherwise -1;
    let 'b': bin64 = to.bin64["not a number"] otherwise 0;
    std::print.stored.stdout['y', " ", 'z', " ", 'b', \n];
Fallback type9 lines
extern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] --> int64;
extern.C fn strlen ['s': ptr[uint8]] --> uint64;
extern.C fn getenv ['name': ptr[uint8]] --> ptr[uint8];
extern.C fn malloc ['bytes': uint64] --> ptr.mut[uint8];
extern.C fn free ['p': ptr.mut[uint8]];
layout.C struct Timeval {
Talking to C35 lines
fn say ['n': int32] {
    let 'by3' := ('n' mod.floor 3) == 0;
    let 'by5' := ('n' mod.floor 5) == 0;
    if 'by3' and 'by5' {
        std::print.stored.stdout["FizzBuzz", \n];
    } else if 'by3' {
FizzBuzz22 lines
MAIN {
    let 'xs': list[int32] = [1, 2];
    for 'x' in lend 'xs' {
        let 'v': int64 = to.int64['x' plus.wrap 10];
        let 'w': int64 = to.int64[not.bit 'x'];
        std::print.useful.stdout['v', " ", 'w', \n];
For item type12 lines
MAIN {
    let mut 'prices': list[int32] = [10, 20, 30];
    for 'x' in lend.mut 'prices' {
        change 'x' = 'x' times 2;
    }
    std::print.stored.stdout['prices', \n];
for over lend.mut10 lines
MAIN {
    let mut 'xs': list[int32] = [1, 2, 3];
    let mut 'seen': list[int32] = [];
    for 'x' in lend.mut 'xs' {
        change 'x' = 'x' times 10;
        list.append[lend.mut 'seen', 'xs'[1]];
Reading during a lend.mut walk12 lines
MAIN {
    let mut 'acc': int64 = 0;
    for !c 'i': int64 in range[1, 1000] {
        change 'acc' = 'acc' plus 'i';
    }
    let mut 'more': int64 = 0;
for !c: no cheating14 lines
MAIN {
    let 'xs': list[int64] = [1, 2, 3, 4];
    let mut 'total': int64 = 0;
    for para 'x' in lend 'xs' {
        change 'total' = 'total' plus 'x';
    }
for para and for seq13 lines
MAIN {
    let 'xs' := ["ann", "bob", "cy"];
    for 'i', 'x' in lend 'xs' {
        std::print.useful.stdout['i', ". ", 'x', \n];
    }
    let 'scores' := [70, 95, 88];
Position and item17 lines
struct Locker { number: int32  owner: str.utf8 }
fn by_number [lent 'a': Locker, lent 'b': Locker] --> bool { return 'a'.number < 'b'.number; }
fn by_owner  [lent 'a': Locker, lent 'b': Locker] --> bool { return str.bytes[lend 'a'.owner] < str.bytes[lend 'b'.owner]; }
fn first_by [lent 'xs': list[Locker], 'less': fn[lent Locker, lent Locker] --> bool] --> str.utf8 {
    let 'best' := 1;
    for 'i' in range[2, list.length[lend 'xs']] {
Functions as values38 lines
interface Number <T> {
    op minus ['a': <T>, 'b': <T>] --> <T>;
    op < ['a': <T>, 'b': <T>] --> bool;
}
op ~= <T>: Number ['a': <T>, 'b': <T>] --> bool {
    if 'a' < 'b' { return 'b' minus 'a' < 10; }
Generic op22 lines
interface Number {
    int32
    int64
    bin64
}
fn max <T>: Number ['a': <T>, 'b': <T>] --> <T> {
Generics39 lines
pub const 'passing' := 60;
struct Student {
    name: str.utf8
    scores: list[int32]
}
fn average [lent 's': Student] --> int32 or fail[no_scores] {
Gradebook46 lines
MAIN {
    std::print.stored.stdout["Hello, World!", \n];
}
Hello, World3 lines
MAIN {
    let 'text' := std::input.line[] otherwise "(no input)";     # a boundary: fails with `closed`
    std::print.stored.stdout["got: ", 'text', \n];
    let 'n' := to.int32[0, 100]['text'] otherwise 50;
    std::print.useful.stdout["n = ", 'n', \n];
}
Input10 lines
interface Number {
    int32
    int64
    bin64
}
interface Ordered <T> {
Interface types41 lines
interface Ordered <T> {
    op < ['a': <T>, 'b': <T>] --> bool;
    op == ['a': <T>, 'b': <T>] --> bool;
}
interface Printable <T> {
    fn show [lent 'x': <T>] --> str.utf8;
Interfaces39 lines
struct Item { name: str.utf8  grams: int32[1, 50_000] }
fn heaviest [...'items': Item] --> Item or fail[none] {
    if list.length[lend 'items'] == 0 { fail none; }
    let mut 'best' := list.remove[lend.mut 'items', 1];
    for 'it' in lend 'items' {
        if 'it'.grams > 'best'.grams { change 'best' = copy 'it'; }
Inventory44 lines
MAIN {
    let 'n' := 3;
    let 's' := "n=" ++ to.stored.str.utf8['n' plus 4];  # arithmetic, convert, then join
    std::print.stored.stdout['s', \n];
    std::print.stored.stdout["count: " ++ to.stored.str.utf8[10], \n];   # a number is converted, in its stored form
    let 'x': bin64 = 0.1;
Join13 lines
interface Key <T> { op == ['a': <T>, 'b': <T>] --> bool;  fn hash [lent 'v': <T>] --> uint64; }
struct Point { x: int32  y: int32 }                      # structural: fields decide
struct User { name: str.utf8 }                           # its own rule: names compare lowercased
op == ['a': User, 'b': User] --> bool { return std::str.lower[lend 'a'.name] == std::str.lower[lend 'b'.name]; }
fn hash [lent 'v': User] --> uint64 { let 'low' := std::str.lower[lend 'v'.name]; return str.bytes[lend 'low']; }
User satisfies Key;
Keys22 lines
fn to [] --> int32 { return 5; }
fn true [] --> int32 { return 1; }
MAIN {
    let 'x' := limb.to[];
    let 'y' := limb.true[];
    std::print.useful.stdout['x', " ", 'y', \n];
Keyword fn9 lines
fn pick @x [lent 'a': list[int32], lent@x 'b': list[int32]] --> lent@x list[int32] {
    return lend 'b';
}
MAIN {
    let 's': list[int32] = [1, 2];
    let 'r' := pick[lend 's', lend 's'];
Lent twice10 lines
fn longer @x [lent@x 'a': str.utf8, lent@x 'b': str.utf8] --> lent@x str.utf8 {
    if str.bytes[lend 'a'] >/== str.bytes[lend 'b'] { return lend 'a'; }
    return lend 'b';
}
fn pick @x @y [lent@x 'a': str.utf8, lent@y 'b': str.utf8] --> lent@x str.utf8 {
    return lend 'a';
Lifetimes: @x24 lines
MAIN {
    let mut 'l': list[int32] = [1, 2];
    list.append[lend.mut 'l', 3];
    let 'a' := to.array[give 'l'];                 # [1, 2, 3], can no longer grow
    let mut 'm' := to.list[copy 'a'];              # 'a' is still here
    list.append[lend.mut 'm', 4];
list and array13 lines
MAIN {
    let mut 'scores' := list.fill[0, 5];
    change 'scores'[2] = 7;
    std::print.stored.stdout['scores', \n];
    let 'n': int64 = 3;
    let 'flags': list[bool] = list.fill[false, 'n'];
list.fill12 lines
MAIN {
    let immut 'row': list[int32] = list.fill[0, count 4];
    std::print.stored.stdout['row', \n];
}
list.fill in #strict6 lines
MAIN {
    let 'i': uint8 = 5;
    let 'xs': list[uint8] = [0 minus.wrap 1, 0 minus.wrap 'i', 250 plus.wrap 10];
    let 'ys': list[uint32] = [2 shl 3, not.bit 0];
    let 'zs': list[list[uint8]] = [[0 minus.wrap 2,], [1,]];
    let 'big': list[uint64] = [9692729768110883998 minus 3166240359165959856,];
List item context12 lines
MAIN {
    let 'bs': list[bin64] = [1, 2];
    let 'more': list[bin64] = list.fill[1, 3];
    let 'ds': list[deci64] = list.fill[0.25, 3];
    std::print.stored.stdout['bs', \n];
    std::print.stored.stdout['more', \n];
List literal family10 lines
MAIN {
    let 'xs' := [1, 2.5];
    let 'ys': list[bin64] = [2.5, 1];
    std::print.useful.stdout['xs', \n];
    std::print.useful.stdout['ys', \n];
    std::print.useful.stdout['xs'[1] plus ('xs'[2] times 2.0), \n];
List literal mixed14 lines
fn count [lent 'items'] --> int32 {
    let 'n' := 0;
    for 'item' in lend 'items' {
        change 'n' = 'n' plus 1;
    }
    return 'n';
Lists18 lines
fn sum [lent 'items': list[int32]] --> int32 {
    let 'total' := 0;
    for 'n' in lend 'items' {
        change 'total' = 'total' plus 'n';
    }
    return 'total';
Lists231 lines
pub const 'capacity' := 12;
struct Locker {
    immut number: int32
    owner: str.utf8
    items: list[str.utf8]
}
Lockers43 lines
fn double ['n': int32] --> int32 { return 'n' times 2; }
fn apply ['v': int32, 'f': fn[int32] --> int32] --> int32 { return 'f'['v']; }
MAIN {
    std::print.useful.stdout[double[4], " ", apply[5, double], \n];
    std::print.weird.useful.stdout <-- "weird: " <-- double[2] <-- \n;
}
Long arrow7 lines
MAIN {
    let 'i' := 0;
    while 'i' < 3 {
        std::print.useful.stdout["while ", 'i', \n];
        change 'i' = 'i' plus 1;
    }
Loops23 lines
extern.C {
    fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] --> int64;
    fn malloc ['bytes': uint64] --> ptr.mut[uint8];
    fn free ['p': ptr.mut[uint8]];
}
layout.C struct Header { magic: uint32  flags: uint8  length: uint64 }
Low level56 lines
enum E1 { c1: int32  c2: uint8  c3 }
MAIN {
    let 'e' := E1.c1[5];
    match 'e' {
        c1 'd' { std::print.useful.stdout[not.bit 'd', " ", 'd' times.wrap 1000000000, \n]; }
        c2 'b' { }
A match arm's data14 lines
enum Status { empty  claimed: str.utf8  locked }
fn label ['s': Status] --> str.utf8 {
    let 'text' := match 's' {
        empty         { "free" }
        claimed 'who' { "taken by " ++ 'who' }
        locked        { return "(locked)"; }
match gives a value21 lines
MAIN {
    let 'angle': bin64 = -370.0;
    let 'turn': bin64 = 360.0;
    let 'a': bin64 = 7.5;
    let 'b': bin64 = -2.0;
    let 'h': brain16 = -5.0;
Mod bin15 lines
const 'scale' := 10;
module counter {
    pub const 'start' := 1;
    pub struct Count { n: int32 }
    pub fn bump ['c': Count] --> Count {
        return Count[n = 'c'.n plus step[]];
Modules26 lines
fn twice [immut 'n': int32] --> int32 {
    return 'n' times 2;
}
MAIN {
    let 'plain' := 1;
    change 'plain' = 'plain' plus 1;
Mutability12 lines
enum Order { cancelled  paid }
enum Ship { pickup  post }
MAIN {
    let 'o' := Order.paid;
    let 's' := Ship.post;
    match 'o' {
Nested match14 lines
extern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] --> int64;
MAIN {
    let 'msg' := "hello";
    NO_FUCKING_CHECKS let 'n' := write[1, lend 'msg', str.bytes[lend 'msg']];
    NO_FUCKING_CHECKS {
        let 'a': int32 = 10;
NO_FUCKING_CHECKS14 lines
extern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] --> int64;
fn emit ['m': uint64, 'neg': bool] {
    let mut 'buf': list[uint8] = [];
    if 'neg' { list.append[lend.mut 'buf', 45]; }
    let mut 'v': uint64 = 'm';
    let mut 'ds': list[uint8] = [];
Not bit fill45 lines
MAIN {
    let 'pos': int32[1, max] = 1;
    let 'score': int32[min, 100] = 50;
    let 'small': int8[1, max] = 120;
    let 'whole': uint8[0, 255] = 200;          # the whole type: harmless, allowed (owner)
    let 'also': int32[min, max] = -7;
One ended9 lines
struct Vec { x: bin64  y: bin64 }
op dot ['a': Vec, 'b': Vec] --> bin64 { return 'a'.x times 'b'.x plus 'a'.y times 'b'.y; }
fn fold [lent 'xs': list[int32], 'start': int32, 'f': fn[int32, int32] --> int32] --> int32 {
    let mut 'acc' := 'start';
    for 'x' in lend 'xs' { change 'acc' = 'f'['acc', 'x']; }
    return 'acc';
Operators as values27 lines
struct Vec { x: bin64  y: bin64 }
op <+> ['a': Vec, 'b': Vec] --> Vec {
    return Vec[x = 'a'.x plus 'b'.x, y = 'a'.y plus 'b'.y];
}
op dot ['a': Vec, 'b': Vec] --> bin64 {
    return 'a'.x times 'b'.x plus 'a'.y times 'b'.y;
Operators30 lines
MAIN {
    let 'small': int16 = 5;
    let 'big': int64 = (20000 plus 20000) plus 'small';    # 40005: the true answer, and it fits int64
    let 's': int16 = 30000;
    let 'twice': int64 = 's' plus 's';                   # 60000, though no int16 holds it
    let 'tiny': int8 = (100 plus 100) minus 150;             # 50: the 200 in between is fine
Overflow sites21 lines
fn count [lent 'items'] --> int32 {
    let 'n' := 0;
    for 'item' in lend 'items' {
        change 'n' = 'n' plus 1;
    }
    return 'n';
Ownership34 lines
MAIN {
    let 'xs' := [1, 2, 3];
    let 'count' := fn [] --> uint64 with [copy 'xs'] { return list.length[lend 'xs']; };
    let 'again' := copy 'count';        # its own copy of the list inside
    let 'moved' := give 'count';        # 'count' is gone after this
    let 'inc' := fn ['a': int32] --> int32 { return 'a' plus 1; };
Owning function12 lines
MAIN {
    let mut 'phones': map[str.utf8, int32] = map["ann" = 5551, "bob" = 5552];   # a literal: key = value
    map.set[lend.mut 'phones', "ann", 5553];                          # same key: replaced
    std::print.useful.stdout[map.length[lend 'phones'], " entries", \n];
    let 'a' := map.get[lend 'phones', "ann"] otherwise 0;
    let 'c' := map.get[lend 'phones', "cat"] otherwise 0;             # missing → 0
Phonebook23 lines
fn pi_digits ['n': int32] {
    let 'len' := (10 times 'n') div.floor 3 plus 1;
    let mut 'a' := list.empty;
    for 'i' in range[1, 'len'] { list.append[lend.mut 'a', 2]; }
    let mut 'nines' := 0;
    let mut 'predigit' := 0;
Digits of pi36 lines
MAIN {
    std::print.useful.stdout[(2 pow (3 pow 2)), \n];
    std::print.useful.stdout[((2 pow 3) pow 2), \n];
    let 'kilo' := 2 pow 10;
    std::print.useful.stdout['kilo', \n];
    std::print.useful.stdout[(10 minus 3 minus 2), \n];
Power7 lines
MAIN {
    std::print.useful.stdout[(1 plus 2 times 3), \n];
    std::print.useful.stdout[(2 times (3 pow 2)), \n];
    std::print.useful.stdout[-(2 pow 2), \n];
    std::print.useful.stdout[((-2) pow 2), \n];
    std::print.useful.stdout[(10 minus 4 minus 3), \n];
Precedence14 lines
struct Vec { x: bin64  y: bin64 }
precedence.low_first {
    level.when_chained.refused 1 { ==  ~= }
    level.when_chained.left_first 2 { plus }
    level.when_chained.left_first 3 { times  dot }
    level.when_chained.left_first 4 { scale }
Precedence levels33 lines
MAIN {
    std::print.useful.auto_newline.stdout["Hello, World!"];
    let 'n' := 3;
    std::print.useful.auto_newline.stdout["n is ", 'n'];
    std::print.useful.stdout["the same, by hand: ", 'n', \n];
}
print.useful.auto_newline7 lines
MAIN {
    let 'name' := "rope";
    let 'count' := 3;
    let 'price': bin64 = 12.5;
    let 'pay': deci64 = 37.5 times 12.40;
    let 'tenth': bin64 = 0.1;
print.format19 lines
MAIN {
    let 'x': bin64 = 0.1;
    std::print.stored.stdout['x', \n];
    std::print.useful.stdout['x', \n];
    let 'y': bin16 = 1000;
    std::print.useful.stdout['y', \n];
print vs print.useful13 lines
MAIN {
    let 'tenth': bin64 = 0.1;
    let 'n' := 3;
    std::print.weird.useful.stdout <-- "tenth is " <-- 'tenth' <-- ", n is " <-- 'n' <-- \n;
    std::print.weird.stored.stdout <-- "stored: " <-- 'tenth' <-- \n;
}
print.weird10 lines
MAIN {
    let 'name' := "Ann";
    let 'count' := 6;
    let 'x': bin64 = 0.1;
    std::print.stored.stdout["x is ", 'x', \n];
    std::print.useful.stdout["x is ", 'x', \n];
Prints20 lines
extern.C fn malloc ['bytes': uint64] --> ptr.mut[uint8];
extern.C fn free ['p': ptr.mut[uint8]];
layout.C struct Point {
    x: int32
    y: int32
}
ptr.cast18 lines
struct Job { name: str.utf8 }
fn run ['job': Job] { std::print.useful.stdout["ran ", 'job'.name, \n]; }
MAIN {
    let mut 'queue' := [Job[name = "a"], Job[name = "b"], Job[name = "c"]];
    run[list.remove[lend.mut 'queue', 1]];              # straight into a call
    let 'next' := list.remove[lend.mut 'queue', 1];     # or kept
Queue12 lines
MAIN {
    for 'i' in range[1, 3] {
        std::print.useful.stdout['i', " "];
    }
    std::print.stored.stdout[\n];
    for 'k' in range[0, 10, 5] {
Range33 lines
struct Percent { value: uint8[0, 100] }
fn scale ['p': Percent, 'total': int32[0, 1000]] --> int32[0, 1000] {
    return ('total' times to.int32['p'.value]) div.floor 100;
}
MAIN {
    let 'half' := Percent[value = 50];
Ranges24 lines
enum Pick { quick  slow }
fn f [] --> uint64 {
    let 'v': uint16 = match Pick.quick {
        quick { return 8997704354644785536 shr 42; }
        slow { 1 }
    };
return inside a match15 lines
fn half ['x': f64] --> f64 { return 'x' div.near 2.0; }
MAIN {
    let 'a': i32 = -7;
    let 'b': u8 = 255;
    let 'c': int32 = 'a';                       # the same type, spelled two ways
    let 'xs': list[i64] = [1, 2, 3];
Rust types13 lines
fn is_even ['n': int32] --> bool {
    return ('n' mod.floor 2) == 0;
}
fn clamp ['n': int32, 'low': int32, 'high': int32] --> int32 {
    if 'n' < 'low' {
        return 'low';
Scores49 lines
MAIN {
    let immut 'a' := 1;
    let 'a' $:= 2;                  # an immut can be shadowed: nothing is changed
    let !mut 'b' := 3;              # !mut is immut written another way
    let !$ mut 'total' := 0;        # changeable, and 'total' always means this one
    change 'total' = 'total' plus 'a' plus 'b';
Sealed12 lines
MAIN {
    let 'x' := 1;
    if true {
        let 'x' $:= 2;
        std::print.useful.stdout['x', \n];
    }
Shadow10 lines
interface Priced <T> {
    fn price [lent 'x': <T>] --> int32;
}
enum Stock {
    out
    few: int32
Shop64 lines
fn double ['n': int32] -> int32 { return 'n' times 2; }
MAIN {
    std::print.weird.useful.stdout <- double[3] <- \n;
}
Short arrow6 lines
fn is_ascii [lent 'bytes': array[uint8]] --> bool {
    let mut 'acc': simd[uint8, 16] = simd.fill[0];
    for 'i' in range[1, array.length[lend 'bytes'], 16] {
        change 'acc' = 'acc' or.bit simd.load[lend 'bytes', 'i'];
    }
    return simd.max['acc'] < 128;
SIMD23 lines
struct Particle { place: simd[bin32, 4]  vel: simd[bin32, 4] }
fn step ['p': Particle] --> Particle {
    return Particle[place = 'p'.place plus 'p'.vel, vel = 'p'.vel];
}
MAIN {
    let 'p' := Particle[place = simd.fill[0.0], vel = simd.fill[0.5]];
SIMD fields15 lines
MAIN {
    let 'a': simd[int32, 4] = simd.fill[4];
    let 'x': int32 = 'a'[1];
    let 'u': simd[uint8, 16] = simd.fill[200];
    let 'w' := 'u' plus.wrap 'u';
    std::print.stored.stdout['x', " ", 'w'[1], " ", 'w'[16], \n];
SIMD lanes13 lines
fn count_on ['m': simd[bool, 4]] --> bool {
    return simd.all['m'];
}
MAIN {
    let 'xs': array[int32] = [5, 20, 30, 40];
    let 'a': simd[int32, 4] = simd.load[lend 'xs', 1];
SIMD mask15 lines
MAIN {
    let 'xs': array[int32] = [10, 20, 30, 40];
    let 'v': simd[int32, 4] = simd.load[lend 'xs', 1];
    let 'r' := simd.shuffle['v', [4, 3, 2, 1]];         # reversed
    let 'pairs' := simd.shuffle['v', [2, 1, 4, 3]];     # neighbours swapped
    let 'first' := simd.shuffle['v', [1, 1, 1, 1]];     # lane 1 everywhere
SIMD shuffle11 lines
MAIN {
    let mut 'xs': list[int32] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    let 'n' := list.length[lend 'xs'];
    let mut 'acc': simd[int32, 4] = simd.fill[0];
    let mut 'i': uint64 = 1;
    while 'i' </== 'n' {
SIMD tail25 lines
MAIN {
    let 'xs' := [10, 20, 30, 40, 50];
    let 'mid' := list.slice[lend 'xs', 2, 4];
    std::print.useful.stdout['mid', " ", list.length[lend 'mid'], \n];
    let 'fixed': array[int32] = [1, 2, 3];
    std::print.useful.stdout[array.slice[lend 'fixed', 1, 2], \n];
Slicing11 lines
fn sum [...'nums': int32] --> int32 {
    let mut 'total' := 0;
    for 'n' in lend 'nums' { change 'total' = 'total' plus 'n'; }
    return 'total';
}
fn show [lent 'label': str.utf8, lent ...'parts'] {
Spread33 lines
fn ratio ['n': int32[1, max]] --> int32 { return 100 div.floor 'n'; }
MAIN {
    let immut 'xs': list[int32] = [10, 20, 30, 40];
    let mut 'total': int32 = 0;
    for temp 'i': uint64 in range[from 1, to 4, by 1] {
        change 'total' = 'total' plus 'xs'[pos 'i'];
#strict17 lines
enum Status { empty  claimed: str.utf8  locked }
fn label ['s': Status] --> str.utf8 {
    let immut 'text' := match 's' {
        empty         { giveS "free"; }
        claimed 'who' { let immut 'prefix' := "taken by "; giveS 'prefix' ++ 'who'; }
        locked        { giveS "locked"; }
match in #strict15 lines
enum Order { cancelled  paid }
enum Ship { pickup  post }
fn handle ['o': Order, 's': Ship] {
    match 'o' {
        cancelled { }
        paid {
Nested match in #strict28 lines
MAIN {
    std::print.stored.stdout["a\nb", \n];               # \n inside quotes is a backslash and an n
    std::print.stored.stdout["say \"hi\"", \n];         # \" is the one escape that is needed
    std::print.stored.stdout["back\\slash", \n];        # and \\ for the backslash itself
    std::print.stored.stdout["tab	here", \n];           # a tab is typed, not escaped
    let 'nl' := \n;                              # the newline is the bare \n outside quotes
Strings8 lines
struct Locker {
    name: str.utf8
    immut number: int32
    capacity: int32
    occupied: bool
}
Structs37 lines
fn add ['a': int32, 'b': int32] --> int32 { return 'a' plus 'b'; }
fn wrong ['a': int32, 'b': int32] --> int32 { return 'a' plus 'b' plus 1; }
test fn adds_up [] {
    must add[2, 3] == 5;
    must add[2, 3] > 4;
}
Tests: test fn and must27 lines
MAIN {
    let 's' := "héllo 👨‍👩‍👧 wörld";
    std::print.useful.stdout[str.bytes[lend 's'], " bytes, ", str.points[lend 's'], " points, ", str.graphemes[lend 's'], " graphemes", \n];
    let 'vowels' := 0;
    for 'g' in str.each.grapheme[lend 's'] {
        if 'g' == `o` or 'g' == `e` { change 'vowels' = 'vowels' plus 1; }
Text27 lines
fn greet [lent 'name': str.utf8[1, 10 graphemes]] { std::print.useful.stdout["hi ", 'name', \n]; }
MAIN {
    let 'thai': str.utf8[1, 10 graphemes] = "เกี่ยว";       # 4 graphemes (เ|กี่|ย|ว), 6 points, 18 bytes: fits
    let 'ascii': str.utf8[0, 5 bytes] = "hello";            # 5 bytes: fits
    greet[lend 'thai'];
    greet[lend 'ascii'];
Text bound14 lines
MAIN {
    let 'a' := to.int32["42"] otherwise 0;
    let 'b' := to.int32["99999999999"] otherwise -1;                               # too big for int32
    let 'c' := to.int64["99999999999999999999999999999999999999999999"] otherwise -1;  # too big for anything
    let 'd' := to.uint8["-1"] otherwise 0;                                         # below what uint8 holds
    let 'e' := to.int32["12x"] otherwise -2;                                      # not a number at all
Text to number16 lines
MAIN {
    let 'plain': str.utf8 = "hello";
    let 'accented': str.utf8 = "héllo";
    let 'a' := to.str.ascii['plain'] otherwise "?";
    let 'b' := to.str.ascii['accented'] otherwise "not ascii";
    std::print.stored.stdout['a', \n, 'b', \n];
To ASCII13 lines
enum Size {
    small
    big
}
MAIN {
    let 'x': bin64 = 0.1;
To text forms20 lines
MAIN {
    let 'b': bin64 = 10000000000.0;
    let 'd': deci128 = 99999999999999999999.0;
    let 'small': deci64 = 42.0;
    let 'x' := to.int32['b'] otherwise -1;
    let 'y' := to.int64['d' times 1000] otherwise -2;
Too big to int12 lines
extern.C fn exit ['code': int32];
fn judge ['n': int32] --> fail[small] { if 'n' < 10 { fail small; } }
MAIN {
    let mut 'seen': int32 = 0;
    {
        concur { judge[3] otherwise fail; } otherwise { small { change 'seen' = 'seen' plus 1; } };
Two tasks fail12 lines
fn sum [...'nums'] --> bin64 {
    let 'total': bin64 = 0;
    for 'n' in lend 'nums' { change 'total' = 'total' plus to.bin64['n']; }
    return 'total';
}
fn count_ints [...'nums': int32] --> uint64 {
Variadic24 lines
fn fetch [lent 'url': str.utf8] --> str.utf8 or fail[no_network] {
    if 'url' == "down.com" { fail no_network; }
    return "page of " ++ 'url';
}
MAIN {
    let 'inbox' := channel[str.utf8][0, 10];
whichever30 lines
MAIN {
    let 'x': bin64 = 2.0;
    let 'y': bin64 = 2.7;
    let 'a' := to.int32['x'] otherwise -1;                    # whole: 2
    let 'b' := to.int32['y'] otherwise -1;                    # not whole: fails → -1
    let 'c' := to.int32[std::round['y']] otherwise -1;        # said: 3
Whole19 lines
MAIN {
    let 'a': !std_uint256 = 2 pow 200 plus 12345;
    let 'b': !std_uint256 = (2 pow 255) minus 1;
    std::print.useful.stdout[('a' and.bit 'b'), \n];
    std::print.useful.stdout[('a' xor 'b'), \n];
    std::print.useful.stdout[('a' shl 55), " ", ('a' shr 190), \n];
Wide bits18 lines
MAIN {
    let 'max': !std_uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
    let 'half': !std_uint256 = 'max' div.floor 2;
    let 'top': !std_uint256 = 2 pow 255;
    std::print.useful.stdout['half', \n];
    std::print.useful.stdout[('top' minus 1 plus 'top'), \n];
Wide integers16 lines
MAIN {
    let 'small': !std_int300 = 5;
    let 'huge': !std_int300 = 2 pow 200;
    let 'a' := to.deci128['small'] otherwise 0.0;
    let 'b' := to.deci128['huge'] otherwise 0.0;
    std::print.useful.stdout['a', " ", 'b', \n];
Wide to deci9 lines
MAIN {
    let 'v': uint32 = 3;
    let 'a': uint32 = 2 shl 3;
    let 'b': uint32 = ('v' mod.floor 16) shl 2;
    let 'c': uint32 = not.bit ('v' plus 1);
    let 'd': uint32 = ('v' plus 1) plus.wrap 5;
Widths10 lines
MAIN {
    let 'a': int32 = 7;
    let 'b': int32 = 3;
    std::print.useful.stdout[('a' plus 'b'), " ", ('a' minus 'b'), " ", ('a' times 'b'), " ", ('a' pow 'b'), " ", ('a' std::pow 2), " ", -'a', \n];
    let 'h': uint8 = 200;
    std::print.useful.stdout[('h' plus.wrap 100), " ", ('h' times.wrap 2), " ", ('h' pow.wrap 2), \n];
Word operators11 lines
fn hash [lent 'text': str.utf8] --> uint32 {
    let 'h': uint32 = 2_166_136_261;                 # FNV-1a
    for 'b' in str.each.byte[lend 'text'] {
        change 'h' = 'h' xor 'b';                          # FNV-1a: xor, then a wrapping multiply
        change 'h' = 'h' times.wrap 16_777_619;                    # wraps instead of overflowing
    }
Wrapping23 lines