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.
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.
| bits |
bits := Array new: 17 withAll: false.
{bits size. bits anySatisfy: [:b | b]}Expected: {17. false.}

Try it: A false flag means no inserted item has selected that position yet.
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.
| 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.}

Try it: Change one letter and inspect both selected positions.
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.
| 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.}

Try it: Count the flags before and after inserting the same word again.
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.
| 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.}

Try it: Never label a positive answer definitely present.
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.
| 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: maybeExpected: true

Try it: Repeat an insertion: the result should not change.
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.
| 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.}

Try it: This is a deterministic demonstration, not an estimate of a real-world false-positive probability.
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.
| 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

Try it: This checkpoint intentionally demonstrates an invalid deletion strategy; do not add it to the insert-only API.
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.
| 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.}

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

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.