Core implementation of the Prism programming language - a language designed for expressing and managing uncertainty in computational systems.
📚 Full Documentation | 🚀 Getting Started | 📖 API Reference
npm install @prism-lang/core
The easiest way to run Prism code is using the runPrism
helper function, which handles parsing and runtime setup for you.
import { runPrism } from '@prism-lang/core';
// Run Prism code directly
const result = await runPrism('x = 5 ~> 0.9; x * 2');
console.log(result); // ConfidenceValue { value: 10, confidence: 0.9 }
// Access the actual value
console.log(result.value); // 10
console.log(result.confidence); // 0.9
// With custom globals
const result2 = await runPrism('PI * radius * radius', {
globals: { PI: 3.14159, radius: 5 }
});
console.log(result2); // 78.53975
// With LLM provider
import { MockLLMProvider } from '@prism-lang/llm';
const provider = new MockLLMProvider();
provider.setMockResponse('Hello! How can I help?', 0.85);
const result3 = await runPrism('response = llm("Hello"); response', {
llmProvider: provider
});
console.log(result3.value); // "Hello! How can I help?"
console.log(result3.confidence); // 0.85
Note: The
globals
option currently only supports primitive values (numbers, strings, booleans) and simple objects. Functions cannot be injected as globals due to runtime limitations.
import { parse, createRuntime } from '@prism-lang/core';
const code = `
// Confidence values
prediction = "rain" ~> 0.8
temperature = 72 ~> 0.95
// Confidence-aware control flow
uncertain if (prediction) {
high { "definitely raining" }
medium { "might rain" }
low { "probably sunny" }
}
`;
const ast = parse(code);
const runtime = createRuntime();
const result = await runtime.execute(ast);
-
Confidence Values: First-class support for uncertainty with the
~>
operator -
Confidence Operators: Extract (
<~
), multiply (~*
), combine (~||>
) -
Uncertain Control Flow:
uncertain if/while/for
with high/medium/low/default branches - Adaptive Confidence: Default branches for confidence recalibration and fallback logic
-
LLM Integration: Built-in
llm()
function with automatic confidence - Functional Programming: Lambdas, array methods, destructuring
-
Pipeline Operator: Chain operations with
|>
for cleaner code
// Assign confidence
value = 100 ~> 0.9
// Extract confidence
conf = <~ value
// Confidence operations
doubled = value ~* 2 // Maintains confidence
combined = value1 ~||> value2 // Picks highest confidence
uncertain if (measurement) {
high {
// confidence >= 0.7
perform_critical_action()
}
medium {
// 0.5 <= confidence < 0.7
request_human_review()
}
low {
// confidence < 0.5
abort_and_log()
}
}
// With default branch for unmatched cases
uncertain while (sensor_reading ~> confidence) {
high {
process_automatically()
}
low {
skip_reading()
}
default {
// Handle cases where branch is missing
// or confidence needs recalibration
confidence = recalibrate_sensor()
if (confidence < 0.1) break
}
}
// Note: llm() requires provider setup first
response = llm("Is this safe?")
conf = <~ response // Automatic confidence extraction
// With confidence threshold
safe = response ~> 0.9
if (<~ safe >= 0.9) {
proceed()
}
-
runPrism(code: string, options?: RunPrismOptions): Promise<Value>
- Run Prism code directly-
options.globals
- Object with global variables to inject -
options.llmProvider
- LLM provider instance -
options.defaultProviderName
- Name for the LLM provider (default: 'default')
-
-
parse(code: string): Program
- Parse Prism code into AST
-
createRuntime(): Runtime
- Create a new runtime instance -
runtime.execute(ast: Program): Promise<Value>
- Execute AST -
runtime.registerLLMProvider(name: string, provider: LLMProvider)
- Register LLM provider -
runtime.setDefaultLLMProvider(name: string)
- Set default LLM provider
-
NumberValue
,StringValue
,BooleanValue
-
ArrayValue
,ObjectValue
,FunctionValue
-
ConfidenceValue
- Wraps any value with confidence -
NullValue
,UndefinedValue
-
llm(prompt: string, options?: object)
- Make LLM calls (requires provider setup) -
map(array: Array, fn: Function)
- Map over array elements -
filter(array: Array, fn: Function)
- Filter array elements -
reduce(array: Array, fn: Function, initial?: any)
- Reduce array to single value -
max(...values: number[])
- Find maximum value -
min(...values: number[])
- Find minimum value - Array and Object methods are available as built-in functions
- Note: Use
console.log()
for output (no built-inprint
function)
Arrays support the following methods:
-
.map(fn)
- Transform elements -
.filter(fn)
- Filter elements -
.reduce(fn, initial?)
- Reduce to single value -
.push(...items)
- Add elements -
.forEach(fn)
- Iterate over elements -
.join(separator?)
- Join elements as string -
.length
- Get array length
See the examples directory for more complex examples.
-
@prism-lang/llm
- LLM provider integrations -
@prism-lang/confidence
- Confidence extraction utilities -
@prism-lang/validator
- Validation toolkit -
@prism-lang/cli
- Command-line interface -
@prism-lang/repl
- Interactive REPL
MIT