Skip to content

Commit 558a9fc

Browse files
committed
typed-protocols: added StateToken type family to Protocol type class
1 parent 1ca4aa7 commit 558a9fc

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

typed-protocols-examples/src/Network/TypedProtocol/PingPong/Type.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ instance Protocol PingPong where
7676
type StateAgency StBusy = ServerAgency
7777
type StateAgency StDone = NobodyAgency
7878

79+
type StateToken = SPingPong
80+
7981

8082
deriving instance Show (Message PingPong from to)

typed-protocols-examples/src/Network/TypedProtocol/ReqResp/Type.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ instance Protocol (ReqResp req resp) where
4646
type StateAgency StBusy = ServerAgency
4747
type StateAgency StDone = NobodyAgency
4848

49+
type StateToken = SReqResp
50+
4951

5052
deriving instance (Show req, Show resp)
5153
=> Show (Message (ReqResp req resp) from to)

typed-protocols-examples/src/Network/TypedProtocol/ReqResp2/Type.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ instance Protocol (ReqResp2 req resp) where
5555
type StateAgency StBusy' = ServerAgency
5656
type StateAgency StDone = NobodyAgency
5757

58+
type StateToken = SReqResp2
59+
5860

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

typed-protocols-examples/src/Network/TypedProtocol/Trans/Wedge.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ instance Protocol (Wedge ps (stIdle :: ps) ps' (stIdle' :: ps')) where
105105
type StateAgency (StFst st) = StateAgency st
106106
type StateAgency (StSnd st) = StateAgency st
107107

108+
type StateToken = SingWedge
109+
108110

109111
type PingPong2 = Wedge PingPong.PingPong PingPong.StIdle
110112
PingPong.PingPong PingPong.StIdle

typed-protocols/src/Network/TypedProtocol/Core.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module Network.TypedProtocol.Core
4747
, IsActiveState (..)
4848
, ActiveState
4949
, notActiveState
50+
-- * Utils
51+
, stateToken
5052
) where
5153

5254
import Data.Kind (Constraint, Type)
@@ -402,6 +404,16 @@ class Protocol ps where
402404
--
403405
type StateAgency (st :: ps) :: Agency
404406

407+
-- | A type alias for protocol state token, e.g. term level representation of
408+
-- type level state (also known as singleton).
409+
--
410+
type StateToken :: ps -> Type
411+
412+
-- | An alias for 'sing'.
413+
--
414+
stateToken :: (SingI st, Sing st ~ StateToken st) => StateToken st
415+
stateToken = sing
416+
405417
type ActiveAgency' :: ps -> Agency -> Type
406418
data ActiveAgency' st agency where
407419
ClientHasAgency :: StateAgency st ~ ClientAgency

0 commit comments

Comments
 (0)