Skip to content

Commit 0d9cb77

Browse files
committed
typed-protocols: moved types to Core module
1 parent d2f1e74 commit 0d9cb77

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
{-# LANGUAGE BangPatterns #-}
21
{-# LANGUAGE ConstraintKinds #-}
32
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DerivingVia #-}
44
{-# LANGUAGE EmptyCase #-}
55
{-# LANGUAGE FlexibleContexts #-}
66
{-# LANGUAGE FlexibleInstances #-}
77
{-# LANGUAGE GADTs #-}
88
{-# LANGUAGE MultiParamTypeClasses #-}
9+
{-# LANGUAGE PatternSynonyms #-}
910
{-# LANGUAGE PolyKinds #-}
1011
{-# LANGUAGE QuantifiedConstraints #-}
1112
{-# LANGUAGE RankNTypes #-}
1213
{-# LANGUAGE ScopedTypeVariables #-}
1314
{-# LANGUAGE StandaloneDeriving #-}
1415
{-# LANGUAGE StandaloneKindSignatures #-}
15-
{-# LANGUAGE TypeFamilies #-}
1616
{-# LANGUAGE TypeFamilyDependencies #-}
1717
{-# LANGUAGE TypeOperators #-}
1818
-- need for 'Show' instance of 'ProtocolState'
1919
{-# LANGUAGE UndecidableInstances #-}
20+
{-# LANGUAGE ViewPatterns #-}
2021

2122
-- | This module defines the core of the typed protocol framework.
2223
--
@@ -43,6 +44,9 @@ module Network.TypedProtocol.Core
4344
, IsPipelined (..)
4445
, Outstanding
4546
, N (..)
47+
, Nat (Succ, Zero)
48+
, natToInt
49+
, unsafeIntToNat
4650
, ActiveAgency
4751
, ActiveAgency' (..)
4852
, IsActiveState (..)
@@ -51,6 +55,7 @@ module Network.TypedProtocol.Core
5155
) where
5256

5357
import Data.Kind (Constraint, Type)
58+
import Unsafe.Coerce (unsafeCoerce)
5459

5560
import Data.Singletons
5661

@@ -504,3 +509,37 @@ type Outstanding :: IsPipelined -> N
504509
type family Outstanding pl where
505510
Outstanding 'NonPipelined = Z
506511
Outstanding ('Pipelined n _) = n
512+
513+
-- | A value level inductive natural number, indexed by the corresponding type
514+
-- level natural number 'N'.
515+
--
516+
-- This is often needed when writing pipelined peers to be able to count the
517+
-- number of outstanding pipelined yields, and show to the type checker that
518+
-- 'SenderCollect' and 'SenderDone' are being used correctly.
519+
--
520+
newtype Nat (n :: N) = UnsafeInt Int
521+
deriving Show via Int
522+
523+
data IsNat (n :: N) where
524+
IsZero :: IsNat Z
525+
IsSucc :: Nat n -> IsNat (S n)
526+
527+
toIsNat :: Nat n -> IsNat n
528+
toIsNat (UnsafeInt 0) = unsafeCoerce IsZero
529+
toIsNat (UnsafeInt n) = unsafeCoerce (IsSucc (UnsafeInt (pred n)))
530+
531+
pattern Zero :: () => Z ~ n => Nat n
532+
pattern Zero <- (toIsNat -> IsZero) where
533+
Zero = UnsafeInt 0
534+
535+
pattern Succ :: () => (m ~ S n) => Nat n -> Nat m
536+
pattern Succ n <- (toIsNat -> IsSucc n) where
537+
Succ (UnsafeInt n) = UnsafeInt (succ n)
538+
539+
{-# COMPLETE Zero, Succ #-}
540+
541+
natToInt :: Nat n -> Int
542+
natToInt (UnsafeInt n) = n
543+
544+
unsafeIntToNat :: Int -> Nat n
545+
unsafeIntToNat = UnsafeInt

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
{-# LANGUAGE DerivingVia #-}
44
{-# LANGUAGE FlexibleContexts #-}
55
{-# LANGUAGE GADTs #-}
6-
{-# LANGUAGE PatternSynonyms #-}
76
{-# LANGUAGE PolyKinds #-}
87
{-# LANGUAGE RankNTypes #-}
98
{-# LANGUAGE StandaloneDeriving #-}
109
{-# LANGUAGE StandaloneKindSignatures #-}
1110
{-# LANGUAGE TypeOperators #-}
12-
{-# LANGUAGE ViewPatterns #-}
1311

1412
-- | Protocol EDSL.
1513
--
@@ -28,7 +26,6 @@ module Network.TypedProtocol.Peer
2826
) where
2927

3028
import Data.Kind (Type)
31-
import Unsafe.Coerce (unsafeCoerce)
3229

3330
import Network.TypedProtocol.Core as Core
3431

@@ -257,37 +254,3 @@ data Receiver ps pr st stdone m c where
257254
-> Receiver ps pr st stdone m c
258255

259256
deriving instance Functor m => Functor (Receiver ps pr st stdone m)
260-
261-
-- | A value level inductive natural number, indexed by the corresponding type
262-
-- level natural number 'N'.
263-
--
264-
-- This is often needed when writing pipelined peers to be able to count the
265-
-- number of outstanding pipelined yields, and show to the type checker that
266-
-- 'SenderCollect' and 'SenderDone' are being used correctly.
267-
--
268-
newtype Nat (n :: N) = UnsafeInt Int
269-
deriving Show via Int
270-
271-
data IsNat (n :: N) where
272-
IsZero :: IsNat Z
273-
IsSucc :: Nat n -> IsNat (S n)
274-
275-
toIsNat :: Nat n -> IsNat n
276-
toIsNat (UnsafeInt 0) = unsafeCoerce IsZero
277-
toIsNat (UnsafeInt n) = unsafeCoerce (IsSucc (UnsafeInt (pred n)))
278-
279-
pattern Zero :: () => Z ~ n => Nat n
280-
pattern Zero <- (toIsNat -> IsZero) where
281-
Zero = UnsafeInt 0
282-
283-
pattern Succ :: () => (m ~ S n) => Nat n -> Nat m
284-
pattern Succ n <- (toIsNat -> IsSucc n) where
285-
Succ (UnsafeInt n) = UnsafeInt (succ n)
286-
287-
{-# COMPLETE Zero, Succ #-}
288-
289-
natToInt :: Nat n -> Int
290-
natToInt (UnsafeInt n) = n
291-
292-
unsafeIntToNat :: Int -> Nat n
293-
unsafeIntToNat = UnsafeInt

typed-protocols/src/Network/TypedProtocol/Peer/Client.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Network.TypedProtocol.Peer.Client
3333
import Data.Kind (Type)
3434

3535
import Network.TypedProtocol.Core
36-
import Network.TypedProtocol.Peer (Peer, Nat (..))
36+
import Network.TypedProtocol.Peer (Peer)
3737
import qualified Network.TypedProtocol.Peer as TP
3838

3939

typed-protocols/src/Network/TypedProtocol/Peer/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Network.TypedProtocol.Peer.Server
3333
import Data.Kind (Type)
3434

3535
import Network.TypedProtocol.Core
36-
import Network.TypedProtocol.Peer (Peer, Nat (..))
36+
import Network.TypedProtocol.Peer (Peer)
3737
import qualified Network.TypedProtocol.Peer as TP
3838

3939

0 commit comments

Comments
 (0)