Module

Rebus

Package
purescript-rebus
Repository
afcondon/purescript-rebus

Rebus — identity without a name.

A rebus is a deterministic map from content to a memorable pictographic identity: an ORDERED RUN of one to five coloured icons — "red cow, blue star" — with a typeable hyphenated alias, "cow-ambulance".

It exists to dissolve the tension between saving fast and saving named. A thing worth keeping usually arrives mid-flow, when stopping to invent a name is exactly the wrong demand; but a pile of anonymous captures is unusable, because nothing in it can be recognised, referred to, or typed. So every capture gets an identity derived from its own content, immediately and for free, and naming becomes a later, optional promotion rather than a toll on the way in.

Two halves, and they answer different questions:

• content → icon shapes. One weakly-independent hash per position picks an ordered run from the deck. Identical content lands on the identical run, which gives deduplication for nothing; a real edit avalanches to a visibly different run. Runs rather than singles because 64 icons give only 64 identities but ~4000 ordered pairs, and because an absurd juxtaposition ("cow-ambulance", "star-bomb") is far more memorable than any lone icon — the bizarre-imagery trick that mnemonists have used for centuries. • icon name → colour. Each icon's colour is a hash of its OWN name, so cow is always red and star always blue. This is what makes the alias lossless: a rebus reconstructed from the thirteen typed characters of "cow-ambulance" renders identically to one computed live from the content, because nothing about the colour came from the content.

How many icons is the caller's decision and it follows from arithmetic, not taste: an identity must be unique across the set of things it distinguishes, so the width is set by how many things that is. See Width, and the table in the README.

Rebus is renderer-free. It yields an icon NAME and a colour STRING and draws nothing; mapping those onto FontAwesome classes, SVG symbols, ANSI escapes or a plotter is the consumer's business.

What is here DEALS: content in, identity out, by hash alone. Three things a hash cannot do live beside it and are imported by name: Rebus.Choice (this one is declared, not dealt), Rebus.Assign (a finite set of keys, given glyphs that share no icon) and Rebus.Parts (a family and a member in one name). The deck they deal from can be restricted and themed in Rebus.Deck.

#Width Source

data Width

How many icons one identity is made of.

This is a decision about UNIQUENESS, not about how much room the chip has. A rebus is only an identity if no two of the things it distinguishes collide, and with a 64-icon deck the supply is small enough that the arithmetic bites almost immediately: one icon gives 64 identities, two give 64×63 = 4032, three 249984. Even a perfect hash puts two of 289 items in the same bucket of 4032 about ten times over (the birthday problem, not a flaw in the hash). So: dozens of things → Two; hundreds to a few thousand → Three; more only above that.

Five is the ceiling, and deliberately. Past four the mnemonic stops being one absurd image and becomes a list to recite, and the alias stops being typeable — cow-ambulance-anchor-spider-tornado is thirty-five characters, which is not something anyone puts in a filename or a pattern by hand.

A closed set rather than a clamped Int because there is no meaning for a width of 0, 6 or -1: a caller who computes one should be made to say what it does about it, which is what widthFromInt is for.

Constructors

Instances

#allWidths Source

allWidths :: Array Width

Every width, narrowest first — for a test sweep, or a UI that offers them.

#widthToInt Source

#widthFromInt Source

widthFromInt :: Int -> Maybe Width

A width from a count — for a width that arrived as data (a config field, a column in a table, the number of parts in an alias). Nothing for anything outside 1–5, because silently clamping would mint identities at a width the caller did not ask for and could not detect.

#GlyphIcon Source

type GlyphIcon = { color :: String, icon :: String }

One drawable element: an icon name from the deck, and the colour to tint it.

A record rather than a newtype on purpose — this is the output boundary, consumed field by field by a renderer that has its own vocabulary. There is no invariant here to protect; the invariants live upstream, in the deck and the canonical text.

#Glyph Source

type Glyph = { alias :: String, icons :: Array GlyphIcon }

A content's identity: the picture (one to five coloured icons, in order) and the alias that spells it, "cow-ambulance". The alias is the typeable form — the one that goes in a text field, a filename, a pattern, a completion menu — and it round-trips through rebusFromAnyAlias without loss.

icons is an array and not a tuple of named fields because the width is a property of the identity SPACE, not of the type: one program mints pairs and another triples, and a renderer should be able to draw either without knowing which it was handed. Its length is always widthToInt of the width it was minted at.

