Module

Rebus.Assign

Package
purescript-rebus
Repository
afcondon/purescript-rebus

Rebus.Assign — glyphs for a known, finite set of keys.

Dealing by hash treats every key alone, and for a set that is open-ended (captures, projects) that is the only thing it can do. But the node types of a diagram, the parties to a contract, the subjects of a workbench are a SET, known all at once, and a hash over a set says false things by chance: two keys land on one glyph, or two unrelated keys share a cow and read as relatives. A reader takes a shared icon for a family.

So for a known set the glyphs are ASSIGNED: every key is considered with the others, and no icon is given to two keys while the deck has icons left.

What is promised, and tested as laws in Test.Rebus.Assign:

• A function of the set. The same keys, deck and width give the same glyphs, in whatever order and however many times the keys are listed. • No two keys the same glyph, and no two the same icons in a different order either, while the deck has that many combinations. • No icon in two glyphs while keys × width fits in the deck. When it does not, icons are reused as evenly as distinct glyphs allow: lowest use first, so the most-used and least-used icon differ by one (by two where a key had to step off the least-used icons because that set was already another key's; held over the test sweep, not proved). • A deck narrower than the width has no run without a repeat to give, and every key falls back to what the hash deals it. • It agrees with the hash where the hash was right. A key alone, the first key in assignOrder, and every key of a set the hash dealt without sharing, get exactly rebusOfWithWidth. Assignment is a repair of dealing, not a second scheme. • Stability, which is partial and has to be. See assign.

#Named Source

type Named = { glyph :: Glyph, key :: String }

A key and the glyph it was given.

The key is the thing's canonical text, as everywhere in Rebus: an immutable id for a durable name, the type's own name for a type. Every function here returns its keys SORTED, each once, so that two results can be compared with == and a result can be stored and diffed.

#glyphOfKey Source

glyphOfKey :: String -> Array Named -> Maybe Glyph

The glyph a key was given, if it is among them.

#assign Source

assign :: Width -> Deck -> Array String -> Array Named

A glyph for every key of a set, such that no two keys share an icon while the deck allows.

HOW. The keys are taken in assignOrder, a fixed order that is a property of each key and not of the set. Each key in turn takes the icons it prefers among the least-used icons of the deck. A key's preference begins with the icons the hash would have dealt it and continues through the rest of the deck in an order of the key's own.

STABILITY. Full stability (adding a key never moves another) and "no icon shared" cannot both hold for a function of the set alone: if no key ever moved, each key's glyph would depend on that key only, which is a hash, and a hash shares. This function keeps "no icon shared" and gives the stability that is left:

Adding a key never changes the glyph of any key that comes before it in assignOrder; a key after it changes only if the newcomer, or a key the newcomer displaced, took one of its icons. Removing a key is the same statement read backwards.

In a deck with room that is few: adding a thirteenth key to twelve pairs from the default deck moves a little under half a key on average (measured in the tests), and more as the deck fills. When an assignment has been SHOWN to someone and must not move at all, store it and grow it with assignMore.

A deck with fewer icons than the width has no run to give, and each key falls back to what rebusOfWithWidth deals it.

#assignMore Source

assignMore :: Width -> Deck -> Array Named -> Array String -> Array Named

Grow a stored assignment: every glyph already given is KEPT exactly, and only the keys that are new are assigned, around the ones that are there.

This is the other answer to the stability question. Nothing existing ever moves, and the price is that the result now depends on history: the same set reached by a different route can wear different glyphs, so the assignment has to be stored (it is an Array Named; store the aliases). assignMore width deck [] keys == assign width deck keys.

The existing glyphs are taken as they are, whatever made them: they need not have come from this deck or this width. The first entry for a key wins.

#assignChoosing Source

assignChoosing :: Width -> Deck -> (String -> Choice) -> Array String -> Array Named

Assignment for a set in which some things have icons of their own.

A key the policy says is Chosen gets exactly its icons. Every icon any of them was given is withdrawn from the deck, and the rest are assigned from what remains: so a declared icon appears where it was declared and nowhere else. Two keys declared the same icons get the same glyph, since that is what was declared.

#assignOrder Source

assignOrder :: Array String -> Array String

The order assign takes keys in: by a hash of each key, then by the key.

The hash is a product of two, because a single one puts sequential ids in nearly their numeric order and all of them together, and an order with that much pattern in it makes "which keys can a newcomer disturb" depend on what the keys happen to be called.

It is exported because the stability law is stated in it. The order of two keys never depends on what other keys are in the set, which is the whole of why a key cannot be disturbed by one that comes after it.

#deal Source

deal :: Width -> Deck -> Array String -> Array Named

Every key dealt by hash, alone: rebusOfWithWidth over a set, in the same shape assign returns, so that the two can be compared and the dealt set inspected with clashes and sharedIcons.

#dealWidening Source

dealWidening :: Width -> Deck -> Array String -> Array Named

Every key dealt by hash at the width, and the ones that clash WIDENED by an icon until they do not (or until five, the ceiling).

This keeps what a hash is good for: a key's glyph is still its own, needs no stored table, and at whatever width it ends is exactly rebusOfWithWidth at that width, so the narrow glyph is a prefix of the wide one. Adding a key changes only the keys it clashes with, which grow an icon. What it does not do is stop two keys sharing an icon; that is assign.

Two keys that agree in all five icons stay clashing, and clashes of the result says so.

#clashes Source

clashes :: Array Named -> Array (Array String)

The keys that cannot be told apart: each group is two or more keys with the same icons in the same order. Empty when every glyph is its own.

#sharedIcons Source

sharedIcons :: Array Named -> Array { icon :: String, keys :: Array String }

The icons that stand in more than one key's glyph, each with the keys that hold it: the false families of a dealt set. Empty for an assignment that fitted its deck.

#TwoPart Source

type TwoPart = { familyDeck :: Deck, familyWidth :: Width, memberDeck :: Deck, memberWidth :: Width }

The two sources of a two-part name: how wide the family part is and the deck it is assigned from, and the same for the member part.

Two decks, because the clearest two-part name draws its parts from different places (the family always an animal, the member never one). The same deck for both is allowed; a family's own icons are withdrawn from its members' deck either way.

#Membership Source

type Membership = { family :: String, member :: String }

One thing to be named: the family it belongs to and its own name.

#NamedMember Source

type NamedMember = { family :: String, glyph :: Glyph, member :: String }

#assignTwoPart Source

assignTwoPart :: TwoPart -> Array Membership -> Array NamedMember

A two-part name for every member: first its family's icons, the same for every member of the family, then its own.

The families are assigned among themselves from the family deck, so no two families share an icon while that deck allows. Within each family the members are assigned among themselves from the member deck (less that family's icons), so no two members of a family share a member icon while it allows. Members of DIFFERENT families may share member icons, and a member name that occurs in two families will tend to wear the same ones: the first part is what tells them apart.

Stability is assign's, twice: a new family can move only families after it in assignOrder, and a new member only members of its own family after it. A family that keeps its glyph keeps all its members' glyphs.

Returned sorted by family, then member, each pair once. The alias reads back while the two widths add to five or fewer (see rebusAppend).

#assignThemes Source

assignThemes :: Array String -> Array { key :: String, theme :: Theme }

A theme for every subject, so that each subject's pictures are dealt from a part of the deck of their own (themeDeck) and look like one family.

It is assign over the six themes, with everything that implies: no two subjects share a theme while there are six or fewer, a seventh shares as evenly as it can, and the stability is assign's. Themes differ in size (see Theme), and this does not look at how many things a subject has to name; a subject with many should be given a large theme by hand.