{-# 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 :: Solution -> a -> Maybe (Decoded a)
encode :: Decoded a -> a
instance Codec Literal where
type Decoded Literal = Bool
decode :: Solution -> Literal -> Maybe (Decoded Literal)
decode Solution
s Literal
a = case Solution -> Literal -> Maybe Bool
solutionLiteral Solution
s Literal
a of
sol :: Maybe Bool
sol@(Just Bool
_) -> Maybe Bool
Maybe (Decoded Literal)
sol
Maybe Bool
Nothing
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
| Bool
otherwise -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
where
i :: Int
i = Literal -> Int
literalId 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 :: Solution -> () -> Maybe (Decoded ())
decode Solution
_ () = () -> Maybe ()
forall a. a -> Maybe a
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 :: Solution -> (a, b) -> Maybe (Decoded (a, b))
decode Solution
s (a
a,b
b) = (,) (Decoded a -> Decoded b -> (Decoded a, Decoded b))
-> Maybe (Decoded a) -> Maybe (Decoded b -> (Decoded a, Decoded b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe (Decoded b -> (Decoded a, Decoded b))
-> Maybe (Decoded b) -> Maybe (Decoded a, Decoded b)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b
encode :: Decoded (a, b) -> (a, b)
encode (Decoded a
a,Decoded b
b) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
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 :: Solution -> (a, b, c) -> Maybe (Decoded (a, b, c))
decode Solution
s (a
a,b
b,c
c) = (,,) (Decoded a
-> Decoded b -> Decoded c -> (Decoded a, Decoded b, Decoded c))
-> Maybe (Decoded a)
-> Maybe
(Decoded b -> Decoded c -> (Decoded a, Decoded b, Decoded c))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe (Decoded b -> Decoded c -> (Decoded a, Decoded b, Decoded c))
-> Maybe (Decoded b)
-> Maybe (Decoded c -> (Decoded a, Decoded b, Decoded c))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe (Decoded c -> (Decoded a, Decoded b, Decoded c))
-> Maybe (Decoded c) -> Maybe (Decoded a, Decoded b, Decoded c)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
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 :: Solution -> (a, b, c, d) -> Maybe (Decoded (a, b, c, d))
decode Solution
s (a
a,b
b,c
c,d
d) = (,,,) (Decoded a
-> Decoded b
-> Decoded c
-> Decoded d
-> (Decoded a, Decoded b, Decoded c, Decoded d))
-> Maybe (Decoded a)
-> Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> (Decoded a, Decoded b, Decoded c, Decoded d))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> (Decoded a, Decoded b, Decoded c, Decoded d))
-> Maybe (Decoded b)
-> Maybe
(Decoded c
-> Decoded d -> (Decoded a, Decoded b, Decoded c, Decoded d))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe
(Decoded c
-> Decoded d -> (Decoded a, Decoded b, Decoded c, Decoded d))
-> Maybe (Decoded c)
-> Maybe
(Decoded d -> (Decoded a, Decoded b, Decoded c, Decoded d))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s c
c Maybe (Decoded d -> (Decoded a, Decoded b, Decoded c, Decoded d))
-> Maybe (Decoded d)
-> Maybe (Decoded a, Decoded b, Decoded c, Decoded d)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> d -> Maybe (Decoded d)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
forall a. Codec a => Decoded a -> a
encode Decoded c
c, Decoded d -> d
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 :: Solution -> (a, b, c, d, e) -> Maybe (Decoded (a, b, c, d, e))
decode Solution
s (a
a,b
b,c
c,d
d,e
e) = (,,,,) (Decoded a
-> Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
-> Maybe (Decoded a)
-> Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
-> Maybe (Decoded b)
-> Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
-> Maybe (Decoded c)
-> Maybe
(Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s c
c Maybe
(Decoded d
-> Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
-> Maybe (Decoded d)
-> Maybe
(Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> d -> Maybe (Decoded d)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s d
d Maybe
(Decoded e
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e))
-> Maybe (Decoded e)
-> Maybe (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> e -> Maybe (Decoded e)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
forall a. Codec a => Decoded a -> a
encode Decoded c
c, Decoded d -> d
forall a. Codec a => Decoded a -> a
encode Decoded d
d, Decoded e -> e
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 :: Solution
-> (a, b, c, d, e, f) -> Maybe (Decoded (a, b, c, d, e, f))
decode Solution
s (a
a,b
b,c
c,d
d,e
e,f
f) = (,,,,,) (Decoded a
-> Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded a)
-> Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded b)
-> Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded c)
-> Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s c
c Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded d)
-> Maybe
(Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> d -> Maybe (Decoded d)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s d
d Maybe
(Decoded e
-> Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded e)
-> Maybe
(Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> e -> Maybe (Decoded e)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s e
e Maybe
(Decoded f
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f))
-> Maybe (Decoded f)
-> Maybe
(Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> f -> Maybe (Decoded f)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
forall a. Codec a => Decoded a -> a
encode Decoded c
c, Decoded d -> d
forall a. Codec a => Decoded a -> a
encode Decoded d
d, Decoded e -> e
forall a. Codec a => Decoded a -> a
encode Decoded e
e, Decoded f -> f
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 :: Solution
-> (a, b, c, d, e, f, g) -> Maybe (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) = (,,,,,,) (Decoded a
-> Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded a)
-> Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded b)
-> Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded c)
-> Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s c
c Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded d)
-> Maybe
(Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> d -> Maybe (Decoded d)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s d
d Maybe
(Decoded e
-> Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded e)
-> Maybe
(Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> e -> Maybe (Decoded e)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s e
e Maybe
(Decoded f
-> Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded f)
-> Maybe
(Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> f -> Maybe (Decoded f)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s f
f Maybe
(Decoded g
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g))
-> Maybe (Decoded g)
-> Maybe
(Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f,
Decoded g)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> g -> Maybe (Decoded g)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
forall a. Codec a => Decoded a -> a
encode Decoded c
c, Decoded d -> d
forall a. Codec a => Decoded a -> a
encode Decoded d
d, Decoded e -> e
forall a. Codec a => Decoded a -> a
encode Decoded e
e, Decoded f -> f
forall a. Codec a => Decoded a -> a
encode Decoded f
f, Decoded g -> g
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 :: Solution
-> (a, b, c, d, e, f, g, h)
-> Maybe (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) = (,,,,,,,) (Decoded a
-> Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded a)
-> Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a Maybe
(Decoded b
-> Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded b)
-> Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b Maybe
(Decoded c
-> Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded c)
-> Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> c -> Maybe (Decoded c)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s c
c Maybe
(Decoded d
-> Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded d)
-> Maybe
(Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> d -> Maybe (Decoded d)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s d
d Maybe
(Decoded e
-> Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded e)
-> Maybe
(Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> e -> Maybe (Decoded e)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s e
e Maybe
(Decoded f
-> Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded f)
-> Maybe
(Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> f -> Maybe (Decoded f)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s f
f Maybe
(Decoded g
-> Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded g)
-> Maybe
(Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> g -> Maybe (Decoded g)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s g
g Maybe
(Decoded h
-> (Decoded a, Decoded b, Decoded c, Decoded d, Decoded e,
Decoded f, Decoded g, Decoded h))
-> Maybe (Decoded h)
-> Maybe
(Decoded a, Decoded b, Decoded c, Decoded d, Decoded e, Decoded f,
Decoded g, Decoded h)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Solution -> h -> Maybe (Decoded h)
forall a. Codec a => Solution -> a -> Maybe (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) = (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a, Decoded b -> b
forall a. Codec a => Decoded a -> a
encode Decoded b
b, Decoded c -> c
forall a. Codec a => Decoded a -> a
encode Decoded c
c, Decoded d -> d
forall a. Codec a => Decoded a -> a
encode Decoded d
d, Decoded e -> e
forall a. Codec a => Decoded a -> a
encode Decoded e
e, Decoded f -> f
forall a. Codec a => Decoded a -> a
encode Decoded f
f, Decoded g -> g
forall a. Codec a => Decoded a -> a
encode Decoded g
g, Decoded h -> h
forall a. Codec a => Decoded a -> a
encode Decoded h
h)
instance Codec a => Codec [a] where
type Decoded [a] = [Decoded a]
decode :: Solution -> [a] -> Maybe (Decoded [a])
decode = (a -> Maybe (Decoded a)) -> [a] -> Maybe [Decoded a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((a -> Maybe (Decoded a)) -> [a] -> Maybe [Decoded a])
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> [a]
-> Maybe [Decoded a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded [a] -> [a]
encode = (Decoded a -> a) -> [Decoded a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Decoded a -> a
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 :: Solution -> Array i e -> Maybe (Decoded (Array i e))
decode = (e -> Maybe (Decoded e))
-> Array i e -> Maybe (Array i (Decoded e))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Array i a -> m (Array i b)
mapM ((e -> Maybe (Decoded e))
-> Array i e -> Maybe (Array i (Decoded e)))
-> (Solution -> e -> Maybe (Decoded e))
-> Solution
-> Array i e
-> Maybe (Array i (Decoded e))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> e -> Maybe (Decoded e)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (Array i e) -> Array i e
encode = (Decoded e -> e) -> Array i (Decoded e) -> Array i e
forall a b. (a -> b) -> Array i a -> Array i b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded e -> e
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 :: Solution -> Either a b -> Maybe (Decoded (Either a b))
decode Solution
s (Left a
a) = Decoded a -> Either (Decoded a) (Decoded b)
forall a b. a -> Either a b
Left (Decoded a -> Either (Decoded a) (Decoded b))
-> Maybe (Decoded a) -> Maybe (Either (Decoded a) (Decoded b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s a
a
decode Solution
s (Right b
b) = Decoded b -> Either (Decoded a) (Decoded b)
forall a b. b -> Either a b
Right (Decoded b -> Either (Decoded a) (Decoded b))
-> Maybe (Decoded b) -> Maybe (Either (Decoded a) (Decoded b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Solution -> b -> Maybe (Decoded b)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode Solution
s b
b
encode :: Decoded (Either a b) -> Either a b
encode (Left Decoded a
a) = a -> Either a b
forall a b. a -> Either a b
Left (Decoded a -> a
forall a. Codec a => Decoded a -> a
encode Decoded a
a)
encode (Right Decoded b
b) = b -> Either a b
forall a b. b -> Either a b
Right (Decoded b -> b
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 :: Solution -> HashMap k a -> Maybe (Decoded (HashMap k a))
decode = (a -> Maybe (Decoded a))
-> HashMap k a -> Maybe (HashMap k (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HashMap k a -> m (HashMap k b)
mapM ((a -> Maybe (Decoded a))
-> HashMap k a -> Maybe (HashMap k (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> HashMap k a
-> Maybe (HashMap k (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (HashMap k a) -> HashMap k a
encode = (Decoded a -> a) -> HashMap k (Decoded a) -> HashMap k a
forall a b. (a -> b) -> HashMap k a -> HashMap k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (IntMap a) where
type Decoded (IntMap a) = IntMap (Decoded a)
decode :: Solution -> IntMap a -> Maybe (Decoded (IntMap a))
decode = (a -> Maybe (Decoded a)) -> IntMap a -> Maybe (IntMap (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> IntMap a -> m (IntMap b)
mapM ((a -> Maybe (Decoded a))
-> IntMap a -> Maybe (IntMap (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> IntMap a
-> Maybe (IntMap (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (IntMap a) -> IntMap a
encode = (Decoded a -> a) -> IntMap (Decoded a) -> IntMap a
forall a b. (a -> b) -> IntMap a -> IntMap b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
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 :: Solution -> Map k a -> Maybe (Decoded (Map k a))
decode = (a -> Maybe (Decoded a)) -> Map k a -> Maybe (Map k (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Map k a -> m (Map k b)
mapM ((a -> Maybe (Decoded a)) -> Map k a -> Maybe (Map k (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> Map k a
-> Maybe (Map k (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (Map k a) -> Map k a
encode = (Decoded a -> a) -> Map k (Decoded a) -> Map k a
forall a b. (a -> b) -> Map k a -> Map k b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Maybe a) where
type Decoded (Maybe a) = Maybe (Decoded a)
decode :: Solution -> Maybe a -> Maybe (Decoded (Maybe a))
decode = (a -> Maybe (Decoded a)) -> Maybe a -> Maybe (Maybe (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Maybe a -> m (Maybe b)
mapM ((a -> Maybe (Decoded a)) -> Maybe a -> Maybe (Maybe (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> Maybe a
-> Maybe (Maybe (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (Maybe a) -> Maybe a
encode = (Decoded a -> a) -> Maybe (Decoded a) -> Maybe a
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Seq a) where
type Decoded (Seq a) = Seq (Decoded a)
decode :: Solution -> Seq a -> Maybe (Decoded (Seq a))
decode = (a -> Maybe (Decoded a)) -> Seq a -> Maybe (Seq (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Seq a -> m (Seq b)
mapM ((a -> Maybe (Decoded a)) -> Seq a -> Maybe (Seq (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> Seq a
-> Maybe (Seq (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (Seq a) -> Seq a
encode = (Decoded a -> a) -> Seq (Decoded a) -> Seq a
forall a b. (a -> b) -> Seq a -> Seq b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
forall a. Codec a => Decoded a -> a
encode
instance Codec a => Codec (Tree a) where
type Decoded (Tree a) = Tree (Decoded a)
decode :: Solution -> Tree a -> Maybe (Decoded (Tree a))
decode = (a -> Maybe (Decoded a)) -> Tree a -> Maybe (Tree (Decoded a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Tree a -> m (Tree b)
mapM ((a -> Maybe (Decoded a)) -> Tree a -> Maybe (Tree (Decoded a)))
-> (Solution -> a -> Maybe (Decoded a))
-> Solution
-> Tree a
-> Maybe (Tree (Decoded a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> a -> Maybe (Decoded a)
forall a. Codec a => Solution -> a -> Maybe (Decoded a)
decode
encode :: Decoded (Tree a) -> Tree a
encode = (Decoded a -> a) -> Tree (Decoded a) -> Tree a
forall a b. (a -> b) -> Tree a -> Tree b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Decoded a -> a
forall a. Codec a => Decoded a -> a
encode