{-# LANGUAGE GADTs           #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators   #-}
--------------------------------------------------------------------------------
-- |
-- Module      :  Data.Comp.Equality
-- Copyright   :  (c) 2010-2011 Patrick Bahr
-- License     :  BSD3
-- Maintainer  :  Patrick Bahr <paba@diku.dk>
-- Stability   :  experimental
-- Portability :  non-portable (GHC Extensions)
--
-- This module defines equality for signatures, which lifts to equality for
-- terms and contexts.
--
--------------------------------------------------------------------------------
module Data.Comp.Equality
    (
     EqF(..),
     eqMod,
    ) where

import Control.Monad hiding (mapM_)
import Data.Comp.Derive.Equality
import Data.Comp.Derive.Utils
import Data.Comp.Ops
import Data.Comp.Term
import Data.Foldable
import Prelude hiding (all, mapM_)

-- instance (EqF f, Eq p) => EqF (f :*: p) where
--    eqF (v1 :*: p1) (v2 :*: p2) = p1 == p2 && v1 `eqF` v2

{-|
  From an 'EqF' functor an 'Eq' instance of the corresponding
  term type can be derived.
-}
instance (EqF f, Eq a) => Eq (Cxt h f a) where
    == :: Cxt h f a -> Cxt h f a -> Bool
(==) = Cxt h f a -> Cxt h f a -> Bool
forall (f :: * -> *) a. (EqF f, Eq a) => f a -> f a -> Bool
eqF

instance (EqF f) => EqF (Cxt h f) where
    eqF :: Cxt h f a -> Cxt h f a -> Bool
eqF (Term f (Cxt h f a)
e1) (Term f (Cxt h f a)
e2) = f (Cxt h f a)
e1 f (Cxt h f a) -> f (Cxt h f a) -> Bool
forall (f :: * -> *) a. (EqF f, Eq a) => f a -> f a -> Bool
`eqF` f (Cxt h f a)
e2
    eqF (Hole a
h1) (Hole a
h2) = a
h1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
h2
    eqF Cxt h f a
_ Cxt h f a
_ = Bool
False

{-|
  'EqF' is propagated through sums.
-}
instance (EqF f, EqF g) => EqF (f :+: g) where
    eqF :: (:+:) f g a -> (:+:) f g a -> Bool
eqF (Inl f a
x) (Inl f a
y) = f a -> f a -> Bool
forall (f :: * -> *) a. (EqF f, Eq a) => f a -> f a -> Bool
eqF f a
x f a
y
    eqF (Inr g a
x) (Inr g a
y) = g a -> g a -> Bool
forall (f :: * -> *) a. (EqF f, Eq a) => f a -> f a -> Bool
eqF g a
x g a
y
    eqF (:+:) f g a
_ (:+:) f g a
_ = Bool
False

{-| This function implements equality of values of type @f a@ modulo
the equality of @a@ itself. If two functorial values are equal in this
sense, 'eqMod' returns a 'Just' value containing a list of pairs
consisting of corresponding components of the two functorial
values. -}
eqMod :: (EqF f, Functor f, Foldable f) => f a -> f b -> Maybe [(a,b)]
eqMod :: f a -> f b -> Maybe [(a, b)]
eqMod f a
s f b
t
    | f a -> f ()
forall b. f b -> f ()
unit f a
s f () -> f () -> Bool
forall (f :: * -> *) a. (EqF f, Eq a) => f a -> f a -> Bool
`eqF` f b -> f ()
forall b. f b -> f ()
unit' f b
t = [(a, b)] -> Maybe [(a, b)]
forall a. a -> Maybe a
Just [(a, b)]
args
    | Bool
otherwise = Maybe [(a, b)]
forall a. Maybe a
Nothing
    where unit :: f b -> f ()
unit = (b -> ()) -> f b -> f ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> b -> ()
forall a b. a -> b -> a
const ())
          unit' :: f b -> f ()
unit' = (b -> ()) -> f b -> f ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> b -> ()
forall a b. a -> b -> a
const ())
          args :: [(a, b)]
args = f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f a
s [a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` f b -> [b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f b
t

$(derive [makeEqF] $ [''Maybe, ''[]] ++ tupleTypes 2 10)