Your First Program

Build a complete program with uncertainty tracking

Your First Real Program

Let’s build something more substantial: a BMI calculator with uncertainty tracking.

The Problem

Traditional BMI calculators give you a single number. But measurements have uncertainty:

  • Your scale has ±0.5 kg accuracy
  • Your height measurement has ±1 cm accuracy

Sounio handles this naturally.

The Code

fn main() with IO {
    // Measurements with uncertainty
    let mass: Knowledge<kg> = measure(
        value: 75.3,
        uncertainty: 0.5,
        source: "bathroom_scale"
    )

    let height: Knowledge<m> = measure(
        value: 1.82,
        uncertainty: 0.01,
        source: "tape_measure"
    )

    // BMI calculation - uncertainty propagates automatically
    let bmi = mass / (height * height)

    // Output includes uncertainty
    print("Your BMI: " + bmi.to_string())
    // Output: Your BMI: 22.73 ± 0.31 kg/m²

    // Confidence gate: only give advice if confident
    if bmi.confidence > 0.90 {
        if bmi.value < 18.5 {
            print("Category: Underweight")
        } else if bmi.value < 25.0 {
            print("Category: Normal")
        } else {
            print("Category: Overweight")
        }
    } else {
        print("Measurements too uncertain for classification")
    }
}

Key Concepts Demonstrated

1. Knowledge Types

The Knowledge<T> type wraps any value with:

  • The measured value
  • Its uncertainty (standard deviation)
  • The data source (provenance)
  • A confidence level

2. Automatic Propagation

When you compute mass / (height * height), Sounio automatically:

  • Propagates uncertainty using GUM rules
  • Tracks which sources contributed
  • Updates confidence levels

3. Confidence Gates

The if bmi.confidence > 0.90 check ensures we only make decisions when we’re confident enough.

Running It

sounio run bmi.sio

What’s Next?