Assignment B – The Dragon Curve

Assignment B – The Dragon Curve🔗

Due Thu 6/5 11:59pm

This year, Shin-Cheng Mu visited us and taught us about the Heighway Dragon, this beautiful curve:

During his lecture, he gave us two characterizations of the dragon. Both describe it as functions from the natural numbers to lists of turns, either left turns or right turns. To understand the curve as a list of turns, imagine that you are working your way down a list of turns, following them as if they were instructions. An L means to walk one pace forward, turn left, and then walk another pace forward. Similarly, R means to walk one pace forward, turn right, and then walk another pace forward. The path that you walk is the dragon.

In both characterizations of the curve, the 0th dragon is an empty list of turns. To go from the nth dragon to the n+1st dragon in the first of the characterizations, start with the nth dragon, and make a copy of it, connecting the end of the original to the end of the copy with a left turn in between. Because the end of the copy is connected to the end of the original, however, this means you’re going through the same turns, but in the opposite order and with the turns swapped (L becomes R and vice versa).

To understand this, have a look at the dragons of sizes 2, 3, and 4.

The size 2 dragon is the turns L, L, R the size 3 dragon is the turns L, L, R, L, L, R, R. Read the size three dragon from the left and the right at the same time. As you work your way inwards from the ends, the turns match up, where the ones on the right are the opposites of the ones on the left. Specifically, starting from the beginning of the size 3 curve you have exactly the size 2 curve: L, L, R, and reading from end towards the middle you have R, R, L. Then, the one turn in the middle is a left turn. This happens again to build the size four from the size thee, as L, L, R, L, L, R, R becomes L, L, R, L, L, R, R, L, L, L, R, R, L, R, R

Here’s the agda code for this dragon:

 

data Turn : Set where

  L : Turn

  R : Turn

 

inv : Turn -> Turn

inv L = R

inv R = L

 

dragonU : ℕ -> 𝕃 Turn

dragonU zero = []

dragonU (suc n) =

  dragonU n ++

 ((L :: []) ++

  map inv (reverse (dragonU n)))

The second characterization of the curve that Shin-Cheng gave us is to alternatingly add Ls and Rs in between the existing turns to go from the nth dragon to the n+1st dragon. So, if we start with the dragon of size 2 again, L, L, R, and then look at the odd index elements of the size 3 dragon (colored red here): L, L, R, L, L, R, R we see that the they are exactly the size 2 dragon. The other turns start with L and alternate between L and R.

Here’s the agda code for this characterization:

 

LR> : 𝕃 Turn -> 𝕃 Turn

RL> : 𝕃 Turn -> 𝕃 Turn

 

LR> [] = L :: []

LR> (t :: ts) = L :: (t :: (RL> ts))

 

RL> [] = R :: []

RL> (t :: ts) = R :: (t :: (LR> ts))

 

dragonE : ℕ -> 𝕃 Turn

dragonE zero = []

dragonE (suc n) = LR> (dragonE n)

This code is slightly different from the code Shin-Cheng used. Specifically, he works with a generic interleave function (written ) that is happy to receive an infinite lists and a finite list, so he passes in this infinite list:

LR = L :: R :: LR

And he also uses the tail of that infinite list, which is this infinite list:

RL = R :: L :: RL

Defining such things is possible in Agda but requires more machinery than we really want to get into. Since he only ever uses the interleave function on these two lists, I’ve specialized his general-purpose interleave function to two specific functions, LR> that interleaves the LR list into its argument and the RL> function that interleaves the RL list into its argument.

So, to complete this assignment, you must produce a proof in Agda that the two formulations are the same, filling in the hole.

 

thm : ∀ n -> dragonU n ≡ dragonE n

thm = ?

 

The way I succeeded at this was by watching the video of his talk and following his proof, step by step.

Please be aware that this proof can get unwieldy. Be sure to be disciplined when you work on it! Agda will punish you if you go too deep into “video-gaming” mode. In small doses, video gaming mode can be helpful, but be aware you’re doing it and be prepared to back out and think about what the goals mean.

I ran into a few complexities as I was solving the exercise and here is some advice based on that experience. Don’t feel like you have to follow the path I took, however; indeed if you wish to take an approach that’s different from Shin-Cheng’s, that’s also fine. As long as you don’t change any of the code above, any proof that Agda accepts in this hole is a good proof!

Good luck!