Zahlen

From Esolang
Jump to: navigation, search

Zahlen is a stack based golfing language discovered by User:Orby during March of 2019. Zahlen is particularly suited toward solving integer problems in a small amount of space. This page is a work in progress. Many operators are yet undocumented. Please contribute comments and ideas on the discussion page.

Introduction

Zahlen is intended for use in code golf competitions involving integers. Each Zahlen instruction is represented by a five bit code. This contrasts with the 8-bit codes used in most golfing languages. This makes Zahlen instructions more compact, but also restricts the number of available instructions. The code 11111 is reserved for future expansion.

Types

Zahlen operates on five types: logical, integer, list, and set. Logical values are sequences of bits. Operations performed on logical values are performed bit-wise. Integer values are unbounded integers. They are similar to the Java BigInteger. The list type represents a finite sequence of integers. In this documentation, lists are enclosed in parentheses. The set type represents a finite set of integers. In this documentation, sets are enclosed in curly braces. Logical values and integers can be trivially cast to one another in the usual way.

Type precedence

Whenever an operation involves multiple types, the result is the type of highest precedence unless noted otherwise. Type precedence from lowest to highest is: logical, integer, list, and set.

Unary operators

Each unary operator pops a value from the stack which we refer to here as a. The value pushed is dependent upon the type of a and the operator selected. Each operation is named after it's effect on an integer.

Count

00000 Result
Logical ( 1, ..., a )
Integer ( 1, ..., a )
List Sum of ai
Set Sum of a ∈ A

Factorial

00001 Result
Logical Sum of bits in a
Integer a!
List Product of ai
Set Product of a ∈ A

Increment

00010 Result
Logical ~a
Integer a + 1
List a1, (a2, ..., an)
Set min(A), A - { min(A) }

Decrement

00011 Result
Logical ~a
Integer a - 1
List an, (a1, ..., an - 1)
Set max(A), A - { max(A) }

Prime Factor

00100 Result
Logical ( ai ) where ai is the i-th bit of a.
Integer ( i : i is a prime factor of a repeated appropriately )
List sort A in ascending order
Set gcd(A)

Factor

00101 Result
Logical { i : i-th bit of a is set }
Integer { x : x is a factor of a }
List sort A in descending order
Set lcm(A)

Length

00110 Result
Logical Index of MSB in a
Integer abs(a)
List ( abs(ai) )
Set Cardinality of A

Negate

00111 Result
Logical ~a
Integer -a
List Reversal of A
Set Logical s.t. i-th bit is set if i ∈ A

Cast to Logical

10011 Result
Logical a as integer
Integer a as logical
List A as set (duplicates dropped)
Set A as tuple (sorted in ascending order)

Cast to Set

10100 Result
Logical { a }
Integer { a }
List Cardinality of A, a1, ..., an where ai ∈ A
Set Cardinality of A, a1, ..., an where ai ∈ A

Sign

10101 Result
Logical sign(a)
Integer sign(a)
List ( sign(ai) )
Set Cardinality of A


Binary operators

All binary operators in Zahlen pop two arguments from the stack and push the result. The first argument on the stack is represented as a and it's type is provided in the first column of each table. The second argument on the stack is represented as b and it's type is provided in the first row of each table. The operation performed depends upon the types of those arguments. Each operation is named after it's effect on two integers.

Addition

01000 Logical Integer List Set
Logical a ∨ b a + b ( a ∨ bi ) { a ∨ b : b ∈ B }
Integer a + b a + b ( a + bi ) { a + b : b ∈ B }
List ( ai ∨ b ) ( ai + b ) ( ai + bi ) A ∪ B
Set { a ∨ b : a ∈ A } { a + b : a ∈ A } A ∪ B A ∪ B

Multiplication

01001 Logical Integer List Set
Logical a ∧ b a * b ( a ∧ bi ) { a ∧ b : b ∈ B }
Integer a * b a * b ( a * bi ) { a * b : b ∈ B }
List ( ai ∧ b ) ( ai * b ) ( ai * bi ) A ∩ B
Set { a ∧ b : a ∈ A } { a * b : a ∈ A } A ∩ B A ∩ B

Subtraction

01010 Logical Integer List Set
Logical a ∧ ~b a - b ( a ∧ ~bi ) { a ∧ ~b : b ∈ B }
Integer a - b a - b ( a - bi ) { a - b : b ∈ B }
List ( ai ∧ ~b ) ( ai - b ) ( ai - bi ) A - B
Set { a ∧ ~b : a ∈ A } { a - b : a ∈ A } A - B A - B

