{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Composite.Record.Binary where

import Composite.Record((:->), Record, Rec((:&)), val, getVal)
import Control.Applicative(liftA2)
import Data.Binary(Binary(put, get))
import Data.Functor.Identity(runIdentity)

instance Binary a => Binary (s :-> a) where
  put :: (s :-> a) -> Put
put = forall t. Binary t => t -> Put
put forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: Symbol) a. (s :-> a) -> a
getVal
  get :: Get (s :-> a)
get = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: Symbol) a. a -> Identity (s :-> a)
val) forall t. Binary t => Get t
get

instance Binary (Record '[])

instance (Binary x, Binary (Record xs)) => Binary (Record (x : xs)) where
  put :: Record (x : xs) -> Put
put (Identity r
x :& Rec Identity rs
xs) = forall t. Binary t => t -> Put
put Identity r
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Rec Identity rs
xs
  get :: Get (Record (x : xs))
get = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&) forall t. Binary t => Get t
get forall t. Binary t => Get t
get