Who answers a choice
The code's prior, System One's judgment and a person's decision, each in its own layer.
The code expects one tone, System One judges another, and Sam presses a third. The card can show only one of them, and the other two have to go somewhere.
The code's expectation: a prior
In Quick start the joke is choices<Joke>(...JOKES): pun or tease, equally likely, so the joke stays on its default until someone answers. The code does have a view, though. A pun about 90 laps around the sun is safer than teasing Grandma about her age. Write it as weights:
/** @jsxImportSource @reintent/core */
import { choices, host, useChoose } from '@reintent/core';
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];
/** Which joke: the code expects a pun. The weights are the code's prior. */
export function useJoke() {
return useChoose(
choices<Joke>([
{ value: 'pun', weight: 0.8 },
{ value: 'tease', weight: 0.2 },
]),
{ of: 'card', name: 'joke', defaultValue: 'pun' },
);
}
export default function App() {
const joke = useJoke();
return host('div', {
style: { maxWidth: 380, padding: 24, borderRadius: 12, background: '#fff2a8', color: '#3a2a1c' },
children: [
host('h1', {
style: { color: '#7a4fd6' },
children:
joke === 'tease' ? 'Grandma, you don’t look a day over 89!' : '90 laps around the sun, Grandma!',
}),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: '— Sam' }),
],
});
}
Starting…
Starting…
Nothing logged.
choices([{ value, weight }]) gives each possibility a weight. The weights are the code's prior, the bottom layer of the choice, and Choices says where the value came from: code's prior. The pun leads by 0.6, more than the 0.2 the default policy asks for, so the prior settles the joke with nobody asked.
Change the weights to 0.55 and 0.45. The lead is 0.1, short of 0.2, so the joke is unresolved and the card uses its default. Change them to 0.3 and 0.7, and the card teases.
Equal weights say nothing
choices('pun', 'tease') writes no prior at all, and 0.5 against 0.5 gives neither a lead. Either way, the choice waits for a writer above the code. Write a prior when the code really does expect one possibility more than another.
Layers, from the top
Here the code expects formal (0.6), System One judges, and two buttons let Sam decide or hand the tone back:
/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoiceStatus, 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 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) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${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). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
export const Forget = createContext<(id: Id) => void>('Forget', { key: () => undefined });
/** The tone: the code expects formal (its prior), System One judges, a person decides. */
/** The tone, and the layer it came from. */
export function useTone() {
return useChoiceStatus(
choices<Tone>([
{ value: 'warm', weight: 0.2 },
{ value: 'funny', weight: 0.2 },
{ value: 'formal', weight: 0.6 },
]),
{
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
},
);
}
export function Card({ p, tone }: { p: Person; tone: Tone }) {
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(p, tone) }),
host('p', { children: 'Wishing you a wonderful day.' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
],
});
}
export function Buttons({ tone }: { tone: { value: Tone; source: string } }) {
const id = { of: 'card', name: 'tone' };
const decided = tone.source === 'decision';
return host('p', {
children: [
// A person's decision is pressed; a value the card took from a layer below is only marked.
...TONES.map((v) =>
host('button', {
disabled: decided && v === tone.value,
...(!decided && v === tone.value && { style: { textDecoration: 'underline dotted' } }),
onClick: action.decide(id, v),
children: v,
}),
),
host('button', {
disabled: !decided,
onClick: action.forget(id),
style: {
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(decided && { background: 'transparent' }),
},
children: 'no preference',
}),
host('small', { style: { marginLeft: 12 }, children: `from: ${tone.source}` }),
],
});
}
export default function App() {
const tone = useTone();
return host('div', { children: [<Card p={GRANDMA} tone={tone.value} />, <Buttons tone={tone} />] });
}
Starting…
Starting…
Nothing logged.
The card reads the top layer that has something to say:
- A person's decision:
intent.decide, here a button. - System One's judgment, a belief: the answer to
judge. - The code's prior: the weights in
choices. - The default:
defaultValue, when none of the above settles it.
System One judges warm, 0.80, and its belief is above the code's formal. Press funny, and a person's decision is above both. Press no preference, and the decision is forgotten, so System One's warm is back. Each writer keeps its own layer, and an answer above leaves the ones below as they are.
To see the prior, take System One away: delete model: recordedModel() in index.intent.tsx. Nothing answers the judgment, and the card turns formal, from the code's prior.
The value and its layer
The buttons above mark the tone differently when Sam pressed it and when the card took it from a layer below. They read the tone with useChoiceStatus, which takes the same arguments as useChoose and returns a ChoiceStatus:
const tone = useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
// tone.value: 'warm', tone.source: 'belief', tone.status: 'resolved'valueis whatuseChoosewould return.sourceis the layer, aSource. Thefrom:after the buttons shows it.status, aStatus, says how settled it is:'decided'by a person,'resolved'by a layer that leads by enough, or'unresolved', where the<World>around it says what happens (How sure is sure enough).stalemarks an earlier answer of System One standing in for a new one (Asking System One), andwaitinglists decisions the card does not offer right now.
Reading every choice from the intent
A test has no buttons to look at. It holds the intent, and intent.realize() returns λ as tree (Showing λ) and every choice the evaluation read as reads:
/** @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 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) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${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). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
export const Forget = createContext<(id: Id) => void>('Forget', { key: () => undefined });
export function useTone() {
return useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
}
export function Card({ p, tone }: { p: Person; tone: Tone }) {
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(p, tone) }),
host('p', { children: 'Wishing you a wonderful day.' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
],
});
}
export function Buttons({ tone }: { tone: Tone }) {
const id = { of: 'card', name: 'tone' };
return host('p', {
children: [
...TONES.map((v) =>
host('button', { disabled: v === tone, onClick: action.decide(id, v), children: v }),
),
host('button', {
onClick: action.forget(id),
style: { marginLeft: 12, borderStyle: 'dashed', background: 'transparent', fontStyle: 'italic' },
children: 'no preference',
}),
],
});
}
export default function App() {
const tone = useTone();
return host('div', { children: [<Card p={GRANDMA} tone={tone} />, <Buttons tone={tone} />] });
}
Starting…
Starting…
Nothing logged.
Each read is a ChoiceRead, with the same value, source and status as useChoiceStatus gives, and the choice's id, key and kinds. What it logs is in What happened.
source has a few more values than the four layers. 'constraint' and 'preference' come from a person's words short of a decision (What a person says), 'preview' from a <World as> (Seeing other possibilities), 'measured' from what the host measured in this context, which is below a person and above System One (What the host measured), and 'unknown' from an entity whose data is not loaded yet, with the status 'unknown' too.
Recap
choices([{ value, weight }])writes the code's prior;choices(a, b)writes none.- The layers, from the top: a person's decision, System One's judgment, the code's prior, the default.
- The card reads the top layer that settles the choice; the layers below stay as they are.
useChoiceStatusgives a choice's value with itssourceandstatus.intent.realize().readslists every choice read, each aChoiceRead.
Last updated on
Edit on GitHub