Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
08ee7c4
typed-protocols: new API
coot Jul 25, 2024
81f2fbe
typed-protocols: Peer pattern synonyms
coot Jul 25, 2024
fe36759
typed-protocols-examples: updated
coot Jul 25, 2024
88d246f
typed-protocols: ActiveAgency
coot Sep 6, 2021
b121a80
typed-protocols: simplify runPeerWithDriver
coot Mar 27, 2022
bdf8100
typed-protocols: internal lemmas module
coot May 23, 2022
564da48
typed-protocols: provide ReflRelativeAgency type aliases
coot Jul 13, 2022
6f5ce41
typed-protocols-examples: socketAsChannel
coot Sep 4, 2021
c69993a
typed-protocols-examples: ReqResp2 example
coot Sep 17, 2021
e4bcf98
typed-protocols-examples: Wedge
coot Sep 18, 2021
86d007a
typed-protocols-examples: requestOnce
coot Sep 28, 2021
b4070d4
typed-protocols-examples: fixed cborg tests
coot Oct 2, 2021
74a64f2
typed-protocols-examples: pipelined tests
coot Oct 2, 2021
5ef5f2d
typed-protocols-examples: unbounded buffered channel
coot Mar 27, 2022
ce833a9
typed-protocols-examples: added runConnectedPeersAsymetric
coot Mar 27, 2022
1ca4aa7
typed-protocols: simplify evidence of termination in a terminal state
coot May 13, 2022
558a9fc
typed-protocols: added StateToken type family to Protocol type class
coot Jun 23, 2022
03270e4
typed-protocols: renamed Pipelined kind to IsPipelined
coot Jul 13, 2022
6b6118d
typed-protocols: added AnyMessageAndAgency pattern synonym
coot Sep 8, 2023
abecdaf
typed-protocols: added application specific singletons for protocol s…
coot Sep 26, 2023
a81caa1
typed-protocols-examples: relaxed constraint in PingPong client
coot Jul 1, 2022
166f5af
typed-protocols-examples: fixed a socket test on macos
coot Aug 17, 2022
f5ccedd
typed-protocols: bump version to `0.2.0.0`
coot Aug 25, 2023
148a91b
Added CHANGELOG and bumped versions of the packages
coot May 17, 2022
6c6a8ce
typed-protocols: moved outstanding counter into IsPipelined kind
coot Jul 23, 2024
d2f1e74
typed-protocols-stateful: non-pipelined stateful Peer
coot Jul 24, 2024
0d9cb77
typed-protocols: moved types to `Core` module
coot Jul 24, 2024
5ec9a8c
typed-protocols: connectPipelined type signature
coot Jul 24, 2024
8d31814
typed-protocols: added PeerPipelined newtype wrapper
coot Jul 24, 2024
b3ba779
Support io-classes-1.7
coot Jul 26, 2024
b23f327
typed-protocols: added haddocks
coot Aug 19, 2024
72a799c
disabled `typed-protocols-doc` in GHA
coot Aug 22, 2024
5cf79f3
stylish-haskell: fixes
coot Aug 22, 2024
b9e71bb
typed-protocols: removed unused extensions
coot Aug 23, 2024
20aa2fe
Improved haddocks, removed unused pragmas (extensions)
coot Aug 23, 2024
bdcf30a
Core module cannot be checked with stylish-haskell
coot Aug 23, 2024
0042b72
Removed Stateful PingPong example
coot Sep 4, 2024
b195c92
Stateful RPC example
coot Sep 5, 2024
f451040
typed-protocols: fixed typos
coot Sep 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
typed-protocols-examples: ReqResp2 example
An example which demonstrates that one can pipeline two kinds of
requests, and collect all the responses.
  • Loading branch information
coot committed Sep 16, 2024
commit c69993a02883bef3d78ef6c21eab383510a8f28b
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}



module Network.TypedProtocol.ReqResp2.Client where

