kernel.nix
Source: /nix/store/85n2k2gkxxwwpmhwfp2asxfln07nbklv-source/nix/kernel.nix
Module Description
Core kernel primitives for the NFX effect system
Combinators
adapt
The fundamental combinator - everything else builds on this.
adapt is the core primitive that enables all effect transformations.
It simultaneously handles:
- Contravariant mapping of context requirements (what the effect needs)
- Covariant mapping of continuations (what happens given the result)
Type Signature
Fx<S,V> -> (T -> S) -> (T -> S -> V -> Fx<T,U>) -> Fx<T,U>
Parameters
e: The effect to transformcmap: Context mapperT -> S- extracts inner requirement from outer contextfmap: Continuation(T, S, V) -> Fx<T,U>- transforms the result
How It Works
Creates an adapt node { _tag="adapt", e, cmap, fmap } — a pure data
structure. The interpreter (runFx) peels adapt nodes iteratively,
eliminating the O(N) stack growth that a recursive closure would cause.
Example
# Double the result of an effect
adapt (pure 21) (s: s) (_t: _s: v: immediate {} (v * 2))
See Also
map- Simpler value transformation (built on adapt)contraMap- Simpler context transformation (built on adapt)
immediate
Creates an immediate (already resolved) effect.
An immediate effect is the terminal case of the Fx type - it carries both a state and a value, meaning it needs no further context to produce its result. This is one of two fundamental constructors for the Fx algebraic data type.
Type Signature
state -> value -> Fx<state, value>
Parameters
state: The current state/context that was used to produce the valuevalue: The computed result
Example
immediate {} 42 # Effect holding value 42 with empty state
immediate { count = 1; } "done" # Effect with state and string value
See Also
pending- The other Fx constructor for suspended computationsrunFx- Evaluates an effect to extract its value
pending
Creates a pending (suspended) effect awaiting context.
A pending effect wraps an “ability” - a function that will be called given context to produce the next step of computation. This is the mechanism by which effects express their requirements: the ability requests something from the context.
Type Signature
(context -> Fx<S, V>) -> Fx<S, V>
Parameters
ability: A functioncontext -> Fx<S, V>that consumes context and produces the next computation step
Example
# Effect that reads a number from context and doubles it
pending (n: immediate n (n * 2))
# Effect requesting an attribute from context
pending (ctx: immediate ctx ctx.username)
See Also
immediate- The terminal case, already has its valueadapt- The fundamental combinator for transforming effects
Generated from /nix/store/85n2k2gkxxwwpmhwfp2asxfln07nbklv-source/nix/kernel.nix