Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 145da5c

Browse files
Add Data instance to Record
1 parent 70aa72e commit 145da5c

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

composite-base/src/Composite/Record.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import qualified Data.Vinyl.TypeLevel as Vinyl
3030
import Data.Vinyl.XRec(IsoHKD(HKD, toHKD, unHKD))
3131
import Foreign.Storable (Storable)
3232
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
33+
import Data.Data (Data (gfoldl, toConstr, dataTypeOf, gunfold), Typeable, DataType, Constr, Fixity (Prefix, Infix), mkDataType, constrIndex, mkConstr)
3334

3435
-- FIXME this file is a big bin of random stuff, and should be at least organized if not split up.
3536

@@ -102,6 +103,33 @@ instance KnownSymbol s => IsoHKD Identity (s :-> a) where
102103
unHKD = Identity . Val
103104
toHKD (Identity (Val x)) = x
104105

106+
deriving stock instance (Typeable k, Data k, KnownSymbol s) => Data (s :-> k)
107+
108+
rnilConstr :: Constr
109+
rnilConstr = mkConstr recDataType "RNil" [] Prefix
110+
111+
rconsConstr :: Constr
112+
rconsConstr = mkConstr recDataType "(:&)" [] Infix
113+
114+
recDataType :: DataType
115+
recDataType = mkDataType "Data.Vinyl.Core.Rec" [rnilConstr, rconsConstr]
116+
117+
instance Data (Record '[]) where
118+
gfoldl _ z RNil = z RNil
119+
toConstr RNil = rnilConstr
120+
dataTypeOf _ = recDataType
121+
gunfold _ z c = case constrIndex c of
122+
1 -> z RNil
123+
_ -> errorWithoutStackTrace "Data.Data.gunfold(Rec)"
124+
125+
instance (Data x, Typeable xs, Data (Record xs)) => Data (Record (x ': xs)) where
126+
gfoldl f z (x :& xs) = z (:&) `f` x `f` xs
127+
toConstr (_ :& _) = rconsConstr
128+
dataTypeOf _ = recDataType
129+
gunfold k z c = case constrIndex c of
130+
2 -> k (k (z (:&)))
131+
_ -> errorWithoutStackTrace "Data.Data.gunfold(Rec)"
132+
105133
-- |Convenience function to make an @'Identity' (s ':->' a)@ with a particular symbol, used for named field construction.
106134
--
107135
-- For example:

0 commit comments

Comments
 (0)