cove

package module
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 1 Imported by: 0

README

Go Reference Go Report Card Coverage Status

English | 简体中文 | Español | 日本語 | Français

cove

Coalgebraic context layer for kont suspensions in Go, the coeffect dual of kont's algebraic effects.

Overview

When an external runtime, such as a proactor, event loop, or dispatcher 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. Without explicit context carriers, this state ends up in ad-hoc side maps or implicit globals that break composition.

cove pairs ambient context with values and suspensions so the context travels through async stepping boundaries as typed, composable data.

_, sv := cove.StepExprWith(Runtime{Budget: 8}, computation)
for sv.Suspension != nil {
    result := dispatch(sv.Ask(), sv.Op())
    _, sv = sv.ResumeWith(result, func(r Runtime) Runtime {
        r.Budget--
        return r
    })
}

The package is policy-free: it carries and checks context but never schedules, retries, or dispatches.

Installation

go get code.hybscloud.com/cove

Requires Go 1.26+.

Core Types

Type Purpose
View[C, A] Comonadic carrier: value A under ambient context C
SuspensionView[C, A] kont suspension paired with ambient context
Req[C] Contravariant predicate over C (closure form): func(C) bool
ReqExpr[C] Contravariant predicate over C (defunctionalized form)
Rule[C] / RuleExpr[C] Named predicate with diagnostic Report
Checked[C, A] / CheckedExpr[C, A] Value gated by a requirement
Guarded[C, A] / GuardedExpr[C, A] Value gated by a named rule

Ambient constrains context type parameters (C, D). Focus constrains value type parameters (A, B).

Contextual Stepping

StepWith and StepExprWith evaluate a kont computation and pair the first suspension with ambient context. Each suspension is affine: consume it exactly once via Resume, ResumeWith, or Discard.

val, sv := cove.StepExprWith(ctx, expr)
for sv.Suspension != nil {
    op := sv.Op()
    result := handle(op)
    val, sv = sv.Resume(result)
}

ResumeWith evolves context between steps, for example by decrementing budget, updating capabilities, or advancing protocol phase:

val, sv = sv.ResumeWith(result, func(r Runtime) Runtime {
    r.Budget--
    return r
})

ObserveSuspension attaches ambient context to an existing kont.Suspension without a requirement check. CheckSuspension and CheckSuspensionExpr add gated forms:

sv, ok := cove.CheckSuspension(runtime, susp, func(r Runtime) bool {
    return r.Budget > 0
})
if !ok {
    susp.Discard()
    return
}
result := dispatch(sv.Ask(), sv.Op())
val, sv = sv.Resume(result)
exprReq := cove.ExprAtom(func(r Runtime) bool { return r.Budget > 0 })
sv, ok := cove.CheckSuspensionExpr(runtime, susp, exprReq)
if !ok {
    susp.Discard()
    return
}
result := dispatch(sv.Ask(), sv.Op())
val, sv = sv.Resume(result)

Step and StepExpr re-export kont's evaluators without context binding.

Requirements

Requirements are predicates over context, available in closure and data forms. Both support All/Any/Not, with True and False as the neutral elements for conjunction and disjunction.

Closure form (Req) — direct and concise:

req := cove.All(
    cove.Req[Runtime](func(r Runtime) bool { return r.Budget > 0 }),
    cove.Req[Runtime](func(r Runtime) bool { return r.CanDispatch }),
)
ok := cove.Need(runtime, req)

Data form (ReqExpr) — composable Boolean structure, avoids closure allocation on composition:

expr := cove.ExprAll(
    cove.ExprAtom(func(r Runtime) bool { return r.Budget > 0 }),
    cove.ExprAtom(func(r Runtime) bool { return r.CanDispatch }),
)
ok := cove.NeedExpr(runtime, expr)

Combinators: All/ExprAll (conjunction ∧), Any/ExprAny (disjunction ∨), Not/ExprNot (negation ¬), True/ExprTrue (⊤), False/ExprFalse (⊥).

Pullback and ExprPullback transport requirements contravariantly along a context projection. Given f: C → D, Pullback(req, f) maps Req[D] to Req[C]. ExprPullback preserves Boolean structure and distributes over ExprNot, ExprAll, and ExprAny.

Choosing Req vs ReqExpr

Use Req for ad-hoc one-off predicates. Use ReqExpr when composing many requirements or when closure allocation at construction time matters.

Bridge helpers keep the two forms aligned: ReifyReq wraps a closure-form requirement as ExprAtom, so ReifyReq(nil) is invalid and panics when evaluated; ReflectReq evaluates an Expr-world requirement through a closure.

