module Circuit.Agent.Tensor
( AgentShard,
silentShard,
awaitShard,
raceShard,
fanOutShard,
fanInShard,
runSubShard,
closeShardIO,
ioShard,
writeBatch,
readBatch,
synthesisSummary,
)
where
import Circuit (Body (..), close, companion, conjoint)
import Circuit.Agent (Name, Post (..), PostId, mkPost, synthesis)
import Circuit.Category (K (..))
import Circuit.Poles (Poles, poles0)
import Data.Text (Text)
import Data.Text qualified as T
type AgentShard a b = Poles (Body (,) [Post Text] (K IO)) a b
runSubShard :: AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
runSubShard :: AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
runSubShard AgentShard [Post Text] [Post Text]
sh [Post Text]
xs = do
let thread :: Body (,) [Post Text] (K IO) [Post Text] [Post Text]
thread = In (Body (,) [Post Text] (K IO)) [Post Text]
-> Out (Body (,) [Post Text] (K IO)) [Post Text]
-> Body (,) [Post Text] (K IO) [Post Text] [Post Text]
forall {k} (arr :: k -> k -> *) (a :: k).
In arr a -> Out arr a -> arr a a
close (AgentShard [Post Text] [Post Text]
-> In (Body (,) [Post Text] (K IO)) [Post Text]
forall {k1} {k2} (arr :: k1 -> k2 -> *) (a :: k1) (b :: k2).
Poles arr a b -> In arr a
conjoint AgentShard [Post Text] [Post Text]
sh) (AgentShard [Post Text] [Post Text]
-> Out (Body (,) [Post Text] (K IO)) [Post Text]
forall {k1} {k2} (arr :: k1 -> k2 -> *) (a :: k1) (b :: k2).
Poles arr a b -> Out arr b
companion AgentShard [Post Text] [Post Text]
sh)
(_, ys) <- K IO ([Post Text], [Post Text]) ([Post Text], [Post Text])
-> ([Post Text], [Post Text]) -> IO ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). K m a b -> a -> m b
runK (Body (,) [Post Text] (K IO) [Post Text] [Post Text]
-> K IO ([Post Text], [Post Text]) ([Post Text], [Post Text])
forall {k1} {k2} {k3} (t :: k1 -> k2 -> k3) (ch :: k1)
(arr :: k3 -> k3 -> *) (a :: k2) (b :: k2).
Body t ch arr a b -> arr (t ch a) (t ch b)
morphism Body (,) [Post Text] (K IO) [Post Text] [Post Text]
thread) ([], [Post Text]
xs)
pure ys
closeShardIO :: Poles (Body (,) s (K IO)) a a -> a -> s -> IO (a, s)
closeShardIO :: forall s a. Poles (Body (,) s (K IO)) a a -> a -> s -> IO (a, s)
closeShardIO Poles (Body (,) s (K IO)) a a
sh a
x s
s0 = do
let thread :: Body (,) s (K IO) a a
thread = In (Body (,) s (K IO)) a
-> Out (Body (,) s (K IO)) a -> Body (,) s (K IO) a a
forall {k} (arr :: k -> k -> *) (a :: k).
In arr a -> Out arr a -> arr a a
close (Poles (Body (,) s (K IO)) a a -> In (Body (,) s (K IO)) a
forall {k1} {k2} (arr :: k1 -> k2 -> *) (a :: k1) (b :: k2).
Poles arr a b -> In arr a
conjoint Poles (Body (,) s (K IO)) a a
sh) (Poles (Body (,) s (K IO)) a a -> Out (Body (,) s (K IO)) a
forall {k1} {k2} (arr :: k1 -> k2 -> *) (a :: k1) (b :: k2).
Poles arr a b -> Out arr b
companion Poles (Body (,) s (K IO)) a a
sh)
(s', y) <- K IO (s, a) (s, a) -> (s, a) -> IO (s, a)
forall {k} (m :: k -> *) a (b :: k). K m a b -> a -> m b
runK (Body (,) s (K IO) a a -> K IO (s, a) (s, a)
forall {k1} {k2} {k3} (t :: k1 -> k2 -> k3) (ch :: k1)
(arr :: k3 -> k3 -> *) (a :: k2) (b :: k2).
Body t ch arr a b -> arr (t ch a) (t ch b)
morphism Body (,) s (K IO) a a
thread) (s
s0, a
x)
pure (y, s')
writeBatch :: Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch :: Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch = K IO ([Post Text], [Post Text]) ([Post Text], ())
-> Body (,) [Post Text] (K IO) [Post Text] ()
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], [Post Text]) ([Post Text], ())
-> Body (,) [Post Text] (K IO) [Post Text] ())
-> K IO ([Post Text], [Post Text]) ([Post Text], ())
-> Body (,) [Post Text] (K IO) [Post Text] ()
forall a b. (a -> b) -> a -> b
$ (([Post Text], [Post Text]) -> IO ([Post Text], ()))
-> K IO ([Post Text], [Post Text]) ([Post Text], ())
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], [Post Text]) -> IO ([Post Text], ()))
-> K IO ([Post Text], [Post Text]) ([Post Text], ()))
-> (([Post Text], [Post Text]) -> IO ([Post Text], ()))
-> K IO ([Post Text], [Post Text]) ([Post Text], ())
forall a b. (a -> b) -> a -> b
$ \([Post Text]
_, [Post Text]
xs) -> ([Post Text], ()) -> IO ([Post Text], ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Post Text]
xs, ())
readBatch :: Body (,) [Post Text] (K IO) () [Post Text]
readBatch :: Body (,) [Post Text] (K IO) () [Post Text]
readBatch = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> ([Post Text], [Post Text]) -> IO ([Post Text], [Post Text])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [Post Text]
s)
ioShard :: ([Post Text] -> IO [Post Text]) -> AgentShard [Post Text] [Post Text]
ioShard :: ([Post Text] -> IO [Post Text])
-> AgentShard [Post Text] [Post Text]
ioShard [Post Text] -> IO [Post Text]
emit = Body (,) [Post Text] (K IO) [Post Text] ()
-> Body (,) [Post Text] (K IO) () [Post Text]
-> AgentShard [Post Text] [Post Text]
forall (arr :: * -> * -> *) a b.
HasDual () arr =>
arr a () -> arr () b -> Poles arr a b
poles0 Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch Body (,) [Post Text] (K IO) () [Post Text]
read'
where
read' :: Body (,) [Post Text] (K IO) () [Post Text]
read' = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> do
outs <- [Post Text] -> IO [Post Text]
emit [Post Text]
s
pure ([], outs)
silentShard :: AgentShard [Post Text] [Post Text]
silentShard :: AgentShard [Post Text] [Post Text]
silentShard = ([Post Text] -> IO [Post Text])
-> AgentShard [Post Text] [Post Text]
ioShard ([Post Text] -> IO [Post Text]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Post Text] -> IO [Post Text])
-> ([Post Text] -> [Post Text]) -> [Post Text] -> IO [Post Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Post Text] -> [Post Text] -> [Post Text]
forall a b. a -> b -> a
const [])
awaitShard ::
AgentShard [Post Text] [Post Text] ->
AgentShard [Post Text] [Post Text] ->
AgentShard [Post Text] [Post Text]
awaitShard :: AgentShard [Post Text] [Post Text]
-> AgentShard [Post Text] [Post Text]
-> AgentShard [Post Text] [Post Text]
awaitShard AgentShard [Post Text] [Post Text]
sh1 AgentShard [Post Text] [Post Text]
sh2 = Body (,) [Post Text] (K IO) [Post Text] ()
-> Body (,) [Post Text] (K IO) () [Post Text]
-> AgentShard [Post Text] [Post Text]
forall (arr :: * -> * -> *) a b.
HasDual () arr =>
arr a () -> arr () b -> Poles arr a b
poles0 Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch Body (,) [Post Text] (K IO) () [Post Text]
read'
where
read' :: Body (,) [Post Text] (K IO) () [Post Text]
read' = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> do
o1 <- AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
runSubShard AgentShard [Post Text] [Post Text]
sh1 [Post Text]
s
o2 <- runSubShard sh2 s
pure ([], o1 ++ o2)
raceShard ::
AgentShard [Post Text] [Post Text] ->
AgentShard [Post Text] [Post Text] ->
AgentShard [Post Text] [Post Text]
raceShard :: AgentShard [Post Text] [Post Text]
-> AgentShard [Post Text] [Post Text]
-> AgentShard [Post Text] [Post Text]
raceShard AgentShard [Post Text] [Post Text]
sh1 AgentShard [Post Text] [Post Text]
sh2 = Body (,) [Post Text] (K IO) [Post Text] ()
-> Body (,) [Post Text] (K IO) () [Post Text]
-> AgentShard [Post Text] [Post Text]
forall (arr :: * -> * -> *) a b.
HasDual () arr =>
arr a () -> arr () b -> Poles arr a b
poles0 Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch Body (,) [Post Text] (K IO) () [Post Text]
read'
where
read' :: Body (,) [Post Text] (K IO) () [Post Text]
read' = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> do
o1 <- AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
runSubShard AgentShard [Post Text] [Post Text]
sh1 [Post Text]
s
o2 <- runSubShard sh2 s
pure ([], if null o1 then o2 else o1)
fanOutShard ::
[AgentShard [Post Text] [Post Text]] ->
AgentShard [Post Text] [Post Text]
fanOutShard :: [AgentShard [Post Text] [Post Text]]
-> AgentShard [Post Text] [Post Text]
fanOutShard [AgentShard [Post Text] [Post Text]]
shs = Body (,) [Post Text] (K IO) [Post Text] ()
-> Body (,) [Post Text] (K IO) () [Post Text]
-> AgentShard [Post Text] [Post Text]
forall (arr :: * -> * -> *) a b.
HasDual () arr =>
arr a () -> arr () b -> Poles arr a b
poles0 Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch Body (,) [Post Text] (K IO) () [Post Text]
read'
where
read' :: Body (,) [Post Text] (K IO) () [Post Text]
read' = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> do
os <- (AgentShard [Post Text] [Post Text] -> IO [Post Text])
-> [AgentShard [Post Text] [Post Text]] -> IO [[Post Text]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
`runSubShard` [Post Text]
s) [AgentShard [Post Text] [Post Text]]
shs
pure ([], concat os)
fanInShard ::
([[Post Text]] -> [Post Text]) ->
[AgentShard [Post Text] [Post Text]] ->
AgentShard [Post Text] [Post Text]
fanInShard :: ([[Post Text]] -> [Post Text])
-> [AgentShard [Post Text] [Post Text]]
-> AgentShard [Post Text] [Post Text]
fanInShard [[Post Text]] -> [Post Text]
summary [AgentShard [Post Text] [Post Text]]
shs = Body (,) [Post Text] (K IO) [Post Text] ()
-> Body (,) [Post Text] (K IO) () [Post Text]
-> AgentShard [Post Text] [Post Text]
forall (arr :: * -> * -> *) a b.
HasDual () arr =>
arr a () -> arr () b -> Poles arr a b
poles0 Body (,) [Post Text] (K IO) [Post Text] ()
writeBatch Body (,) [Post Text] (K IO) () [Post Text]
read'
where
read' :: Body (,) [Post Text] (K IO) () [Post Text]
read' = K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
(arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text])
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
-> Body (,) [Post Text] (K IO) () [Post Text]
forall a b. (a -> b) -> a -> b
$ (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text]))
-> (([Post Text], ()) -> IO ([Post Text], [Post Text]))
-> K IO ([Post Text], ()) ([Post Text], [Post Text])
forall a b. (a -> b) -> a -> b
$ \([Post Text]
s, ()) -> do
os <- (AgentShard [Post Text] [Post Text] -> IO [Post Text])
-> [AgentShard [Post Text] [Post Text]] -> IO [[Post Text]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (AgentShard [Post Text] [Post Text] -> [Post Text] -> IO [Post Text]
`runSubShard` [Post Text]
s) [AgentShard [Post Text] [Post Text]]
shs
pure ([], summary os)
synthesisSummary ::
Name ->
[Name] ->
[PostId] ->
([[Post Text]] -> Text) ->
[[Post Text]] ->
[Post Text]
synthesisSummary :: Text
-> [Text]
-> [PostId]
-> ([[Post Text]] -> Text)
-> [[Post Text]]
-> [Post Text]
synthesisSummary Text
who [Text]
audience [PostId]
parentIds [[Post Text]] -> Text
f [[Post Text]]
oss =
case ([[Post Text]] -> [Post Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Post Text]]
oss, Text -> Text
T.strip ([[Post Text]] -> Text
f [[Post Text]]
oss)) of
([], Text
_) -> []
([Post Text]
_, Text
b) | Text -> Bool
T.null Text
b -> []
([Post Text]
ps, Text
b) ->
let ids :: [PostId]
ids = Int -> [PostId] -> [PostId]
forall a. Int -> [a] -> [a]
take ([Post Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Post Text]
ps) [PostId]
parentIds
in [if [PostId] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PostId]
ids then Text -> [Text] -> Text -> Post Text
forall a. Text -> [Text] -> a -> Post a
mkPost Text
who [Text]
audience Text
b else Text -> [Text] -> [PostId] -> Text -> Post Text
forall b. Text -> [Text] -> [PostId] -> b -> Post b
synthesis Text
who [Text]
audience [PostId]
ids Text
b]