Skip to content

Commit 7ac6091

Browse files
committed
wip: add more instances of HasChunkRefs and minor exports clean-up
1 parent 1300982 commit 7ac6091

File tree

1 file changed

+47
-20
lines changed
  • code/drasil-database/lib/Drasil/Database

1 file changed

+47
-20
lines changed

code/drasil-database/lib/Drasil/Database/Chunk.hs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66
FlexibleContexts,
77
UndecidableInstances,
88
FlexibleInstances #-}
9-
module Drasil.Database.Chunk
10-
( Chunk,
11-
IsChunk,
12-
HasChunkRefs (..),
13-
mkChunk, -- FIXME: mkChunk should not be exported but is temporarily because this module is NOT in `drasil-database`
14-
unChunk,
15-
chunkType
16-
)
17-
where
9+
module Drasil.Database.Chunk (
10+
Chunk,
11+
IsChunk,
12+
HasChunkRefs(..),
13+
mkChunk, -- FIXME: mkChunk should not be exported but is temporarily because this module is NOT in `drasil-database`
14+
unChunk,
15+
chunkType
16+
) where
1817

1918
import Control.Lens ((^.), to, Getter)
20-
import qualified Data.Foldable as F
2119
import qualified Data.Set as S
2220
import Data.Typeable (Proxy (Proxy), TypeRep, Typeable, cast, typeOf, typeRep)
23-
import GHC.Generics (Generic (Rep, from), M1 (..), K1 (..), type (:*:) (..), type (:+:) (..), U1, Generically(..))
21+
import GHC.Generics (Generic (Rep, from), M1 (..), K1 (..), type (:*:) (..),
22+
type (:+:) (..), U1, Generically(..))
2423

2524
import Drasil.Database.UID (HasUID (..), UID)
2625

@@ -64,26 +63,54 @@ chunkType (Chunk c) = typeOf c
6463
class HasChunkRefs a where
6564
chunkRefs :: a -> S.Set UID
6665

67-
instance HasChunkRefs String where
66+
instance HasChunkRefs UID where
67+
-- | UIDs are UIDs, not *UID references*, a TypedUIDRef is a *reference*.
68+
-- Therefore, `UID` has no chunk references. They should only be used for the
69+
-- UID of a thing being defined, *not as a reference (unless specifically
70+
-- within the 'ChunkDB')*.
6871
chunkRefs _ = S.empty
6972
{-# INLINABLE chunkRefs #-}
7073

71-
instance HasChunkRefs a => HasChunkRefs [a] where
72-
chunkRefs = F.foldl' S.union S.empty . map chunkRefs
74+
instance HasChunkRefs Int where
75+
chunkRefs _ = S.empty
7376
{-# INLINABLE chunkRefs #-}
7477

75-
instance HasChunkRefs (Maybe String) where
78+
instance HasChunkRefs Integer where
7679
chunkRefs _ = S.empty
7780
{-# INLINABLE chunkRefs #-}
7881

79-
instance HasChunkRefs UID where
80-
-- | UIDs are UIDs, not *UID references*, a TypedUIDRef is a *reference*.
81-
-- Therefore, `UID` has no chunk references. They should only be used for the
82-
-- UID of a thing being defined, *not as a reference (unless specifically
83-
-- within the 'ChunkDB')*.
82+
instance HasChunkRefs Double where
83+
chunkRefs _ = S.empty
84+
{-# INLINABLE chunkRefs #-}
85+
86+
instance HasChunkRefs Bool where
87+
chunkRefs _ = S.empty
88+
{-# INLINABLE chunkRefs #-}
89+
90+
instance HasChunkRefs Char where
91+
chunkRefs _ = S.empty
92+
{-# INLINABLE chunkRefs #-}
93+
94+
-- NOTE: 'OVERLAPPING' instance here because [Char] is instantiated with
95+
-- `HasChunkRefs [a]`, but very inefficient. We already know the result will be
96+
-- empty.
97+
instance {-# OVERLAPPING #-} HasChunkRefs String where
8498
chunkRefs _ = S.empty
8599
{-# INLINABLE chunkRefs #-}
86100

101+
instance HasChunkRefs a => HasChunkRefs [a] where
102+
chunkRefs = S.unions . map chunkRefs
103+
{-# INLINABLE chunkRefs #-}
104+
105+
instance HasChunkRefs a => HasChunkRefs (Maybe a) where
106+
chunkRefs Nothing = S.empty
107+
chunkRefs (Just v) = chunkRefs v
108+
{-# INLINABLE chunkRefs #-}
109+
110+
instance (HasChunkRefs l, HasChunkRefs r) => HasChunkRefs (Either l r) where
111+
chunkRefs = either chunkRefs chunkRefs
112+
{-# INLINABLE chunkRefs #-}
113+
87114
instance (Generic a, GHasCRefs (Rep a)) => HasChunkRefs (Generically a) where
88115
chunkRefs (Generically a) = gChunkRefs $ from a
89116
{-# INLINABLE chunkRefs #-}

0 commit comments

Comments
 (0)