#ChipView Source

type ChipView = { diverged :: Boolean, glyph :: Glyph }

An identity plus whether the live thing has drifted from it — the pair a status indicator needs. diverged is the "you have edited this since it was parked" flag, conventionally drawn as a fade; a thing with no identity at all is Nothing, so Maybe ChipView is the whole state of one indicator.

#rebusOf Source

rebusOf :: forall a. Canonical a => a -> Glyph

The rebus for anything that can say what it canonically is. Two icons, the width this library shipped with; say otherwise with rebusOfWidth.

#rebusOfWidth Source

rebusOfWidth :: forall a. Canonical a => Width -> a -> Glyph

The rebus for anything canonical, at a width you have chosen for the number of things you must tell apart.

#rebusOfText Source

rebusOfText :: String -> Glyph

The rebus for text that is ALREADY canonical — a printed eDSL, a normalised document, whatever the caller's own format says the content is.

This is the escape hatch for callers whose canonical form is produced somewhere else entirely (a pretty-printer, a parser's inverse) and would gain nothing from a Canonical instance. It is the same function rebusOf ends up calling, so the two are interchangeable given the same string.

#rebusOfTextWidth Source

rebusOfTextWidth :: Width -> String -> Glyph

Canonical text at a chosen width.

This is also the DURABLE-identity entry point: hand it an immutable key ("marginalia:160", a UUID, a content hash) rather than anything a human can edit, and the picture is pinned to the thing for as long as the key lives. See the README — content identity and durable identity use the same machinery with opposite intent, and choosing the wrong input is silent.

#rebusOfWith Source

rebusOfWith :: Deck -> String -> Glyph

The rebus for canonical text against a deck of your own.

Remember what a different deck means: a different identity space. Nothing here stops two programs using different decks, and nothing will tell them — they will simply draw different pictures for the same content, which is the one failure a rebus is supposed to make impossible. Pick a deck once, per identity space, and treat a change to it as a migration.

#rebusOfWithWidth Source

rebusOfWithWidth :: Width -> Deck -> String -> Glyph

The general form, and the one every other content entry point is defined in terms of: a width, a deck, and the canonical text.

#rebusFromAlias Source

rebusFromAlias :: String -> Glyph

Recover a rebus from a PAIR alias — for drawing an identity that arrived as text, from a file, a pattern, or someone's notes.

It consults no deck, and it doesn't need to: the alias already names the icons, and colour follows the name. That is why a hand-typed or promoted alias still renders, and why this is the one function in the library that cannot disagree with another implementation.

This one is FIXED at two, which is a compatibility decision and not a design one. Anything that is not exactly two hyphen-separated parts degenerates to the whole string drawn twice, and there are persisted aliases — and goldens — that record exactly that. New code that may meet a wider alias should use rebusFromAnyAlias, which reads the width off the hyphens instead.

#rebusFromAliasWidth Source

rebusFromAliasWidth :: Width -> String -> Glyph

Recover a rebus from an alias you already know the width of — the case where the caller stored triples and a two-part string is therefore not one of its identities but corrupt data.

The degenerate for a mismatch is the whole alias repeated to the expected width: it renders something, at the right size, and it looks wrong, which is the useful combination for a value that should not have got this far.

#rebusFromAnyAlias Source

rebusFromAnyAlias :: String -> Glyph

Recover a rebus from an alias of ANY width, read off the hyphens — the entry point for a renderer that is handed identities minted elsewhere and has no business knowing how wide they were.

This is the true inverse of rebusOfTextWidth: the default deck's icon names contain no hyphen, so splitting on - recovers exactly the names that were joined. Anything wider than five parts is not an identity this library could have minted, so it degenerates to a single icon named by the whole string — one obviously-broken picture rather than a plausible wrong one.

#glyphOfIcons Source

glyphOfIcons :: Array String -> Glyph

The glyph of icons you NAME, in the order you name them: the same colouring and the same alias as a dealt glyph, with no hash and no deck involved.

This is how an icon is placed by declaration rather than by chance (see Rebus.Choice), and how two glyphs are joined into one (Rebus.Parts). The names need not be in any deck: an icon withdrawn from the deck because it means something, or one drawn specially for a subject, is named here.

rebusFromAnyAlias (glyphOfIcons names).alias == glyphOfIcons names holds when there are one to five names and none contains a hyphen, which is exactly the condition for an alias to be read back at all. Outside it the glyph is still drawn and still has an alias; the alias just cannot be turned back into the picture by this library.

#iconNames Source

iconNames :: Glyph -> Array String

The icon names of a glyph, in order, without their colours: what the alias spells, as an array.

#sessionAliasOf Source

sessionAliasOf :: Int -> String

A CONTAINER's identity: three distinct icons from a seed, hyphen-joined — "cat-rocket-anchor".

Two differences from rebusOf, and both are the point. It is a TRIPLE, so a container never reads as one of the pairs it contains — a session is not a take, a folder is not a file, and the eye should know that before it reads the words. And it is seeded rather than content-hashed, because a container has no content to hash: it should NOT dedup with another container that happens to be empty in the same way, and it should not change identity as things are put into it. Mint the seed once, persist the alias, and recover the icons by splitting on - exactly as rebusFromAnyAlias does.

Consumers conventionally draw a triple monochrome, leaving colour to mean "this is content".

#sessionAliasOfWith Source

sessionAliasOfWith :: Deck -> Int -> String

A container's identity against a deck of your own. The deck warning on rebusOfWith applies here too.

#seededAliasOf Source

seededAliasOf :: Width -> Int -> String

A seeded alias at a chosen width. sessionAliasOf is this at Three.

A width narrower than the contents' is the thing to avoid here: the triple earns its third icon by being unmistakably not a pair, and a seeded Two would collide with the content identities as well as reading as one.

#seededAliasOfWith Source

seededAliasOfWith :: Width -> Deck -> Int -> String

A seeded alias, at a width, against a deck of your own.

It is literally the alias of the rebus of the seed's decimal digits — the seed IS its own canonical text — and writing it that way is what guarantees a triple and a pair are drawn from the same well, with the same de-duplication, rather than by two rules that only look alike.

Re-exports from Rebus.Canonical

#Chords Source

newtype Chords

A chord progression as MIDI note numbers — the case two independent programs were always going to need to agree on, and therefore the case that most needed pinning down.

Its canonical form is chords separated by spaces, notes within a chord by commas, each note rendered as a decimal integer:

Chords [[42,61,66],[46,58,65]]  ⇒  "42,61,66 46,58,65"

THIS STRING IS THE WIRE FORMAT. It is not a pretty-printer and it is not open to improvement: sorting the notes, padding them, or switching the separators would re-identify every rebus ever minted from a progression. Note in particular that the order of the notes is preserved rather than normalised, so a voicing is part of the identity, and duplicates are kept, so a strum reads as its own thing.

Constructors

Instances

#Canonical Source

class Canonical a  where

Types that can say what they are, canonically, as one line of text.

An instance is a PROMISE about a serialisation, not a convenience: change the string an instance produces and every identity previously derived from it changes with it. Write instances the way you would write a file format.

There is deliberately no instance for String. Claiming that any string is already canonical is exactly the mistake this class exists to prevent — a caller who genuinely holds canonical text should say so by reaching for rebusOfText instead.

Members

Instances

#chordsOf Source

chordsOf :: Array (Array Int) -> Chords

The normal form for chords that SOUND TOGETHER, and the constructor two programs should both reach for when they want to agree.

Chords preserves the order it is given, which is right for the general case and wrong for the common one. A simultaneity has no order: the notes of a chord arrive in whatever sequence the fingers landed, or bass-first from a voicing engine, or ascending from a printer — and those are three orderings of ONE chord. Left alone, two honest programs hash the same chord to different pictures and neither can see why.

So the shared path sorts each chord ascending, and the progression's own order is left alone because a progression played backwards IS a different progression. Duplicates survive the sort, so a doubled octave still reads as its own thing.

Use the bare Chords constructor only when the order WITHIN a chord is meant — an arpeggio, a strum, a roll — and say so where you do, because nothing downstream can tell the two intentions apart afterwards.

Re-exports from Rebus.Deck

#Theme Source

data Theme

A named part of defaultDeck. The six themes PARTITION it: every one of the sixty-four icons is in exactly one theme.

A theme is how a picture can say which subject it belongs to before any word is read: deal one subject's names from Animals and another's from Travel and the two cannot share an icon, so they look like two families rather than one. (Rebus.Assign.assignThemes hands themes out to subjects.)

They are unequal in size, and the size is the limit on what a theme can name without two things sharing an icon: Nature 14, Animals 13, Travel and Emblems 12, Tools 9, Music 4. Join themes with themesDeck when one is too small.

A closed set, like Width: the themes are a fact about defaultDeck, not a vocabulary a caller extends. A subset of your own is only.

Constructors

Instances

#Palette Source

newtype Palette

An ordered list of colours, as whatever string the consumer's renderer understands. The shipped palette is CSS, since that is where these were first drawn, but nothing here parses them.

Constructors

Instances

#Deck Source

newtype Deck

An ordered list of icon NAMES. Rebus never draws anything; a name is whatever the consumer's renderer can look up — a FontAwesome class, an SVG id, a key into a sprite sheet.

The deck must hold at least as many entries as the widest rebus drawn from it, since a run is bumped apart so no icon repeats. Size is the whole of the collision story: 64 entries give 64 identities at width one, 64x63 = 4032 at two and 249984 at three, so how wide a consumer draws follows from how many things it must tell apart (see Rebus.Width).

Constructors

Instances

#without Source

without :: Array String -> Deck -> Deck

The deck with the named icons WITHDRAWN, the rest left in their order.

This is for icons that mean something to your subject. A reader reads a flame as heat and a skull as danger whether or not anyone meant it, so a pure hash will sooner or later say something false: a flame on the thing that is cold. Withdraw every icon that carries meaning for the subject, deal names from what is left, and place the withdrawn ones only by declaration (Rebus.Choice).

The library ships no list of "loaded" icons, on purpose: what is loaded depends on the subject (a bolt means nothing in a build and electricity in a house), so the list belongs with the subject's vocabulary.

The warning on defaultDeck applies with full force. A restricted deck is a different identity space: the same text deals to a different glyph, and an icon withdrawn LATER re-deals everything dealt before. Decide what is withdrawn once, per identity space.

#themesDeck Source

themesDeck :: Array Theme -> Deck

The deck of several themes together, in defaultDeck's order whatever order the themes are named in. themesDeck allThemes == defaultDeck.

#themeOfIcon Source

themeOfIcon :: String -> Maybe Theme

Which theme an icon of defaultDeck belongs to; Nothing for a name that is not in the default deck.

#themeName Source

themeName :: Theme -> String

A theme's name as text, for a label or a stored setting.

#themeIcons Source

themeIcons :: Theme -> Array String

The icons of a theme. Membership is the fact; the order a theme's deck deals in is always defaultDeck's (see themeDeck).

#themeFromName Source

themeFromName :: String -> Maybe Theme

The theme a stored name spells, if it spells one.

#themeDeck Source

themeDeck :: Theme -> Deck

The deck of one theme: its icons, in defaultDeck's order.

A different deck, so a different identity space, like any other.

#paletteSize Source

#only Source

only :: Array String -> Deck -> Deck

The deck with ONLY the named icons kept, in the deck's order (not the order they are named in, so two callers who list the same icons differently still hold the same deck). A name the deck does not hold adds nothing.

#defaultPalette Source

defaultPalette :: Palette

Eight hues spaced around the wheel, all mid-dark so a small icon still reads against a light ground.

Unlike the deck, the palette is NOT part of the identity: a rebus is identified by its icons, which is what the alias records, and colour is a memorability accent laid on top ("red cow, blue star" is easier to hold than "cow, star"). Recolouring is therefore a visual change and not a migration — which is why rebusOfWith takes a deck and no palette.

Eight is also why colour de-duplication is only ever pairwise. A run of five icons cannot be given five well-separated hues out of eight, and should not try: all that is needed is that two icons standing side by side are not mistaken for one shape drawn twice.

#defaultDeck Source

defaultDeck :: Deck

The deck Rebus ships with: sixty-four concrete, drawable nouns, each of which happens to be a FontAwesome free-solid name so the common case needs no mapping table at all.

ORDER IS THE IDENTITY. An index into this array is what a content hash actually produces, so reordering an entry, removing one, or inserting in the middle silently re-identifies every rebus ever minted: the alias someone wrote down stops matching the picture they remember. The array is therefore FIXED and APPEND-ONLY — and even appending changes identities, because the hash is reduced modulo the deck size.

The same goes for rebusOfWith and a deck of your own. A different deck is a different identity space; two programs that must agree have to agree on the deck first, and a program that changes its deck has performed a data migration whether or not it migrated anything.

#deckSize Source

#allThemes Source

allThemes :: Array Theme

Every theme, in a fixed order.