Reintent

After the program changes

See what a new program did to a person's decisions, choice by choice, and find the ones nothing reads any more.

Sam made the card funny, with a tease. Then the family wants to sign it too, and it becomes the family's card, with no funny tone and no joke. Sam's decisions are all still there, and some of them have nowhere to go. A host can tell Sam which still apply, which wait, and which come back if the card is Sam's again.

You will learn

  • How changes sorts a person's decisions after the program changes
  • How diff lists what changed, choice by choice
  • How orphans finds the decisions nothing reads any more

What happened to the decisions

Two programs, Sam's card and the family's. A button switches between them with intent.setProgram. Accepting a reprogramming System Two proposed changes the program the same way (Quick start):

/** @jsxImportSource @reintent/core */
import { action, 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}!`;
}

/** What the buttons do: the intent gives them to every evaluation (index.intent.tsx). */

function Card({ tone, joke, from }: { tone: Tone; joke?: Joke; from: string }) {
  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: `— ${from}` }),
    ],
  });
}

function Buttons({ name, values, now }: { name: string; values: readonly string[]; now?: string }) {
  return host('p', {
    children: values.map((v) =>
      host('button', { disabled: v === now, onClick: action.decide({ of: 'card', name }, v), children: v }),
    ),
  });
}

/** Sam's card: three tones, and a joke when it is funny. */
export function SamsCard() {
  const tone = useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
  const joke =
    tone === 'funny'
      ? useChoose(choices<Joke>('pun', 'tease'), { of: 'card', name: 'joke', defaultValue: 'pun' })
      : undefined;
  return host('div', {
    children: [
      <Buttons name="tone" values={TONES} now={tone} />,
      ...(joke ? [<Buttons name="joke" values={['pun', 'tease']} now={joke} />] : []),
      host('button', { onClick: { event: 'to', args: ['family'] }, children: 'Make it the family’s card' }),
      <Card tone={tone} joke={joke} from="Sam" />,
    ],
  });
}

/** The card the whole family signs, for the dinner: no funny tone, so no joke. */
export function FamilyCard() {
  const tone = useChoose(choices<Tone>('warm', 'formal'), { of: 'card', name: 'tone', defaultValue: 'warm' });
  return host('div', {
    children: [
      <Buttons name="tone" values={['warm', 'formal']} now={tone} />,
      host('button', { onClick: { event: 'to', args: ['sam'] }, children: 'Back to Sam’s card' }),
      <Card tone={tone} from="the whole family" />,
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

changes(before, after, decided) compares the choices read before and after, and returns a ProgramChange that sorts the decided ones:

  • added and removed list choices read now and not before, and the other way round.
  • kept is a decision that applied before and still does.
  • waiting is one that applied before and does not now. Its choice is not read, or its value is not one of the possibilities.
  • back is one that waited and applies again.

Press funny, then tease, then Make it the family’s card. The joke is removed, and both decisions wait: the family's card has no funny tone and reads no joke. Press Back to Sam’s card, and both are back. Decide warm before switching, and the tone is kept.

changes only reports. A waiting decision stays in its layer, and applies as soon as the program can read it.

Choice by choice

The lines after changes come from diff(before, after). Each choice whose value or source changed gets a line, key: was → is (source):

card#tone: "funny" → "warm" (default)
card#joke: "tease" → (gone)

A choice read for the first time is (new); one no longer read is (gone). The lines are for a person or a log. Each says what changed and which layer the value comes from now.

Decisions nothing reads

On the family's card, nothing reads Sam's tease:

/** @jsxImportSource @reintent/core */
import { action, 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}!`;
}

/** What the buttons do: the intent gives them to every evaluation (index.intent.tsx). */

function Card({ tone, joke, from }: { tone: Tone; joke?: Joke; from: string }) {
  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: `— ${from}` }),
    ],
  });
}

function Buttons({ name, values, now }: { name: string; values: readonly string[]; now?: string }) {
  return host('p', {
    children: values.map((v) =>
      host('button', { disabled: v === now, onClick: action.decide({ of: 'card', name }, v), children: v }),
    ),
  });
}

/** Sam's card: three tones, and a joke when it is funny. */
export function SamsCard() {
  const tone = useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
  const joke =
    tone === 'funny'
      ? useChoose(choices<Joke>('pun', 'tease'), { of: 'card', name: 'joke', defaultValue: 'pun' })
      : undefined;
  return host('div', {
    children: [
      <Buttons name="tone" values={TONES} now={tone} />,
      ...(joke ? [<Buttons name="joke" values={['pun', 'tease']} now={joke} />] : []),
      host('button', { onClick: { event: 'to', args: ['family'] }, children: 'Make it the family’s card' }),
      <Card tone={tone} joke={joke} from="Sam" />,
    ],
  });
}

/** The card the whole family signs, for the dinner: no funny tone, so no joke. */
export function FamilyCard() {
  const tone = useChoose(choices<Tone>('warm', 'formal'), { of: 'card', name: 'tone', defaultValue: 'warm' });
  return host('div', {
    children: [
      <Buttons name="tone" values={['warm', 'formal']} now={tone} />,
      host('button', { onClick: { event: 'to', args: ['sam'] }, children: 'Back to Sam’s card' }),
      <Card tone={tone} from="the whole family" />,
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

orphans(intent.layers, results) lists the decisions that none of results reads, here the joke, [ 'card#joke' ]. The tone is read and waiting, so it is not an orphan. An orphan is only reported. The decision stays in intent.layers and comes back with Sam's card. The host decides what to do with it, perhaps asking Sam whether to intent.forget it.

Pass every realization the host shows: a decision read on any screen is not an orphan.

Recap

  • changes(before, after, decided) sorts decisions into kept, waiting and back, and lists choices added and removed.
  • A waiting decision stays in its layer and applies again when the program reads it.
  • diff(before, after) gives one line per choice that changed, with its new source.
  • orphans(intent.layers, results) lists decisions nothing reads; the host decides what to do with them.

Last updated on

Edit on GitHub