Reintent

Choosing a subtree

A choice whose possibilities are whole parts of the card.

The front of Grandma's card could be an old photo of her, a drawing of her flowers, or a short poem. Each is a different part of the card, and the poem has a question of its own: how long it is. <Branch> writes a choice like this in one place, with its possibilities and the subtree each one shows.

You will learn

  • How <Branch> shows one subtree for a choice
  • How a case reads the value the choice took
  • What the branch node in λ tells the host

One of several subtrees

Three fronts, one choice:

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

export const FRONTS = ['photo', 'drawing', 'poem'] as const;
export type Front = (typeof FRONTS)[number];

export function Photo() {
  return host('div', {
    style: {
      height: 120,
      borderRadius: 8,
      background: '#c8b8a6',
      color: '#fff',
      display: 'grid',
      placeItems: 'center',
    },
    children: 'Grandma, 1956',
  });
}

export function Drawing() {
  return host('div', {
    style: { fontSize: 40, textAlign: 'center', color: '#d9719b', letterSpacing: 8 },
    children: '✿ ✿ ✿',
  });
}

/** Only the poem has a question of its own: how long it is. */
export function Poem() {
  const length = useChoose(choices('short', 'long'), { of: 'poem', name: 'length', defaultValue: 'short' });
  const lines = ['Ninety springs of garden light,', 'ninety years of holding tight.'];
  if (length === 'long') lines.push('Every one of us is here', 'to thank you for another year.');
  return host('div', {
    style: { fontStyle: 'italic', textAlign: 'center' },
    children: lines.map((l) => host('div', { children: l })),
  });
}

export function Card() {
  return host('div', {
    style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
    children: [
      // The front: one of three subtrees.
      <Branch
        on={choices<Front>(...FRONTS)}
        of="card"
        name="front"
        defaultValue="photo"
        cases={{ photo: <Photo />, drawing: <Drawing />, poem: <Poem /> }}
      />,
      host('h1', { style: { color: '#b5562b' }, children: 'Happy 90th birthday, Grandma!' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

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

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

<Branch on of name defaultValue cases> reads a choice the way useChoose does. on is its possibilities, of and name its identity, defaultValue its default. cases has an entry for every possibility, the subtree to show for it, and TypeScript checks that none is missing.

Pick poem in Choices. The front is the poem, and a second choice appears, poem#length. Only the case the choice took is evaluated, so the poem's question exists only while the poem is the front.

Cases that read the value

Each front can carry what it needs: the photo its year, the poem how many lines.

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

/** Each front carries what it needs: the photo its year, the poem how many lines. */
export type Front = { kind: 'photo'; year: number } | { kind: 'drawing' } | { kind: 'poem'; lines: number };

export const FRONTS = choices<Front>([
  { kind: 'photo', year: 1956 },
  { kind: 'drawing' },
  { kind: 'poem', lines: 4 },
]);

const POEM = [
  'Ninety springs of garden light,',
  'ninety years of holding tight.',
  'Every one of us is here',
  'to thank you for another year.',
];

export function Photo(p: { year: number }) {
  return host('div', {
    style: {
      height: 120,
      borderRadius: 8,
      background: '#c8b8a6',
      color: '#fff',
      display: 'grid',
      placeItems: 'center',
    },
    children: `Grandma, ${p.year}`,
  });
}

export function Drawing() {
  return host('div', {
    style: { fontSize: 40, textAlign: 'center', color: '#d9719b', letterSpacing: 8 },
    children: '✿ ✿ ✿',
  });
}

export function Poem(p: { lines: number }) {
  return host('div', {
    style: { fontStyle: 'italic', textAlign: 'center' },
    children: POEM.slice(0, p.lines).map((l) => host('div', { children: l })),
  });
}

export function Card() {
  return host('div', {
    style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
    children: [
      <Branch
        on={FRONTS}
        of="card"
        name="front"
        defaultValue={{ kind: 'photo', year: 1956 }}
        cases={{
          // A function of the value the choice took: its data is in reach.
          photo: (front) => <Photo year={front.year} />,
          drawing: <Drawing />,
          poem: (front) => <Poem lines={front.lines} />,
        }}
      />,
      host('h1', { style: { color: '#b5562b' }, children: 'Happy 90th birthday, Grandma!' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
    ],
  });
}

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

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

The possibilities are objects with a kind (Possibilities that carry data), and cases is keyed by their tags. A case can be a function. It gets the value the choice took, typed for its kind, so photo: (front) => <Photo year={front.year} /> reads the year. A case with nothing to read, like drawing, stays an element.

Change lines: 4 to lines: 2 in FRONTS and pick poem. The poem is two lines long.

The branch in λ

The host draws the front Sam's card has. To offer the other fronts beside it, the host needs to know them:

/** @jsxImportSource @reintent/core */
import { createIntent, type HostNode } from '@reintent/core';
import App from './App.intent';

const intent = createIntent({ program: <App /> });

// The card's first child is the branch: the choice, every case it could take, and the one it took.
const branch = (intent.realize().tree as HostNode).children[0] as HostNode;
const { of, name, chosen, kinds } = branch.props;
console.log(branch.kind, of, name); // → 'branch' 'card' 'front'
console.log(chosen, kinds); // → 'photo' [ 'photo', 'drawing', 'poem' ]
// Its children are the case it took.
console.log(branch.children.length); // → 1
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

<Branch> leaves a branch node in λ: host('branch', { of, name, chosen, kinds }), with the case it took as its children. chosen is the tag the choice took, and kinds every tag it could take. The sandbox's host draws the children. Another host could list the other kinds in a menu beside them. What happened shows the branch's props.

Inside <World each>, the choice is enumerated, and every case is evaluated, one path each. Its props are BranchProps.

Branch is a choice like any other

Decide it, give it a question and ask={['model']}, put it under a <World>. The front is read with useChoose, so everything said about choices holds for it.

Recap

  • <Branch on of name defaultValue cases> is a choice whose possibilities are subtrees.
  • Only the case taken is evaluated; the choices inside the others are read once they are taken.
  • A case can be a function of the value the choice took.
  • λ holds a branch node with chosen, kinds, and the case taken as its children.

Last updated on

Edit on GitHub