Reintent

The other possibilities

Show what else the card could be, one choice changed at a time, and find the one question that matters most.

Sam's card is warm. Before the dinner, Sam wants to see what else it could have been: the funny card, the formal one, and with a funny card, the other joke. Quick start previews the tones by hand, one <World as> each. A host can find the other cards on its own, and also the one question worth asking Sam first.

You will learn

  • How alternatives previews every other possibility, one choice changed at a time
  • Which other cards it leaves out
  • How split picks the one question that tells the likeliest cards apart
  • How holds and relax offer only the cards the host can make, with the rest held; how take writes the flip, and takeScheme the whole scheme

Every other card

The card with its tone buttons. What happened lists the other cards it could be, and lists them again after each button:

/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoose, type Id } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export type Joke = 'pun' | 'tease';

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

export const LOOK: Record<Tone, { background: string; accent: string }> = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: Tone, joke?: Joke) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  if (joke === 'tease') return `90? You don't look a day over 89, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

/** What a button does: the intent gives it to every evaluation (index.intent.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });

export default function App() {
  const tone = useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
  // Only a funny card has a joke.
  const joke =
    tone === 'funny'
      ? useChoose(choices<Joke>('pun', 'tease'), { of: 'card', name: 'joke', defaultValue: 'pun' })
      : undefined;
  return host('div', {
    children: [
      host('p', {
        children: TONES.map((v) =>
          host('button', {
            disabled: v === tone,
            onClick: action.decide({ of: 'card', name: 'tone' }, v),
            children: v,
          }),
        ),
      }),
      host('div', {
        style: {
          maxWidth: 380,
          padding: 24,
          borderRadius: 12,
          background: LOOK[tone].background,
          color: '#3a2a1c',
        },
        children: [
          host('h1', { style: { color: LOOK[tone].accent }, children: title(GRANDMA, tone, joke) }),
          host('p', { children: 'Wishing you a wonderful day.' }),
          host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${GRANDMA.from}` }),
        ],
      }),
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

alternatives(intent, now) goes through the choices now read. For each other possibility of each, it asks intent.preview for the card with that one choice changed. Each result is an Alternative: the choice (key, name), the possibility it tries (kind), the value now (from) and the realization it gives (result). A host can draw result the way it draws the card. Nothing is written, and Sam's card stays as it is.

The warm card has two others, funny and formal. It reads no joke, so no joke is listed. Press funny: the card now reads the joke, and the tease joins the list.

An alternative is kept only when its λ differs from the card shown, so a possibility that changes nothing on the card is left out. Two more arguments narrow the list. provide previews in a context, as intent.realize(provide) does. only keeps the choices a host wants to offer: alternatives(intent, now, [], (r) => r.name === 'joke') lists only the other jokes.

The one question

The code leans warm, 45 to 35 to 20, and a funny card is a pun more often than a tease. Sam has time for one question:

/** @jsxImportSource @reintent/core */
import { choices, host, useChoose } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export type Joke = 'pun' | 'tease';

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

export const LOOK: Record<Tone, { background: string; accent: string }> = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: Tone, joke?: Joke) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  if (joke === 'tease') return `90? You don't look a day over 89, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

export default function App() {
  // The code leans warm, but not by enough to settle it: the tone stays open.
  const tone = useChoose(
    choices<Tone>([
      { value: 'warm', weight: 0.45 },
      { value: 'funny', weight: 0.35 },
      { value: 'formal', weight: 0.2 },
    ]),
    { of: 'card', name: 'tone', defaultValue: 'warm' },
  );
  const joke =
    tone === 'funny'
      ? useChoose(
          choices<Joke>([
            { value: 'pun', weight: 0.6 },
            { value: 'tease', weight: 0.4 },
          ]),
          { of: 'card', name: 'joke', defaultValue: 'pun' },
        )
      : undefined;
  return host('div', {
    style: {
      maxWidth: 380,
      padding: 24,
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(GRANDMA, tone, joke) }),
      host('p', { children: 'Wishing you a wonderful day.' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${GRANDMA.from}` }),
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

intent.explore({ k }) gives the k likeliest whole cards. Each is a PossibleWorld: the value of every choice it reads, and how likely it is. split(worlds) finds the first choice they disagree on, and adds up how likely each answer is. Funny is 35%, the pun's world and the tease's world together. The tone is the question to ask; the joke matters in only some of the worlds.

Answer it in Choices: pick funny. The next question is the joke, 60 to 40. Pick a joke, and split gives undefined, since one world is left. It does the same when the worlds all agree.

Holding the rest

Sam's card also has a fold, flat or folded, and the host lays it out. The funny card's joke is long, and fits only on a folded card. A list that shows funny on a flat card offers a card the host cannot make. With the host's word on what fits, alternatives offers the funny card folded:

import { createIntent, entity, host, useChoose, type HostNode, type Realization } from '@reintent/core';
import { alternatives, take, takeScheme, type Holds } from '@reintent/lib/alternatives';

const card = entity('card', {
  tone: { kinds: ['warm', 'funny', 'formal'], default: 'warm' },
  fold: { kinds: ['flat', 'folded'], default: 'flat' },
});
const Card = card(() => host('card', { tone: useChoose(card.tone), fold: useChoose(card.fold) }));
const intent = createIntent({ program: <Card /> });

// The host: the funny card fits only folded, and folds it when the fold is let go.
const props = (r: Realization) => (r.tree as HostNode).props;
const holds: Holds = (r, flip) =>
  props(r).tone !== 'funny' || props(r).fold === 'folded' || flip.released.includes('card#fold');

const offered = alternatives(intent, intent.realize(), [], (r) => r.name === 'tone', {
  holds,
  relax: ['fold'],
});
// → funny, with relaxed [ 'fold' ]; formal, with relaxed []
take(intent, offered[0]); // Sam's decision: funny
// or, when Sam asks for the whole card as offered:
takeScheme(intent, offered[0], intent.realize(), { seen: { 'card#fold': 'folded' } });

The fifth argument is a Relaxing. With it, one choice flips and every other choice is held at freeze, by default the values read now. Holds is the host's answer to whether the world a flip gives really has it. When it does not, the choices of the names in relax give way in order, one name at a time, until it holds or nothing is left to let go. A flip is offered only when it holds. Its relaxed lists the names that gave way, released the choices let go, and held what it was evaluated under.

Funny on a flat card does not hold. The fold gives way, and funny is offered with relaxed: ['fold']. Formal holds as it is.

take(intent, alt, opts) writes the flip alone, as Sam's decision: the tone is funny. The fold is not written: nobody decided it, and it stays what the cascade gives it. decide writes it another way, for a choice decided along an axis, and note names the commit.

When Sam asks for the card as it was offered, the host calls takeScheme(intent, alt, now, opts). It writes the flip and each choice it let go at the value its world shows (seen, by default what the preview read), as Sam's decisions, in one commit, and returns them in a Taken, so the host can show every change Sam accepts. released writes only some of them. The scheme was computed on one evaluation: when a choice it held has another value in now, it is refused with StaleScheme, listing what moved. One undo takes the whole scheme back.

Recap

  • alternatives(intent, now) previews every other possibility, one choice changed at a time, and writes nothing.
  • An alternative is kept when its λ differs from the card shown; only and provide narrow the list.
  • intent.explore({ k }) gives the likeliest worlds; split(worlds) gives the one question that tells them apart, with the weight of each answer.
  • alternatives(intent, now, provide, only, { holds, relax }) holds every other choice, lets the names in relax give way in order, and offers a flip only where the host's holds says it holds; take writes the flip as a person's decision, and takeScheme, asked for, the whole scheme it was offered as, unless it went stale.

Last updated on

Edit on GitHub