This short section of code implements an agent that can traverse a hexagonally tiled space. The implementation follows from the 3-colourability and 7-colourability of a regular hexagonal tiling. By providing a valid colouring in the instantiation step the agent can STEP, turn LEFT and turn RIGHT while keeping track of the colour of tile it is on now and tiles around it.
The agent is uses closures to implement a quine pattern: once the initial agent is created, whenever it is given an instruction to move or turn it creates an "evolved" version updated to reflect the change.
This code is very minimal. It could be adapted to create a space on the fly rather than just traversing one, for example. The decision to add a tape means that the program is Turing-complete, so this program also satisfies the criteria for a Universal Turing Machine (proof is left as an exercise, but contact me if you want clarification).
The style here was heavily influenced by Prolog programming and the Recipe in the Perl Cookbook relating to using closures. This particular code came about as part of a general interest in using Perl to experiment in geometry and number theory.
Program output shows blurble from instantiation through actions "STEP", "LEFT", "STEP", "RIGHT", "STEP":
I start at 0 in both three-coloured and seven-coloured space. The environment in the respective spaces are [0,1,2] and [0,1,2,3,4,5, +6]. I am now at 1 in 3-space and 0 in 7-space. I am now at 1 in 3-space and 0 in 7-space. (the above two lines should print the same) The environment in 3-space is (1,2,0) The environment in 7-space is (0,1,2,3,4,5,6) My tape reads: STEP3 0 1 STEP7 0 1 LEFT STEP3 1 0 STEP7 1 4 RIGHT STEP3 0 1 STEP7 4 0 SELF SELVES
#!/usr/bin/perl -w # (c) Declan Malone 2008 # Licensed under the terms of GPL 2 or later at your discretion # Spurious info at project343.blogspot.com sub quine { # agent for traversing a bi-coloured hexagonal space my $colouring_mod3=shift; # ref to list of colours [0,1,2] my $colouring_mod7=shift; # ref to list of colours [0,1,2,3,4,5,6 +] my $construction=shift; # debug trail my ($t0,$t1,$t2)=@$colouring_mod3; my ($s0,$s1,$s2,$s3,$s4,$s5,$s6)=@$colouring_mod7; my $methods= { "STEP" => # forward one step (0->1) sub { push @$construction, "STEP3 $t0 $t1"; push @$construction, "STEP7 $s0 $s1"; quine([$t1,$t2,$t0],[$s1,$s3,$s5,$s2,$s0,$s6,$s4],$construction +); }, "LEFT" => # turn left 60 degrees sub { push @$construction, "LEFT"; quine([$t0,$t2,$t1],[$s0,$s6,$s1,$s2,$s3,$s4,$s5],$construction +); }, "RIGHT" => # turn right 60 degrees sub { push @$construction, "RIGHT"; quine([$t0,$t2,$t1],[$s0,$s2,$s3,$s4,$s5,$s6,$s1],$construction +); }, "SELF" => sub { push @$construction, "SELF"; return ($t0,$s0); }, "SELVES" => sub { push @$construction, "SELVES"; return ($colouring_mod3,$colouring_mod7); }, "TRAIL" => sub { $construction; } }; return $methods; } my ($threes,$sevens); my $trail=[]; $h=quine([0..2],[0..6],$trail); print "I start at 0 in both three-coloured and seven-coloured space.\n +"; print "The environment in the respective spaces are [0,1,2] and [0,1,2 +,3,4,5,6].\n"; $h=$h->{STEP}->(); $h=$h->{LEFT}->(); $h=$h->{STEP}->(); $h=$h->{RIGHT}->(); $h=$h->{STEP}->(); ($three,$seven)=$h->{SELF}->(); ($threes,$sevens)=$h->{SELVES}->(); #print "threes is $threes and its ref is ", ref($threes), "\n"; #print "sevens is $sevens and its ref is ", ref($sevens), "\n"; print "I am now at $threes->[0] in 3-space and $sevens->[0] in 7-space +.\n"; print "I am now at $three in 3-space and $seven in 7-space.\n"; print "(the above two lines should print the same)\n"; print "The environment in 3-space is (", (join ",", @$threes), ")\n"; print "The environment in 7-space is (", (join ",", @$sevens), ")\n"; print "My tape reads:\n ", (join "\n ", @{$h->{TRAIL}->()}), "\n";
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |