{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
module Ersatz.Codec
( Codec(..)
) where
import Control.Applicative
import Control.Monad hiding (mapM)
import Data.Array
import Data.HashMap.Lazy (HashMap)
import Data.IntMap (IntMap)
import Data.Kind
import Data.Map (Map)
import Data.Sequence (Seq)
import Data.Traversable
import Data.Tree (Tree)
import Ersatz.Internal.Literal
import Ersatz.Solution
import Prelude hiding (mapM)
class Codec a where
type Decoded a :: Type
decode :: MonadPlus f => Solution -> a -> f (Decoded a)
encode :: Decoded a -> a
instance Codec Literal where
type Decoded Literal = Bool
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Literal -> f (Decoded Literal)
decode Solution
s Literal
a = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True) forall (f :: * -> *) a. Applicative f => a -> f a
pure (Solution -> Literal -> Maybe Bool
solutionLiteral Solution
s Literal
a)
encode :: Decoded Literal -> Literal
encode Bool
Decoded Literal
True = Literal
literalTrue
encode Bool
Decoded Literal
False = Literal
literalFalse
instance Codec () where
type Decoded () = ()
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> () -> f (Decoded ())
decode Solution
_ () = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
encode :: Decoded () -> ()
encode () = ()
instance (Codec a, Codec b) => Codec (a,b) where
type Decoded (a,b) = (Decoded a, Decoded b)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> (a, b) -> f (Decoded (a, b))
decode Solution
s (a
a,b
b) = (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b
encode :: Decoded (a, b) -> (a, b)
encode (Decoded a
a,Decoded b
b) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b)
instance (Codec a, Codec b, Codec c) => Codec (a,b,c) where
type Decoded (a,b,c) = (Decoded a, Decoded b, Decoded c)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> (a, b, c) -> f (Decoded (a, b, c))
decode Solution
s (a
a,b
b,c
c) = (,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c
encode :: Decoded (a, b, c) -> (a, b, c)
encode (Decoded a
a,Decoded b
b,Decoded c
c) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c)
instance (Codec a, Codec b, Codec c, Codec d) => Codec (a,b,c,d) where
type Decoded (a,b,c,d) = (Decoded a, Decoded b, Decoded c, Decoded d)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> (a, b, c, d) -> f (Decoded (a, b, c, d))
decode Solution
s (a
a,b
b,c
c,d
d) = (,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s d
d
encode :: Decoded (a, b, c, d) -> (a, b, c, d)
encode (Decoded a
a,Decoded b
b,Decoded c
c,Decoded d
d) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c, forall a. Codec a => Decoded a -> a
encode Decoded d
d)
instance (Codec a, Codec b, Codec c, Codec d, Codec e) => Codec (a,b,c,d,e) where
type Decoded (a,b,c,d,e) = (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> (a, b, c, d, e) -> f (Decoded (a, b, c, d, e))
decode Solution
s (a
a,b
b,c
c,d
d,e
e) = (,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s d
d forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s e
e
encode :: Decoded (a, b, c, d, e) -> (a, b, c, d, e)
encode (Decoded a
a,Decoded b
b,Decoded c
c,Decoded d
d,Decoded e
e) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c, forall a. Codec a => Decoded a -> a
encode Decoded d
d, forall a. Codec a => Decoded a -> a
encode Decoded e
e)
instance (Codec a, Codec b, Codec c, Codec d, Codec e, Codec f) => Codec (a,b,c,d,e,f) where
type Decoded (a,b,c,d,e,f) = (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> (a, b, c, d, e, f) -> f (Decoded (a, b, c, d, e, f))
decode Solution
s (a
a,b
b,c
c,d
d,e
e,f
f) = (,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s d
d forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s e
e forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s f
f
encode :: Decoded (a, b, c, d, e, f) -> (a, b, c, d, e, f)
encode (Decoded a
a,Decoded b
b,Decoded c
c,Decoded d
d,Decoded e
e,Decoded f
f) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c, forall a. Codec a => Decoded a -> a
encode Decoded d
d, forall a. Codec a => Decoded a -> a
encode Decoded e
e, forall a. Codec a => Decoded a -> a
encode Decoded f
f)
instance (Codec a, Codec b, Codec c, Codec d, Codec e, Codec f, Codec g) => Codec (a,b,c,d,e,f,g) where
type Decoded (a,b,c,d,e,f,g) = (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f, Decoded g)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution
-> (a, b, c, d, e, f, g) -> f (Decoded (a, b, c, d, e, f, g))
decode Solution
s (a
a,b
b,c
c,d
d,e
e,f
f,g
g) = (,,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s d
d forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s e
e forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s f
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s g
g
encode :: Decoded (a, b, c, d, e, f, g) -> (a, b, c, d, e, f, g)
encode (Decoded a
a,Decoded b
b,Decoded c
c,Decoded d
d,Decoded e
e,Decoded f
f,Decoded g
g) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c, forall a. Codec a => Decoded a -> a
encode Decoded d
d, forall a. Codec a => Decoded a -> a
encode Decoded e
e, forall a. Codec a => Decoded a -> a
encode Decoded f
f, forall a. Codec a => Decoded a -> a
encode Decoded g
g)
instance (Codec a, Codec b, Codec c, Codec d, Codec e, Codec f, Codec g, Codec h) => Codec (a,b,c,d,e,f,g,h) where
type Decoded (a,b,c,d,e,f,g,h) = (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f, Decoded g, Decoded h)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution
-> (a, b, c, d, e, f, g, h) -> f (Decoded (a, b, c, d, e, f, g, h))
decode Solution
s (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h) = (,,,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s c
c forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s d
d forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s e
e forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s f
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s g
g forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s h
h
encode :: Decoded (a, b, c, d, e, f, g, h) -> (a, b, c, d, e, f, g, h)
encode (Decoded a
a,Decoded b
b,Decoded c
c,Decoded d
d,Decoded e
e,Decoded f
f,Decoded g
g,Decoded h
h) = (forall a. Codec a => Decoded a -> a
encode Decoded a
a, forall a. Codec a => Decoded a -> a
encode Decoded b
b, forall a. Codec a => Decoded a -> a
encode Decoded c
c, forall a. Codec a => Decoded a -> a
encode Decoded d
d, forall a. Codec a => Decoded a -> a
encode Decoded e
e, forall a. Codec a => Decoded a -> a
encode Decoded f
f, forall a. Codec a => Decoded a -> a
encode Decoded g
g, forall a. Codec a => Decoded a -> a
encode Decoded h
h)
instance Codec a => Codec [a] where
type Decoded [a] = [Decoded a]
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> [a] -> f (Decoded [a])
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded [a] -> [a]
encode = forall a b. (a -> b) -> [a] -> [b]
map forall a. Codec a => Decoded a -> a
encode
instance (Ix i, Codec e) => Codec (Array i e) where
type Decoded (Array i e) = Array i (Decoded e)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Array i e -> f (Decoded (Array i e))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (Array i e) -> Array i e
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance (Codec a, Codec b) => Codec (Either a b) where
type Decoded (Either a b) = Either (Decoded a) (Decoded b)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Either a b -> f (Decoded (Either a b))
decode Solution
s (Left a
a) = forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s a
a
decode Solution
s (Right b
b) = forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode Solution
s b
b
encode :: Decoded (Either a b) -> Either a b
encode (Left Decoded a
a) = forall a b. a -> Either a b
Left (forall a. Codec a => Decoded a -> a
encode Decoded a
a)
encode (Right Decoded b
b) = forall a b. b -> Either a b
Right (forall a. Codec a => Decoded a -> a
encode Decoded b
b)
instance Codec a => Codec (HashMap k a) where
type Decoded (HashMap k a) = HashMap k (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> HashMap k a -> f (Decoded (HashMap k a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (HashMap k a) -> HashMap k a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (IntMap a) where
type Decoded (IntMap a) = IntMap (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> IntMap a -> f (Decoded (IntMap a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (IntMap a) -> IntMap a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Map k a) where
type Decoded (Map k a) = Map k (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Map k a -> f (Decoded (Map k a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (Map k a) -> Map k a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Maybe a) where
type Decoded (Maybe a) = Maybe (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Maybe a -> f (Decoded (Maybe a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (Maybe a) -> Maybe a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Seq a) where
type Decoded (Seq a) = Seq (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Seq a -> f (Decoded (Seq a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (Seq a) -> Seq a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Tree a) where
type Decoded (Tree a) = Tree (Decoded a)
decode :: forall (f :: * -> *).
MonadPlus f =>
Solution -> Tree a -> f (Decoded (Tree a))
decode = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (f :: * -> *).
(Codec a, MonadPlus f) =>
Solution -> a -> f (Decoded a)
decode
encode :: Decoded (Tree a) -> Tree a
encode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Codec a => Decoded a -> a
encode