Floor
Floor is a programming language based on arithmetic expressions.
A Floor program defines a function from a tuple of integers to the integers using only the arithmetic operators
+
-
*
/
, integral powers (^
) as well as the floor
-function
Syntax
A Floor program consists of a series of functions from tuples of integers to the integers.
The when executing a Floor program the function f
will be called with the arguments given to the program.
The syntax for a function definition is name: arg1 ... argN -> expr
:
- First the name of the function followed by a
:
- The a sequence of argument names separated by spaces terminated by a
->
- The a arithmetic expression using integers, operators function arguments and previously defined functions
Example:
# Lines starting with # are comments
ceil: x -> - floor -x add: x y -> x+y f: (add ceil x floor x)/2
The usual arithmetic rules for +
-
*
/
and ^
apply,
Parenthesis are evaluated before exponentiation which is evaluated before multiplication and division which are evaluated before addition and subtraction. Exponentiation is evaluated right to left, all other operations are evaluated left to right.
1+2*3^4^5-6 -> (1+(2*(3^(4^5))))-6 2/3/4 -> (2/3)/4 # + and - can be used as unary operators -1*3--4 -> ((-1)*3)-(-4) # functions have precedence over operators floor x + 1/2 -> (floor x)+(1/2)
All calculations are performed on rational numbers.
Special cases:
- If the right operand of
^
is not a integer it will be rounded down (towards minus infinity) to the nearest integer,e.g2^(1/2) -> 2^0 -> 1
. - Division by zero will evaluate to
0
, this includes taking zero to negative powers. - The exception to this rule are
0/0
and0^0
which will evaluate to1
.
The ^
operator can also be used on functions, in this context it refers to repeated application of that function,
if the function has more than one argument, the remaining arguments will be passed to all calls.
As recursive definition: f^n(a,b1,...bN) = f^(n-1)(f(a,b1,...,bN),b1,...,BN)
Example:
inc: n -> n+1 add: a b -> inc^a b mult: a b -> add^b 0 a
defines multiplication through repeated incrementation
Conditional Statements:
The basic building block for control flow this is that floor x
evaluates to 0
if x
is in (0,1)
but to -1
if x is in (-1,0)
.
This asymmetry can be used to construct indicator functions for different conditions, for instance:
- being a positive/negative:
isPositive: x -> -floor(-x/(x^2+1))
isNegative: x -> -floor(x/(x^2+1))
- being nonzero:
bool: x -> - floor( -x²/(x²+1))
- being smaller than a given number:
lt: x y -> -(floor((x-y)/((x-y)²+1)))
- being a integer:
isInt: x -> 1+floor((floor x) - x)
Using indicator functions is is possible to simulate conditional statements:
if: c x y -> (bool c)*x+(1-(bool c))*y # if(c,x,y) evaluates to x if c is nonzero and to y is c is zero
# this can for instance be used to define minimum and maximum functions min: x y -> if lt x y x y max: x y -> if lt x y y x
IO
Floor is a purely functional language, the only way to provide input is through the function arguments, which will be passed via the program arguments of the interpreter.
The only way of output is the single integer that is returned by the main function f
.
By default input and output are treated as decimal integers, the interpreter also allows input/output as:
- Hexadecimal numbers (
-x
and-X
flags) - Binary numbers (
-b
and-B
flags) - Unicode strings (
-s
and-S
flags)
To convert numbers from and to string the absolute value of the integer part is interpreted as as sequence of bytes (in little endian order),
for instance 264560896834502173346923783098410 = 21646c726f57202c6f6c6c654816
will be interpreted as
48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21
giving the string Hello, World!
.
Examples
Hello World (needs to be run with -S
flag for string output)
f: -> 2645608968345021733469237830984
Compute the minimum of two numbers:
bool: x -> - floor( -x²/(x²+1)) if: c x y -> (bool c)*x+(1-(bool c))*y lt: x y -> -(floor((x-y)/((x-y)²+1))) min: x y -> if lt x y x y f: a b -> min a b
Fibonacci sequence:
bool: x -> - floor( -x²/(x²+1)) lt: x y -> -(floor((x-y)/((x-y)²+1))) intPair: x y -> x + 1/y left: x -> floor x right: x -> 1/(x-floor x) # fib-step will be repeatedly applyed to its own return value fib_step: xy -> intPair right xy (left xy + right xy) fib: n -> (bool lt n 2)+ (1-(bool lt n 2))*(left fib_step^(n-1)(3/2)) f: n -> fib n