| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Circuit.Process
Description
A pointed Moore machine packaged as a Circuit base arrow.
data Process a b = forall s. Process (a -> s) (s -> a -> s) (s -> b)
Process is the monomial special case of System: the
interface is Mono a b and the initial state is supplied by the first input.
The underlying span-shaped carrier is Body.
injectconverts the first input into an initial state.stepupdates the state given the current input.extractproduces the output from the current state.
This is the circuits-native carrier for streaming state machines. It is
intended to replace the hand-rolled state-machine arrow: stats packages
become boxes Process a b, while the arrow itself lives in the substrate
next to Trace and Net.
The semantics are intentionally tied to the circuits substrate:
scanis the reference runner over lists.scanStreamgeneralizes this to anyUnconsinput andConsoutput.encodemaps a process into a stream-levelTraceEither(->)over lists; the two runners are verified equivalent by oracle.- The arrow-level
TracedEither instance is per-tick Conway/Elgot settle, not cross-tick state feedback; seeregisterfor the latter.
Pointed systems
The pointed-Moore view of a stateful morphism is System with
an explicit seed. Use mooreSystem to build such a system,
and systemToProcess to turn it into a first-input-seeded Process.
Synopsis
- data Process a b where
- data Boundary k a
- isMark :: Boundary k a -> Bool
- isPayload :: Boundary k a -> Bool
- systemToProcess :: s -> (s -> b) -> System (->) s (Mono a b) -> Process a b
- systemAsProcess :: System (->) s (Mono i o) -> s -> Process i o
- markSystem :: (k -> Bool) -> (s -> b) -> System (->) s (Mono a b) -> System (->) (Either s s) (Mono (Boundary k a) (Maybe b))
- iterateSystem :: System (->) s (Mono i o) -> s -> [i] -> [o]
- after :: System (->) s (Mono i o) -> s -> [i] -> s
- scan :: Process a b -> [a] -> [b]
- scanStream :: (Uncons f a, Cons g b) => Process a b -> f -> g
- fold :: Process a b -> [a] -> Maybe b
- foldStream :: Uncons f a => Process a b -> f -> Maybe b
- encode :: Process a b -> Trace Either (->) [a] [b]
- encodeStream :: (Uncons f a, Cons g b) => Process a b -> Trace Either (->) f g
- mealy :: ch -> (ch -> a -> (ch, Maybe b)) -> Process a (Maybe b)
- runMealy :: Process a (Maybe b) -> [a] -> [b]
- runMealyStream :: (Uncons f a, Cons g b) => Process a (Maybe b) -> f -> g
- delay :: s -> Process s s
- register :: s -> Process (a, s) (b, s) -> Process a b
- processToBody :: Process a b -> SomeBody Either (->) [a] [b]
- processToSomeBody :: Process a b -> SomeBody (,) (->) a b
Stream transformer (monomial special case)
data Process a b where Source #
A stateful process from a to b.
The existential state type s is hidden; the observable interface is the
triple inject step extract. Keeping the triple as the primitive (rather
than fusing extract into the step) preserves the streaming-statistics
invariant that the first output is extract (inject x), before any step.
Instances
| Copy (->) a => Copy Process a Source # | |
Defined in Circuit.Process | |
| Merge (->) a => Merge Process a Source # | |
Defined in Circuit.Process | |
| Category Process Source # | |
| Shared (,) Process Source # | Cartesian shared fusion on processes. The two processes share one feedback channel |
| Discard Process (a :: Type) Source # | |
Defined in Circuit.Process | |
| Zero (->) a => Zero Process (a :: Type) Source # | |
Defined in Circuit.Process | |
| Channel Either Process Source # | |
| Channel (,) Process Source # | |
| Strength Either Process Source # | |
| Strength (,) Process Source # | |
| Traced Either Process Source # | |
| Traced (,) Process Source # | |
| Action (,) Process Source # | |
Defined in Circuit.Process | |
| Tensor (,) Process Source # | |
| Unital (,) Process Source # | |
Boundary tokens (K + payload)
The free boundary K + payload.
A token on the boundary is either a mark from a finite alphabet k or a
payload value a. This is the level-0 grammar of process boundaries:
marks are the control tokens, payloads are the data.
fmap acts only on the payload side; marks are carried through unchanged.
>>>fmap length (Payload "hi")Payload 2>>>fmap length (Mark "halt")Mark "halt"
Instances
| Bifunctor Boundary Source # | |
| Functor (Boundary k) Source # | |
| Foldable (Boundary k) Source # | |
Defined in Circuit.Process Methods fold :: Monoid m => Boundary k m -> m # foldMap :: Monoid m => (a -> m) -> Boundary k a -> m # foldMap' :: Monoid m => (a -> m) -> Boundary k a -> m # foldr :: (a -> b -> b) -> b -> Boundary k a -> b # foldr' :: (a -> b -> b) -> b -> Boundary k a -> b # foldl :: (b -> a -> b) -> b -> Boundary k a -> b # foldl' :: (b -> a -> b) -> b -> Boundary k a -> b # foldr1 :: (a -> a -> a) -> Boundary k a -> a # foldl1 :: (a -> a -> a) -> Boundary k a -> a # toList :: Boundary k a -> [a] # null :: Boundary k a -> Bool # length :: Boundary k a -> Int # elem :: Eq a => a -> Boundary k a -> Bool # maximum :: Ord a => Boundary k a -> a # minimum :: Ord a => Boundary k a -> a # | |
| Traversable (Boundary k) Source # | |
Defined in Circuit.Process | |
| (Eq k, Eq a) => Eq (Boundary k a) Source # | |
| (Show k, Show a) => Show (Boundary k a) Source # | |
System - Process conversions
markSystem :: (k -> Bool) -> (s -> b) -> System (->) s (Mono a b) -> System (->) (Either s s) (Mono (Boundary k a) (Maybe b)) Source #
Lift a monomial System and a state observation into a boundary system
over Boundary tokens.
Payloads are stepped through the inner system. Marks satisfying the halt
predicate freeze the system and produce Nothing thereafter; non-halt
marks leave the state unchanged and emit the current output. The halted
state remembers the final inner state.
The returned system carries state Either s s: Left is running, Right
is halted. This is the core combinator behind mark-driven halt: the finite
mark alphabet k carries control tokens, while payloads carry data.
Runners
iterateSystem :: System (->) s (Mono i o) -> s -> [i] -> [o] Source #
Run a system for as many steps as there are inputs, emitting one output
per input. The output is the state after consuming the input, matching
the Process semantics of systemAsProcess.
scan :: Process a b -> [a] -> [b] Source #
List specialization of scanStream.
foldStream :: Uncons f a => Process a b -> f -> Maybe b Source #
Run a process over a stream, returning the final output (if any).
encodeStream :: (Uncons f a, Cons g b) => Process a b -> Trace Either (->) f g Source #
Encode a process as a stream-level Trace over arbitrary Uncons/Cons
streams.
This is the definitional runner: scanStream is eval
composed with encodeStream. The feedback channel carries
(Maybe channel, remaining input, accumulated output).
Mealy-style processes
runMealyStream :: (Uncons f a, Cons g b) => Process a (Maybe b) -> f -> g Source #
Collect the emitted outputs of a 'Process (Maybe b)' over any stream.
Cross-tick feedback
delay :: s -> Process s s Source #
One-tick delay with an initial value.
Output is s0 on the first tick and the input from the previous tick
thereafter. This is the primitive that makes register productive: the
feedback wire is observable one tick late.
register :: s -> Process (a, s) (b, s) -> Process a b Source #
Cross-tick register feedback.
Given an initial feedback value s0 and a process Process (a, s) (b, s),
close the s wire so that the s produced at one tick is fed back as
input at the next tick. This is the productive, strict-accumulator-safe
analogue of the cartesian trace: the delay is explicit in the wiring
rather than implicit in a lazy knot.
Compare with the cartesian trace on Process, which ties a lazy knot
and diverges for strict state; register keeps strict state cells sound
by making the one-tick delay observable.
For bodies whose fixed-point is independent of the initial feedback value
(e.g. affine/stateless feedback such as ewmaBody), the same wiring can
be expressed by swapping the feedback wire into the active position,
applying strength (delay s0), and tracing.
Body conversions
processToBody :: Process a b -> SomeBody Either (->) [a] [b] Source #
List specialization of processToBodyStream.