{-# LANGUAGE CPP #-}
module Data.HList.FakePrelude
(module Data.HList.FakePrelude,
module Data.Proxy,
module Data.Tagged,
Monoid(..),
Any) where
import Data.Proxy
import Data.Tagged
import GHC.Exts (Constraint,Any)
import GHC.TypeLits
#if __GLASGOW_HASKELL__ >= 800
import qualified GHC.TypeLits as Data.HList.FakePrelude (ErrorMessage((:$$:), (:<>:)))
#endif
import Control.Applicative
#if NEW_TYPE_EQ
import Data.Type.Equality (type (==))
#endif
#if !OLD_TYPEABLE
import Data.Typeable
#endif
#if __GLASGOW_HASKELL__ < 709
import Data.Monoid (Monoid(..))
#endif
class Apply f a where
type ApplyR f a :: *
apply :: f -> a -> ApplyR f a
class ApplyAB f a b where
applyAB :: f -> a -> b
data Fun (cxt :: k1) (getb :: k2)
= Fun (forall a. FunCxt cxt a => a -> FunApp getb a)
data Fun' (cxt :: k1) (geta :: k2)
= Fun' (forall b. FunCxt cxt b => FunApp geta b -> b)
type family FunApp (fns :: k) a
type instance FunApp (fn :: *) a = fn
type instance FunApp (fn :: * -> *) a = fn a
type instance FunApp (fn :: ()) a = a
type family FunCxt (cxts :: k) a :: Constraint
type instance FunCxt (x ': xs) a = (x a, FunCxt xs a)
type instance FunCxt (cxt :: * -> Constraint) a = cxt a
type instance FunCxt '[] a = ()
type instance FunCxt (cxt :: ()) a = ()
type instance FunCxt (cxt :: *) a = (cxt ~ a)
instance (FunCxt cxt a, FunApp getb a ~ b) => ApplyAB (Fun cxt getb) a b where
applyAB :: Fun cxt getb -> a -> b
applyAB (Fun forall a. FunCxt cxt a => a -> FunApp getb a
f) a
x = a -> FunApp getb a
forall a. FunCxt cxt a => a -> FunApp getb a
f a
x
instance (FunCxt cxt b, FunApp geta b ~ a) => ApplyAB (Fun' cxt geta) a b where
applyAB :: Fun' cxt geta -> a -> b
applyAB (Fun' forall b. FunCxt cxt b => FunApp geta b -> b
f) a
x = FunApp geta b -> b
forall b. FunCxt cxt b => FunApp geta b -> b
f a
FunApp geta b
x
instance (x' ~ x, y' ~ y) => ApplyAB (x' -> y') x y where
applyAB :: (x' -> y') -> x -> y
applyAB x' -> y'
f x
x = x' -> y'
f x'
x
x
data HPrint = HPrint
instance (io ~ IO (), Show x) => ApplyAB HPrint x io where
applyAB :: HPrint -> x -> io
applyAB HPrint
_ x
x = x -> IO ()
forall a. Show a => a -> IO ()
print x
x
data HRead = HRead
instance (String ~ string, Read a) => ApplyAB HRead string a where
applyAB :: HRead -> string -> a
applyAB HRead
_ string
x = String -> a
forall a. Read a => String -> a
read string
String
x
data HShow = HShow
instance (String ~ string, Show a) => ApplyAB HShow a string where
applyAB :: HShow -> a -> string
applyAB HShow
_ a
x = a -> String
forall a. Show a => a -> String
show a
x
data HComp g f = HComp g f
instance (ApplyAB f a b, ApplyAB g b c) => ApplyAB (HComp g f) a c where
applyAB :: HComp g f -> a -> c
applyAB ~(HComp g
g f
f) a
x = g -> b -> c
forall f a b. ApplyAB f a b => f -> a -> b
applyAB g
g (f -> a -> b
forall f a b. ApplyAB f a b => f -> a -> b
applyAB f
f a
x :: b)
data Comp = Comp
instance (y ~ y', fg ~ (x -> y, y' -> z), r ~ (x -> z)) => ApplyAB Comp fg r
where
applyAB :: Comp -> fg -> r
applyAB Comp
_ (f,g) = y' -> z
g (y' -> z) -> (x -> y') -> x -> z
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> y'
f
newtype HSeq x = HSeq x
instance (Monad m, ApplyAB f x fx, fx ~ m (), pair ~ (x,m ()),
ApplyAB f x (m ()) ) => ApplyAB (HSeq f) pair fx where
applyAB :: HSeq f -> pair -> fx
applyAB (HSeq f
f) (x,c) = do m () -> m ()
asVoid (f -> x -> m ()
forall f a b. ApplyAB f a b => f -> a -> b
applyAB f
f x
x); m ()
c
where asVoid :: m () -> m ()
asVoid :: m () -> m ()
asVoid m ()
t = m ()
t
instance hJustA ~ HJust a => ApplyAB (HJust t) a hJustA where
applyAB :: HJust t -> a -> hJustA
applyAB HJust t
_ a
a = a -> HJust a
forall x. x -> HJust x
HJust a
a
data HFlip = HFlip
instance (f1 ~ (a -> b -> c), f2 ~ (b -> a -> c)) => ApplyAB HFlip f1 f2 where
applyAB :: HFlip -> f1 -> f2
applyAB HFlip
_ = f1 -> f2
forall a b c. (a -> b -> c) -> b -> a -> c
flip
newtype HFmap f = HFmap f
instance (x ~ t a,
y ~ t b,
Functor t,
ApplyAB f a b) =>
ApplyAB (HFmap f) x y where
applyAB :: HFmap f -> x -> y
applyAB (HFmap f
f) = (a -> b) -> t a -> t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (f -> a -> b
forall f a b. ApplyAB f a b => f -> a -> b
applyAB f
f)
newtype LiftA2 f = LiftA2 f
instance (ApplyAB f (x,y) z,
mz ~ m z,
mxy ~ (m x, m y),
Applicative m) => ApplyAB (LiftA2 f) mxy mz where
applyAB :: LiftA2 f -> mxy -> mz
applyAB (LiftA2 f
f) mxy
xy = (x -> y -> z) -> m x -> m y -> m z
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (((x, y) -> z) -> x -> y -> z
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (f -> (x, y) -> z
forall f a b. ApplyAB f a b => f -> a -> b
applyAB f
f)) (m x -> m y -> m z) -> (m x, m y) -> m z
forall a b c. (a -> b -> c) -> (a, b) -> c
`uncurry` mxy
(m x, m y)
xy
data HUntag = HUntag
instance (Tagged t x ~ tx) => ApplyAB HUntag tx x where
applyAB :: HUntag -> tx -> x
applyAB HUntag
_ (Tagged x) = x
x
data Label l = Label
labelToProxy :: Label l -> Proxy l
labelToProxy :: Label l -> Proxy l
labelToProxy Label l
_ = Proxy l
forall k (t :: k). Proxy t
Proxy
class ShowLabel l where
showLabel :: Label l -> String
hTrue :: Proxy True ; hTrue :: Proxy 'True
hTrue = Proxy 'True
forall k (t :: k). Proxy t
Proxy
hFalse :: Proxy False; hFalse :: Proxy 'False
hFalse = Proxy 'False
forall k (t :: k). Proxy t
Proxy
type family HAnd (t1 :: Bool) (t2 :: Bool) :: Bool
type instance HAnd False t = False
type instance HAnd True t = t
hAnd :: Proxy t1 -> Proxy t2 -> Proxy (HAnd t1 t2)
hAnd :: Proxy t1 -> Proxy t2 -> Proxy (HAnd t1 t2)
hAnd Proxy t1
_ Proxy t2
_ = Proxy (HAnd t1 t2)
forall k (t :: k). Proxy t
Proxy
type family HOr (t1 :: Bool) (t2 :: Bool) :: Bool
type instance HOr False t = t
type instance HOr True t = True
hOr :: Proxy t1 -> Proxy t2 -> Proxy (HOr t1 t2)
hOr :: Proxy t1 -> Proxy t2 -> Proxy (HOr t1 t2)
hOr Proxy t1
_ Proxy t2
_ = Proxy (HOr t1 t2)
forall k (t :: k). Proxy t
Proxy
type family HNot (x :: Bool) :: Bool
type instance HNot True = False
type instance HNot False = True
class HNotFD (b :: Bool) (nb :: Bool) | b -> nb, nb -> b
instance HNotFD True False
instance HNotFD False True
hNot :: HNotFD a notA => Proxy a -> Proxy notA
hNot :: Proxy a -> Proxy notA
hNot Proxy a
_ = Proxy notA
forall k (t :: k). Proxy t
Proxy
class HCond (t :: Bool) x y z | t x y -> z
where
hCond :: Proxy t -> x -> y -> z
instance HCond False x y y
where
hCond :: Proxy 'False -> x -> y -> y
hCond Proxy 'False
_ x
_ y
y = y
y
instance HCond True x y x
where
hCond :: Proxy 'True -> x -> y -> x
hCond Proxy 'True
_ x
x y
_ = x
x
type family HBoolEQ (t1 :: Bool) (t2 :: Bool) :: Bool
type instance HBoolEQ False False = True
type instance HBoolEQ False True = False
type instance HBoolEQ True False = False
type instance HBoolEQ True True = True
data HNat = HZero | HSucc HNat
hZero :: Proxy HZero; hZero :: Proxy 'HZero
hZero = Proxy 'HZero
forall k (t :: k). Proxy t
Proxy
hSucc :: Proxy (n :: HNat) -> Proxy (HSucc n); hSucc :: Proxy n -> Proxy ('HSucc n)
hSucc Proxy n
_ = Proxy ('HSucc n)
forall k (t :: k). Proxy t
Proxy
hPred :: Proxy (HSucc n) -> Proxy n; hPred :: Proxy ('HSucc n) -> Proxy n
hPred Proxy ('HSucc n)
_ = Proxy n
forall k (t :: k). Proxy t
Proxy
class HNat2Integral (n::HNat) where
hNat2Integral :: Integral i => Proxy n -> i
type family HNat2Nat (n :: HNat) :: Nat
type instance HNat2Nat HZero = 0
type instance HNat2Nat (HSucc n) = 1 + HNat2Nat n
#if MIN_VERSION_base(4,7,0)
instance KnownNat (HNat2Nat n) => HNat2Integral n where
hNat2Integral :: Proxy n -> i
hNat2Integral Proxy n
_ = Integer -> i
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy (HNat2Nat n) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (HNat2Nat n)
forall k (t :: k). Proxy t
Proxy :: Proxy (HNat2Nat n)))
#else
instance HNat2Integral HZero where
hNat2Integral _ = 0
instance HNat2Integral n => HNat2Integral (HSucc n) where
hNat2Integral n = hNat2Integral (hPred n) + 1
#endif
class HNats2Integrals (ns :: [HNat]) where
hNats2Integrals :: Integral i => Proxy ns -> [i]
instance HNats2Integrals '[] where
hNats2Integrals :: Proxy '[] -> [i]
hNats2Integrals Proxy '[]
_ = []
instance (HNats2Integrals ns,
HNat2Integral n)
=> HNats2Integrals (n ': ns) where
hNats2Integrals :: Proxy (n : ns) -> [i]
hNats2Integrals Proxy (n : ns)
_ = Proxy n -> i
forall (n :: HNat) i. (HNat2Integral n, Integral i) => Proxy n -> i
hNat2Integral (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n) i -> [i] -> [i]
forall a. a -> [a] -> [a]
:
Proxy ns -> [i]
forall (ns :: [HNat]) i.
(HNats2Integrals ns, Integral i) =>
Proxy ns -> [i]
hNats2Integrals (Proxy ns
forall k (t :: k). Proxy t
Proxy :: Proxy ns)
type family HNatEq (t1 :: HNat) (t2 :: HNat) :: Bool
type instance HNatEq HZero HZero = True
type instance HNatEq HZero (HSucc n) = False
type instance HNatEq (HSucc n) HZero = False
type instance HNatEq (HSucc n) (HSucc n') = HNatEq n n'
type family HLt (x :: HNat) (y :: HNat) :: Bool
type instance HLt HZero HZero = False
type instance HLt HZero (HSucc n) = True
type instance HLt (HSucc n) HZero = False
type instance HLt (HSucc n) (HSucc n') = HLt n n'
hLt :: Proxy x -> Proxy y -> Proxy (HLt x y)
hLt :: Proxy x -> Proxy y -> Proxy (HLt x y)
hLt Proxy x
_ Proxy y
_ = Proxy (HLt x y)
forall k (t :: k). Proxy t
Proxy
type family HLe (x :: HNat) (y :: HNat) :: Bool
type instance HLe HZero HZero = True
type instance HLe (HSucc x) y = HLt x y
hLe :: Proxy x -> Proxy y -> Proxy (HLe x y)
hLe :: Proxy x -> Proxy y -> Proxy (HLe x y)
hLe Proxy x
_ Proxy y
_ = Proxy (HLe x y)
forall k (t :: k). Proxy t
Proxy
type family HDiv2 (x :: HNat) :: HNat
type instance HDiv2 HZero = HZero
type instance HDiv2 (HSucc HZero) = HZero
type instance HDiv2 (HSucc (HSucc a)) = HSucc (HDiv2 a)
data HNothing = HNothing deriving Int -> HNothing -> ShowS
[HNothing] -> ShowS
HNothing -> String
(Int -> HNothing -> ShowS)
-> (HNothing -> String) -> ([HNothing] -> ShowS) -> Show HNothing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HNothing] -> ShowS
$cshowList :: [HNothing] -> ShowS
show :: HNothing -> String
$cshow :: HNothing -> String
showsPrec :: Int -> HNothing -> ShowS
$cshowsPrec :: Int -> HNothing -> ShowS
Show
newtype HJust x = HJust x deriving Int -> HJust x -> ShowS
[HJust x] -> ShowS
HJust x -> String
(Int -> HJust x -> ShowS)
-> (HJust x -> String) -> ([HJust x] -> ShowS) -> Show (HJust x)
forall x. Show x => Int -> HJust x -> ShowS
forall x. Show x => [HJust x] -> ShowS
forall x. Show x => HJust x -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HJust x] -> ShowS
$cshowList :: forall x. Show x => [HJust x] -> ShowS
show :: HJust x -> String
$cshow :: forall x. Show x => HJust x -> String
showsPrec :: Int -> HJust x -> ShowS
$cshowsPrec :: forall x. Show x => Int -> HJust x -> ShowS
Show
class HEq (x :: k) (y :: k) (b :: Bool) | x y -> b
type HEqK (x :: k1) (y :: k2) (b :: Bool) = HEq (Proxy x) (Proxy y) b
#if NEW_TYPE_EQ
instance ((Proxy x == Proxy y) ~ b) => HEq x y b
#endif
hEq :: HEq x y b => x -> y -> Proxy b
hEq :: x -> y -> Proxy b
hEq x
_ y
_ = Proxy b
forall k (t :: k). Proxy t
Proxy
class HEqByFn f => HEqBy (f :: t) (x :: k) (y :: k) (b :: Bool) | f x y -> b
class HEqByFn f
type Arity f n = (ArityFwd f n, ArityRev f n)
class ArityFwd (f :: *) (n :: HNat) | f -> n
class ArityRev (f :: *) (n :: HNat)
instance ArityRev f HZero
instance (xf ~ (x -> f), ArityRev f n) => ArityRev xf (HSucc n)
class HCast x y where
hCast :: x -> Maybe y
instance (HEq x y b, HCast1 b x y) => HCast x y where
hCast :: x -> Maybe y
hCast = Proxy b -> x -> Maybe y
forall (b :: Bool) x y. HCast1 b x y => Proxy b -> x -> Maybe y
hCast1 (Proxy b
forall k (t :: k). Proxy t
Proxy :: Proxy b)
class HCast1 (b :: Bool) x y where
hCast1 :: Proxy b -> x -> Maybe y
instance (x ~ y) => HCast1 True x y where
hCast1 :: Proxy 'True -> x -> Maybe y
hCast1 Proxy 'True
_ x
x = x -> Maybe x
forall a. a -> Maybe a
Just x
x
instance HCast1 False x y where
hCast1 :: Proxy 'False -> x -> Maybe y
hCast1 Proxy 'False
_ x
_ = Maybe y
forall a. Maybe a
Nothing
class Fail (x :: k)
#if __GLASGOW_HASKELL__ >= 800
type ErrText x = GHC.TypeLits.Text x
type ErrShowType x = GHC.TypeLits.ShowType x
instance TypeError x => Fail x
#else
type ErrText x = x
type ErrShowType x = x
type x :<>: y = '(x,y)
type x :$$: y = '(x,y)
infixl 6 :<>:
infixl 5 :$$:
#endif
type FieldNotFound key collection = ErrText "key" :<>: ErrShowType key
:$$: ErrText "could not be found in" :<>: ErrShowType collection
type ExcessFieldFound key collection = ErrText "found field" :<>: ErrShowType key
:$$: ErrText "when it should be absent from" :<>: ErrShowType collection
type HNatIndexTooLarge (nat :: HNat) (r :: [k] -> *) (xs :: [k]) =
ErrText "0-based index" :<>: ErrShowType (HNat2Nat nat) :<>:
ErrText "is too large for collection"
:$$: ErrShowType (r xs)
type x = ErrText "extra field" :<>: ErrShowType x
#if OLD_TYPEABLE
type TypeablePolyK a = (() :: Constraint)
#else
type TypeablePolyK (a :: k) = Typeable a
#endif
class SameLength' (es1 :: [k]) (es2 :: [m])
instance (es2 ~ '[]) => SameLength' '[] es2
instance (SameLength' xs ys, es2 ~ (y ': ys)) => SameLength' (x ': xs) es2
class (SameLength' x y, SameLength' y x) =>
SameLength (x :: [k]) (y :: [m]) where
sameLength :: r x `p` f (q y) -> r x `p` f (q y)
sameLength = p (r x) (f (q y)) -> p (r x) (f (q y))
forall a. a -> a
id
asLengthOf :: SameLength x y => r x -> s y -> r x
asLengthOf :: r x -> s y -> r x
asLengthOf = r x -> s y -> r x
forall a b. a -> b -> a
const
instance (SameLength' x y, SameLength' y x) => SameLength x y
type family SameLengths (xs :: [[k]]) :: Constraint
type instance SameLengths (x ': y ': ys) = (SameLength x y, SameLengths (y ': ys))
type instance SameLengths '[] = ()
type instance SameLengths '[x] = ()
class SameLabels (x :: k) (y :: m)
sameLabels :: SameLabels x y => p (r x) (f (q y)) -> p (r x) (f (q y))
sameLabels :: p (r x) (f (q y)) -> p (r x) (f (q y))
sameLabels = p (r x) (f (q y)) -> p (r x) (f (q y))
forall a. a -> a
id
instance SameLabels '[] '[]
instance SameLabels '[] (x ': xs)
instance SameLabels (x ': xs) '[]
instance (SameLabels x y, SameLabels xs ys) =>
SameLabels (x ': xs) (y ': ys)
instance (Label t ~ Label t') => SameLabels (Label t) (Tagged t' a)
instance (Label t ~ Label t') => SameLabels (Label t) (Label t')
instance (Label t ~ Label t') => SameLabels (Label t) (t' :: Symbol)
instance SameLabels (Label t) s => SameLabels (t :: Symbol) s
instance SameLabels (Label t) s => SameLabels (Tagged t a) s
class HAllTaggedLV (ps :: [*])
instance HAllTaggedLV '[]
instance (HAllTaggedLV xs, x ~ Tagged t v) => HAllTaggedLV (x ': xs)
type family ZipTagged (ts :: [k]) (vs :: [*]) :: [*]
type instance ZipTagged (Label t ': ts) (v ': vs) = Tagged t v ': ZipTagged ts vs
type instance ZipTagged ((t :: Symbol) ': ts) (v ': vs) = Tagged t v ': ZipTagged ts vs
type instance ZipTagged '[] '[] = '[]