# Markov algorithm

In theoretical computer science, a Markov algorithm is a string rewriting system that uses grammar-like rules to operate on strings of symbols. Markov algorithms have been shown to be Turing-complete, which means that they are suitable as a general model of computation and can represent any mathematical expression from its simple notation. Markov algorithms are named after the Soviet mathematician Andrey Markov, Jr.

Refal is a programming language based on Markov algorithms.

## Description

Normal algorithms are verbal, that is intended to be applied to words in different alphabets.

Definition of any normal algorithm consists of two parts: the definition of the alphabet algorithm (the algorithm will be applied to words of these alphabet symbols), and the definition of its scheme. Scheme normal algorithm is a finite ordered set of so-called substitution formulas, each of which can be simple or final. A simple formula substitutions are called words such as $L\to D$ , where $L$  and $D$  – are two arbitrary words in the alphabet of the algorithm (called, respectively, the left and right side of the formula substitution). Similarly, the final substitution formulas are called words of the form $L\to \cdot D$ , where $L$  and $D$  – are two arbitrary words in the alphabet of the algorithm. This assumes that the auxiliary characters $\to$  and $\to \cdot$  do not belong to the alphabet of the algorithm (otherwise an executable their role divider left and right sides should select the other two letters).

An example of a normal algorithm scheme in five-letter alphabet $|*abc$  can serve the following scheme:

$\left\{{\begin{matrix}|b&\to &ba|\\ab&\to &ba\\b&\to &\\{*}|&\to &b*&\\{*}&\to &c&\\|c&\to &c\\ac&\to &c|\\c&\to \cdot \end{matrix}}\right.$

The process of applying the normal algorithm to an arbitrary word $V$  in the alphabet of this algorithm is a discrete sequence of elementary steps, consisting of the following. Let’s assume that $V'$  is the word obtained in the previous step of the algorithm (or the original word $V$ , if the current step is the first). If among formulas of substitution there is no left-hand side of which would be included in the $V'$ , then the work of the algorithm is considered completed, and the result of this work is considered to be the word $V'$ . Otherwise, including the substitution of the left side of which is included in the $V'$ , the very first part is selected. If the substitution formula looks like $L\to \cdot D$ , then out of all of possible representations of the word $V'$  that looks like $RLS$  it is chosen one $R$ , which is the shortest. Then the work of the algorithm is considered completed with the result $RDS$ . However, if this substitution formula looks like $L\to D$ , then out of all of possible representations of the word $V'$  in the form of $RLS$  it is chosen one, in which $R$  – the shortest, after which the word $RDS$  is considered to be the result of the current step, subject to further processing in the next step.

For example, the process of applying the algorithm described above to the word $|*||$  results in the sequence of words $|b*|$ , $ba|*|$ , $a|*|$ , $a|b*$ , $aba|*$ , $baa|*$ , $aa|*$ , $aa|c$ , $aac$ , $ac|$  and $c||$ , after which the algorithm stops with the result $.||$

For other examples, see below.

Any normal algorithm is equivalent to some Turing machine, and vice versa – any Turing machine is equivalent to some normal algorithm. A version of the Church-Turing thesis formulated in relation to the normal algorithm is called the "principle of normalization."

Normal algorithms have proved to be a convenient means for the construction of many sections of constructive mathematics. Moreover, inherent in the definition of a normal algorithm used in a number of ideas aimed at handling symbolic information programming languages – for example, in Refal.

## Algorithm

The Rules is a sequence of pair of strings, usually presented in the form of patternreplacement. Each rule may be either ordinary or terminating.

Given an input string:

1. Check the Rules in order from top to bottom to see whether any of the patterns can be found in the input string.
2. If none is found, the algorithm stops.
3. If one (or more) is found, use the first of them to replace the leftmost occurrence of matched text in the input string with its replacement.
4. If the rule just applied was a terminating one, the algorithm stops.
5. Go to step 1.

Note that after each rule application the search starts over from the first rule.

## Example

The following example shows the basic operation of a Markov algorithm.

### Rules

1. "A" -> "apple"
2. "B" -> "bag"
3. "S" -> "shop"
4. "T" -> "the"
5. "the shop" -> "my brother"
6. "a never used" -> ."terminating rule"

### Symbol string

"I bought a B of As from T S."

### Execution

If the algorithm is applied to the above example, the Symbol string will change in the following manner.

1. "I bought a B of apples from T S."
2. "I bought a bag of apples from T S."
3. "I bought a bag of apples from T shop."
4. "I bought a bag of apples from the shop."
5. "I bought a bag of apples from my brother."

The algorithm will then terminate.

## Another Example

These rules give a more interesting example. They rewrite binary numbers to their unary counterparts. For example: 101 will be rewritten to a string of 5 consecutive bars.

### Rules

1. "1" -> "0|"
2. "|0" -> "0||"
3. "0" -> ""

"101"

### Execution

If the algorithm is applied to the above example, it will terminate after the following steps.

1. "101"
2. "0|01"
3. "00||1"
4. "00||0|"
5. "00|0|||"
6. "000|||||"
7. "00|||||"
8. "0|||||"
9. "|||||"