Documentation
¶
Overview ¶
Package cove carries explicit ambient context across kont suspension boundaries.
When a runtime advances a kont computation one suspension at a time, each suspended operation may depend on state that lives outside the operation itself, such as dispatch budget, ring capabilities, protocol phase, or buffer-group validity. cove pairs that context with values and suspensions so it stays explicit across asynchronous boundaries instead of travelling through hidden globals or ad-hoc side maps.
Read through the lens of modal effects, the carrier shape can be read as naming what happens to ambient context: kont (C → D) replaces it, cove (W(C, A) → B) handles it relatively, and pure pass-through preserves it. Effect structure rides on the carrier, not on a function colour, which keeps cove policy-free.
Responsibilities stay split by layer:
- code.hybscloud.com/iox classifies outcome and progress evidence
- kont defines suspension and resumption shape
- cove carries and checks explicit context
- code.hybscloud.com/takt advances execution without taking ownership of that context
The package is policy-free: it carries and checks context but never schedules, retries, or dispatches. Kernel mechanics belong to uring, and semantic outcome branching belongs to iox, including code.hybscloud.com/iox.Classify. The join point with takt is structural: a contextual carrier exposes the current suspended operation and accepts the matching resumption without transferring ownership of the context to the runner.
Carriers:
- [View[C, A]], a focused value paired with ambient context
- [Cmd[C, A, B]], a contextual command over View
- [SuspensionView[C, A]], a kont.Suspension paired with ambient context
- [Req[C]] / [ReqExpr[C]], context predicates in closure and data forms
- [Rule[C]] / [RuleExpr[C]] with Report, named predicates for diagnostics
- [Checked[C, A]] / [CheckedExpr[C, A]], requirement-gated values
- [Guarded[C, A]] / [GuardedExpr[C, A]], rule-gated values
Commands:
- ExtractCmd returns the focused value as the identity contextual command
- LiftCmd lifts a focus-only transform into a contextual command
- Compose composes contextual commands through Extend
- Run applies a command to a concrete View
Contextual suspension boundary:
- StepWith / StepExprWith run a kont computation and pair its first suspension with context
- MapContextSuspension / WithContextSuspension map or replace carried context explicitly
- SuspensionView.Op / SuspensionView.Resume expose the structural join point consumed by takt
- SuspensionView.ResumeWith advances a suspension and evolves context for the next step
- CheckSuspension / CheckSuspensionExpr gate contextualization on a requirement
Nil-completion convention: cove forwards kont's stepping classifier, so Step, StepExpr, StepWith, StepExprWith, SuspensionView.Resume, and SuspensionView.ResumeWith report completion by yielding no further suspension frontier (a nil *kont.Suspension, or a SuspensionView whose [SuspensionView.Suspension] field is nil). The completed payload returned in that case is the zero value of A. Suspended steps may also return the zero value of A, so callers must use the suspension/frontier result—not A itself, and for SuspensionView specifically its [SuspensionView.Suspension] field—to detect completion. Computations whose result type A is a nilable type (pointer, interface, map, slice, channel, or function) therefore cannot use nil as a meaningful completed value; wrap nil in an explicit sum or witness type when that distinction matters. Carrier context is unaffected: SuspensionView.Ask still returns the ambient context after completion.
Bridge helpers:
- Step, StepExpr, Reify, Reflect, ReifyReq, and ReflectReq remain available as convenience wrappers around kont
Laws:
- Duplicate(v).Extract() == v
- Extend(v, func(w View[C, A]) A { return w.Extract() }) == v
- Compose(g, f)(v) == g(Extend(v, f))
- Compose(ExtractCmd, f) == f and Compose(g, ExtractCmd) == g
- NeedExpr(ctx, ReifyReq(req)) == Need(ctx, req), when req != nil
- Need(ctx, ReflectReq(expr)) == NeedExpr(ctx, expr)
- ExprPullback distributes over ExprNot, ExprAll, and ExprAny
Index ¶
- func ExtractCmd[C Ambient, A Focus](v View[C, A]) A
- func Need[C Ambient](ctx C, req Req[C]) bool
- func NeedExpr[C Ambient](ctx C, req ReqExpr[C]) bool
- func Reflect[A Focus](m kont.Expr[A]) kont.Cont[Resumed, A]
- func Reify[A Focus](m kont.Cont[Resumed, A]) kont.Expr[A]
- func Run[C Ambient, A, B Focus](v View[C, A], cmd Cmd[C, A, B]) B
- func Step[A Focus](m kont.Cont[Resumed, A]) (A, *kont.Suspension[A])
- func StepExpr[A Focus](m kont.Expr[A]) (A, *kont.Suspension[A])
- type Ambient
- type Checked
- type CheckedExpr
- type Cmd
- type Focus
- type Guarded
- type GuardedExpr
- type Operation
- type Report
- type Req
- type ReqExpr
- func ExprAll[C Ambient](reqs ...ReqExpr[C]) ReqExpr[C]
- func ExprAny[C Ambient](reqs ...ReqExpr[C]) ReqExpr[C]
- func ExprAtom[C Ambient](fn func(C) bool) ReqExpr[C]
- func ExprFalse[C Ambient]() ReqExpr[C]
- func ExprNot[C Ambient](inner ReqExpr[C]) ReqExpr[C]
- func ExprPullback[C, D Ambient](req ReqExpr[D], f func(C) D) ReqExpr[C]
- func ExprTrue[C Ambient]() ReqExpr[C]
- func ReifyReq[C Ambient](req Req[C]) ReqExpr[C]
- type Resumed
- type Rule
- type RuleError
- type RuleExpr
- type SuspensionView
- func CheckSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A], req Req[C]) (SuspensionView[C, A], bool)
- func CheckSuspensionExpr[C Ambient, A Focus](ctx C, susp *kont.Suspension[A], req ReqExpr[C]) (SuspensionView[C, A], bool)
- func MapContextSuspension[C, D Ambient, A Focus](v SuspensionView[C, A], f func(C) D) SuspensionView[D, A]
- func ObserveSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A]) SuspensionView[C, A]
- func StepExprWith[C Ambient, A Focus](ctx C, m kont.Expr[A]) (A, SuspensionView[C, A])
- func StepWith[C Ambient, A Focus](ctx C, m kont.Cont[Resumed, A]) (A, SuspensionView[C, A])
- func WithContextSuspension[C Ambient, A Focus](v SuspensionView[C, A], ctx C) SuspensionView[C, A]
- func (v SuspensionView[C, A]) Ask() C
- func (v SuspensionView[C, A]) Discard()
- func (v SuspensionView[C, A]) Extract() *kont.Suspension[A]
- func (v SuspensionView[C, A]) Op() Operation
- func (v SuspensionView[C, A]) Resume(value Resumed) (A, SuspensionView[C, A])
- func (v SuspensionView[C, A]) ResumeWith(value Resumed, f func(C) C) (A, SuspensionView[C, A])
- type View
- func Duplicate[C Ambient, A Focus](v View[C, A]) View[C, View[C, A]]
- func Extend[C Ambient, A, B Focus](v View[C, A], f func(View[C, A]) B) View[C, B]
- func Map[C Ambient, A, B Focus](v View[C, A], f func(A) B) View[C, B]
- func MapContext[C, D Ambient, A Focus](v View[C, A], f func(C) D) View[D, A]
- func Observe[C Ambient, A Focus](ctx C, value A) View[C, A]
- func Replace[C Ambient, A, B Focus](v View[C, A], value B) View[C, B]
- func WithContext[C Ambient, A Focus](v View[C, A], ctx C) View[C, A]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractCmd ¶ added in v0.1.0
ExtractCmd returns the focused value and serves as the identity command for View: Compose(ExtractCmd, f) == f and Compose(g, ExtractCmd) == g.
func Reflect ¶
Reflect converts a kont.Expr computation to kont.Cont. Prefer StepWith or StepExprWith when the caller needs the contextual suspension boundary.
func Reify ¶
Reify converts a kont.Cont computation to kont.Expr. Prefer StepWith or StepExprWith when the caller needs the contextual suspension boundary.
func Step ¶
Step re-exports kont.Step. Prefer StepWith when the suspension should remain paired with explicit context. As with kont.Step, a nil suspension result denotes completion; the returned A follows kont's nil-completion convention (a nil payload becomes the zero value of A).
func StepExpr ¶
func StepExpr[A Focus](m kont.Expr[A]) (A, *kont.Suspension[A])
StepExpr re-exports kont.StepExpr. Prefer StepExprWith when the suspension should remain paired with explicit context. As with kont.StepExpr, a nil suspension result denotes completion; the returned A follows kont's nil-completion convention (a nil payload becomes the zero value of A).
Types ¶
type Checked ¶
Checked pairs a value with a requirement.
func MapChecked ¶
MapChecked maps the value and preserves the requirement.
func PullbackChecked ¶
PullbackChecked transports c along a context projection.
type CheckedExpr ¶
CheckedExpr pairs a value with a requirement expression.
func GuardExpr ¶
func GuardExpr[C Ambient, A Focus](req ReqExpr[C], value A) CheckedExpr[C, A]
GuardExpr constructs a checked value from a requirement expression.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type runtime struct{ Budget int }
checked := cove.GuardExpr(
cove.ExprAtom(func(r runtime) bool { return r.Budget > 0 }),
"payload",
)
view, ok := checked.IntoView(runtime{Budget: 2})
fmt.Println(ok, view.Extract())
_, ok = checked.IntoView(runtime{Budget: 0})
fmt.Println(ok)
}
Output: true payload false
func MapCheckedExpr ¶
func MapCheckedExpr[C Ambient, A, B Focus](c CheckedExpr[C, A], f func(A) B) CheckedExpr[C, B]
MapCheckedExpr maps the value and preserves the requirement.
func PullbackCheckedExpr ¶
func PullbackCheckedExpr[C, D Ambient, A Focus](checked CheckedExpr[C, A], f func(D) C) CheckedExpr[D, A]
PullbackCheckedExpr transports c along a context projection.
func (CheckedExpr[C, A]) Check ¶
func (c CheckedExpr[C, A]) Check(ctx C) bool
Check reports whether ctx satisfies c's requirement.
func (CheckedExpr[C, A]) IntoView ¶
func (c CheckedExpr[C, A]) IntoView(ctx C) (View[C, A], bool)
IntoView returns a view when c's requirement holds.
func (CheckedExpr[C, A]) MustView ¶
func (c CheckedExpr[C, A]) MustView(ctx C) View[C, A]
MustView returns a view and panics if c's requirement does not hold.
type Cmd ¶ added in v0.1.0
Cmd is the coKleisli arrow for View: it consumes a contextual observation and produces a focus result without hiding the ambient context.
type Guarded ¶
Guarded pairs a value with a named rule.
func MapGuarded ¶
MapGuarded maps the value and preserves the rule.
func PullbackGuarded ¶
PullbackGuarded transports g along a context projection.
type GuardedExpr ¶
GuardedExpr pairs a value with a named requirement expression.
func GuardRuleExpr ¶
func GuardRuleExpr[C Ambient, A Focus](rule RuleExpr[C], value A) GuardedExpr[C, A]
GuardRuleExpr constructs a guarded value from a rule expression.
func MapGuardedExpr ¶
func MapGuardedExpr[C Ambient, A, B Focus](g GuardedExpr[C, A], f func(A) B) GuardedExpr[C, B]
MapGuardedExpr maps the value and preserves the rule.
func PullbackGuardedExpr ¶
func PullbackGuardedExpr[C, D Ambient, A Focus](guarded GuardedExpr[C, A], f func(D) C) GuardedExpr[D, A]
PullbackGuardedExpr transports g along a context projection.
func (GuardedExpr[C, A]) Check ¶
func (g GuardedExpr[C, A]) Check(ctx C) Report
Check checks the guarded rule against ctx.
func (GuardedExpr[C, A]) IntoView ¶
func (g GuardedExpr[C, A]) IntoView(ctx C) (View[C, A], Report)
IntoView returns a view together with the rule report.
type Report ¶
type Report struct {
Failed RuleError // zero value means success
Checked int // number of rules examined before success or first failure
}
Report records the result of checking one or more named rules.
func CheckRuleExpr ¶
CheckRuleExpr checks a single rule expression.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type runtime struct{ Budget int }
rule := cove.RequireExpr("budget", cove.ExprAtom(func(r runtime) bool {
return r.Budget > 0
}))
report := cove.CheckRuleExpr(runtime{Budget: 5}, rule)
fmt.Println(report.OK())
report = cove.CheckRuleExpr(runtime{Budget: 0}, rule)
fmt.Println(report.OK(), report.Failed)
}
Output: true false budget
func CheckRules ¶
CheckRules checks rules from left to right and stops at the first failure.
func CheckRulesExpr ¶
CheckRulesExpr checks rule expressions from left to right and stops at the first failure.
func (Report) Err ¶ added in v0.1.0
Err returns the first failed rule as an error, or nil on success.
func (Report) FailedRule ¶ added in v0.1.0
FailedRule reports the first failed rule label, or "" on success.
For direct Rule literals with an empty name, the label falls back to "cove: unnamed rule".
type Req ¶
Req is a requirement over ambient context C.
func All ¶
All returns the conjunction of reqs.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type caps struct {
CanSubmit bool
HasToken bool
}
req := cove.All(
func(c caps) bool { return c.CanSubmit },
func(c caps) bool { return c.HasToken },
)
fmt.Println(cove.Need(caps{CanSubmit: true, HasToken: true}, req))
}
Output: true
func Pullback ¶
Pullback transports req along a context projection.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type runtime struct{ Budget int }
req := cove.Pullback(func(budget int) bool { return budget > 0 }, func(rt runtime) int {
return rt.Budget
})
fmt.Println(cove.Need(runtime{Budget: 2}, req))
}
Output: true
func ReflectReq ¶
ReflectReq returns a closure-form requirement that delegates to NeedExpr. It preserves the observable predicate, but once converted back to Req the original Expr structure is no longer recoverable from the closure alone.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type runtime struct {
Budget int
CanDispatch bool
}
expr := cove.ExprAll(
cove.ExprAtom(func(r runtime) bool { return r.Budget > 0 }),
cove.ExprAtom(func(r runtime) bool { return r.CanDispatch }),
)
req := cove.ReflectReq(expr)
ctx := runtime{Budget: 2, CanDispatch: true}
fmt.Println(cove.NeedExpr(ctx, expr))
fmt.Println(cove.Need(ctx, req))
}
Output: true true
type ReqExpr ¶
type ReqExpr[C Ambient] struct { // contains filtered or unexported fields }
ReqExpr stores a requirement as data instead of a closure. The zero value is ExprTrue.
func ExprPullback ¶
ExprPullback transports req along a context projection and preserves its explicit Boolean structure.
func ReifyReq ¶
ReifyReq wraps a closure-form Req as an ExprAtom. It is a lossy quotation helper for bridging into Expr-world composition, not a structural inverse of ReflectReq. In particular, ReifyReq(nil) is invalid and panics when the resulting expression is evaluated through NeedExpr.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
req := cove.Req[int](func(v int) bool { return v > 0 })
expr := cove.ReifyReq(req)
fmt.Println(cove.NeedExpr(1, expr))
fmt.Println(cove.NeedExpr(-1, expr))
}
Output: true false
type Rule ¶
Rule names a requirement for diagnostics.
func PullbackRule ¶
PullbackRule transports r along a context projection.
type RuleExpr ¶
RuleExpr names a requirement expression for diagnostics.
func PullbackRuleExpr ¶
PullbackRuleExpr transports r along a context projection.
func RequireExpr ¶
RequireExpr constructs a named requirement expression for diagnostics.
type SuspensionView ¶
type SuspensionView[C Ambient, A Focus] struct { Ctx C Suspension *kont.Suspension[A] }
SuspensionView pairs a kont.Suspension with ambient context. Its Op and Resume methods keep the carrier structurally joinable with `takt` without transferring ownership of the carried context to the runner. A nil Suspension means the computation has completed; Op, Resume, ResumeWith, and Discard panic after completion.
func CheckSuspension ¶
func CheckSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A], req Req[C]) (SuspensionView[C, A], bool)
CheckSuspension returns a suspension view when req holds.
func CheckSuspensionExpr ¶
func CheckSuspensionExpr[C Ambient, A Focus](ctx C, susp *kont.Suspension[A], req ReqExpr[C]) (SuspensionView[C, A], bool)
CheckSuspensionExpr returns a suspension view when req holds in expression form.
func MapContextSuspension ¶ added in v0.1.0
func MapContextSuspension[C, D Ambient, A Focus](v SuspensionView[C, A], f func(C) D) SuspensionView[D, A]
MapContextSuspension maps the ambient context and preserves the observed suspension frontier.
func ObserveSuspension ¶
func ObserveSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A]) SuspensionView[C, A]
ObserveSuspension returns a suspension view for ctx and susp.
func StepExprWith ¶
func StepExprWith[C Ambient, A Focus](ctx C, m kont.Expr[A]) (A, SuspensionView[C, A])
StepExprWith runs an Expr computation and pairs its first suspension with ctx. It is the primary contextual stepping entry point for callers already in Expr form. Completion is observed when the returned [SuspensionView.Suspension] is nil; in that case, the result A follows kont's nil-completion convention (nilable types cannot use nil as a meaningful completed payload without an explicit witness).
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
"code.hybscloud.com/kont"
)
func main() {
type runtime struct{ Budget int }
type ping struct{ kont.Phantom[int] }
expr := kont.ExprBind(kont.ExprPerform(ping{}), func(v int) kont.Expr[int] {
return kont.ExprReturn(v + 1)
})
_, step := cove.StepExprWith(runtime{Budget: 2}, expr)
fmt.Println(step.Suspension != nil)
fmt.Println(step.Ask().Budget)
_, isPing := step.Op().(ping)
fmt.Println(isPing)
result, sv := step.Resume(41)
fmt.Println(result, sv.Suspension != nil)
}
Output: true 2 true 42 false
func StepWith ¶
StepWith runs a Cont computation and pairs its first suspension with ctx. It is the primary contextual stepping entry point. Completion is observed when the returned [SuspensionView.Suspension] is nil; in that case, the result A follows kont's nil-completion convention (nilable types cannot use nil as a meaningful completed payload without an explicit witness).
func WithContextSuspension ¶ added in v0.1.0
func WithContextSuspension[C Ambient, A Focus](v SuspensionView[C, A], ctx C) SuspensionView[C, A]
WithContextSuspension replaces the ambient context and preserves the observed suspension frontier.
func (SuspensionView[C, A]) Ask ¶
func (v SuspensionView[C, A]) Ask() C
Ask returns the ambient context.
func (SuspensionView[C, A]) Discard ¶
func (v SuspensionView[C, A]) Discard()
Discard consumes the suspension without resuming it. It panics after completion.
func (SuspensionView[C, A]) Extract ¶
func (v SuspensionView[C, A]) Extract() *kont.Suspension[A]
Extract returns the underlying suspension.
func (SuspensionView[C, A]) Op ¶
func (v SuspensionView[C, A]) Op() Operation
Op returns the suspended operation. It panics after completion.
func (SuspensionView[C, A]) Resume ¶
func (v SuspensionView[C, A]) Resume(value Resumed) (A, SuspensionView[C, A])
Resume advances the suspension and keeps the current context. When the resumed computation completes, the returned value follows kont's nil-completion convention: a nil completed payload becomes the zero value of A. Callers must use the returned SuspensionView (which has a nil suspension on completion) to detect completion, rather than testing the value of A. It panics after completion.
func (SuspensionView[C, A]) ResumeWith ¶
func (v SuspensionView[C, A]) ResumeWith(value Resumed, f func(C) C) (A, SuspensionView[C, A])
ResumeWith advances the suspension and optionally maps the context for the next step. When the resumed computation completes, the returned value follows kont's nil-completion convention: a nil completed payload becomes the zero value of A. Callers must use the returned SuspensionView (which has a nil suspension on completion) to detect completion, rather than testing the value of A. It panics after completion. A nil mapper keeps the current context, so ResumeWith strictly generalizes [Resume].
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
"code.hybscloud.com/kont"
)
func main() {
type runtime struct {
Budget int
kont.Phantom[int]
}
type ping struct{ kont.Phantom[int] }
expr := kont.ExprBind(kont.ExprPerform(ping{}), func(v int) kont.Expr[int] {
return kont.ExprReturn(v + 1)
})
_, step := cove.StepExprWith(runtime{Budget: 10}, expr)
fmt.Println(step.Ask().Budget)
result, sv := step.ResumeWith(41, func(r runtime) runtime {
r.Budget--
return r
})
fmt.Println(result, sv.Suspension != nil)
}
Output: 10 42 false
type View ¶
View represents a focused value under ambient context.
func Extend ¶
Extend applies f to the whole view and preserves the ambient context. Together with [Extract] and Duplicate, it defines the core view operations.
Example ¶
package main
import (
"fmt"
"code.hybscloud.com/cove"
)
func main() {
type ctx struct{ Budget int }
v := cove.Observe(ctx{Budget: 4}, 10)
next := cove.Extend(v, func(v cove.View[ctx, int]) int {
return v.Value + v.Ctx.Budget
})
fmt.Println(next.Extract())
}
Output: 14
func MapContext ¶
MapContext maps the ambient context and preserves the focused value.
func WithContext ¶
WithContext substitutes the ambient context and preserves the focused value.