Skip to content

Comments

H-5641: Probability Distributions in Petrinaut#8463

Draft
kube wants to merge 6 commits intocf/h-5839-create-an-lsp-and-re-use-in-monaco-editorfrom
cf/h-5641-add-input-for-probabilistic-transition-kernels
Draft

H-5641: Probability Distributions in Petrinaut#8463
kube wants to merge 6 commits intocf/h-5839-create-an-lsp-and-re-use-in-monaco-editorfrom
cf/h-5641-add-input-for-probabilistic-transition-kernels

Conversation

@kube
Copy link
Collaborator

@kube kube commented Feb 20, 2026

Kapture.2026-02-21.at.03.40.43.mp4

This example is accessible in the menu (Probabilistic Satellites Launcher)

🌟 What is the purpose of this PR?

Adds probability distribution support to Petrinaut transition kernels. Users can now return Distribution.Gaussian(mean, deviation) or Distribution.Uniform(min, max) values from transition kernel code instead of plain numbers, enabling stochastic token generation.

It allows to define Transition Kernels using Distribution, like:

export default TransitionKernel((tokensByPlace, parameters) => {
  const distance = 80;
  const angle = Distribution.Uniform(0, Math.PI * 2);

  return {
    Space: [
      {
        // Here (x,y) are derived from the same Uniform `angle`
        x: angle.map(a => Math.cos(a) * distance),
        y: angle.map(a => Math.sin(a) * distance),
        direction: Distribution.Uniform(0, Math.PI * 2),
        velocity: Distribution.Gaussian(60, 20)
      }
    ],
  };
});

🔗 Related links

🔍 What does this change?

  • Extracts generateVirtualFiles from create-sdcpn-language-service.ts into its own module
  • Adds a global sdcpn-lib.d.ts virtual file exposing Distribution type and Distribution.Gaussian/Distribution.Uniform namespace functions for type checking
  • Adds Probabilistic<T> utility type so kernel output allows number | Distribution for numeric fields
  • Creates distribution.ts runtime module with:
    • RuntimeDistribution tagged union type for runtime detection
    • distributionRuntimeCode injected into compiled user code context
    • sampleDistribution() using seeded RNG (Box-Muller transform for Gaussian)
  • Updates compile-user-code.ts to inject the Distribution namespace into the execution environment
  • Updates compute-possible-transition.ts to resolve Distribution values in kernel output by sampling, threading RNG state through all samples

Pre-Merge Checklist 🚀

🚢 Has this modified a publishable library?

This PR:

  • modifies an npm-publishable library and I have added a changeset file(s)

📜 Does this require a change to the docs?

The changes in this PR:

  • are in a state where docs changes are not yet required but will be

🕸️ Does this require a change to the Turbo Graph?

The changes in this PR:

  • do not affect the execution graph

🛡 What tests cover this?

  • Existing simulator tests continue to pass (no Distribution values = no behavior change)

❓ How to test this?

  1. Checkout the branch
  2. In a transition kernel editor, use Distribution.Gaussian(0, 1) or Distribution.Uniform(0, 10) as a value in the returned output tokens
  3. Confirm that the type checker accepts Distribution where number is expected in kernel output
  4. Run a simulation and confirm sampled values vary per the seeded RNG

kube and others added 2 commits February 20, 2026 17:25
Extract `generateVirtualFiles` into its own module and add a global
`sdcpn-lib.d.ts` virtual file exposing `DistributionGaussian` and `Distribution`
types for use in transition kernel code.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use a namespace pattern so additional distribution types can be added
alongside Gaussian (e.g. Distribution.Uniform, Distribution.Exponential).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hash Error Error Feb 21, 2026 3:01am
petrinaut Ready Ready Preview Feb 21, 2026 3:01am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
hashdotdesign Ignored Ignored Preview Feb 21, 2026 3:01am
hashdotdesign-tokens Ignored Ignored Preview Feb 21, 2026 3:01am

@github-actions github-actions bot added area/libs Relates to first-party libraries/crates/packages (area) type/eng > frontend Owned by the @frontend team labels Feb 20, 2026
Copy link
Collaborator Author

kube commented Feb 20, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@kube kube changed the title H-5641: Extract virtual file generation and add global Distribution type H-5641: Probability Distributions in Petrinaut Feb 20, 2026
Add Distribution.Gaussian and Distribution.Uniform as runtime-available
functions in transition kernel code. Distribution values in kernel output
are sampled using the seeded RNG (Box-Muller for Gaussian) before being
stored as token values.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kube and others added 2 commits February 21, 2026 03:36
Add `.map(fn)` method to Distribution for deriving correlated values from
a single sample (e.g. angle.map(Math.cos), angle.map(Math.sin)). Sampled
values are cached on the distribution object so mapped siblings share one
draw. Also add a probabilistic satellites launcher example.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fix import sort order, remove unused eslint-disable directive, add
no-param-reassign override for distribution caching, and replace .map()
closure with imperative loop to avoid no-loop-func violation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/infra Relates to version control, CI, CD or IaC (area) area/libs Relates to first-party libraries/crates/packages (area) type/eng > frontend Owned by the @frontend team

Development

Successfully merging this pull request may close these issues.

1 participant