Drawing a value
A World can draw each choice from its distribution, again and again, with or without replacement; every draw is a commit with its result.
Grandma's card leans warm, 0.80 to funny's 0.15. Taking the likeliest gives the warm card every time. Sometimes the software should surprise instead: a Surprise me button, a card of the day, a door that opens onto somewhere new. Then the value is drawn from the distribution, and the weights say how often each one comes up.
You will learn
- How
<World sample>draws each choice in it from its distribution - How
replace: falsedraws a value only once a round - What a draw records, and why undo and replay give the same card
Draw
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus, World } 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}!`;
}
/** The card's tone: System One weighs it; inside a sampling World, a draw takes it. */
export function Card() {
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'],
});
return (
<div style={{ padding: 14, borderRadius: 10, background: LOOK[tone.value].background, color: '#3a2a1c' }}>
<h3 style={{ color: LOOK[tone.value].accent, margin: 0 }}>{title(GRANDMA, tone.value)}</h3>
<small>
{tone.value} · {tone.source}
</small>
</div>
);
}
export default function App() {
return (
<div style={{ display: 'grid', gap: 8 }}>
<World id="card" sample>
<Card />
</World>
{/* Each press draws a tone from System One's weights: warm most of the time, now and then another. */}
<button onClick={action.draw('card')}>Surprise me</button>
</div>
);
}
Starting…
Starting…
Nothing logged.
<World id="card" sample> is a handler, like minMargin: it says how the choices under it collapse. Press Surprise me. The button holds action.draw('card'), and the card takes the tone the draw took, with the source drawn. Press it a few times: mostly warm, now and then funny, rarely formal, as System One weighs them. Before the first press nothing is drawn, and the World collapses as any other: warm, by System One's lead.
Not the same twice
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus, World } 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}!`;
}
/** The card's tone: System One weighs it; inside a sampling World, a draw takes it. */
export function Card() {
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'],
});
return (
<div style={{ padding: 14, borderRadius: 10, background: LOOK[tone.value].background, color: '#3a2a1c' }}>
<h3 style={{ color: LOOK[tone.value].accent, margin: 0 }}>{title(GRANDMA, tone.value)}</h3>
<small>
{tone.value} · {tone.source}
</small>
</div>
);
}
export default function App() {
return (
<div style={{ display: 'grid', gap: 8 }}>
<World id="card" sample={{ replace: false }}>
<Card />
</World>
{/* Each press draws a tone this round has not drawn yet: three presses, three tones. */}
<button onClick={action.draw('card')}>Another one</button>
</div>
);
}
Starting…
Starting…
Nothing logged.
With sample={{ replace: false }}, a tone this round has drawn is not drawn again. Press Another one three times, and you see the three tones, in an order the weights favour. The fourth press begins a new round. Use it for a deck, a set of suggestions, or places to find.
What a draw is
A draw is a commit, like a decision, and the history keeps it. At that moment Reintent evaluates the program and, for every choice read in the World that nobody decided, draws a value from its distribution and records it with the seed (drawn, in the choice's own record).
- The result is kept. If System One's weights change later, the card keeps what was drawn. Undo takes the draw back, redo brings back the same card, and the same choices always give the same λ.
- Only what is read is drawn. A choice read only on a branch the draw took, such as a joke behind a funny tone, is drawn in the same draw. A choice that was not read is not drawn.
- A person comes first. A decided tone is not drawn. Forget the decision, and the tone the last draw took comes back.
- Code draws, not the host. The host only says "draw". The value comes from the distribution the cascade gives, and its Actor is Code.
intent.draw('card') draws from code. Inside a commit, tx.draw('card', seed) draws together with other changes, for example opening a door in the same step (The Door Above the Sea).
Recap
<World id sample>collapses each choice under it by drawing from its distribution.replace: falsedraws each value once a round.action.draw(id)andintent.draw(id)draw. A draw is a commit that records each value it took.- A person's decision stays above every draw.
Last updated on
Edit on GitHub