{-# LANGUAGE CPP #-}

{- |
   The HList library

   (C) 2004, Oleg Kiselyov, Ralf Laemmel, Keean Schupke

   Type-indexed products.
   The public interface is described in
   <Data-HList-CommonMain.html#t:TIP CommonMain#TIP>
-}

module Data.HList.TIP
  (module Data.HList.TIPtuple,
   module Data.HList.TIP) where


import Data.HList.HListPrelude
import Data.HList.FakePrelude
import Data.HList.HList
import Data.HList.Record
import Data.HList.HTypeIndexed ()
import Data.HList.TIPtuple
import Data.List (intercalate)
import Data.Array (Ix)
import Data.Semigroup (Semigroup)


#if __GLASGOW_HASKELL__ > 710
import Data.Coerce
#endif

import LensDefs

-- --------------------------------------------------------------------------
-- * The newtype for type-indexed products

-- | TIPs are like 'Record', except element \"i\" of the list \"l\"
-- has type @Tagged e_i e_i@
newtype TIP (l :: [*]) = TIP{forall (l :: [*]). TIP l -> HList l
unTIP:: HList l}

deriving instance Semigroup (HList a) => Semigroup (TIP a)
deriving instance Monoid (HList a) => Monoid (TIP a)
deriving instance Eq (HList a) => Eq (TIP a)
deriving instance (Ord (HList r)) => Ord (TIP r)
deriving instance (Ix (HList r)) => Ix (TIP r)
deriving instance (Bounded (HList r)) => Bounded (TIP r)


instance HMapOut (HShow `HComp` HUntag) l String => Show (TIP l) where
  showsPrec :: Int -> TIP l -> ShowS
showsPrec Int
_ (TIP HList l
l) = (String
"TIPH[" forall a. [a] -> [a] -> [a]
++)
                              forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. [a] -> [[a]] -> [a]
intercalate String
"," (forall f e (l :: [*]). HMapOut f l e => f -> HList l -> [e]
hMapOut (HShow
HShow forall g f. g -> f -> HComp g f
`HComp` HUntag
HUntag) HList l
l) forall a. [a] -> [a] -> [a]
++)
                              forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
']' forall a. a -> [a] -> [a]
:)


mkTIP :: HTypeIndexed l => HList l -> TIP l
mkTIP :: forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP = forall (l :: [*]). HList l -> TIP l
TIP

emptyTIP :: TIP '[]
emptyTIP :: TIP '[]
emptyTIP = forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP HList '[]
HNil

-- --------------------------------------------------------------------------
-- * Type-indexed type sequences

-- | this constraint ensures that a TIP created by 'mkTIP' has no
-- duplicates
class (HAllTaggedEq l, HRLabelSet l) => HTypeIndexed (l :: [*])

instance (HAllTaggedEq l, HRLabelSet l) => HTypeIndexed l

class HAllTaggedEq (l :: [*])
instance HAllTaggedEq '[]
instance (HAllTaggedEq l, tee ~ Tagged e e') => HAllTaggedEq (tee ': l)

-- --------------------------------------------------------------------------
-- Implementing the HListPrelude interface

