© Hindawi Publishing Corp. A CLASSIFICATION OF PERIODIC TURTLE SEQUENCES

A turtle sequence is a word constructed from an alphabet of two letters: F, which represents the forward motion of a turtle in the plane, and L, which represents a counterclockwise turn. In this paper, we investigate such sequences and establish links between the combinatoric properties of words and the geometric properties of the curves they generate. In particular, we classify periodic turtle sequences in terms of their closure (or lack thereof).


Introduction.
In [1], Abelson and diSessa wrote "A final important difference between turtle geometry and coordinate geometry is that turtle geometry characteristically describes geometric objects in terms of procedures rather than in terms of equations. In formulating turtle-geometric descriptions we have access to an entire range of procedural mechanisms (such as iteration) that are hard to capture in the traditional algebraic formalism (pages 14-15)." In this note, we show that iteration is, in fact, fairly simple to capture using algebraic formalism. Using the algebraic structure of words constructed from an alphabet of turtle commands, we examine the geometry of the turtle trajectories produced by repeated iteration of a given turtle program. In particular, we provide a classification of periodic turtle sequences based on the closure (or lack thereof) of the polygonal curves they encode. In doing so, we revisit some previously known results from [1]: the Closed-Path theorem, the POLY Closing theorem, and the Looping lemma. Such results fall out easily once the algebraic formalism is in place.
A turtle trajectory can be encoded efficiently as a string of characters. Let F represent a forward motion of the turtle by one unit and L a change in the turtle's heading by a fixed angle θ in the counterclockwise direction. For our purposes, forward steps always have length one and the angle of rotation is fixed at θ = 2π/N for some integer N. Then a string, or word, of F 's and L's represents a succession of turtle commands and hence a polygonal curve in the plane. Any curve described in this way is said to be a turtle trajectory. Following the definition given in [1], a turtle trajectory is said to be closed if its turtle program restores the turtle's original heading and position.

Turtle sequences.
Let Σ be the alphabet Σ = {L, F }. Define a turtle sequence w to be any word over Σ, that is, any finite sequence of letters of Σ. The length of the turtle sequence, denoted by |w|, is the number of letters in w. The set of all words over Σ, denoted by Σ * , is a monoid under the operation of concatenation of words, that is, for w 1 ,w 2 ∈ Σ * , w 1 · w 2 = w 1 w 2 . The word that has no letters is the empty word and we denote it by ε. The empty word signifies no change in the turtle's heading or position. Clearly, ε is the identity element of the monoid Σ * since ε · w = w = w · ε.
Each turtle sequence w in Σ * has a unique representation of the form where all m i and n i are positive, except perhaps n 1 and m k , which may be zero. Note that each factor L n i F m i represents a linear segment of the turtle trajectory of length m i , so that the word w encodes a curve composed of k linear segments (or k − 1 linear segments if m k = 0). By |w| L , we denote the number of L's in w, so that |w| L = k i=1 n i . Similarly, |w| F denotes the number of F 's in w and |w| F = k i=1 m i . The latter quantity, |w| F , measures the total length of the turtle trajectory described by w, while |w| L measures the total turning.

