{-# LANGUAGE CPP #-}
#ifdef __GLASGOW_HASKELL__
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE Trustworthy #-}
#endif

-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2012 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- The problem with locally nameless approaches is that original names are
-- often useful for error reporting, or to allow for the user in an interactive
-- theorem prover to convey some hint about the domain. A @'Name' n b@ is a value
-- @b@ supplemented with a (discardable) name that may be useful for error
-- reporting purposes. In particular, this name does not participate in
-- comparisons for equality.
--
-- This module is /not/ exported from "Bound" by default. You need to explicitly
-- import it, due to the fact that 'Name' is a pretty common term in other
-- people's code.
----------------------------------------------------------------------------
module Bound.Name
  ( Name(..)
  , _Name
  , name
  , abstractName
  , abstract1Name
  , abstractEitherName
  , instantiateName
  , instantiate1Name
  , instantiateEitherName
  ) where

import Bound.Scope
import Bound.Var
import Control.Comonad
import Control.DeepSeq
import Control.Monad (liftM, liftM2)
import Data.Bifunctor
import Data.Bifoldable
import qualified Data.Binary as Binary
import Data.Binary (Binary)
import Data.Bitraversable
import Data.Bytes.Serial
import Data.Functor.Classes
#ifdef __GLASGOW_HASKELL__
import Data.Data
import GHC.Generics
#endif
import Data.Hashable (Hashable(..))
import Data.Hashable.Lifted (Hashable1(..), Hashable2(..))
import Data.Profunctor
import qualified Data.Serialize as Serialize
import Data.Serialize (Serialize)

-------------------------------------------------------------------------------
-- Names
-------------------------------------------------------------------------------

-- |
-- We track the choice of 'Name' @n@ as a forgettable property that does /not/ affect
-- the result of ('==') or 'compare'.
--
-- To compare names rather than values, use @('Data.Function.on' 'compare' 'name')@ instead.
data Name n b = Name n b deriving
  ( Int -> Name n b -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall n b. (Show n, Show b) => Int -> Name n b -> ShowS
forall n b. (Show n, Show b) => [Name n b] -> ShowS
forall n b. (Show n, Show b) => Name n b -> String
showList :: [Name n b] -> ShowS
$cshowList :: forall n b. (Show n, Show b) => [Name n b] -> ShowS
show :: Name n b -> String
$cshow :: forall n b. (Show n, Show b) => Name n b -> String
showsPrec :: Int -> Name n b -> ShowS
$cshowsPrec :: forall n b. (Show n, Show b) => Int -> Name n b -> ShowS
Show
  , ReadPrec [Name n b]
ReadPrec (Name n b)
ReadS [Name n b]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall n b. (Read n, Read b) => ReadPrec [Name n b]
forall n b. (Read n, Read b) => ReadPrec (Name n b)
forall n b. (Read n, Read b) => Int -> ReadS (Name n b)
forall n b. (Read n, Read b) => ReadS [Name n b]
readListPrec :: ReadPrec [Name n b]
$creadListPrec :: forall n b. (Read n, Read b) => ReadPrec [Name n b]
readPrec :: ReadPrec (Name n b)
$creadPrec :: forall n b. (Read n, Read b) => ReadPrec (Name n b)
readList :: ReadS [Name n b]
$creadList :: forall n b. (Read n, Read b) => ReadS [Name n b]
readsPrec :: Int -> ReadS (Name n b)
$creadsPrec :: forall n b. (Read n, Read b) => Int -> ReadS (Name n b)
Read
#ifdef __GLASGOW_HASKELL__
  , Name n b -> DataType
Name n b -> Constr
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall {n} {b}. (Data n, Data b) => Typeable (Name n b)
forall n b. (Data n, Data b) => Name n b -> DataType
forall n b. (Data n, Data b) => Name n b -> Constr
forall n b.
(Data n, Data b) =>
(forall b. Data b => b -> b) -> Name n b -> Name n b
forall n b u.
(Data n, Data b) =>
Int -> (forall d. Data d => d -> u) -> Name n b -> u
forall n b u.
(Data n, Data b) =>
(forall d. Data d => d -> u) -> Name n b -> [u]
forall n b r r'.
(Data n, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
forall n b r r'.
(Data n, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
forall n b (m :: * -> *).
(Data n, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
forall n b (m :: * -> *).
(Data n, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
forall n b (c :: * -> *).
(Data n, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name n b)
forall n b (c :: * -> *).
(Data n, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name n b -> c (Name n b)
forall n b (t :: * -> *) (c :: * -> *).
(Data n, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Name n b))
forall n b (t :: * -> * -> *) (c :: * -> *).
(Data n, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name n b))
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name n b)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name n b -> c (Name n b)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name n b))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
$cgmapMo :: forall n b (m :: * -> *).
(Data n, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
$cgmapMp :: forall n b (m :: * -> *).
(Data n, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
$cgmapM :: forall n b (m :: * -> *).
(Data n, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> Name n b -> m (Name n b)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Name n b -> u
$cgmapQi :: forall n b u.
(Data n, Data b) =>
Int -> (forall d. Data d => d -> u) -> Name n b -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Name n b -> [u]
$cgmapQ :: forall n b u.
(Data n, Data b) =>
(forall d. Data d => d -> u) -> Name n b -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
$cgmapQr :: forall n b r r'.
(Data n, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
$cgmapQl :: forall n b r r'.
(Data n, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name n b -> r
gmapT :: (forall b. Data b => b -> b) -> Name n b -> Name n b
$cgmapT :: forall n b.
(Data n, Data b) =>
(forall b. Data b => b -> b) -> Name n b -> Name n b
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name n b))
$cdataCast2 :: forall n b (t :: * -> * -> *) (c :: * -> *).
(Data n, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name n b))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Name n b))
$cdataCast1 :: forall n b (t :: * -> *) (c :: * -> *).
(Data n, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Name n b))
dataTypeOf :: Name n b -> DataType
$cdataTypeOf :: forall n b. (Data n, Data b) => Name n b -> DataType
toConstr :: Name n b -> Constr
$ctoConstr :: forall n b. (Data n, Data b) => Name n b -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name n b)
$cgunfold :: forall n b (c :: * -> *).
(Data n, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name n b)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name n b -> c (Name n b)
$cgfoldl :: forall n b (c :: * -> *).
(Data n, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name n b -> c (Name n b)
Data
  , forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall n b x. Rep (Name n b) x -> Name n b
forall n b x. Name n b -> Rep (Name n b) x
$cto :: forall n b x. Rep (Name n b) x -> Name n b
$cfrom :: forall n b x. Name n b -> Rep (Name n b) x
Generic
  , forall n a. Rep1 (Name n) a -> Name n a
forall n a. Name n a -> Rep1 (Name n) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall n a. Rep1 (Name n) a -> Name n a
$cfrom1 :: forall n a. Name n a -> Rep1 (Name n) a
Generic1
#endif
  )