import Network.TypedProtocol.ReqResp2.Type

import Network.TypedProtocol.Core
import Network.TypedProtocol.Peer.Client


reqResp2Client :: forall req resp m.
()
=> [Either req req]
-> Client (ReqResp2 req resp) ('Pipelined (Either resp resp)) Z StIdle m [Either resp resp]
reqResp2Client = send Zero
where
-- pipeline all the requests, either through `MsgReq` or `MsgReq'`.
send :: forall (n :: N).
Nat n
-> [Either req req] -- requests to send
-> Client (ReqResp2 req resp) ('Pipelined (Either resp resp)) n StIdle m [Either resp resp]

send !n (Left req : reqs) =
YieldPipelined (MsgReq req) receiver (send (Succ n) reqs)

send !n (Right req : reqs) =
YieldPipelined (MsgReq' req) receiver' (send (Succ n) reqs)

send !n [] = collect n []


receiver :: Receiver (ReqResp2 req resp) StBusy StIdle m (Either resp resp)
receiver = ReceiverAwait (\(MsgResp resp) -> ReceiverDone (Left resp))


receiver' :: Receiver (ReqResp2 req resp) StBusy' StIdle m (Either resp resp)
receiver' = ReceiverAwait (\(MsgResp' resp) -> ReceiverDone (Right resp))


-- collect all the responses
collect :: Nat n
-> [Either resp resp] -- all the responses received so far
-> Client (ReqResp2 req resp) ('Pipelined (Either resp resp)) n StIdle m [Either resp resp]

collect Zero !resps = Yield MsgDone (Done (reverse resps))

collect (Succ n) !resps =
Collect Nothing $ \c -> collect n (c : resps)



Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}


module Network.TypedProtocol.ReqResp2.Type where

import Data.Singletons

import Network.TypedProtocol.Core


data ReqResp2 req resp where
StIdle :: ReqResp2 req resp
StBusy :: ReqResp2 req resp
StBusy' :: ReqResp2 req resp
StDone :: ReqResp2 req resp

data SReqResp2 (st :: ReqResp2 req resp) where
SingIdle :: SReqResp2 StIdle
SingBusy :: SReqResp2 StBusy
SingBusy' :: SReqResp2 StBusy'
SingDone :: SReqResp2 StDone

deriving instance Show (SReqResp2 st)

type instance Sing = SReqResp2
instance SingI StIdle where
sing = SingIdle
instance SingI StBusy where
sing = SingBusy
instance SingI StBusy' where
sing = SingBusy'
instance SingI StDone where
sing = SingDone


instance Protocol (ReqResp2 req resp) where

data Message (ReqResp2 req resp) from to where
MsgReq :: req -> Message (ReqResp2 req resp) StIdle StBusy
MsgResp :: resp -> Message (ReqResp2 req resp) StBusy StIdle

MsgReq' :: req -> Message (ReqResp2 req resp) StIdle StBusy'
MsgResp' :: resp -> Message (ReqResp2 req resp) StBusy' StIdle

MsgDone :: Message (ReqResp2 req resp) StIdle StDone

type StateAgency StIdle = ClientAgency
type StateAgency StBusy = ServerAgency
type StateAgency StBusy' = ServerAgency
type StateAgency StDone = NobodyAgency


deriving instance (Show req, Show resp)
=> Show (Message (ReqResp2 req resp) from to)

deriving instance (Eq req, Eq resp)
=> Eq (Message (ReqResp2 req resp) from to)

2 changes: 2 additions & 0 deletions typed-protocols-examples/typed-protocols-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ library
, Network.TypedProtocol.ReqResp.Codec.CBOR
, Network.TypedProtocol.ReqResp.Examples

, Network.TypedProtocol.ReqResp2.Type
, Network.TypedProtocol.ReqResp2.Client
other-extensions: GADTs
, RankNTypes
, PolyKinds
Expand Down