Turtle geometry and the turtle transformation group.
A turtle state is an ordered pair ( r ,v) consisting of a position vector r ∈ R 2 and a unit vector v describing the turtle's heading. The basic turtle commands F and L define certain changes in the turtle's state; the command F represents the transformation T F mapping the state ( r ,v) to the state ( r +v,v), and L represents the transformation T L mapping ( r ,v) to ( r ,R θv ), where R θ is the rotation matrix A string w of F 's and L's then describes the general turtle transformation T w consisting of compositions of these two basic transformations. As it turns out, such transformation T w can be expressed in the form T w ( r ,v) = ( r +Mv, Rv), where M is a matrix of the form a −b b a and R = R k θ for some a, b ∈ R and some positive integer k [2]. To illustrate, if w = LF , then T w ( r ,v) = T F (T L ( r ,v)) = T F ( r ,R θv ) = ( r + R θv , R θv ). Hence, if M 2 (R) is the set and R θ = {R k θ | k ∈ Z}, then every turtle transformation can be described using a pair of matrices (M, R) ∈ M 2 (R)× R θ . For example, F corresponds to the pair (I, I), L corresponds to (0,R θ ), and LF corresponds to (R θ ,R θ ), where I is the 2 × 2 identity matrix and 0 the 2 × 2 zero matrix. Equipped with the right binary operation, the set of pairs (M, R) forms a group Ᏻ called the turtle transformation group.
To see that the given operation is a binary operation, note that M 2 (R) is closed under matrix addition and multiplication and that R θ is a subgroup of M 2 (R). Hence, The associativity follows from a straightforward computation.
The reader may recognize Ᏻ as the semidirect product of M 2 (R) and R θ with respect to the group action φ : [3]. As it turns out, this group is a very useful computational tool. Indeed, Ᏻ provides an efficient way of computing the turtle's new position and heading following a succession of motions defined by a string in Σ * . Given (2.5) be two words in Σ * , and consider ψ(w 1 · w 2 ). Set n k+i = s i and m k+i = r i for 1 ≤ i ≤ l, and compute (2.6) Next, let π 1 : Ᏻ → M 2 (R) and π 2 : Ᏻ → R θ be, respectively, the projection mappings Then, for any given turtle program in Σ * , the projection π 1 • ψ : Σ * → M 2 (R) describes the turtle's change of position, while π 2 • ψ describes the change in heading. Sequences in Σ * encoding closed turtle trajectories are precisely those elements in the kernel of ψ, while sequences encoding turtle trajectories that restore the turtle's position are identified with the kernel of π 1 • ψ. Similarly, the kernel of π 2 • ψ contains those sequences representing trajectories that restore the turtle's heading.
Note that the definition of a closed sequence is consistent with the definition of a closed turtle trajectory. Certainly, w ∈ ker(ψ) if and only if w ∈ ker(π 1 • ψ) and w ∈ ker(π 2 • ψ), so w is a closed sequence if and only if w is both PP and HP. The theorems to follow illustrate the benefit of reformulating closure in this algebraic way.
We will start by proving the Closed-Path theorem of Abelson and diSessa, which states that the total turning along any closed turtle path is an integer multiple of 2π [1]. Since L represents a counterclockwise rotation of 2π/N, where N is an integer, the quantity |w| L · 2π/N measures the total turning of a turtle following the trajectory defined by w. Clearly, |w| L · 2π/N is an integer multiple of 2π if and only if |w| L is an integer multiple of N. Hence, the Closed-Path theorem of Abelson and diSessa translates into the following theorem.
Theorem 2.4. If w ∈ Σ * is a closed sequence, then |w| L = kN for some positive integer k.
encodes a closed turtle path, then w must preserve the turtle's heading, meaning that w ∈ ker(π 2 • ψ). But w ∈ ker(π 2 • ψ) if and only if R