Gated Values

Guard pairs a value with a requirement; IntoView yields a View only when the context satisfies it:

checked := cove.Guard(canDispatch, payload)
if view, ok := checked.IntoView(runtime); ok {
    _ = view.Extract()
}

GuardRule adds diagnostics via named rules and Report:

rule := cove.Require("budget", func(r Runtime) bool { return r.Budget > 0 })
guarded := cove.GuardRule(rule, payload)
if view, report := guarded.IntoView(runtime); report.OK() {
    _ = view.Extract()
}

CheckRules and CheckRulesExpr evaluate multiple rules, stopping at the first failure:

report := cove.CheckRules(runtime, budgetRule, permRule)
if !report.OK() {
    // report.Failed names the first rule that did not hold
}

Expr-world diagnostics follow the same shape with CheckRulesExpr and GuardRuleExpr:

type Runtime struct{ Budget int }

runtime := Runtime{Budget: 1}
payload := "packet"
exprRule := cove.RequireExpr("budget", cove.ExprAtom(func(r Runtime) bool {
    return r.Budget > 0
}))
report := cove.CheckRulesExpr(runtime, exprRule)
if !report.OK() {
    return
}
guardedExpr := cove.GuardRuleExpr(exprRule, payload)
if view, report := guardedExpr.IntoView(runtime); report.OK() {
    _ = view.Extract()
}

Expr-world equivalents: GuardExpr, GuardRuleExpr, CheckedExpr, GuardedExpr. Map and pullback helpers: MapChecked, MapGuarded, PullbackChecked, PullbackGuarded (and their Expr variants).

View Operations

View[C, A] is a comonad over the category of Go types. The carrier is the product C × A; Extract (ε) projects the value; Duplicate (δ) embeds the observation as the focused value:

v := cove.Observe(ctx, value)
v.Ask()       // ambient context  (π₁)
v.Extract()   // value in focus   (ε = π₂)

Transformations: Map (functorial lift on A), MapContext (functorial lift on C), Replace (substitute value), WithContext (substitute context).

Duplicate and Extend satisfy the comonad laws:

// Counit law:  ε ∘ δ = id
cove.Duplicate(v).Extract() == v

// CoKleisli identity:  extend(ε) = id
cove.Extend(v, func(w cove.View[C, A]) A { return w.Extract() }) == v

// Associativity:  extend(f) ∘ extend(g) = extend(f ∘ extend(g))

Extend is coKleisli extension: given f: W(C,A) → B, it lifts f to W(C,A) → W(C,B) while preserving ambient context. The induced coKleisli composition with g: W(C,B) → D is g ∘ Extend(f).

Ecosystem Position

Package Owns Categorical Aspect
kont Effect operations, handlers, suspension and resumption Algebra / Free monad / Fold
cove Ambient context across suspension boundaries Coalgebra / Comonad / Unfold
takt Proactor dispatch, outcome classification, event loop Comodel (handler dual)
uring Kernel I/O: SQE/CQE, ring management, buffer rings
iox Outcome algebra and error semantics

Formal Structure

View[C, A] is an environment comonad (Uustalu & Vene 2008) with carrier C × A, counit ε = π₂ (Extract), and comultiplication δ(c, a) = (c, (c, a)) (Duplicate). Extend implements coKleisli extension.

Req[C] and ReqExpr[C] are objects in the Boolean algebra of predicates over C. Pullback implements the contravariant functor f* induced by a morphism f: C → D, mapping predicates on D to predicates on C. ExprPullback preserves the Boolean algebra structure: f*(p ∧ q) = f*(p) ∧ f*(q), f*(¬p) = ¬f*(p).

kont models algebraic effects (free monad, handlers, fold). cove models coeffects (comonad, requirements, unfold). The two are categorical duals at the suspension boundary: kont asks what the computation does to the world; cove states what the computation needs from the world.

ReqExpr defunctionalizes the Boolean algebra into a tagged union — the initial algebra of the Boolean signature functor — so that requirement structure is inspectable data rather than opaque closures (Reynolds 1972).

References

Platform Support

cove is a pure Go package in this module. It has no platform-specific files or build tags; the module requirement is Go 1.26+.

License

MIT License. See LICENSE for details.

©2026 Hayabusa Cloud Co., Ltd.

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:

Laws:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Need

func Need[C Ambient](ctx C, req Req[C]) bool

