sentient.prism

This page contains some example Sentient programs highlighted using this extension.

Subset sum

array20<int> numbers;
array20<bool> members;

sum = 0;

numbers.each(function^ (number, index) {
  sum += members[index] ? number : 0;
});

expose numbers, members, sum;

Knights tour

function main () {
  array25<array2<int4>> knightsTour;

  knightsTour.eachCons(2, function (move) {
    from, to = move.first, move.last;
    invariant validMove?(from, to);
  });

  invariant knightsTour.all?(*inBounds?);
  invariant knightsTour.uniq?;

  isClosed = validMove?(knightsTour.last, knightsTour.first);

  expose knightsTour, isClosed;
};

function validMove? (from, to) {
  xDelta = (from.x - to.x).abs;
  yDelta = (from.y - to.y).abs;

  return xDelta == 1 && yDelta == 2
      || xDelta == 2 && yDelta == 1;
};

function inBounds? (coordinate) {
  return coordinate.x.between?(1, 5)
      && coordinate.y.between?(1, 5);
};

function x (coordinate) {
  return coordinate.first;
};

function y (coordinate) {
  return coordinate.last;
};

main();

Nonogram

function main () {
  array25<array25<bool>> grid;
  array25<array13<int6>> rowNumbers, columnNumbers;

  rowNumbers = removeZeros(rowNumbers);
  columnNumbers = removeZeros(columnNumbers);

  grid.validate!(rowNumbers);
  grid.transpose.validate!(columnNumbers);

  expose grid, rowNumbers, columnNumbers;
};

function removeZeros (numbersArray) {
  return numbersArray.map(function (numbers) {
    return numbers.reject(*zero?);
  });
};

function validate! (grid, rowNumbers) {
  rowNumbers.each(function^ (numbers, rowIndex) {
    cells = grid[rowIndex];

    previous = false;
    count = 0;
    index = -1;

    cells.each(function^ (cell) {
      edge = cell != previous;
      endOfRun = edge && !cell;

      tickOffNumberIf(endOfRun);

      count = edge ? 1 : count + 1;
      previous = cell;
    });

    tickOffNumberIf(previous);
    invariant index == numbers.length - 1;
  });
};

function^ tickOffNumberIf (condition) {
  index += condition ? 1 : 0;
  number, inBounds = numbers.get(index);
  invariant condition ? number == count : true;
};

main();

Syntax rules

This is used for testing purposes.

# Keywords:
expose
invariant
return

# Numbers:
0, 1, 2, 12, 123456
-1, -1234

# Booleans:
true
false

# Declarations:
int
int8
bool
array3<bool>
array123<int3>

# Brackets:
[], { }, [123, 456, []]

# Functions and Pointers:
function foo () {};
function^ bar (*baz) {};
qux(  *foo, *bar?);

# Semicolons:
;, ;;

# Edge cases to check for:
exposex
xexpose
;expose;
--1, ----1, -0
-[123].first
xtrue
falsex
[true, false]
array
xarray3
bool3
boolx
xint
functionx
xfunction
aaaaaaaaaaaa bbbbbbbbbbbbbb ccccccccccccc dddddddddddddd eeeeeeeeeeeeeeeee ffffffffffffff
# expose 123 true int