Documentation
¶
Overview ¶
Package cove carries ambient context across kont suspension boundaries.
When an external runtime steps through kont suspensions one at a time, each suspended operation may need state that lives outside the operation itself: dispatch budget, ring capabilities, protocol phase, buffer-group validity. cove pairs that context with suspensions and values so it travels through async boundaries explicitly, without side maps or hidden globals.
The package is policy-free. It carries and checks context but does not schedule, retry, or dispatch. Scheduling belongs to takt; kernel mechanics to uring; outcome classification to iox.
Carriers:
- [View[C, A]] — a value paired with ambient context
- [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
Stepping:
- StepWith / StepExprWith evaluate a kont computation and pair the first suspension with context
- SuspensionView.Resume advances a suspension, preserving context
- SuspensionView.ResumeWith advances a suspension and evolves context
- CheckSuspension / CheckSuspensionExpr gate contextualization on a requirement
Laws:
Index ¶
- 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 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 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 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 (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 StepExpr ¶
func StepExpr[A Focus](m kont.Expr[A]) (A, *kont.Suspension[A])
StepExpr re-exports kont.StepExpr.
Types ¶
type Ambient ¶
type Ambient interface{}
Ambient is the type constraint for ambient context type parameters.
type Checked ¶
Checked couples a value with a requirement.
func MapChecked ¶
MapChecked transforms the value and preserves its requirement.
func PullbackChecked ¶
PullbackChecked transports a checked value along a context projection.
type CheckedExpr ¶
CheckedExpr couples a value with an Expr-world requirement.
func GuardExpr ¶
func GuardExpr[C Ambient, A Focus](req ReqExpr[C], value A) CheckedExpr[C, A]
GuardExpr constructs an Expr-world checked value.
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 transforms the value and preserves its requirement.
func PullbackCheckedExpr ¶
func PullbackCheckedExpr[C, D Ambient, A Focus](checked CheckedExpr[C, A], f func(D) C) CheckedExpr[D, A]
PullbackCheckedExpr transports an Expr-world checked value along a context projection.
func (CheckedExpr[C, A]) Check ¶
func (c CheckedExpr[C, A]) Check(ctx C) bool
Check reports whether ctx satisfies the requirement.
func (CheckedExpr[C, A]) IntoView ¶
func (c CheckedExpr[C, A]) IntoView(ctx C) (View[C, A], bool)
IntoView returns a view when the requirement holds.
func (CheckedExpr[C, A]) MustView ¶
func (c CheckedExpr[C, A]) MustView(ctx C) View[C, A]
MustView panics if the requirement does not hold.
type Focus ¶
type Focus interface{}
Focus is the type constraint for type parameters that denote values in focus.
type Guarded ¶
Guarded couples a value with a named rule.
func MapGuarded ¶
MapGuarded transforms the value and preserves the rule.
func PullbackGuarded ¶
PullbackGuarded transports a guarded value along a context projection.
type GuardedExpr ¶
GuardedExpr couples a value with a named Expr-world rule.
func GuardRuleExpr ¶
func GuardRuleExpr[C Ambient, A Focus](rule RuleExpr[C], value A) GuardedExpr[C, A]
GuardRuleExpr constructs an Expr-world guarded value.
func MapGuardedExpr ¶
func MapGuardedExpr[C Ambient, A, B Focus](g GuardedExpr[C, A], f func(A) B) GuardedExpr[C, B]
MapGuardedExpr transforms 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 an Expr-world guarded value along a context projection.
func (GuardedExpr[C, A]) Check ¶
func (g GuardedExpr[C, A]) Check(ctx C) Report
Check evaluates the Expr-world guarded rule.
func (GuardedExpr[C, A]) IntoView ¶
func (g GuardedExpr[C, A]) IntoView(ctx C) (View[C, A], Report)
IntoView returns a view and 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 is the result of checking one or more named rules.
func CheckRuleExpr ¶
CheckRuleExpr evaluates a single rule.
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 evaluates rules left to right and stops at the first failure.
func CheckRulesExpr ¶
CheckRulesExpr evaluates rules left to right and stops at the first failure.
type Req ¶
Req is a contextual prerequisite over C.
func All ¶
All conjunctively composes requirements.
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 a requirement 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 evaluates an Expr-world requirement through NeedExpr.
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 a requirement along a context projection and preserves the explicit boolean structure.
func ReifyReq ¶
ReifyReq wraps a Req as an ExprAtom.
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 a named rule along a context projection.
type RuleExpr ¶
RuleExpr names an Expr-world requirement for diagnostics.
func PullbackRuleExpr ¶
PullbackRuleExpr transports a rule along a context projection.
func RequireExpr ¶
RequireExpr constructs a named Expr-world rule.
type SuspensionView ¶
type SuspensionView[C Ambient, A Focus] struct { Ctx C Suspension *kont.Suspension[A] }
SuspensionView pairs a kont suspension with ambient context. A nil Suspension means the computation has completed, and Op, Resume, ResumeWith, and Discard require a pending suspension.
func CheckSuspension ¶
func CheckSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A], req Req[C]) (SuspensionView[C, A], bool)
CheckSuspension contextualizes a kont suspension 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 contextualizes a suspension when an Expr-world requirement holds.
func ObserveSuspension ¶
func ObserveSuspension[C Ambient, A Focus](ctx C, susp *kont.Suspension[A]) SuspensionView[C, A]
ObserveSuspension contextualizes a kont suspension.
func StepExprWith ¶
func StepExprWith[C Ambient, A Focus](ctx C, m kont.Expr[A]) (A, SuspensionView[C, A])
StepExprWith runs an Expr-world computation and pairs the first suspension with ctx.
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 (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, and it panics if the computation has completed.
func (SuspensionView[C, A]) Extract ¶
func (v SuspensionView[C, A]) Extract() *kont.Suspension[A]
Extract returns the pending suspension.
func (SuspensionView[C, A]) Op ¶
func (v SuspensionView[C, A]) Op() Operation
Op returns the suspended operation, and it panics if the computation has completed.
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, and it panics if the computation has completed.
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 after mapping the context for the next step, and it panics if the computation has completed.
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 is a value in focus under ambient context.
func Extend ¶
Extend maps the whole view to a new value in focus.
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 transforms the ambient context and preserves the value in focus.
func WithContext ¶
WithContext substitutes the ambient context and preserves the value in focus.