instance (HRLabelSet (Tagged e e ': l), HTypeIndexed l) => HExtend e (TIP l)
 where
  type HExtendR e (TIP l) = TIP (Tagged e e ': l)
  e
e .*. :: e -> TIP l -> HExtendR e (TIP l)
.*. TIP HList l
l = forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP (forall x (xs :: [*]). x -> HList xs -> HList (x : xs)
HCons (forall {k} (s :: k) b. b -> Tagged s b
Tagged e
e) HList l
l)



instance (e ~ e', HasField e (Record l) e') => HasField e (TIP l) e' where
    hLookupByLabel :: Label e -> TIP l -> e'
hLookupByLabel Label e
lab (TIP HList l
l) = forall k (l :: k) r v. HasField l r v => Label l -> r -> v
hLookupByLabel Label e
lab (forall (r :: [*]). HList r -> Record r
Record HList l
l)

-- | One occurrence and nothing is left
--
-- This variation provides an extra feature for singleton lists.
-- That is, the result type is unified with the element in the list.
-- Hence the explicit provision of a result type can be omitted.
--

instance (tee ~ Tagged e e) => HOccurs e (TIP '[tee]) where
  hOccurs :: TIP '[tee] -> e
hOccurs (TIP (HCons (Tagged e
e) HList '[]
_)) = e
e

instance HasField e (Record (x ': y ': l)) e
      => HOccurs e (TIP (x ': y ': l)) where
  hOccurs :: TIP (x : y : l) -> e
hOccurs (TIP HList (x : y : l)
l) = forall (r :: [*]). HList r -> Record r
Record HList (x : y : l)
l forall {k} (l :: k) r v. HasField l r v => r -> Label l -> v
.!. (forall {k} (l :: k). Label l
Label :: Label e)


instance (HAppend (HList l) (HList l'), HTypeIndexed (HAppendListR l l'))
           => HAppend (TIP l) (TIP l')
 where
  hAppend :: TIP l -> TIP l' -> HAppendR (TIP l) (TIP l')
hAppend (TIP HList l
l) (TIP HList l'
l') = forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP (forall l1 l2. HAppend l1 l2 => l1 -> l2 -> HAppendR l1 l2
hAppend HList l
l HList l'
l')

type instance HAppendR (TIP l) (TIP l') = TIP (HAppendListR l l')


-- instance HOccurrence HList e l l' => HOccurrence TIP e l l'
--  where
--   hOccurrence e = TIP . hOccurrence e . unTIP

-- --------------------------------------------------------------------------
-- * Shielding type-indexed operations
-- $note The absence of signatures is deliberate! They all must be inferred.

onRecord :: (Record r -> Record l) -> TIP r -> TIP l
onRecord Record r -> Record l
f (TIP HList r
l) = let Record HList l
l' = Record r -> Record l
f (forall (r :: [*]). HList r -> Record r
Record HList r
l) in forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP HList l
l'

instance (HDeleteAtLabel Record e v v',
          HTypeIndexed v')
      => HDeleteAtLabel TIP e v v' where
  hDeleteAtLabel :: Label e -> TIP v -> TIP v'
hDeleteAtLabel Label e
e TIP v
v = forall {l :: [*]} {r :: [*]}.
(HAllTaggedEq l, HLabelSet (LabelsOf l), HAllTaggedLV l) =>
(Record r -> Record l) -> TIP r -> TIP l
onRecord (forall k (r :: [*] -> *) (l :: k) (v :: [*]) (v' :: [*]).
HDeleteAtLabel r l v v' =>
Label l -> r v -> r v'
hDeleteAtLabel Label e
e) TIP v
v


tipyUpdate :: v -> record r -> record r
tipyUpdate  v
e record r
t  = forall {k} (record :: [*] -> *) (l :: k) v (r :: [*]).
HTPupdateAtLabel record l v r =>
Label l -> v -> record r -> record r
hTPupdateAtLabel (forall e. e -> Label e
fromValue v
e) v
e record r
t
  where fromValue :: e -> Label e
        fromValue :: forall e. e -> Label e
fromValue e
_ = forall {k} (l :: k). Label l
Label

instance (HUpdateAtLabel Record e' e r r',
          HTypeIndexed r',
         e ~ e') => HUpdateAtLabel TIP e' e r r' where
  hUpdateAtLabel :: SameLength r r' => Label e' -> e -> TIP r -> TIP r'
hUpdateAtLabel Label e'
l e
e TIP r
r = forall {l :: [*]} {r :: [*]}.
(HAllTaggedEq l, HLabelSet (LabelsOf l), HAllTaggedLV l) =>
(Record r -> Record l) -> TIP r -> TIP l
onRecord (forall k (record :: [*] -> *) (l :: k) v (r :: [*]) (r' :: [*]).
(HUpdateAtLabel record l v r r', SameLength r r') =>
Label l -> v -> record r -> record r'
hUpdateAtLabel Label e'
l e
e) TIP r
r


-- | Use 'Labels' to specify the first argument
tipyProject :: proxy ls -> TIP r -> TIP l
tipyProject proxy ls
ps TIP r
t = forall {l :: [*]} {r :: [*]}.
(HAllTaggedEq l, HLabelSet (LabelsOf l), HAllTaggedLV l) =>
(Record r -> Record l) -> TIP r -> TIP l
onRecord (forall (a :: [*]) (ls :: [*]) (t :: [*]) (b :: [*])
       (proxy :: [*] -> *).
(HRLabelSet a, H2ProjectByLabels ls t a b) =>
proxy ls -> Record t -> Record a
hProjectByLabels proxy ls
ps) TIP r
t

-- | provides a @Lens' (TIP s) a@. 'hLens'' @:: Label a -> Lens' (TIP s) a@
-- is another option.
#if __GLASGOW_HASKELL__ < 707
tipyLens' x = isSimple tipyLens x -- rejected by GHC-7.10RC1
#else
tipyLens' :: (a -> f a) -> TIP t -> f (TIP t)
tipyLens' a -> f a
f TIP t
s = forall {k1} {k2} optic (p :: k1 -> k2 -> *) (a :: k1)
       (f :: k1 -> k2) (s :: k1).
(optic ~ (p a (f a) -> p s (f s))) =>
optic -> optic
isSimple (forall {k} (x :: k) (r :: [*] -> *) (s :: [*]) (t :: [*]) a b.
HLens x r s t a b =>
Label x
-> forall (f :: * -> *). Functor f => (a -> f b) -> r s -> f (r t)
hLens Label a
x) a -> f a
f (forall (a :: [*]). TIP a -> TIP a
asTIP TIP t
s) -- rejected by GHC-7.6.3
  where
    x :: Label a
x = forall a (f :: * -> *). (a -> f a) -> Label a
getA a -> f a
f
    getA :: (a -> f a) -> Label a
    getA :: forall a (f :: * -> *). (a -> f a) -> Label a
getA a -> f a
_ = forall {k} (l :: k). Label l
Label

    asTIP :: TIP a -> TIP a
    asTIP :: forall (a :: [*]). TIP a -> TIP a
asTIP = forall a. a -> a
id
#endif

{- | provides a @Lens (TIP s) (TIP t) a b@

When using @set@ (also known as @.~@), 'tipyLens'' can address the
ambiguity as to which field \"a\" should actually be updated.

-}
tipyLens :: (a -> f a)
-> TIP (Tagged x x : xs)
-> f (TIP (HAppendListR l1 (Tagged a a : xs)))
tipyLens a -> f a
f (TIP HList (Tagged x x : xs)
s) =
      case forall (n :: HNat) (xsys :: [*]) (xs :: [*]) (ys :: [*]).
HSplitAt n xsys xs ys =>
Proxy n -> HList xsys -> (HList xs, HList ys)
hSplitAt (forall {k} a (s :: [*]) (n :: HNat) (f :: k -> *) (b :: k).
HFind (Label a) (LabelsOf s) n =>
HList s -> (a -> f b) -> Proxy n
getN HList (Tagged x x : xs)
s a -> f a
f) (forall x (xs :: [*]).
HList (Tagged x x : xs) -> HList (Tagged x x : xs)
ghc8fix1 HList (Tagged x x : xs)
s) of
          (HList l1
x, ta :: Tagged a a
ta@(Tagged a
a) `HCons` HList xs
ys)
             | () <- forall a. Tagged a a -> ()
ghc8fix2 Tagged a a
ta ->
              let mkt :: a -> TIP (HAppendListR l1 (Tagged a a : xs))
mkt a
b = forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP (HList l1
x forall (l1 :: [*]) (l2 :: [*]).
HAppendList l1 l2 =>
HList l1 -> HList l2 -> HList (HAppendListR l1 l2)
`hAppendList` (forall a. a -> Tagged a a
tagSelf a
b forall x (xs :: [*]). x -> HList xs -> HList (x : xs)
`HCons` HList xs
ys))
              in a -> TIP (HAppendListR l1 (Tagged a a : xs))
mkt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
  where
    getN :: HFind (Label a) (LabelsOf s) n => HList s -> (a -> f b) -> Proxy n
    getN :: forall {k} a (s :: [*]) (n :: HNat) (f :: k -> *) (b :: k).
HFind (Label a) (LabelsOf s) n =>
HList s -> (a -> f b) -> Proxy n
getN HList s
_ a -> f b
_ = forall {k} (t :: k). Proxy t
Proxy

    -- without these, tipyLens has a type that has kind variables,
    -- (that end up being * when an actual TIP is provided), leading to
    -- a Properties.LengthIndependent compile error:
    -- .../.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/Data/HList/TIP.hi
    -- Declaration for tipyLens:
    --   Iface type variable out of scope:  k
    -- Cannot continue after interface file error
    ghc8fix1 :: HList (Tagged x x ': xs) -> HList (Tagged x x ': xs)
    ghc8fix1 :: forall x (xs :: [*]).
HList (Tagged x x : xs) -> HList (Tagged x x : xs)
ghc8fix1 = forall a. a -> a
id

    ghc8fix2 :: Tagged a a -> ()
    ghc8fix2 :: forall a. Tagged a a -> ()
ghc8fix2 Tagged a a
_ = ()



-- | The same as 'tipyProject', except also return the
-- types not requested in the @proxy@ argument
tipyProject2 :: proxy ls -> TIP r -> (TIP l, TIP l)
tipyProject2 proxy ls
ps (TIP HList r
l) = (forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP HList l
l',forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP HList l
l'')
 where
  (HList l
l',HList l
l'') = forall (ls :: [*]) (r :: [*]) (rin :: [*]) (rout :: [*])
       (proxy :: [*] -> *).
H2ProjectByLabels ls r rin rout =>
proxy ls -> HList r -> (HList rin, HList rout)
h2projectByLabels proxy ls
ps HList r
l


-- --------------------------------------------------------------------------

-- | Subtyping for TIPs

instance SubType (TIP l) (TIP '[])
instance (HOccurs e (TIP l1), SubType (TIP l1) (TIP l2))
      =>  SubType (TIP l1) (TIP (e ': l2))


-- --------------------------------------------------------------------------
-- * conversion to and from 'HList'

{- | 'TagR' can also be used to avoid redundancy when defining types for TIC and TIP.

>  type XShort = TagR [A,B,C,D]

>  type XLong = [Tagged A A, Tagged B B, Tagged C C, Tagged D D]


an equivalent FD version, which is slightly better with respect to
simplifying types containing type variables (in ghc-7.8 and 7.6):
<http://stackoverflow.com/questions/24110410/>

With ghc-7.10 (<http://ghc.haskell.org/trac/ghc/ticket/10009>) the FD version is superior
to the TF version:

@
class (UntagR (TagR a) ~ a) => TagUntag a where
    type TagR a :: [*]
    hTagSelf :: HList a -> HList (TagR a)
    hUntagSelf :: HList (TagR a) -> HList a

instance TagUntag '[] where
    type TagR '[] = '[]
    hTagSelf _ = HNil
    hUntagSelf _ = HNil

instance TagUntag xs => TagUntag (x ': xs) where
    type TagR (x ': xs) = Tagged x x ': TagR xs
    hTagSelf (HCons x xs) = Tagged x `HCons` hTagSelf xs
    hUntagSelf (HCons (Tagged x) xs) = x `HCons` hUntagSelf xs

type family UntagR (xs :: [*]) :: [*]
type instance UntagR '[] = '[]
type instance UntagR (x ': xs) = Untag1 x ': UntagR xs
@

Length information should flow backwards

>>> let len2 x = x `asTypeOf` (undefined :: HList '[a,b])
>>> let f = len2 $ hTagSelf (hReplicate Proxy ())
>>> :t f
f :: HList '[Tagged () (), Tagged () ()]

-}
class SameLength a ta => TagUntagFD a ta | a -> ta, ta -> a where
    hTagSelf :: HList a -> HList ta
    hUntagSelf :: HList ta -> HList a

instance TagUntagFD '[] '[] where
    hTagSelf :: HList '[] -> HList '[]
hTagSelf HList '[]
_ = HList '[]
HNil
    hUntagSelf :: HList '[] -> HList '[]
hUntagSelf HList '[]
_ = HList '[]
HNil

instance (TagUntagFD xs ys, txx ~ Tagged x x)
      => TagUntagFD (x ': xs) (txx ': ys) where
    hTagSelf :: HList (x : xs) -> HList (txx : ys)
hTagSelf (HCons x
x HList xs
xs) = forall {k} (s :: k) b. b -> Tagged s b
Tagged x
x forall x (xs :: [*]). x -> HList xs -> HList (x : xs)
`HCons` forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList a -> HList ta
hTagSelf HList xs
xs
    hUntagSelf :: HList (txx : ys) -> HList (x : xs)
hUntagSelf (HCons (Tagged x
x) HList ys
xs) = x
x forall x (xs :: [*]). x -> HList xs -> HList (x : xs)
`HCons` forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList ta -> HList a
hUntagSelf HList ys
xs

type TagUntag xs = TagUntagFD xs (TagR xs)

-- | Sometimes the type variables available have @TagR@ already applied
-- (ie the lists have elements like @Tagged X X@). Then this abbreviation
-- is useful:
type UntagTag xs = (TagR (UntagR xs) ~ xs, TagUntagFD (UntagR xs) xs)

type family TagR (a :: [*]) :: [*]
type family UntagR (ta :: [*]) :: [*]

type instance TagR '[] = '[]
type instance UntagR '[] = '[]

type instance TagR (x ': xs) = Tagged x x ': TagR xs
type instance UntagR (Tagged y y ': ys) = y ': UntagR ys

type family Untag1 (x :: *) :: *
type instance Untag1 (Tagged k x) = x


-- | @Iso (TIP (TagR a)) (TIP (TagR b)) (HList a) (HList b)@
tipHList :: p (HList a) (f (HList a)) -> p (TIP ta) (f (TIP l))
tipHList p (HList a) (f (HList a))
x = forall (p :: * -> * -> *) (f :: * -> *) s a b t.
(Profunctor p, Functor f) =>
(s -> a) -> (b -> t) -> p a (f b) -> p s (f t)
iso (\(TIP HList ta
a) -> forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList ta -> HList a
hUntagSelf HList ta
a) (forall (l :: [*]). HList l -> TIP l
TIP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList a -> HList ta
hTagSelf) p (HList a) (f (HList a))
x

-- | @Iso' (TIP (TagR s)) (HList a)@
tipHList' :: p (HList a) (f (HList a)) -> p (TIP l) (f (TIP l))
tipHList' p (HList a) (f (HList a))
x = forall {k1} {k2} optic (p :: k1 -> k2 -> *) (a :: k1)
       (f :: k1 -> k2) (s :: k1).
(optic ~ (p a (f a) -> p s (f s))) =>
optic -> optic
isSimple forall {p :: * -> * -> *} {f :: * -> *} {a :: [*]} {ta :: [*]}
       {a :: [*]} {l :: [*]}.
(Profunctor p, Functor f, TagUntagFD a ta, TagUntagFD a l) =>
p (HList a) (f (HList a)) -> p (TIP ta) (f (TIP l))
tipHList p (HList a) (f (HList a))
x


-- * conversion to and from 'Record'

-- | @Iso (TIP s) (TIP t) (Record s) (Record t)@
--
-- 'typeIndexed' may be more appropriate
tipRecord :: p (Record r) (f (Record l)) -> p (TIP r) (f (TIP l))
tipRecord p (Record r) (f (Record l))
x = forall (p :: * -> * -> *) (f :: * -> *) b t a s.
(Profunctor p, Functor f, Coercible b t, Coercible a s) =>
(s -> a) -> (b -> t) -> p a (f b) -> p s (f t)
isoNewtype (\(TIP HList r
a) -> forall (r :: [*]). HList r -> Record r
Record HList r
a) (\(Record HList l
b) -> forall (l :: [*]). HList l -> TIP l
TIP HList l
b) p (Record r) (f (Record l))
x

-- | @Iso' (TIP (TagR s)) (Record a)@
tipRecord' :: p (Record l) (f (Record l)) -> p (TIP l) (f (TIP l))
tipRecord' p (Record l) (f (Record l))
x = forall {k1} {k2} optic (p :: k1 -> k2 -> *) (a :: k1)
       (f :: k1 -> k2) (s :: k1).
(optic ~ (p a (f a) -> p s (f s))) =>
optic -> optic
isSimple forall {p :: * -> * -> *} {f :: * -> *} {r :: [*]} {l :: [*]}.
(Profunctor p, Functor f) =>
p (Record r) (f (Record l)) -> p (TIP r) (f (TIP l))
tipRecord p (Record l) (f (Record l))
x

-- --------------------------------------------------------------------------
-- * Zip

#if __GLASGOW_HASKELL__ < 800
-- pre-coerce
instance (HZipList (UntagR x) (UntagR y) (UntagR xy),
          UntagTag x, UntagTag y, UntagTag xy,
          SameLengths [x,y,xy],
          HTypeIndexed x, HTypeIndexed y,
          HUnzip TIP x y xy
          -- HTypeIndexed xy is always satisfied given the above
          -- constraints (with a handwaving proof), so don't require
          -- callers of hZip/hUnzip to supply such proof
    ) => HZip TIP x y xy where
  hZip = hZipTIP


instance (HZipList (UntagR x) (UntagR y) (UntagR xy),
          UntagTag x, UntagTag y, UntagTag xy,
          HTypeIndexed x, HTypeIndexed y,
          SameLengths [x,y,xy]) => HUnzip TIP x y xy where
  hUnzip = hUnzipTIP

#else
-- ghc-7.10.3 has coerce, but rejects these instances
instance (HZipList xL yL xyL,
          lty ~ (HList xyL -> (HList xL,HList yL)),
          Coercible lty (TIP xy -> (TIP x, TIP y)),
          UntagR x ~ xL, TagR xL ~ x, -- `TagR (UntagR x) ~ x` included by UntagTag
          UntagR y ~ yL, TagR yL ~ y,
          UntagR xy ~ xyL, TagR xyL ~ xy,
          SameLengths '[x,y,xy],
          UntagTag x, UntagTag y, UntagTag xy
        ) => HUnzip TIP x y xy where
  hUnzip :: TIP xy -> (TIP x, TIP y)
hUnzip = coerce :: forall a b. Coercible a b => a -> b
coerce (forall (x :: [*]) (y :: [*]) (l :: [*]).
HZipList x y l =>
HList l -> (HList x, HList y)
hUnzipList :: lty)

instance (HUnzip TIP x y xy,
          HZipList xL yL xyL,
          lty ~ (HList xL -> HList yL -> HList xyL),
          Coercible lty (TIP x -> TIP y -> TIP xy) ,
          UntagR x ~ xL,
          UntagR y ~ yL,
          UntagR xy ~ xyL,
          UntagTag x, UntagTag y, UntagTag xy
        ) => HZip TIP x y xy where
  hZip :: TIP x -> TIP y -> TIP xy
hZip = coerce :: forall a b. Coercible a b => a -> b
coerce (forall (x :: [*]) (y :: [*]) (l :: [*]).
HZipList x y l =>
HList x -> HList y -> HList l
hZipList :: lty)
#endif

-- | specialization of 'hZip'
hZipTIP :: TIP ta -> TIP ta -> TIP l
hZipTIP (TIP HList ta
x) (TIP HList ta
y) = forall (l :: [*]). HList l -> TIP l
TIP (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList a -> HList ta
hTagSelf (forall (x :: [*]) (y :: [*]) (l :: [*]).
HZipList x y l =>
HList x -> HList y -> HList l
hZipList (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList ta -> HList a
hUntagSelf HList ta
x) (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList ta -> HList a
hUntagSelf HList ta
y)))

-- | specialization of 'hUnzip'
hUnzipTIP :: TIP ta -> (TIP l, TIP l)
hUnzipTIP (TIP HList ta
xy) = case forall (x :: [*]) (y :: [*]) (l :: [*]).
HZipList x y l =>
HList l -> (HList x, HList y)
hUnzipList (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList ta -> HList a
hUntagSelf HList ta
xy) of
  (HList a
x,HList a
y) -> (forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList a -> HList ta
hTagSelf HList a
x), forall (l :: [*]). HTypeIndexed l => HList l -> TIP l
mkTIP (forall (a :: [*]) (ta :: [*]).
TagUntagFD a ta =>
HList a -> HList ta
hTagSelf HList a
y))



-- --------------------------------------------------------------------------
-- * TIP Transform

{- |

Transforming a TIP: applying to a TIP a (polyvariadic) function
that takes arguments from a TIP and updates the TIP with the result.

In more detail: we have a typed-indexed collection TIP and we
would like to apply a transformation function to it, whose argument
types and the result type are all in the TIP. The function should locate
its arguments based on their types, and update the TIP
with the result. The function may have any number of arguments,
including zero; the order of arguments should not matter.

The problem was posed by Andrew U. Frank on Haskell-Cafe, Sep 10, 2009.
<http://www.haskell.org/pipermail/haskell-cafe/2009-September/066217.html>
The problem is an interesting variation of the keyword argument problem.

Examples can be found in @examples/TIPTransform.hs@ and @examples/TIPTransformM.hs@
-}

class TransTIP op db where
    ttip :: op -> TIP db -> TIP db

instance (HMember (Tagged op op) db b,
          Arity op n,
          TransTIP1 b n op db)
    => TransTIP op db where
    ttip :: op -> TIP db -> TIP db
ttip = forall (b :: Bool) (n :: HNat) op (db :: [*]).
TransTIP1 b n op db =>
Proxy b -> Proxy n -> op -> TIP db -> TIP db
ttip1 (forall {k} (t :: k). Proxy t
Proxy ::Proxy b) (forall {k} (t :: k). Proxy t
Proxy :: Proxy n)

class TransTIP1 (b :: Bool) (n :: HNat) op db where
    ttip1 :: Proxy b -> Proxy n -> op -> TIP db -> TIP db

-- If op is found in a TIP, update the TIP with op
instance HTPupdateAtLabel TIP op op db
    => TransTIP1 True n op db where
    ttip1 :: Proxy 'True -> Proxy n -> op -> TIP db -> TIP db
ttip1 Proxy 'True
_ Proxy n
_ = forall {record :: [*] -> *} {v} {r :: [*]}.
(HUpdateAtLabel record v v r r, SameLength' r r) =>
v -> record r -> record r
tipyUpdate

-- If op is not found in a TIP, it must be a function. Try to look up
-- its argument in a TIP and recur.
instance (HMember (Tagged arg arg) db b,
          TransTIP2 b arg op db)
    => TransTIP1 False (HSucc n) (arg -> op) db where
    ttip1 :: Proxy 'False -> Proxy ('HSucc n) -> (arg -> op) -> TIP db -> TIP db
ttip1 Proxy 'False
_ Proxy ('HSucc n)
_ = forall (b :: Bool) arg op (db :: [*]).
TransTIP2 b arg op db =>
Proxy b -> (arg -> op) -> TIP db -> TIP db
ttip2 (forall {k} (t :: k). Proxy t
Proxy :: Proxy b)

instance Fail (FieldNotFound notfun (TIP db))
      => TransTIP1 False HZero notfun db where
    ttip1 :: Proxy 'False -> Proxy 'HZero -> notfun -> TIP db -> TIP db
ttip1 = forall a. HasCallStack => String -> a
error String
"TransTIP1 Fail failed"

class TransTIP2 (b :: Bool) arg op db where
    ttip2 :: Proxy b -> (arg -> op) -> TIP db -> TIP db

instance (HOccurs arg (TIP db),
         TransTIP op db)
   => TransTIP2 True arg op db where
    ttip2 :: Proxy 'True -> (arg -> op) -> TIP db -> TIP db
ttip2 Proxy 'True
_ arg -> op
f TIP db
db = forall op (db :: [*]). TransTIP op db => op -> TIP db -> TIP db
ttip (arg -> op
f (forall e l. HOccurs e l => l -> e
hOccurs TIP db
db)) TIP db
db

instance Fail (FieldNotFound arg (TIP db))
    => TransTIP2 False arg op db where
    ttip2 :: Proxy 'False -> (arg -> op) -> TIP db -> TIP db
ttip2 = forall a. HasCallStack => String -> a
error String
"TransTIP2 Fail failed"

-- ** Monadic version

{- |

In March 2010, Andrew Frank extended the problem for monadic operations.
This is the monadic version of @TIPTransform.hs@ in the present directory.

This is the TF implementation. When specifying the operation to perform over
a TIP, we can leave it polymorphic over the monad. The type checker
will instantiate the monad based on the context.

-}
class Monad m => TransTIPM m op db where
    ttipM :: op -> TIP db -> m (TIP db)

-- Check to see if the operation is a computation whose result
-- is in the TIP. The type variable m' of the kind *->* below
-- can be instantiated either to a monad type constructor, or (arg->).
instance (Monad m, HMember (Tagged op op) db b,
           Arity (m' op) n,
           TransTIPM1 b n m (m' op) db)
    => TransTIPM m (m' op) db where
    ttipM :: m' op -> TIP db -> m (TIP db)
ttipM = forall (b :: Bool) (n :: HNat) (m :: * -> *) op (db :: [*]).
TransTIPM1 b n m op db =>
Proxy b -> Proxy n -> op -> TIP db -> m (TIP db)
ttipM1 (forall {k} (t :: k). Proxy t
Proxy :: Proxy b) (forall {k} (t :: k). Proxy t
Proxy :: Proxy n)

class Monad m => TransTIPM1 (b :: Bool) (n :: HNat) m op db where
    ttipM1 :: Proxy b -> Proxy n -> op -> TIP db -> m (TIP db)

-- If op is found in a TIP, update the TIP with op.
-- The type variable m' must be equal to the type of the monad
-- in which the final result is reported.
instance (Monad m, m ~ m', HTPupdateAtLabel TIP op op db)
    => TransTIPM1 True n m (m' op) db where
    ttipM1 :: Proxy 'True -> Proxy n -> m' op -> TIP db -> m (TIP db)
ttipM1 Proxy 'True
_ Proxy n
_ m' op
op TIP db
db = do
         op
op' <- m' op
op
         forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall {record :: [*] -> *} {v} {r :: [*]}.
(HUpdateAtLabel record v v r r, SameLength' r r) =>
v -> record r -> record r
tipyUpdate op
op' TIP db
db

instance (Fail (FieldNotFound op (TIP db)), Monad m)
    => TransTIPM1 False HZero m op db where
    ttipM1 :: Proxy 'False -> Proxy 'HZero -> op -> TIP db -> m (TIP db)
ttipM1 Proxy 'False
_ Proxy 'HZero
_ = forall a. HasCallStack => String -> a
error String
"TransTIPM1 Fail failed"

-- If op is not found in a TIP, it must be a function. Look up
-- its argument in a TIP and recur.
instance (Monad m,
          HMember (Tagged arg arg) db b,
          TransTIPM2 b m arg op db)
    => TransTIPM1 False (HSucc n) m (arg-> op) db where
    ttipM1 :: Proxy 'False
-> Proxy ('HSucc n) -> (arg -> op) -> TIP db -> m (TIP db)
ttipM1 Proxy 'False
_ Proxy ('HSucc n)
_ = forall (b :: Bool) (m :: * -> *) arg op (db :: [*]).
TransTIPM2 b m arg op db =>
Proxy b -> (arg -> op) -> TIP db -> m (TIP db)
ttipM2 (forall {k} (t :: k). Proxy t
Proxy :: Proxy b)


class TransTIPM2 (b :: Bool) m arg op db where
    ttipM2 :: Proxy b -> (arg -> op) -> TIP db -> m (TIP db)

instance (HOccurs arg (TIP db), TransTIPM m op db)
      => TransTIPM2 True m arg op db where
    ttipM2 :: Proxy 'True -> (arg -> op) -> TIP db -> m (TIP db)
ttipM2 Proxy 'True
_ arg -> op
f TIP db
db = forall (m :: * -> *) op (db :: [*]).
TransTIPM m op db =>
op -> TIP db -> m (TIP db)
ttipM (arg -> op
f (forall e l. HOccurs e l => l -> e
hOccurs TIP db
db)) TIP db
db


instance Fail (FieldNotFound op (TIP db))
    => TransTIPM2 False m arg op db where
    ttipM2 :: Proxy 'False -> (arg -> op) -> TIP db -> m (TIP db)
ttipM2 Proxy 'False
_ arg -> op
_ = forall a. HasCallStack => String -> a
error String
"TransTIPM1 Fail failed"

-- --------------------------------------------------------------------------

-- tests for tipyTuple. These only work if tipyTuple is compiled
-- in a module that has NoMonoLocalBinds enabled
(Char, Bool)
_ = forall {t1} {r :: [*] -> *} {v1 :: [*]} {v2 :: [*]} {t2}
       {v3 :: [*]} {v'1 :: [*]} {v'2 :: [*]}.
(HOccurs t1 (r v1), HOccurs t1 (r v2), HOccurs t2 (r v2),
 HOccurs t2 (r v3), HDeleteAtLabel r t1 v1 v'1,
 HDeleteAtLabel r t1 v2 v3, HDeleteAtLabel r t2 v2 v1,
 HDeleteAtLabel r t2 v3 v'2) =>
r v2 -> (t2, t1)
tipyTuple ( Char
'1' forall e l. HExtend e l => e -> l -> HExtendR e l
.*. Bool
True forall e l. HExtend e l => e -> l -> HExtendR e l
.*. TIP '[]
emptyTIP ) :: (Char, Bool)
(Bool, Char)
_ = forall {t1} {r :: [*] -> *} {v1 :: [*]} {v2 :: [*]} {t2}
       {v3 :: [*]} {v'1 :: [*]} {v'2 :: [*]}.
(HOccurs t1 (r v1), HOccurs t1 (r v2), HOccurs t2 (r v2),
 HOccurs t2 (r v3), HDeleteAtLabel r t1 v1 v'1,
 HDeleteAtLabel r t1 v2 v3, HDeleteAtLabel r t2 v2 v1,
 HDeleteAtLabel r t2 v3 v'2) =>
r v2 -> (t2, t1)
tipyTuple ( Char
'1' forall e l. HExtend e l => e -> l -> HExtendR e l
.*. Bool
True forall e l. HExtend e l => e -> l -> HExtendR e l
.*. TIP '[]
emptyTIP ) :: (Bool, Char)


-- --------------------------------------------------------------------------

-- * Sample code

{- $setup

[@Assume@]

>>> import Data.HList.TypeEqO
>>> import Data.HList.FakePrelude
>>> import Data.HList.HOccurs

>>> :{
newtype Key    = Key Integer deriving (Show,Eq,Ord)
newtype Name   = Name String deriving (Show,Eq)
data Breed     = Cow | Sheep deriving (Show,Eq)
newtype Price  = Price Float deriving (Show,Eq,Ord)
data Disease   = BSE | FM deriving (Show,Eq)
type Animal =  TagR '[Key,Name,Breed,Price]
:}

>>> :{
let myTipyCow :: TIP Animal -- optional
    myTipyCow = Key 42 .*.  Name "Angus" .*.  Cow .*.  Price 75.5 .*. emptyTIP
    animalKey :: (HOccurs Key l, SubType l (TIP Animal)) => l -> Key
    animalKey = hOccurs
:}

-}

{- $sessionlog
[@Session log@]

>>> :t myTipyCow
myTipyCow
  :: TIP
       '[Tagged Key Key, Tagged Name Name, Tagged Breed Breed,
         Tagged Price Price]

>>> hOccurs myTipyCow :: Breed
Cow

>>> BSE .*. myTipyCow
TIPH[BSE,Key 42,Name "Angus",Cow,Price 75.5]



>>> Sheep .*. hDeleteAtLabel (Label::Label Breed) myTipyCow
TIPH[Sheep,Key 42,Name "Angus",Price 75.5]

>>> tipyUpdate Sheep myTipyCow
TIPH[Key 42,Name "Angus",Sheep,Price 75.5]


>>> tipyProject2 (Proxy :: Labels '[Name,Price]) myTipyCow
(TIPH[Name "Angus",Price 75.5],TIPH[Key 42,Cow])

>>> tipyProject (Proxy :: Labels '[Name,Price]) myTipyCow
TIPH[Name "Angus",Price 75.5]

-}


{- $sessionlog2

Don't bother repeating the type error:


>>> Sheep .*. myTipyCow
...
...No instance for (Fail (DuplicatedLabel (Label Breed)))
...

-}