Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Running Varphi Programs

Once you have the Varphi Interpreter (vpi) installed (see Installation), you can execute, debug, and analyze your Turing Machine programs directly from the command line.

For those who just want to trial Varphi or for students looking for an interactive web-based simulator, check out the Varphi Sandbox!

Basic Usage

The general syntax for the interpreter is:

vpi [OPTIONS] INPUT_FILE
  • INPUT_FILE: The path to your .vp source code.

Running a Program

To run a program, simply pass the filename. The interpreter will compile the code and then prompt you to enter the initial content for each tape defined in your program via standard input (stdin).

vpi example.vp

Example Session:

$ vpi example.vp
Number of input tapes: 1
Tape 1: hello

————————————————————————————————————————————————————————————
HALTED at state 'accept'
Time taken: 25 steps
Space used: 11 cells
Number of tapes: 1
Tape 1: hello

Input Formatting & Tape Behavior

Since Varphi allows alphanumeric characters on tapes, there are specific rules for how to represent blank cells and how the machine initializes head positions.

Representing Blanks (_)

The standard BLANK keyword used in Varphi source code programs cannot be typed easily in a terminal without confusing it for the characters "B", "L", "A", "N", and "K". When providing input, use the underscore _ to represent a blank cell.

  • Example Input: abc_123

    • Varphi will interpret this as: ['a', 'b', 'c', BLANK, '1', '2', '3']

In any output of the Varphi Interpreter, a blank tape cell will also be represented with _.

Customizing the Blank Character: If your tape data naturally contains underscores, you can change the character used to represent blanks in the terminal using the --blank-char (or -b) flag. For example:

Head Positioning

When a program starts, the tape head, for any tape of the machine, is always stationed at the first non-blank character of your input string. Leading blanks are essentially skipped for the purpose of initialization. If there is no non-blank character on the input tape, an arbitrary blank tape cell will be chosen.

Example: If you input _____123, the tape will contain those leading blanks (obviously, because there are infinite blanks in either direction anyways), but the head will start off pointing at 1, as shown below:

Tape Count & Composition

The input system allows you to input more or less tapes than the machine actually needs (kk), according to two rules:

  • Fewer Tapes: If you provide less tapes than required, the remaining tapes are automatically initialized to be blank.

  • More Tapes: If you provide more tapes than required, only the first kk are used.

This allows convenient composition of Turing machines by piping the output of one to another, even if the two machines do not use the same number of tapes.

Execution Modes

vpi supports several flags to alter how the program is run.

Standard Run

By default, vpi runs silently until the machine halts. Upon completion, it prints the final state of all tapes and performance metrics (see below).

Debug Mode (--debug)

The --debug flag activates an interactive, step-by-step debugger. This is essential for visualizing state transitions and head movements.

  • Visual Head: The character currently under the tape head is enclosed in brackets (e.g., [1]).

  • Stepping: Press ENTER to advance the machine by one step.

  • Interruption: You can stop execution at any time using Ctrl+C.

Output Preview:

Syntax Check (--check)

If you only want to verify that your code is valid without running it, use --check. This is useful for catching syntax errors quickly.

If the code is valid, it prints OK. If there are errors, the compiler driver will point to the exact line and column of the issue. An example of a compilation error is shown below:

Understanding Performance Metrics

When a Varphi program halts, the interpreter reports Time taken and Space used. It is important to understand how these are calculated.

Time Complexity

Time taken is simply the total number of transitions (steps) the machine executed from the start state until it reached a halting configuration. You can see the exact steps by using the --debug flag.

Space Complexity (Auxiliary Space)

Space used calculates the Auxiliary Space complexity, not the total tape lengths.

  • The cells occupied by your initial input do not count toward space complexity.

  • Space is only counted when the machine writes to or visits a cell outside the range of the original input.

For example, if you input a string of length 5, and the machine only moves back and forth within those 5 cells, the Space used will be 0 cells. If it moves one step to the right of the input and reads that tape cell, the space used becomes 1 cell.

CLI Reference

Programmatic API (Python)

Varphi provides a robust Python API for executing compiled Turing Machines dynamically from your own Python scripts.

You can compile Varphi programs in-memory using the VarphiToPythonCompiler. By executing the compiled Python code in an isolated namespace, you can extract the machine's configuration and run it cleanly via the run_machine function. This avoids redirecting stdin and stdout, providing direct programmatic access to the TuringMachine and its Tape objects.

Alternatively, you can opt to control the compiled Varphi program over standard input:

Last updated

Was this helpful?