|
6 | 6 | FlexibleContexts, |
7 | 7 | UndecidableInstances, |
8 | 8 | 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 |
18 | 17 |
|
19 | 18 | import Control.Lens ((^.), to, Getter) |
20 | | -import qualified Data.Foldable as F |
21 | 19 | import qualified Data.Set as S |
22 | 20 | 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(..)) |
24 | 23 |
|
25 | 24 | import Drasil.Database.UID (HasUID (..), UID) |
26 | 25 |
|
@@ -64,26 +63,54 @@ chunkType (Chunk c) = typeOf c |
64 | 63 | class HasChunkRefs a where |
65 | 64 | chunkRefs :: a -> S.Set UID |
66 | 65 |
|
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')*. |
68 | 71 | chunkRefs _ = S.empty |
69 | 72 | {-# INLINABLE chunkRefs #-} |
70 | 73 |
|
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 |
73 | 76 | {-# INLINABLE chunkRefs #-} |
74 | 77 |
|
75 | | -instance HasChunkRefs (Maybe String) where |
| 78 | +instance HasChunkRefs Integer where |
76 | 79 | chunkRefs _ = S.empty |
77 | 80 | {-# INLINABLE chunkRefs #-} |
78 | 81 |
|
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 |
84 | 98 | chunkRefs _ = S.empty |
85 | 99 | {-# INLINABLE chunkRefs #-} |
86 | 100 |
|
| 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 | + |
87 | 114 | instance (Generic a, GHasCRefs (Rep a)) => HasChunkRefs (Generically a) where |
88 | 115 | chunkRefs (Generically a) = gChunkRefs $ from a |
89 | 116 | {-# INLINABLE chunkRefs #-} |
|
0 commit comments