Division

01011 Logical Integer List Set
Logical ~a ∧ b a / b ( ~a ∧ bi ) { ~a ∧ b : b ∈ B }
Integer a / b a / b ( a / bi ) { a / b : b ∈ B }
List ( ~ai ∧ b ) ( ai / b ) ( ai / bi ) B - A
Set { ~a ∧ b : a ∈ A } { a / b : a ∈ A } B - A B - A

Remainder

01100 Logical Integer List Set
Logical ~(a ∨ b) a % b ( ~(a ∨ bi) ) { ~(a ∨ b) : b ∈ B }
Integer a % b a % b ( a % bi ) { a % b : b ∈ B }
List ( ~(ai ∨ b) ) ( ai % b ) ( ai % bi ) A ∪ B
Set { ~(a ∨ b) : a ∈ A } { a % b : a ∈ A } A ∪ B A ∪ B

Combinations

01101 Logical Integer List Set
Logical ~(a ∧ b) a C b ( ~(a ∧ bi) ) { ~(a ∧ b) : b ∈ B }
Integer a C b a C b ( a C bi ) { a C b : b ∈ B }
List ( ~(ai ∧ b) ) ( ai C b ) ( ai C bi ) A ∩ B
Set { ~(a ∧ b) : a ∈ A } { a C b : a ∈ A } A ∩ B A ∩ B

Power

01110 Logical Integer List Set
Logical ~a ∨ b ab ( ~a ∨ bi ) { ~a ∨ b : b ∈ B }
Integer ab ab ( abi ) { ab : b ∈ B }
List ( ~ai ∨ b ) ( aib ) ( aibi ) A - B
Set { ~a ∨ b : a ∈ A } { ab : a ∈ A } A - B A - B

Logarithm

01111 Logical Integer List Set
Logical a ∨ ~b loga(b) ( a ∨ ~bi ) { a ∨ ~b : b ∈ B }
Integer loga(b) loga(b) ( loga(bi) ) { loga(b) : b ∈ B }
List ( ~ai ∨ b ) ( logai(b) ) ( logai(bi) ) B - A
Set { ~a ∨ b : a ∈ A } { loga(b) : a ∈ A } B - A B - A

Maximum

10000 Logical Integer List Set
Logical a ⨁ b max(a, b) ( a ⨁ bi ) { a ⨁ b : b ∈ B }
Integer max(a, b) max(a, b) ( max(a, bi) ) { max(a, b) : b ∈ B }
List ( ai ⨁ b ) ( max(ai, b) ) ( max(ai, bi) ) { x : x ∈ A ⨁ x ∈ B }
Set { a ⨁ b : a ∈ A } { max(a, b) : a ∈ A } { x : x ∈ A ⨁ x ∈ B } { x : x ∈ A ⨁ x ∈ B }

Minimum

10001 Logical Integer List Set
Logical a ≡ b min(a, b) ( a ≡ bi ) { a ≡ b : b ∈ B }
Integer min(a, b) min(a, b) ( min(a, bi) ) { min(a, b) : b ∈ B }
List ( ai ≡ b ) ( min(ai, b) ) ( min(ai, bi) ) { x : x ∈ A ⨁ x ∈ B }
Set { a ≡ b : a ∈ A } { min(a, b) : a ∈ A } { x : x ∈ A ⨁ x ∈ B } { x : x ∈ A ⨁ x ∈ B }

Concatenate

10010 Logical Integer List Set
Logical (a, b) (a, b) ( a, bi ) { a } ∪ B
Integer (a, b) (a, b) ( a, bi ) { a } ∪ B
List ( ai, b ) ( ai, b ) ( ai, bi ) A ∪ B
Set A ∪ { b } A ∪ { b } A ∪ B A ∪ B

Ternary operator

Documentation forthcoming.

Miscellaneous operators

The following operators manipulate the stack and control flow.

Encoding Name Description
10111 Swap Swap top two arguments on the stack
11000 Drop Pop the stack and drop it
11001 Dup Pop an integer a from the stack, then duplicate the a-th element on the stack.
11010 For Pop an integer a from the stack, then perform instructions between For and End a times.
11011 While Peek at top of stack. If value is not 0, (), or {} then execute code until End. Repeat.
11100 End See above.
11101 Push imm Push immediate value encoded after this. Encoding to be determined.

See also