Periodic turtle sequences and the classification theorem.
Our purpose now is to examine those turtle trajectories produced by the repeated iteration of a given turtle program w in Σ * . Iterations of w correspond to products of the form w n in Σ * , where n is the number of iterations. That is, iterations correspond to certain periodic turtle sequences.
A word w = n i=1 a i ∈ Σ * is said to be periodic if there exists a positive integer p such that a p+i = a i for i = 1, 2,...,n− p. (3.1) Such an integer p is called a period of w. The smallest period p of w is called the period, and it is denoted by p(w), and the positive rational number |w|/p(w), denoted by ord(w), is called the order of w. Finally, a word u ∈ Σ * is said to be a prefix of w ∈ Σ * if w = ux for some w ∈ Σ * . Because the turtle program w is applied in full with each iteration, it makes sense to consider only those periodic turtle sequences having integer-valued orders. Indeed, every iterated turtle program will have the form z = w n , where w ∈ Σ * and n is a positive integer. We think of w, in this situation, as the generating sequence of the turtle trajectory.
Definition 3.1. A nonempty turtle sequence w is said to generate a closed curve if there exists a positive integer n such that w n is a closed sequence, that is, if for some n, w n ∈ ker ψ.
Clearly, a closed turtle sequence will generate a closed curve. The theorem to follow classifies all the turtle sequences in terms of the closure of their encoded turtle trajectories. As the theorem indicates, most turtle sequences will generate closed curves. Theorem 3.2 (classification of turtle sequences). Any nonempty turtle sequence w will generate a closed curve except for those sequences that are HP but not PP. If w is HP and not PP, then the turtle trajectory generated by w is not closed.
Proof. Let w ∈ Σ * , w ≠ ε. If w is itself a closed sequence, then we are done; w will generate a closed curve. If w ∉ ker ψ, then we consider two cases: w is HP (but not PP) and w is not HP.
The result then follows from the two identities  Thus, ψ(w N ) = (0,I) and w generates a closed curve. Note that, in general, N will not be the smallest integer to produce an element in the kernel. Indeed, as Theorem 4.1 indicates, α = N/ gcd (N, β) is the most efficient choice.
Case 1 of the classification theorem shows that the converse of the Closed-Path theorem is false. If w ∈ Σ * is such that |w| L = kN for some integer k, then w will not generate a closed sequence unless w is PP. More interesting, however, is the fact that any sequence w with |w| L ≠ kN, for any integer k, will generate a closed trajectory. Corollary 3.3. Let w ∈ Σ * . If |w| L ≠ kN, for any integer k, then w generates a closed curve.
Proof. The corollary follows directly from Case 2 of the previous proof because |w| L = kN if and only if w ∈ ker(π 2 • ψ).
Our discussion thus far has focused on the classification of periodic sequences generated by a given sequence w ∈ Σ * . More generally, to classify any periodic sequence v, we simply locate the generating sequence w of v. The theorem to follow illustrates how this can be done. Again, we will assume that the periodic sequences have integer-valued orders. Proof. Assume that w ∈ Σ * is periodic with w = u n , where u is the prefix of w of length p(w) and n = ord(w). Then w k ∈ ker ψ implies that u nk ∈ ker ψ. Hence, if w generates a closed curve, then u does also. Conversely, if u k ∈ ker ψ for some positive integer k, then ψ(w k ) = ψ(u nk ) = ψ(u k ) n = (0,I). Thus, w k ∈ ker ψ and w generates a closed curve.

Other results.
If w generates a closed curve, then w n ∈ ker ψ for some n. But, what is the most efficient choice for n? That is, what is the smallest possible integer n to yield a closed sequence? Theorem 4.1 answers this question.
Theorem 4.1. Assume that w ∈ Σ * generates a closed curve and that α = N/ gcd (N, β), where β = |w| L . Then w α ∈ ker ψ and w k ∉ ker ψ for all integers 0 < k < α. That is, α is the smallest positive integer to produce a closed sequence.
Proof. Following the argument presented in Case 2 of the proof of Theorem 3.2, we find that w k ∈ ker ψ if and only if R β θ + R Next, we revisit Abelson and diSessa's POLY Closing theorem. A POLY program is the simplest of programs; it consists of the repeated iteration of a forward motion followed by a rotation. The POLY Closing theorem states that a turtle path drawn by a POLY program closes precisely when the total turning reaches a multiple of 360 degrees [1]. The result can be reformulated using turtle sequences and proved using Theorems 3.2 and 4.1. The word w = FL β corresponds to the turtle program consisting of a forward motion followed by a rotation of β/N · 2π radians. Hence, considering w = FL β , we allow for a rotation of any rational amount. One could also consider w = F k L β to allow for forward motions of any integer-valued length.
Theorem 4.2. If 1 ≤ β < N, then the word w = FL β generates a closed curve. This curve closes precisely when the total turning reaches an integer multiple of 2π radians.
Assuming that θ = 2π/N and 1 ≤ β < N, we conclude that FL β ∉ ker(π 1 • ψ) and FL β ∉ ker(π 2 • ψ). Hence, w = FL β is neither PP nor HP, and Case 2 of the classification theorem applies. If α = N/ gcd(N, β), then α is the smallest positive integer having the property that w α ∈ ker ψ. The total turning of w α is (4.1) Our final theorem is a translation of Abelson and diSessa's Looping lemma, which states that "any program that is just a repetition of some basic loop of turtle instructions has precisely the structure of POLY with an angle input equal to T , the total turning in the loop." The language of turtle sequences clarifies both the statement and the proof of this nice result.

Further exploration.
In this note, we examined turtle sequences representing forward motions of fixed length (one) and rotations of the form θ = 2π/N for some integer N. Further exploration could address the more general situation, where forward motions can vary in length and θ might be irrational.
Further exploration could also address more complicated turtle trajectories, including curves that are defined by iterating a set of rewriting rules on {F,L}. In particular, it would be interesting to apply the formalism presented here to fractal curves encoded by Lindenmayer systems.