module Exinst.Internal
(
Some1(Some1)
, some1
, fromSome1
, _Some1
, withSome1
, withSome1Sing
, some1SingRep
, same1
, Dict1(dict1)
, Some2(Some2)
, some2
, fromSome2
, _Some2
, withSome2
, withSome2Sing
, some2SingRep
, same2
, Dict2(dict2)
, Some3(Some3)
, some3
, fromSome3
, _Some3
, withSome3
, withSome3Sing
, some3SingRep
, same3
, Dict3(dict3)
, Some4(Some4)
, some4
, fromSome4
, _Some4
, withSome4
, withSome4Sing
, some4SingRep
, same4
, Dict4(dict4)
, Dict0(dict0)
) where
import Data.Constraint
import Data.Kind (Type)
import Data.Profunctor (dimap, Choice(right'))
import Data.Singletons
import Data.Singletons.Decide
import Data.Type.Equality
import Prelude
data Some1 (f1 :: k1 -> Type) = forall a1.
Some1 !(Sing a1) !(f1 a1)
data Some2 (f2 :: k2 -> k1 -> Type) = forall a2 a1.
Some2 !(Sing a2) !(Sing a1) !(f2 a2 a1)
data Some3 (f3 :: k3 -> k2 -> k1 -> Type) = forall a3 a2 a1.
Some3 !(Sing a3) !(Sing a2) !(Sing a1) !(f3 a3 a2 a1)
data Some4 (f4 :: k4 -> k3 -> k2 -> k1 -> Type) = forall a4 a3 a2 a1.
Some4 !(Sing a4) !(Sing a3) !(Sing a2) !(Sing a1) !(f4 a4 a3 a2 a1)
some1
:: forall (f1 :: k1 -> Type) a1
. SingI a1
=> f1 a1
-> Some1 f1
some1 = Some1 (sing :: Sing a1)
some2
:: forall (f2 :: k2 -> k1 -> Type) a2 a1
. (SingI a2, SingI a1)
=> f2 a2 a1
-> Some2 f2
some2 = Some2 (sing :: Sing a2) (sing :: Sing a1)
some3
:: forall (f3 :: k3 -> k2 -> k1 -> Type) a3 a2 a1
. (SingI a3, SingI a2, SingI a1)
=> f3 a3 a2 a1
-> Some3 f3
some3 = Some3 (sing :: Sing a3) (sing :: Sing a2) (sing :: Sing a1)
some4
:: forall (f4 :: k4 -> k3 -> k2 -> k1 -> Type) a4 a3 a2 a1
. (SingI a4, SingI a3, SingI a2, SingI a1)
=> f4 a4 a3 a2 a1
-> Some4 f4
some4 = Some4 (sing :: Sing a4) (sing :: Sing a3)
(sing :: Sing a2) (sing :: Sing a1)
withSome1
:: forall (f1 :: k1 -> Type) (r :: Type)
. Some1 f1
-> (forall a1. SingI a1 => f1 a1 -> r)
-> r
withSome1 s1 g = withSome1Sing s1 (\_ -> g)
withSome2
:: forall (f2 :: k2 -> k1 -> Type) (r :: Type)
. Some2 f2
-> (forall a2 a1. (SingI a2, SingI a1) => f2 a2 a1 -> r)
-> r
withSome2 s2 g = withSome2Sing s2 (\_ _ -> g)
withSome3
:: forall (f3 :: k3 -> k2 -> k1 -> Type) (r :: Type)
. Some3 f3
-> (forall a3 a2 a1. (SingI a3, SingI a2, SingI a1) => f3 a3 a2 a1 -> r)
-> r
withSome3 s3 g = withSome3Sing s3 (\_ _ _ -> g)
withSome4
:: forall (f4 :: k4 -> k3 -> k2 -> k1 -> Type) (r :: Type)
. Some4 f4
-> (forall a4 a3 a2 a1
. (SingI a4, SingI a3, SingI a2, SingI a1)
=> f4 a4 a3 a2 a1 -> r)
-> r
withSome4 s4 g = withSome4Sing s4 (\_ _ _ _ -> g)
withSome1Sing
:: forall (f1 :: k1 -> Type) (r :: Type)
. Some1 f1
-> (forall a1. (SingI a1) => Sing a1 -> f1 a1 -> r)
-> r
withSome1Sing (Some1 sa1 x) g = withSingI sa1 (g sa1 x)
withSome2Sing
:: forall (f2 :: k2 -> k1 -> Type) (r :: Type)
. Some2 f2
-> (forall a2 a1. (SingI a2, SingI a1) => Sing a2 -> Sing a1 -> f2 a2 a1 -> r)
-> r
withSome2Sing (Some2 sa2 sa1 x) g = withSingI sa2 (withSingI sa1 (g sa2 sa1 x))
withSome3Sing
:: forall (f3 :: k3 -> k2 -> k1 -> Type) (r :: Type)
. Some3 f3
-> (forall a3 a2 a1
. (SingI a3, SingI a2, SingI a1)
=> Sing a3 -> Sing a2 -> Sing a1 -> f3 a3 a2 a1 -> r)
-> r
withSome3Sing (Some3 sa3 sa2 sa1 x) g =
withSingI sa3 (withSingI sa2 (withSingI sa1 (g sa3 sa2 sa1 x)))
withSome4Sing
:: forall (f4 :: k4 -> k3 -> k2 -> k1 -> Type) (r :: Type)
. Some4 f4
-> (forall a4 a3 a2 a1
. (SingI a4, SingI a3, SingI a2, SingI a1)
=> Sing a4 -> Sing a3 -> Sing a2 -> Sing a1 -> f4 a4 a3 a2 a1 -> r)
-> r
withSome4Sing (Some4 sa4 sa3 sa2 sa1 x) g =
withSingI sa4 (withSingI sa3 (withSingI sa2 (withSingI sa1
(g sa4 sa3 sa2 sa1 x))))
fromSome1
:: forall (f1 :: k1 -> Type) a1
. (SingI a1, SDecide k1)
=> Some1 f1
-> Maybe (f1 a1)
fromSome1 = \(Some1 sa1' x) -> do
Refl <- testEquality sa1' (sing :: Sing a1)
return x
fromSome2
:: forall (f2 :: k2 -> k1 -> Type) a2 a1
. ( SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Some2 f2
-> Maybe (f2 a2 a1)
fromSome2 = \(Some2 sa2' sa1' x) -> do
Refl <- testEquality sa2' (sing :: Sing a2)
Refl <- testEquality sa1' (sing :: Sing a1)
return x
fromSome3
:: forall (f3 :: k3 -> k2 -> k1 -> Type) a3 a2 a1
. ( SingI a3, SDecide k3
, SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Some3 f3
-> Maybe (f3 a3 a2 a1)
fromSome3 = \(Some3 sa3' sa2' sa1' x) -> do
Refl <- testEquality sa3' (sing :: Sing a3)
Refl <- testEquality sa2' (sing :: Sing a2)
Refl <- testEquality sa1' (sing :: Sing a1)
return x
fromSome4
:: forall (f4 :: k4 -> k3 -> k2 -> k1 -> Type) a4 a3 a2 a1
. ( SingI a4, SDecide k4
, SingI a3, SDecide k3
, SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Some4 f4
-> Maybe (f4 a4 a3 a2 a1)
fromSome4 = \(Some4 sa4' sa3' sa2' sa1' x) -> do
Refl <- testEquality sa4' (sing :: Sing a4)
Refl <- testEquality sa3' (sing :: Sing a3)
Refl <- testEquality sa2' (sing :: Sing a2)
Refl <- testEquality sa1' (sing :: Sing a1)
return x
_Some1
:: forall (f1 :: k1 -> Type) a1
. (SingI a1, SDecide k1)
=> Prism' (Some1 f1) (f1 a1)
_Some1 = prism' some1 fromSome1
_Some2
:: forall (f2 :: k2 -> k1 -> Type) a2 a1
. ( SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Prism' (Some2 f2) (f2 a2 a1)
_Some2 = prism' some2 fromSome2
_Some3
:: forall (f3 :: k3 -> k2 -> k1 -> Type) a3 a2 a1
. ( SingI a3, SDecide k3
, SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Prism' (Some3 f3) (f3 a3 a2 a1)
_Some3 = prism' some3 fromSome3
_Some4
:: forall (f4 :: k4 -> k3 -> k2 -> k1 -> Type) a4 a3 a2 a1
. ( SingI a4, SDecide k4
, SingI a3, SDecide k3
, SingI a2, SDecide k2
, SingI a1, SDecide k1 )
=> Prism' (Some4 f4) (f4 a4 a3 a2 a1)
_Some4 = prism' some4 fromSome4
some1SingRep
:: SingKind k1
=> Some1 (f1 :: k1 -> Type)
-> Demote k1
some1SingRep = \(Some1 sa1 _) -> fromSing sa1
some2SingRep
:: (SingKind k2, SingKind k1)
=> Some2 (f2 :: k2 -> k1 -> Type)
-> (Demote k2, Demote k1)
some2SingRep = \(Some2 sa2 sa1 _) -> (fromSing sa2, fromSing sa1)
some3SingRep
:: (SingKind k3, SingKind k2, SingKind k1)
=> Some3 (f3 :: k3 -> k2 -> k1 -> Type)
-> (Demote k3, Demote k2, Demote k1)
some3SingRep = \(Some3 sa3 sa2 sa1 _) ->
(fromSing sa3, fromSing sa2, fromSing sa1)
some4SingRep
:: (SingKind k4, SingKind k3, SingKind k2, SingKind k1)
=> Some4 (f4 :: k4 -> k3 -> k2 -> k1 -> Type)
-> (Demote k4, Demote k3, Demote k2, Demote k1)
some4SingRep = \(Some4 sa4 sa3 sa2 sa1 _) ->
(fromSing sa4, fromSing sa3, fromSing sa2, fromSing sa1)
same1
:: SDecide k1
=> (forall a1. SingI a1 => f a1 -> g a1 -> x)
-> Some1 (f :: k1 -> Type)
-> Some1 (g :: k1 -> Type)
-> Maybe x
same1 z = \s1f s1g ->
withSome1Sing s1f $ \sa1 f ->
withSome1Sing s1g $ \sa1' g -> do
Refl <- testEquality sa1 sa1'
pure (z f g)
same2
:: (SDecide k2, SDecide k1)
=> (forall a2 a1. SingI a1 => f a2 a1 -> g a2 a1 -> x)
-> Some2 (f :: k2 -> k1 -> Type)
-> Some2 (g :: k2 -> k1 -> Type)
-> Maybe x
same2 z = \s2l s2g ->
withSome2Sing s2l $ \sa2 sa1 f ->
withSome2Sing s2g $ \sa2' sa1' g -> do
Refl <- testEquality sa2 sa2'
Refl <- testEquality sa1 sa1'
pure (z f g)
same3
:: (SDecide k3, SDecide k2, SDecide k1)
=> (forall a3 a2 a1. (SingI a3, SingI a2, SingI a1)
=> f a3 a2 a1 -> g a3 a2 a1 -> x)
-> Some3 (f :: k3 -> k2 -> k1 -> Type)
-> Some3 (g :: k3 -> k2 -> k1 -> Type)
-> Maybe x
same3 z = \s3l s3g ->
withSome3Sing s3l $ \sa3 sa2 sa1 f ->
withSome3Sing s3g $ \sa3' sa2' sa1' g -> do
Refl <- testEquality sa3 sa3'
Refl <- testEquality sa2 sa2'
Refl <- testEquality sa1 sa1'
pure (z f g)
same4
:: (SDecide k4, SDecide k3, SDecide k2, SDecide k1)
=> (forall a4 a3 a2 a1. (SingI a4, SingI a3, SingI a2, SingI a1)
=> f a4 a3 a2 a1 -> g a4 a3 a2 a1 -> x)
-> Some4 (f :: k4 -> k3 -> k2 -> k1 -> Type)
-> Some4 (g :: k4 -> k3 -> k2 -> k1 -> Type)
-> Maybe x
same4 z = \s4l s4g ->
withSome4Sing s4l $ \sa4 sa3 sa2 sa1 f ->
withSome4Sing s4g $ \sa4' sa3' sa2' sa1' g -> do
Refl <- testEquality sa4 sa4'
Refl <- testEquality sa3 sa3'
Refl <- testEquality sa2 sa2'
Refl <- testEquality sa1 sa1'
pure (z f g)
class Dict0 (c :: k0 -> Constraint) where
dict0 :: Sing a0 -> Dict (c a0)
class Dict1 (c :: k0 -> Constraint) (f1 :: k1 -> k0) where
dict1 :: Sing a1 -> Dict (c (f1 a1))
class Dict2 (c :: k0 -> Constraint) (f2 :: k2 -> k1 -> k0) where
dict2 :: Sing a2 -> Sing a1 -> Dict (c (f2 a2 a1))
class Dict3 (c :: k0 -> Constraint) (f3 :: k3 -> k2 -> k1 -> k0) where
dict3 :: Sing a3 -> Sing a2 -> Sing a1 -> Dict (c (f3 a3 a2 a1))
class Dict4 (c :: k0 -> Constraint) (f4 :: k4 -> k3 -> k2 -> k1 -> k0) where
dict4 :: Sing a4 -> Sing a3 -> Sing a2 -> Sing a1 -> Dict (c (f4 a4 a3 a2 a1))
type Prism s t a b
= forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
type Prism' s a = Prism s s a a
prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
prism bt seta = dimap seta (either pure (fmap bt)) . right'
prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' bs sma = prism bs (\s -> maybe (Left s) Right (sma s))