s LearnSqueak.com

SMALLTALK LAB

Bloom filters: trade certainty for a tiny membership sketch

Build an insert-only probabilistic membership filter, distinguish definitely absent from possibly present, and deliberately expose a false positive.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Know Arrays, Strings, blocks and Boolean values. A Bloom filter stores hash-selected flags rather than the original values. We use a tiny Boolean array and two deliberately simple teaching hashes, not production hashing.

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.

01 - Allocate a fixed flag array

All flags start false. Storage stays fixed as items arrive, unlike a Set that retains the items. This teaching representation uses Boolean objects; a compact production implementation normally packs bits.

Independent Workspace expression - select all, then Print it
| bits |
bits := Array new: 17 withAll: false.
{bits size. bits anySatisfy: [:b | b]}

Expected: {17. false.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Allocate a fixed flag array
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: A false flag means no inserted item has selected that position yet.

Download this checkpoint

02 - Map text into two positions

Two different folds turn the character sequence into indices. Modulo seventeen keeps each hash in 0..16; add one for Squeak’s one-based indexing. These deterministic ASCII-based hashes are intentionally weak so collisions are easy to study.

Independent Workspace expression - select all, then Print it
| positions |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
positions value: 'cat'

Expected: {7. 7.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Map text into two positions
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Change one letter and inspect both selected positions.

Download this checkpoint

03 - Insert by setting flags

Insertion only changes false to true. If two hashes choose the same position it is simply set twice; this is allowed. No original string is stored in the array.

Independent Workspace expression - select all, then Print it
| positions bits |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
(positions value: 'cat') do: [:i | bits at: i put: true].
(1 to: bits size) select: [:i | bits at: i]

Expected: {7.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Insert by setting flags
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Count the flags before and after inserting the same word again.

Download this checkpoint

04 - Ask a one-sided question

If any selected flag is false, the item cannot have been inserted into this unchanged filter. If all flags are true, the item is only possibly present: unrelated items may have set them.

Independent Workspace expression - select all, then Print it
| positions bits add maybe |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
add := [:text | (positions value: text) do: [:i | bits at: i put: true]].
maybe := [:text | (positions value: text) allSatisfy: [:i | bits at: i]].
add value: 'cat'.
{maybe value: 'cat'. maybe value: 'dog'}

Expected: {true. false.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Ask a one-sided question
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Never label a positive answer definitely present.

Download this checkpoint

05 - Verify no inserted value is missed

Insert a small list and check every inserted item. The no-false-negative property depends on using the same hash functions, same array size and never clearing flags.

Independent Workspace expression - select all, then Print it
| positions bits add maybe words |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
add := [:text | (positions value: text) do: [:i | bits at: i put: true]].
maybe := [:text | (positions value: text) allSatisfy: [:i | bits at: i]].
words := #('cat' 'dog' 'owl' 'ant').
words do: add.
words allSatisfy: maybe

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Verify no inserted value is missed
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Repeat an insertion: the result should not change.

Download this checkpoint

06 - Construct a false positive

Characters whose ASCII values differ by seventeen produce the same residues in both teaching hashes for a one-character string. After inserting a, r is a positive query even though it was never inserted.

Independent Workspace expression - select all, then Print it
| positions bits add maybe |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
add := [:text | (positions value: text) do: [:i | bits at: i put: true]].
maybe := [:text | (positions value: text) allSatisfy: [:i | bits at: i]].
add value: 'a'.
{(positions value: 'a') = (positions value: 'r'). maybe value: 'r'}

Expected: {true. true.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Construct a false positive
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: This is a deterministic demonstration, not an estimate of a real-world false-positive probability.

Download this checkpoint

07 - Do not delete by clearing a flag

Two items can share flags. Clearing positions for one item would also erase evidence for another, breaking the no-false-negative promise. Keep ordinary Bloom filters insert-only; counting variants need a different representation.

Independent Workspace expression - select all, then Print it
| positions bits add maybe |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
add := [:text | (positions value: text) do: [:i | bits at: i put: true]].
maybe := [:text | (positions value: text) allSatisfy: [:i | bits at: i]].
add value: 'a'. add value: 'r'.
(positions value: 'a') do: [:i | bits at: i put: false].
maybe value: 'r'

Expected: false

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Do not delete by clearing a flag
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: This checkpoint intentionally demonstrates an invalid deletion strategy; do not add it to the insert-only API.

Download this checkpoint

08 - Observe saturation

Once every flag is true, every query is possibly present and the filter provides no useful rejection. This loop inserts single characters covering all seventeen residues and verifies that consequence.

Independent Workspace expression - select all, then Print it
| positions bits add maybe |
positions := [:text | | h1 h2 |
 h1 := 0. h2 := 0.
 text do: [:c |
  h1 := (h1+c asciiValue) \\ 17.
  h2 := (h2*3+c asciiValue) \\ 17].
 {h1+1. h2+1}].
bits := Array new: 17 withAll: false.
add := [:text | (positions value: text) do: [:i | bits at: i put: true]].
maybe := [:text | (positions value: text) allSatisfy: [:i | bits at: i]].
0 to: 16 do: [:i | add value: (String with: (Character value: 65+i))].
{bits allSatisfy: [:b | b]. maybe value: 'never inserted'}

Expected: {true. true.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Observe saturation
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: A filter needs capacity planning, not just a fixed array chosen at random.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Bloom filters: trade certainty for a tiny membership sketch native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.

Reason about correctness and limits

A Bloom filter is a pre-check before an exact store lookup, not an authoritative membership database. A positive query must be checked against the real data when correctness matters.

With k hashes, insert/query work is O(k) after hashing the input. Our two string folds also require O(length of text). The tiny Boolean array and correlated toy hashes are explanatory, not a recommended production design or security mechanism.

False-positive-rate formulas assume suitably distributed hashes and a specified insertion model. This lesson makes no rate claim for its deliberately weak hashes. Increasing the array size usually helps only when hash quality and item count are also considered.

Three practice challenges

Challenge 1

Why can there be no false negative in the insert-only version?

Show solution

Insertion sets every queried flag and no later insertion clears it, so the same item still finds all its flags set.

Challenge 2

Why is a Set different?

Show solution

A Set retains exact items and resolves collisions to answer membership exactly; this filter discards the items.

Challenge 3

How would you support deletion safely?

Show solution

Use a carefully designed counting filter with counters and valid deletion bookkeeping, or rebuild the filter from the authoritative live dataset.

Continue learning

Integer Bitsets | First Set | Probability Lab

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