-- | Extract the 'name'.
name :: Name n b -> n
name :: forall n b. Name n b -> n
name (Name n
n b
_) = n
n
{-# INLINE name #-}

-- |
--
-- This provides an 'Iso' that can be used to access the parts of a 'Name'.
--
-- @
-- '_Name' :: Iso ('Name' n a) ('Name' m b) (n, a) (m, b)
-- @
_Name :: (Profunctor p, Functor f) => p (n, a) (f (m,b)) -> p (Name n a) (f (Name m b))
_Name :: forall (p :: * -> * -> *) (f :: * -> *) n a m b.
(Profunctor p, Functor f) =>
p (n, a) (f (m, b)) -> p (Name n a) (f (Name m b))
_Name = forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (\(Name n
n a
a) -> (n
n, a
a)) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall n b. n -> b -> Name n b
Name))
{-# INLINE _Name #-}

-------------------------------------------------------------------------------
-- Instances
-------------------------------------------------------------------------------

instance Eq b => Eq (Name n b) where
  Name n
_ b
a == :: Name n b -> Name n b -> Bool
== Name n
_ b
b = b
a forall a. Eq a => a -> a -> Bool
== b
b
  {-# INLINE (==) #-}

instance Hashable2 Name where
  liftHashWithSalt2 :: forall a b.
(Int -> a -> Int) -> (Int -> b -> Int) -> Int -> Name a b -> Int
liftHashWithSalt2 Int -> a -> Int
_ Int -> b -> Int
h Int
s (Name a
_ b
a) = Int -> b -> Int
h Int
s b
a
  {-# INLINE liftHashWithSalt2 #-}

instance Hashable1 (Name n) where
  liftHashWithSalt :: forall a. (Int -> a -> Int) -> Int -> Name n a -> Int
liftHashWithSalt Int -> a -> Int
h Int
s (Name n
_ a
a) = Int -> a -> Int
h Int
s a
a
  {-# INLINE liftHashWithSalt #-}

instance Hashable a => Hashable (Name n a) where
  hashWithSalt :: Int -> Name n a -> Int
hashWithSalt Int
m (Name n
_ a
a) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
m a
a
  {-# INLINE hashWithSalt #-}

instance Ord b => Ord (Name n b) where
  Name n
_ b
a compare :: Name n b -> Name n b -> Ordering
`compare` Name n
_ b
b = forall a. Ord a => a -> a -> Ordering
compare b
a b
b
  {-# INLINE compare #-}

instance Functor (Name n) where
  fmap :: forall a b. (a -> b) -> Name n a -> Name n b
fmap a -> b
f (Name n
n a
a) = forall n b. n -> b -> Name n b
Name n
n (a -> b
f a
a)
  {-# INLINE fmap #-}

instance Foldable (Name n) where
  foldMap :: forall m a. Monoid m => (a -> m) -> Name n a -> m
foldMap a -> m
f (Name n
_ a
a) = a -> m
f a
a
  {-# INLINE foldMap #-}

instance Traversable (Name n) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Name n a -> f (Name n b)
traverse a -> f b
f (Name n
n a
a) = forall n b. n -> b -> Name n b
Name n
n forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
  {-# INLINE traverse #-}

instance Bifunctor Name where
  bimap :: forall a b c d. (a -> b) -> (c -> d) -> Name a c -> Name b d
bimap a -> b
f c -> d
g (Name a
n c
a) = forall n b. n -> b -> Name n b
Name (a -> b
f a
n) (c -> d
g c
a)
  {-# INLINE bimap #-}

instance Bifoldable Name where
  bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> Name a b -> m
bifoldMap a -> m
f b -> m
g (Name a
n b
a) = a -> m
f a
n forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
a
  {-# INLINE bifoldMap #-}

instance Bitraversable Name where
  bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> Name a b -> f (Name c d)
bitraverse a -> f c
f b -> f d
g (Name a
n b
a) = forall n b. n -> b -> Name n b
Name forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
n forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> b -> f d
g b
a
  {-# INLINE bitraverse #-}

instance Comonad (Name n) where
  extract :: forall a. Name n a -> a
extract (Name n
_ a
b) = a
b
  {-# INLINE extract #-}
  extend :: forall a b. (Name n a -> b) -> Name n a -> Name n b
extend Name n a -> b
f w :: Name n a
w@(Name n
n a
_) = forall n b. n -> b -> Name n b
Name n
n (Name n a -> b
f Name n a
w)
  {-# INLINE extend #-}

instance Eq2 Name where
  liftEq2 :: forall a b c d.
(a -> b -> Bool)
-> (c -> d -> Bool) -> Name a c -> Name b d -> Bool
liftEq2 a -> b -> Bool
_ c -> d -> Bool
g (Name a
_ c
b) (Name b
_ d
d) = c -> d -> Bool
g c
b d
d

instance Ord2 Name where
  liftCompare2 :: forall a b c d.
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> Name a c -> Name b d -> Ordering
liftCompare2 a -> b -> Ordering
_ c -> d -> Ordering
g (Name a
_ c
b) (Name b
_ d
d) = c -> d -> Ordering
g c
b d
d

instance Show2 Name where
  liftShowsPrec2 :: forall a b.
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> Name a b
-> ShowS
liftShowsPrec2 Int -> a -> ShowS
f [a] -> ShowS
_ Int -> b -> ShowS
h [b] -> ShowS
_ Int
d (Name a
a b
b) = forall a b.
(Int -> a -> ShowS)
-> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
showsBinaryWith Int -> a -> ShowS
f Int -> b -> ShowS
h String
"Name" Int
d a
a b
b

instance Read2 Name where
  liftReadsPrec2 :: forall a b.
(Int -> ReadS a)
-> ReadS [a]
-> (Int -> ReadS b)
-> ReadS [b]
-> Int
-> ReadS (Name a b)
liftReadsPrec2 Int -> ReadS a
f ReadS [a]
_ Int -> ReadS b
h ReadS [b]
_ = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$ forall a b t.
(Int -> ReadS a)
-> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
readsBinaryWith Int -> ReadS a
f Int -> ReadS b
h String
"Name" forall n b. n -> b -> Name n b
Name

instance Eq1 (Name b) where
  liftEq :: forall a b. (a -> b -> Bool) -> Name b a -> Name b b -> Bool
liftEq a -> b -> Bool
f (Name b
_ a
b) (Name b
_ b
d) = a -> b -> Bool
f a
b b
d

instance Ord1 (Name b) where
  liftCompare :: forall a b.
(a -> b -> Ordering) -> Name b a -> Name b b -> Ordering
liftCompare a -> b -> Ordering
f (Name b
_ a
b) (Name b
_ b
d) = a -> b -> Ordering
f a
b b
d

instance Show b => Show1 (Name b) where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Name b a -> ShowS
liftShowsPrec Int -> a -> ShowS
f [a] -> ShowS
_ Int
d (Name b
a a
b) = forall a b.
(Int -> a -> ShowS)
-> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
showsBinaryWith forall a. Show a => Int -> a -> ShowS
showsPrec Int -> a -> ShowS
f String
"Name" Int
d b
a a
b

instance Read b => Read1 (Name b) where
  liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Name b a)
liftReadsPrec Int -> ReadS a
f ReadS [a]
_ = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$ forall a b t.
(Int -> ReadS a)
-> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
readsBinaryWith forall a. Read a => Int -> ReadS a
readsPrec Int -> ReadS a
f String
"Name" forall n b. n -> b -> Name n b
Name

instance Serial2 Name where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> Name a b -> m ()
serializeWith2 a -> m ()
pb b -> m ()
pf (Name a
b b
a) = a -> m ()
pb a
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
pf b
a
  {-# INLINE serializeWith2 #-}

  deserializeWith2 :: forall (m :: * -> *) a b. MonadGet m => m a -> m b -> m (Name a b)
deserializeWith2 = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall n b. n -> b -> Name n b
Name
  {-# INLINE deserializeWith2 #-}

instance Serial b => Serial1 (Name b) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> Name b a -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  {-# INLINE serializeWith #-}
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Name b a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
  {-# INLINE deserializeWith #-}

instance (Serial b, Serial a) => Serial (Name b a) where
  serialize :: forall (m :: * -> *). MonadPut m => Name b a -> m ()
serialize = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  {-# INLINE serialize #-}
  deserialize :: forall (m :: * -> *). MonadGet m => m (Name b a)
deserialize = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
  {-# INLINE deserialize #-}

instance (Binary b, Binary a) => Binary (Name b a) where
  put :: Name b a -> Put
put = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall t. Binary t => t -> Put
Binary.put forall t. Binary t => t -> Put
Binary.put
  get :: Get (Name b a)
get = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall t. Binary t => Get t
Binary.get forall t. Binary t => Get t
Binary.get

instance (Serialize b, Serialize a) => Serialize (Name b a) where
  put :: Putter (Name b a)
put = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall t. Serialize t => Putter t
Serialize.put forall t. Serialize t => Putter t
Serialize.put
  get :: Get (Name b a)
get = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall t. Serialize t => Get t
Serialize.get forall t. Serialize t => Get t
Serialize.get

instance (NFData b, NFData a) => NFData (Name b a) where
  rnf :: Name b a -> ()
rnf (Name b
a a
b) = forall a. NFData a => a -> ()
rnf b
a seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
b

-------------------------------------------------------------------------------
-- Abstraction
-------------------------------------------------------------------------------

-- | Abstraction, capturing named bound variables.
abstractName :: Monad f => (a -> Maybe b) -> f a -> Scope (Name a b) f a
abstractName :: forall (f :: * -> *) a b.
Monad f =>
(a -> Maybe b) -> f a -> Scope (Name a b) f a
abstractName a -> Maybe b
f f a
t = forall b (f :: * -> *) a. f (Var b (f a)) -> Scope b f a
Scope (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall {m :: * -> *}. Monad m => a -> Var (Name a b) (m a)
k f a
t) where
  k :: a -> Var (Name a b) (m a)
k a
a = case a -> Maybe b
f a
a of
    Just b
b  -> forall b a. b -> Var b a
B (forall n b. n -> b -> Name n b
Name a
a b
b)
    Maybe b
Nothing -> forall b a. a -> Var b a
F (forall (m :: * -> *) a. Monad m => a -> m a
return a
a)
{-# INLINE abstractName #-}

-- | Abstract over a single variable
abstract1Name :: (Monad f, Eq a) => a -> f a -> Scope (Name a ()) f a
abstract1Name :: forall (f :: * -> *) a.
(Monad f, Eq a) =>
a -> f a -> Scope (Name a ()) f a
abstract1Name a
a = forall (f :: * -> *) a b.
Monad f =>
(a -> Maybe b) -> f a -> Scope (Name a b) f a
abstractName (\a
b -> if a
a forall a. Eq a => a -> a -> Bool
== a
b then forall a. a -> Maybe a
Just () else forall a. Maybe a
Nothing)
{-# INLINE abstract1Name #-}

-- | Capture some free variables in an expression to yield
-- a 'Scope' with named bound variables. Optionally change the
-- types of the remaining free variables.
abstractEitherName :: Monad f => (a -> Either b c) -> f a -> Scope (Name a b) f c
abstractEitherName :: forall (f :: * -> *) a b c.
Monad f =>
(a -> Either b c) -> f a -> Scope (Name a b) f c
abstractEitherName a -> Either b c
f f a
e = forall b (f :: * -> *) a. f (Var b (f a)) -> Scope b f a
Scope (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall {m :: * -> *}. Monad m => a -> Var (Name a b) (m c)
k f a
e) where
  k :: a -> Var (Name a b) (m c)
k a
y = case a -> Either b c
f a
y of
    Left b
z -> forall b a. b -> Var b a
B (forall n b. n -> b -> Name n b
Name a
y b
z)
    Right c
y' -> forall b a. a -> Var b a
F (forall (m :: * -> *) a. Monad m => a -> m a
return c
y')

-------------------------------------------------------------------------------
-- Instantiation
-------------------------------------------------------------------------------

-- | Enter a scope, instantiating all bound variables, but discarding (comonadic)
-- meta data, like its name
instantiateName :: (Monad f, Comonad n) => (b -> f a) -> Scope (n b) f a -> f a
instantiateName :: forall (f :: * -> *) (n :: * -> *) b a.
(Monad f, Comonad n) =>
(b -> f a) -> Scope (n b) f a -> f a
instantiateName b -> f a
k Scope (n b) f a
e = forall b (f :: * -> *) a. Scope b f a -> f (Var b (f a))
unscope Scope (n b) f a
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Var (n b) (f a)
v -> case Var (n b) (f a)
v of
  B n b
b -> b -> f a
k (forall (w :: * -> *) a. Comonad w => w a -> a
extract n b
b)
  F f a
a -> f a
a
{-# INLINE instantiateName #-}

-- | Enter a 'Scope' that binds one (named) variable, instantiating it.
--
-- @'instantiate1Name' = 'instantiate1'@
instantiate1Name :: Monad f => f a -> Scope n f a -> f a
instantiate1Name :: forall (f :: * -> *) a n. Monad f => f a -> Scope n f a -> f a
instantiate1Name = forall (f :: * -> *) a n. Monad f => f a -> Scope n f a -> f a
instantiate1
{-# INLINE instantiate1Name #-}

instantiateEitherName :: (Monad f, Comonad n) => (Either b a -> f c) -> Scope (n b) f a -> f c
instantiateEitherName :: forall (f :: * -> *) (n :: * -> *) b a c.
(Monad f, Comonad n) =>
(Either b a -> f c) -> Scope (n b) f a -> f c
instantiateEitherName Either b a -> f c
k Scope (n b) f a
e = forall b (f :: * -> *) a. Scope b f a -> f (Var b (f a))
unscope Scope (n b) f a
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Var (n b) (f a)
v -> case Var (n b) (f a)
v of
  B n b
b -> Either b a -> f c
k (forall a b. a -> Either a b
Left (forall (w :: * -> *) a. Comonad w => w a -> a
extract n b
b))
  F f a
a -> f a
a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either b a -> f c
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right
{-# INLINE instantiateEitherName #-}