# Sillycon

SillyCon is a rather silly language for expressing numerical constraint problems, created by Rick van der Meiden.

## Language

The sillycon language is used to represent integer constraint problems. An integer constraint problem is an expression with numbers, variables and operators (such as addition, multiplication, boolean operations, comparisons and special constraint solving operations). A constraint problem defines the allowed values of the variables. A constraint problem is solved by a constraint solver, which is part of the SillyCon interpretor. The constraint solver returns all the solutions, i.e. values for the variables, if there are any.

The language uses a prefix notation for operators (e.g. +4 5 means 4+5, =x3 means x=3). All operators and variables are single characters. All puctuation characters are operators. All alphabetic characters are variables. All sequences of digits are numbers. Whitespace is ignored (but may be used to separate numbers). Note that its not possible to declare variable names (or operators) of more than one character.

The input is a sequence of expressions. Each expression is interpreted as a separate problem and solved seperately, i.e. constraints on variables in one problem are not passed to then next problem.

An expression is a variable, a constant, or a composite expression consisting of an operator and one or more subexpressions.

Constants are sequences of digits; e.g 1 100 828527 00027

Variables are single alphabetic characters a-z and A-Z; so 'Foo' and 'bar' are just sequences of three variables, the interpretation depenends on the context (i.e. preceding operator). For example, '+xy' or '*px' are complete expressions; i.e. one operator with two operands.

Constants and variables are signed integer numbers. Negative constants are entered using the unary - operator. The result of an operator is also a signed integer number. The internal binary representation (i.e. after integer problems are converted to binary problems) is two's compliment. (You may want to know this if you use the boolean operators on numbers).

Variables currently have a fixed size, i.e. the number of bits. This is currently hard-coded in the sillycon interpretor (9 bits in the current implementation, i.e. from -256 to +255) but this will probably be made dynamic in the future. Results of operands can have more bits.

Operators are single punctuation characters; operators have 1 or 2 operands. Operands can be any expression, i.e. a constant, a variables or an operator-expression.

The accepted operators are the following:

``` symbol  name    arity   meaning
-------------------------------
integer arithmetic:
-       NEG     1       integer negation
*       MUL     2       integer multiplication
/       DIV     2       integer division (quotient)
%       MOD     2       integer floor modulus (remainder)
=       EQUAL   2       1 if operands are equal (bit for bit), otherwise 0
>       GT      2       1 if operand 1 greater than operand 2, otherwise 0
<       LT      2       1 if operand 1 less than operand 2, otherwise 0
&       AND     2       binary AND
|       OR      2       binary OR
~       XOR     2       binary XOR
!       NOT     1       binary NOT
@       CON     1       constrain the operand equal to 1
#       COUNT   1       number of solutions of operand 1
\$       MAX     2       maximum value of operand 1 for all solutions of operand 2
_       MIN     2       minimum value of operand 1 for all solutions of operand 2
'       EVAL    2       set of values of operand 1 for all solutions of operand 2
`       IND     2       union of constraints of operand 1 where variables in operand 1
are replaced by variables pointed to by values of those
variables in solutions of operand 2
```

(reserved/unused characters: ? . , \ [] () ^ : ; ")

Comments are encosed in a pair of double quotes: "this is a comment"

The COUNT, EVAL, MIN, MAX and IND operators will interpret the (only or second) operand as a sub-problem and solve it. Afther solving this sub-problem, the first operand (of MIN,MAX,EVAL,IND) will be converted into a new expression, which is part of the main problem. (And this problem will be solved in the normal solving process).

Note that MIN and MAX operators do not generate all solutions for the subproblem. In fact, they are quite efficient. The boolean variables are ordered such that the first solution found is the desired solution. On the other hand, the COUNT, EVAL and IND operators will generate all solutions for he sub-problem, so these can be very expensive operations.

A root expression (or problem) is implicitly constrained to 1 (see @ operator) if it starts with a boolean operator or comparator (EQ, NOT, GREATER, etc, see below) or if the root expression is a numerical expression (see ADD, SUB, etc. below) the result will be constrained equal to a variable '?'.

Also, the problem solving operators (see EVAL, IND, MIN, MAX and COUNT see below) will implicitly constrain the problem expression (one of the input expressions) to 1.

So, the following numerical expression

```+ 3 4
```

will be interpreted as the following problem

```@=?+3 4
```

(and the solution will be ?=7)

And the following boolean expression

```=x 3
```

will be interpreted as the following problem

```@=x3
```

(and the solution will be x=3)

## Example Code

The following expression represents the problem: x*y = 10

```=10*xy
```

The result is:

```x = 10,  y = 1
x = 5,   y = 2
x = 2,   y = 5
x = 1,   y = 10
x = -1,  y = -10
x = -2,  y = -5
x = -5,  y = -2
x = -10, y = -1
```

The following expression approximates x = sqrt(z) (x is the smallest positive integer for which x*x <= z)

``` &<z*+1x+1x&>+1z*xx&>x-1=z200
```

And this one:

``` `&=A1=y+1x&=y+1x&>x64<x90
```

Results in:

```A=1
B=2
C=3
D=4
E=5
F=6
G=7
H=8
I=9
J=10
K=11
L=12
M=13
N=14
O=15
P=16
Q=17
R=18
S=19
T=20
U=21
V=22
W=23
X=24
Y=25
Z=26
```

## Implementation

A sillycon interpretor/solver (also called SillyCon) has been implemented in C. It converts the problem into a Boolean Propagation Problem, and uses a Boolean Propagation Solver to determine the solutions.

The SillyCon interpretor program accepts input from the stdin stream or from a file given as the first command line argument. As soon as a complete expression has been entered (i.e. each operator has the required number of operands), it will be considered a problem and it will be solved.

There is also a SillyCon web demo, which accepts input from a form, or in the URL using the POST format: ?expression=...

## Turing Completeness

No. Sillycon programs always terminate, therefore you cannot implement a Turing machine. It might be possible to implement a time and space and symbol bounded Turing machine.