Need checks whether ctx satisfies req. A nil requirement is treated as true.

func NeedExpr

func NeedExpr[C Ambient](ctx C, req ReqExpr[C]) bool

NeedExpr evaluates an Expr-world requirement against ctx.

func Reflect

func Reflect[A Focus](m kont.Expr[A]) kont.Cont[Resumed, A]

Reflect bridges an Expr-world computation into the Cont world.

func Reify

func Reify[A Focus](m kont.Cont[Resumed, A]) kont.Expr[A]

Reify bridges a Cont-world computation into the Expr world.

func Step

func Step[A Focus](m kont.Cont[Resumed, A]) (A, *kont.Suspension[A])

Step re-exports kont.Step.

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

type Checked[C Ambient, A Focus] struct {
	Req   Req[C]
	Value A
}

Checked couples a value with a requirement.

func Guard

func Guard[C Ambient, A Focus](req Req[C], value A) Checked[C, A]

Guard constructs a Checked value.

func MapChecked

func MapChecked[C Ambient, A, B Focus](c Checked[C, A], f func(A) B) Checked[C, B]

MapChecked transforms the value and preserves its requirement.

func PullbackChecked

func PullbackChecked[C, D Ambient, A Focus](checked Checked[C, A], f func(D) C) Checked[D, A]

PullbackChecked transports a checked value along a context projection.

func (Checked[C, A]) Check

func (c Checked[C, A]) Check(ctx C) bool

Check reports whether ctx satisfies the requirement.

func (Checked[C, A]) IntoView

func (c Checked[C, A]) IntoView(ctx C) (View[C, A], bool)

IntoView returns a view when the requirement holds.

func (Checked[C, A]) MustView

func (c Checked[C, A]) MustView(ctx C) View[C, A]

MustView panics if the requirement does not hold.

type CheckedExpr

type CheckedExpr[C Ambient, A Focus] struct {
	Req   ReqExpr[C]
	Value A
}

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

type Guarded[C Ambient, A Focus] struct {
	Rule  Rule[C]
	Value A
}

Guarded couples a value with a named rule.

func GuardRule

func GuardRule[C Ambient, A Focus](rule Rule[C], value A) Guarded[C, A]

GuardRule constructs a Guarded value.

func MapGuarded

func MapGuarded[C Ambient, A, B Focus](g Guarded[C, A], f func(A) B) Guarded[C, B]

MapGuarded transforms the value and preserves the rule.

func PullbackGuarded

func PullbackGuarded[C, D Ambient, A Focus](guarded Guarded[C, A], f func(D) C) Guarded[D, A]

PullbackGuarded transports a guarded value along a context projection.

func (Guarded[C, A]) Check

func (g Guarded[C, A]) Check(ctx C) Report

Check evaluates the guarded rule.

func (Guarded[C, A]) IntoView

func (g Guarded[C, A]) IntoView(ctx C) (View[C, A], Report)

IntoView returns a view and the rule report.

type GuardedExpr

type GuardedExpr[C Ambient, A Focus] struct {
	Rule  RuleExpr[C]
	Value A
}

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 Operation

type Operation = kont.Operation

Operation is the kont boundary payload shape.

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 CheckRule

func CheckRule[C Ambient](ctx C, rule Rule[C]) Report

CheckRule evaluates a single rule.

func CheckRuleExpr

func CheckRuleExpr[C Ambient](ctx C, rule RuleExpr[C]) Report

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

func CheckRules[C Ambient](ctx C, rules ...Rule[C]) Report

CheckRules evaluates rules left to right and stops at the first failure.

func CheckRulesExpr

func CheckRulesExpr[C Ambient](ctx C, rules ...RuleExpr[C]) Report

CheckRulesExpr evaluates rules left to right and stops at the first failure.

func (Report) OK

func (r Report) OK() bool

OK reports whether all checked rules passed.

type Req

type Req[C Ambient] func(C) bool

Req is a contextual prerequisite over C.

func All

func All[C Ambient](reqs ...Req[C]) Req[C]

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 Any

func Any[C Ambient](reqs ...Req[C]) Req[C]

Any disjunctively composes requirements.

func False

func False[C Ambient]() Req[C]

False returns a requirement that always fails.

func Not

func Not[C Ambient](req Req[C]) Req[C]

Not negates a requirement.

func Pullback

func Pullback[C, D Ambient](req Req[D], f func(C) D) Req[C]

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

func ReflectReq[C Ambient](expr ReqExpr[C]) Req[C]

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

func True

func True[C Ambient]() Req[C]

