RosettaCodeData/Task/Execute-a-Markov-algorithm/00DESCRIPTION

146 lines
3.9 KiB
Text
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
;Task:
Create an interpreter for a [[wp:Markov algorithm|Markov Algorithm]].
Rules have the syntax:
2013-04-10 23:57:08 -07:00
<ruleset> ::= ((<comment> | <rule>) <newline>+)*
<comment> ::= # {<any character>}
<rule> ::= <pattern> <whitespace> -> <whitespace> [.] <replacement>
<whitespace> ::= (<tab> | <space>) [<whitespace>]
2016-12-05 22:15:40 +01:00
There is one rule per line.
If there is a &nbsp; <b>.</b> &nbsp; (period) &nbsp; present before the &nbsp; '''<replacement>''', &nbsp; then this is a terminating rule in which case the interpreter must halt execution.
A ruleset consists of a sequence of rules, with optional comments.
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
<big><big> Rulesets </big></big>
2013-04-10 23:57:08 -07:00
Use the following tests on entries:
2016-12-05 22:15:40 +01:00
;Ruleset 1:
2013-04-10 23:57:08 -07:00
<pre>
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B -> bag
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule
</pre>
Sample text of:
2016-12-05 22:15:40 +01:00
: <code> I bought a B of As from T S. </code>
2013-04-10 23:57:08 -07:00
Should generate the output:
2016-12-05 22:15:40 +01:00
: <code> I bought a bag of apples from my brother. </code>
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
;Ruleset 2:
2013-04-10 23:57:08 -07:00
A test of the terminating rule
<pre>
# Slightly modified from the rules on Wikipedia
A -> apple
B -> bag
S -> .shop
T -> the
the shop -> my brother
a never used -> .terminating rule</pre>
Sample text of:
: <code>I bought a B of As from T S.</code>
Should generate:
: <code>I bought a bag of apples from T shop.</code>
2016-12-05 22:15:40 +01:00
;Ruleset 3:
2013-04-10 23:57:08 -07:00
This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
2016-12-05 22:15:40 +01:00
<pre>
# BNF Syntax testing rules
2013-04-10 23:57:08 -07:00
A -> apple
WWWW -> with
Bgage -> ->.*
B -> bag
->.* -> money
W -> WW
S -> .shop
T -> the
the shop -> my brother
2016-12-05 22:15:40 +01:00
a never used -> .terminating rule
</pre>
2013-04-10 23:57:08 -07:00
Sample text of:
: <code>I bought a B of As W my Bgage from T S.</code>
Should generate:
: <code>I bought a bag of apples with my money from T shop.</code>
2016-12-05 22:15:40 +01:00
;Ruleset 4:
This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. &nbsp; It implements a general unary multiplication engine. &nbsp; (Note that the input expression must be placed within underscores in this implementation.)
2013-04-10 23:57:08 -07:00
<pre>
### Unary Multiplication Engine, for testing Markov Algorithm implementations
### By Donal Fellows.
# Unary addition engine
_+1 -> _1+
1+1 -> 11+
# Pass for converting from the splitting of multiplication into ordinary
# addition
1! -> !1
,! -> !+
_! -> _
# Unary multiplication by duplicating left side, right side times
1*1 -> x,@y
1x -> xX
X, -> 1,1
X1 -> 1X
_x -> _X
,x -> ,X
y1 -> 1y
y_ -> _
# Next phase of applying
1@1 -> x,@y
1@_ -> @_
,@_ -> !_
++ -> +
# Termination cleanup for addition
_1 -> 1
1+_ -> 1
_+_ ->
</pre>
Sample text of:
2016-12-05 22:15:40 +01:00
: <code> _1111*11111_ </code>
2013-04-10 23:57:08 -07:00
should generate the output:
2016-12-05 22:15:40 +01:00
: <code> 11111111111111111111 </code>
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
;Ruleset 5:
2015-02-20 00:35:01 -05:00
A simple [http://en.wikipedia.org/wiki/Turing_machine Turing machine],
implementing a three-state [http://en.wikipedia.org/wiki/Busy_beaver busy beaver].
2016-12-05 22:15:40 +01:00
The tape consists of '''0'''s and '''1'''s, &nbsp; the states are '''A''', '''B''', '''C''' and '''H''' (for '''H'''alt), and the head position is indicated by writing the state letter before the character where the head is.
2015-02-20 00:35:01 -05:00
All parts of the initial tape the machine operates on have to be given in the input.
2013-04-10 23:57:08 -07:00
Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.
<pre>
# Turing machine: three-state busy beaver
#
# state A, symbol 0 => write 1, move right, new state B
A0 -> 1B
# state A, symbol 1 => write 1, move left, new state C
0A1 -> C01
1A1 -> C11
# state B, symbol 0 => write 1, move left, new state A
0B0 -> A01
1B0 -> A11
# state B, symbol 1 => write 1, move right, new state B
B1 -> 1B
# state C, symbol 0 => write 1, move left, new state B
0C0 -> B01
1C0 -> B11
# state C, symbol 1 => write 1, move left, halt
0C1 -> H01
1C1 -> H11
</pre>
This ruleset should turn
2016-12-05 22:15:40 +01:00
: <code> 000000A000000 </code>
2013-04-10 23:57:08 -07:00
into
2016-12-05 22:15:40 +01:00
: <code> 00011H1111000 </code>
<br><br>