Vafix

From Esolang
Jump to navigation Jump to search
Vafix
Paradigm(s) Declarative
Designed by A
Appeared in 2019
Computational class Combinational logic
Major implementations Interpreter
File extension(s) .x


Vafix is a thought experiment related to making all operations variadic by supporting nilads, monads, and dyads in a single operator. This is dedicated to adding 1/3 more operator functions to the existing APL function set. However, this makes calling monadic functions harder because now parentheses are mandatory for all monadic function calls. This is dedicated to be challenging for beginners and as confusing as anyfix.

Instruction reference

Did you assume that the Vafix instruction set was very dense? Actually no. Vafix only has 2 operators listed below:

Operator What it does
+
Number of operands What it does
Dyadic Addition
Monadic Identity
Niladic Return 1
-
Number of operands What it does
Dyadic Subtraction
Monadic Negation
Niladic Return -1

All other functions are only nilads that return 0 and do nothing else. (Except for parentheses, which group operators; I will explain this later.)

Execution scheme

The Vafix execution scheme is actually quite simple: just do a depth-first search to find guesses that make the expression valid. Vafix tries to assume that operators take a specified number of operands in the following order: Dyadic -> Monadic -> Niladic.

Is the description above too abstract? Well, here is an example. Let's try to interpret this expression:

+++++

Right, there is one more thing I have not yet mentioned. The Vafix guessing starts from the right and tries to guess expressions from right to left.

Now Vafix tries to guess the last + in this program. First, it tries to guess that it is dyadic, which is impossible because this requires the right hand side in the standard infix notation(a+b) and this expression only provides the left hand side. Then, it tries to guess that it is monadic, which is also impossible because similar to the dyadic check, the right hand side is mandatory. Therefore, this + can only be niladic. Now let us assume that the 1 symbol represents a niladic +:

++++1

Now Vafix starts to guess the number of operands for the second-to-last + in this expression. It guesses Dyadic, and no errors are generated. (However, if the expression fails to be valid with this guess, it changes the guess to Monadic. Simple as that.) Let us assume that the means the dyadic +:

+++﹢1

Now Vafix tries to guess the number of operands for the third-to-last expression in this expression. First it tries to guess that this is dyadic. This is invalid, because you may not use two dyadic operators in a row. Secondly it tries to guess that it is monadic. This is also invalid because a niladic or monadic operator is expected, yet the operator given is dyadic. Finally it assumes that this is niladic. No errors are generated.

++1﹢1

Similarly to the above process, we can see that the guessing results in the expression

1﹢1﹢1

Vafix in comparision with a normal infix

This is a tutorial for presenting expressions in Vafix and also presents more features.

So, how would we do negation in Vafix? Say we have this infix expression:

-(-1+1)

We can't remove the parentheses, because this will make the expression ambiguous. (Not even APL can remove them. This will produce the unintended result 2.)

We want to replace the -1 and 1 with - and + in its niladic forms.

-(-++)

Now Vafix tries to guess how many operands this operation - takes. First Vafix guesses that the operator takes two operands. This is invalid because an operator that takes two operands require the left hand side. Then, this guesses that the operator is monadic. This is valid, and this is kept. Therefore we can golf the expression into:

--++

Parentheses and rules

  • Use parentheses to group operators. There is no operator precedence in Vafix.
  • Parentheses are used to separate a part of an expression with the rest of the expression. The expression in parentheses will be evaluated separately as different instructions and are evaluated (or guessed in Vafix terms) first.
  • The execution of Vafix is backwards, in the spirit of APL.

Computational class

Obviously Vafix is a model of combinational logic, due to analogies to the Boolean algebra.

Examples

Simply return the number -10

-+-+-+-+-+-+-+-+-+-

Calculates 7+(-5)

++++++++++++++-+-+-+-+-

Implementations

The source code in all its glory:

#!/usr/bin/python3
import sys
file = open(sys.argv[1],"r+")
f=file.read()
f=f.replace("(","")
f=f.replace(")","")
s=[0]*len(f)
for i in range(0,len(f)):
    if i%2==0:
        s[-i]=2

if len(f)%2:
    s[0]=0
else:
    s[0]=1

# Niladic : 0
# Monadic : 1
# Dyadic : 2
out=""
brackets=0
for i in range(len(f)):
    if s[i]==0:
        if f[i]=='+':
            out+='1'
        elif f[i]=='-':
            out+='-1'
    elif s[i]==1:
        out+=f[i]+"("
        brackets+=1
    else:
        out+=f[i]
        
out+=")"*brackets

print(out)
print(eval(out))