Skip to content

Commit fa023d3

Browse files
jasagredoamesgen
authored andcommitted
More precise type for Index
1 parent af0e6ae commit fa023d3

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- empty -->

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/Node.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import Data.Functor.These (These1 (..))
6969
import qualified Data.Map.Strict as Map
7070
import Data.SOP.BasicFunctors
7171
import Data.SOP.Counting
72-
import Data.SOP.Index (Index (..))
72+
import Data.SOP.Index
7373
import Data.SOP.OptNP (NonEmptyOptNP, OptNP (OptSkip))
7474
import qualified Data.SOP.OptNP as OptNP
7575
import Data.SOP.Strict

sop-extras/changelog.d/js-index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Non-Breaking
2+
3+
- Refactor `Index` to use `NS ((:~:) x) xs`. Pattern synonyms `IZ` and `IS` are
4+
provided, making this change transparent for users.

sop-extras/src/Data/SOP/Index.hs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
{-# LANGUAGE FlexibleContexts #-}
55
{-# LANGUAGE GADTs #-}
66
{-# LANGUAGE LambdaCase #-}
7+
{-# LANGUAGE PatternSynonyms #-}
78
{-# LANGUAGE PolyKinds #-}
89
{-# LANGUAGE RankNTypes #-}
910
{-# LANGUAGE ScopedTypeVariables #-}
10-
{-# LANGUAGE StandaloneKindSignatures #-}
1111
{-# LANGUAGE TypeApplications #-}
1212
{-# LANGUAGE TypeOperators #-}
13+
{-# LANGUAGE ViewPatterns #-}
1314

1415
module Data.SOP.Index (
1516
-- * Indexing SOP types
@@ -18,6 +19,8 @@ module Data.SOP.Index (
1819
, indices
1920
, injectNS
2021
, injectNS'
22+
, pattern IS
23+
, pattern IZ
2124
, projectNP
2225
-- * Zipping with indices
2326
, hcimap
@@ -36,29 +39,42 @@ module Data.SOP.Index (
3639
) where
3740

3841
import Data.Coerce
39-
import Data.Kind (Type)
4042
import Data.Proxy
4143
import Data.SOP.BasicFunctors
4244
import Data.SOP.Constraint
4345
import Data.SOP.Dict (Dict (..))
4446
import Data.SOP.Sing
4547
import Data.SOP.Strict
48+
import Data.Type.Equality ((:~:) (..))
4649
import Data.Word
4750

48-
type Index :: [k] -> k -> Type
49-
data Index xs x where
50-
IZ :: Index (x ': xs) x
51-
IS :: Index xs x -> Index (y ': xs) x
51+
newtype Index xs x = Index { getIndex :: NS ((:~:) x) xs }
52+
53+
pattern IZ ::
54+
()
55+
=> xs ~ (x ': xs1)
56+
=> Index xs x
57+
pattern IZ = Index (Z Refl)
58+
59+
pattern IS ::
60+
()
61+
=> xs ~ (x' ': xs')
62+
=> Index xs' x
63+
-> Index xs x
64+
pattern IS z <- Index (S (Index -> z)) where
65+
IS (Index z) = Index (S z)
66+
67+
{-# COMPLETE IZ, IS #-}
5268

5369
indices :: forall xs. SListI xs => NP (Index xs) xs
5470
indices = case sList @xs of
5571
SNil -> Nil
56-
SCons -> IZ :* hmap IS indices
72+
SCons -> IZ :* hmap (Index . S . getIndex) indices
5773

5874
dictIndexAll :: All c xs => Proxy c -> Index xs x -> Dict c x
5975
dictIndexAll p = \case
60-
IZ -> Dict
61-
IS idx' -> dictIndexAll p idx'
76+
IZ -> Dict
77+
IS idx -> dictIndexAll p idx
6278

6379
injectNS :: forall f x xs. Index xs x -> f x -> NS f xs
6480
injectNS idx x = case idx of
@@ -202,5 +218,5 @@ toWord8 = go 0
202218
where
203219
go :: Word8 -> Index ys y -> Word8
204220
go !n = \case
205-
IZ -> n
206-
IS idx' -> go (n + 1) idx'
221+
IZ -> n
222+
IS idx -> go (n + 1) idx

0 commit comments

Comments
 (0)