True returns a requirement that always succeeds.

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 ExprAll

func ExprAll[C Ambient](reqs ...ReqExpr[C]) ReqExpr[C]

ExprAll conjunctively composes requirements.

func ExprAny

func ExprAny[C Ambient](reqs ...ReqExpr[C]) ReqExpr[C]

ExprAny disjunctively composes requirements.

func ExprAtom

func ExprAtom[C Ambient](fn func(C) bool) ReqExpr[C]

ExprAtom wraps a leaf predicate.

func ExprFalse

func ExprFalse[C Ambient]() ReqExpr[C]

ExprFalse returns a requirement that never holds.

func ExprNot

func ExprNot[C Ambient](inner ReqExpr[C]) ReqExpr[C]

ExprNot negates a requirement.

func ExprPullback

func ExprPullback[C, D Ambient](req ReqExpr[D], f func(C) D) ReqExpr[C]

ExprPullback transports a requirement along a context projection and preserves the explicit boolean structure.

func ExprTrue

func ExprTrue[C Ambient]() ReqExpr[C]

ExprTrue returns a requirement that always holds.

func ReifyReq

func ReifyReq[C Ambient](req Req[C]) ReqExpr[C]

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 Resumed

type Resumed = kont.Resumed

Resumed is the kont boundary resumption value shape.

type Rule

type Rule[C Ambient] struct {
	Name  string
	Check Req[C]
}

Rule names a requirement for diagnostics.

func PullbackRule

func PullbackRule[C, D Ambient](rule Rule[C], f func(D) C) Rule[D]

PullbackRule transports a named rule along a context projection.

func Require

func Require[C Ambient](name string, check Req[C]) Rule[C]

Require constructs a named rule.

func (Rule[C]) Match

func (r Rule[C]) Match(ctx C) bool

Match reports whether ctx satisfies the rule.

func (Rule[C]) Req

func (r Rule[C]) Req() Req[C]

Req returns the underlying requirement.

type RuleError

type RuleError string

RuleError names the first failed rule in a report.

type RuleExpr

type RuleExpr[C Ambient] struct {
	Name  string
	Check ReqExpr[C]
}

RuleExpr names an Expr-world requirement for diagnostics.

func PullbackRuleExpr

func PullbackRuleExpr[C, D Ambient](rule RuleExpr[C], f func(D) C) RuleExpr[D]

PullbackRuleExpr transports a rule along a context projection.

func RequireExpr

func RequireExpr[C Ambient](name string, check ReqExpr[C]) RuleExpr[C]

RequireExpr constructs a named Expr-world rule.

func (RuleExpr[C]) Match

func (r RuleExpr[C]) Match(ctx C) bool

Match reports whether ctx satisfies the rule.

func (RuleExpr[C]) Req

func (r RuleExpr[C]) Req() ReqExpr[C]

Req returns the underlying Expr-world requirement.

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 StepWith

func StepWith[C Ambient, A Focus](ctx C, m kont.Cont[Resumed, A]) (A, SuspensionView[C, A])

StepWith runs a Cont-world computation and pairs the first suspension with ctx.

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

type View[C Ambient, A Focus] struct {
	Ctx   C
	Value A
}

View is a value in focus under ambient context.

func Duplicate

func Duplicate[C Ambient, A Focus](v View[C, A]) View[C, View[C, A]]

Duplicate returns the current view as the value in focus.

func Extend

func Extend[C Ambient, A, B Focus](v View[C, A], f func(View[C, A]) B) View[C, B]

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 Map

func Map[C Ambient, A, B Focus](v View[C, A], f func(A) B) View[C, B]

Map transforms the value in focus and preserves the ambient context.

func MapContext

func MapContext[C, D Ambient, A Focus](v View[C, A], f func(C) D) View[D, A]

MapContext transforms the ambient context and preserves the value in focus.

func Observe

func Observe[C Ambient, A Focus](ctx C, value A) View[C, A]

Observe constructs a contextual view.

func Replace

func Replace[C Ambient, A, B Focus](v View[C, A], value B) View[C, B]

Replace substitutes the value in focus and preserves the ambient context.

func WithContext

func WithContext[C Ambient, A Focus](v View[C, A], ctx C) View[C, A]

WithContext substitutes the ambient context and preserves the value in focus.

func (View[C, A]) Ask

func (v View[C, A]) Ask() C

Ask returns the ambient context.

func (View[C, A]) Extract

func (v View[C, A]) Extract() A

Extract returns the value in focus.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL