Reintent

Seeing other possibilities

The card under answers nobody has given, in the card and from code, the likeliest cards, and why a choice is what it is.

Sam leans toward a funny card, but a funny card has two jokes, and Sam wants to see both before deciding anything. Sam's app wants to offer the few cards Sam is most likely to want. At the dinner, Sam's sister asks why the card is warm. Each of these looks at the card under answers nobody has given, or at the reasons behind the one it shows.

You will learn

  • How a <World as> inside another fixes more than one choice
  • How <World each> shows every possible card at once
  • How to realize a possible card from code with intent.preview
  • How to list the likeliest cards with intent.explore
  • How intent.explain says why a choice is what it is

A preview inside a preview

Below Sam's card, the funny card with each joke:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

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

export function Buttons() {
  const tone = useTone();
  const row = <T extends string>(name: string, values: readonly T[]) =>
    host('p', {
      children: values.map((v) =>
        host('button', { onClick: action.decide({ of: 'card', name }, v), children: v }),
      ),
    });
  return host('div', { children: [row('tone', TONES), tone === 'funny' && row('joke', JOKES)] });
}

/** The funny card with each joke: a preview inside a preview adds its own hypothesis. */
export function Jokes() {
  return host('div', {
    style: { display: 'flex', gap: 8, fontSize: 11 },
    children: (
      <World as={{ 'card#tone': 'funny' }}>
        {JOKES.map((j) => (
          <World key={j} as={{ 'card#joke': j }}>
            <Card />
          </World>
        ))}
      </World>
    ),
  });
}

export default function App() {
  return host('div', { children: [<Card />, <Buttons />, <Jokes />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

Quick start shows each other tone with <World as>. Here the outer <World> makes the tone funny, and each inner one adds a joke. An inner World's as goes on top of the outer one's, and a key in both takes the inner value. The joke is read only in a funny card, and inside the outer World the card is funny, so the inner one has a joke to fix. Hypotheses decide nothing: Choices has the tone as it was, and the buttons still decide.

Every possible card

Below Sam's card, every card the choices allow:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

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

export function Buttons() {
  const tone = useTone();
  const row = <T extends string>(name: string, values: readonly T[]) =>
    host('p', {
      children: values.map((v) =>
        host('button', { onClick: action.decide({ of: 'card', name }, v), children: v }),
      ),
    });
  return host('div', { children: [row('tone', TONES), tone === 'funny' && row('joke', JOKES)] });
}

/** Every card the choices allow: one path for each tone, and for each joke of a funny card. */
export function Every() {
  return host('div', {
    style: { display: 'flex', gap: 8, fontSize: 11 },
    children: (
      <World each>
        <Card />
      </World>
    ),
  });
}

export default function App() {
  return host('div', { children: [<Card />, <Buttons />, <Every />] });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

<World each> evaluates its subtree once for each possibility of every open choice it reads, one path each. The tone has three possibilities, and only a funny card reads a joke, which has two, so there are four paths. λ holds a worlds node with a world for each path: its at is the values the path took, by key, and its p their joint probability. Each path is a hypothesis, like as: nobody is asked about it, and nothing is written. Press a tone, and only its paths stay, since a decided choice is not enumerated.

each also takes one choice to enumerate, an EachTarget: its identity, a handle, or { on, of, name } as useChoose reads it. With a target that says its possibilities, the children can be a function of its value: <World each={{ on: tones, of: 'card', name: 'tone' }}>{(t) => …}</World>. max caps the paths, 16 unless given, and a choice that would go past it collapses as usual. Every prop is in WorldProps.

A possible card from code

A host that draws a thumbnail, or a test, wants a possible card without drawing it in the program:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

export default function App() {
  return <Card />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

intent.preview(at) evaluates the program under the same at, by key (card#tone), as a <World as> around the whole program would. It returns a Realization like intent.realize(), with source: 'preview' for the choices it fixes. It writes nothing and sends nothing, so afterwards the card is as it was, with nothing to undo.

The likeliest cards

Sam's app needs the three cards Sam is most likely to want:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

export default function App() {
  return <Card />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

intent.explore({ k }) branches on every open, undecided choice that System One, System Two's candidates or the code weighs, and returns the k likeliest possibilities of the whole card, 3 unless given. At each step it keeps the beam likeliest partial cards, Math.max(k, 8) unless given. Each is a PossibleWorld:

  • choices is the possibility of each choice, by key.
  • p is their joint probability. System One gives funny 0.15, and the code's prior gives the pun 0.7, so 0.105. Funny with the tease, 0.045, is fourth and left out.
  • preview() realizes the intent under the world's choices, as intent.preview would, only when it is called.

only picks which choices to branch on, and the others are summed out, so the tone alone gives three possible worlds. A decided choice stays decided in every world, and a choice with equal weights and no judgment stays as it is.

Exploring asks System One

Exploring needs System One's weights, for the card as it is and under each possibility, and asks for the ones it does not have yet, within a budget. Here nothing has been shown, so the tone is not judged yet:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

export default function App() {
  return <Card />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

judge caps the new judge requests one call sends, 4 unless given, and judge: 0 uses only the answers already there. A world whose judgment is missing is partial, and its p comes from what there is, here equal weights, a third each. With a budget of one, System One is asked, What happened shows the request, and the worlds have its weights. The answers are kept by their inputs like every answer, and nothing is committed.

Why a choice is what it is

The card shows warm, and a read says it came from System One. How sure System One was is in neither:

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

export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

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(tone: Tone, joke?: Joke) {
  if (tone === 'formal') return 'Many happy returns, Grandma.';
  if (tone === 'warm') return 'Happy 90th birthday, Grandma!';
  return joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!';
}

/** System One weighs the tone. */
export function useTone() {
  return useChoose(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

/** A funny card's joke: the code expects the pun, 0.7 to 0.3. */
export function useJoke(tone: Tone) {
  return tone === 'funny'
    ? useChoose(
        choices<Joke>([
          { value: 'pun', weight: 0.7 },
          { value: 'tease', weight: 0.3 },
        ]),
        { of: 'card', name: 'joke', defaultValue: 'pun' },
      )
    : undefined;
}

export function Card() {
  const tone = useTone();
  const joke = useJoke(tone);
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(tone, joke) }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

export default function App() {
  return <Card />;
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

intent.explain(id) answers for one choice:

  • read is the read intent.realize().reads gives, with its value, layer and status.
  • dist is the distribution the policy weighed: warm leads funny by 0.65, well over the 0.2 the default policy asks for.
  • decision, constraints and beliefs are what each layer holds for the choice, its own constraints and those on its name.

After intent.constrain(TONE, { not: ['warm'] }) the card turns funny, the constraint is listed, and the belief is kept as it was. After intent.decide(TONE, 'formal') the decision is on top. Each layer is listed as it is held, whichever one the card uses.

Explaining changes nothing

explain evaluates the program afresh, sends no request and writes nothing, so a host can call it whenever someone asks why. For a choice read in a context, pass the context the way intent.realize takes it: intent.explain(id, [[Ctx, value]]).

Recap

  • A <World as> inside another adds its hypotheses to the outer one's; for the same key, the inner one wins.
  • <World each> evaluates its subtree once per possibility, one world path each, with at and p.
  • intent.preview(at) evaluates the program under answers nobody gave, writing and sending nothing.
  • intent.explore({ k, only }) returns the likeliest possible worlds, each with choices, p and preview().
  • judge caps how many new judgments exploring asks for; a world missing one is partial.
  • intent.explain(id) gives a choice's read, the distribution its policy weighed, and what each layer holds.

Last updated on

Edit on GitHub