s LearnSqueak.com

SMALLTALK LAB

De Bruijn Sequences in Squeak: Every Bit Pattern in One Ring

Build a cyclic binary sequence in which every short pattern appears once. Enumerate cyclic windows, then construct a ring with a stack.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - See why overlap saves space

Two length-three words normally take six characters. By overlapping their shared 01, four characters show both. A ring takes this trick one step further.

Independent Workspace expression - select all, then Print it
| text |
text := '0010'.
{text copyFrom: 1 to: 3. text copyFrom: 2 to: 4}

Expected: {'001'. '010'}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: See why overlap saves space
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Name the two shared characters before you run it.

Download this checkpoint

02 - Read across the seam

One window starts at each position, including the last. Convert the index to zero-based, wrap with remainder, then add one again for Smalltalk indexing.

Independent Workspace expression - select all, then Print it
| scan |
scan := [:ring :width | (1 to: ring size) collect: [:start |
 String streamContents: [:s |
  0 to: width - 1 do: [:offset |
   s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
(scan value: '0011' value: 2) asArray

Expected: {'00'. '01'. '11'. '10'}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Read across the seam
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The final window uses the last character and then the first.

Download this checkpoint

03 - Encode a shifted two-bit node

For width three, graph nodes carry two bits. Node 2 is 10. Appending 0 or 1 traverses edge 100 or 101 and retains the rightmost two bits as the next node.

Independent Workspace expression - select all, then Print it
| node |
node := 2.
{((node bitShift: 1) + 0) bitAnd: 3.
 ((node bitShift: 1) + 1) bitAnd: 3}

Expected: #(0 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Encode a shifted two-bit node
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try node 1 (01). It should lead to 2 and 3.

Download this checkpoint

04 - Build a cyclic ring

Each node tracks which of its two outgoing edges to use next. Push the destination when an unused edge exists; otherwise pop it into the reverse trail. Reading the low bits of the finished trail gives the ring, up to rotation. Use width two or greater with this encoding.

Independent Workspace expression - select all, then Print it
| make |
make := [:width | | mask next stack back digits |
 mask := (1 bitShift: width - 1) - 1.
 next := Array new: mask + 1 withAll: 0.
 stack := OrderedCollection with: 0.
 back := OrderedCollection new.
 [stack notEmpty] whileTrue: [ | node bit newNode |
  node := stack last.
  bit := next at: node + 1.
  bit < 2 ifTrue: [
   next at: node + 1 put: bit + 1.
   newNode := ((node bitShift: 1) + bit) bitAnd: mask.
   stack addLast: newNode]
   ifFalse: [back addLast: stack removeLast]].
 digits := back reversed allButFirst.
 String streamContents: [:s | digits do: [:node |
  s nextPut: ((node bitAnd: 1) = 0 ifTrue: [$0] ifFalse: [$1])]]].
make value: 3

Expected: '01011100'

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Build a cyclic ring
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Start at a different compatible node and you may get a rotation or a different valid ring.

Download this checkpoint

05 - Prove this output covers every window

There are eight starting positions and eight distinct windows, so all binary three-bit words occur exactly once. This checks the property directly rather than relying on the appearance of a particular string.

Independent Workspace expression - select all, then Print it
| scan |
scan := [:ring :width | (1 to: ring size) collect: [:start |
 String streamContents: [:s |
  0 to: width - 1 do: [:offset |
   s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
(scan value: '01011100' value: 3) asSet size

Expected: 8

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Prove this output covers every window
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change one bit and repeat the check. For example, change the final 0 to 1 and look for a repeated window.

Download this checkpoint

06 - Try width four

There are two outgoing edges per three-bit node and eight nodes. Using every edge once produces 16 bits. Both checks return 16: the generated ring length and the count of unique windows.

Independent Workspace expression - select all, then Print it
| make scan result |
make := [:width | | mask next stack back digits |
 mask := (1 bitShift: width - 1) - 1.
 next := Array new: mask + 1 withAll: 0.
 stack := OrderedCollection with: 0.
 back := OrderedCollection new.
 [stack notEmpty] whileTrue: [ | node bit newNode |
  node := stack last.
  bit := next at: node + 1.
  bit < 2 ifTrue: [
   next at: node + 1 put: bit + 1.
   newNode := ((node bitShift: 1) + bit) bitAnd: mask.
   stack addLast: newNode]
   ifFalse: [back addLast: stack removeLast]].
 digits := back reversed allButFirst.
 String streamContents: [:s | digits do: [:node |
  s nextPut: ((node bitAnd: 1) = 0 ifTrue: [$0] ifFalse: [$1])]]].
scan := [:ring :width | (1 to: ring size) collect: [:start |
 String streamContents: [:s |
  0 to: width - 1 do: [:offset |
   s nextPut: (ring at: (((start - 1 + offset) \\ ring size) + 1))]]]].
result := make value: 4.
{result size. (scan value: result value: 4) asSet size}

Expected: #(16 16)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Try width four
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change both width arguments together to 2. Expect four positions and four distinct windows.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Native Squeak view highlighting the first three-bit cyclic window
Native Morphic replay of the eight three-bit windows in cyclic order. Yellow marks the current window; the pale tail repeats the first two bits to show wraparound.

Download the native cyclic-window demo. Do it in Workspace. Close the window, change shown := 1. to a number from 2 to 8, and run again.

Reason about correctness and limits

An Euler trail consumes edges once; this application makes each edge a different window. A Gray code solves a different problem: its successive whole words differ by one bit.

The output size doubles per width. Keep the example small. The starter code encodes widths at least two; width one is simply 01.

Three practice challenges

Find the seam

Which width-three word starts at the last bit of 01011100?

Show solution

001: take the final 0, then the leading 0 and 1.

Count nodes

How many nodes and edges are used for width five?

Show solution

16 nodes, 32 edges.

Verify a ring

How do you know a 16-bit candidate is valid for width four?

Show solution

Extract all 16 cyclic windows and check that the set has size 16.

Further reading

N. G. de Bruijn’s 1946 paper, A Combinatorial Problem. The Workspace examples above are original.

Continue learning

Euler Trail | Gray Code | Integer Bitsets

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.