{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
module Data.Type.Predicate.Quantification (
Any, WitAny(..), None, anyImpossible
, decideAny, idecideAny, decideNone, idecideNone
, entailAny, ientailAny, entailAnyF, ientailAnyF
, All, WitAll(..), NotAll
, decideAll, idecideAll
, entailAll, ientailAll, entailAllF, ientailAllF
, decideEntailAll, idecideEntailAll
, allToAny
, allNotNone, noneAllNot
, anyNotNotAll, notAllAnyNot
) where
import Data.Kind
import Data.Singletons
import Data.Singletons.Decide
import Data.Type.Functor.Product
import Data.Type.Predicate
import Data.Type.Predicate.Logic
import Data.Type.Universe
idecideNone
:: forall f k (p :: k ~> Type) (as :: f k). Universe f
=> (forall a. Elem f as a -> Sing a -> Decision (p @@ a))
-> (Sing as -> Decision (None f p @@ as))
idecideNone :: forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Decision (None f p @@ as)
idecideNone forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a)
f Sing as
xs = forall {k1} (p :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (Not p @@ a)
decideNot @(Any f p) forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Decision (Any f p @@ as)
idecideAny forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a)
f Sing as
xs
decideNone
:: forall f k (p :: k ~> Type). Universe f
=> Decide p
-> Decide (None f p)
decideNone :: forall (f :: * -> *) k (p :: k ~> *).
Universe f =>
Decide p -> Decide (None f p)
decideNone Decide p
f = forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Decision (None f p @@ as)
idecideNone (forall a b. a -> b -> a
const Decide p
f)
ientailAny
:: forall f p q as. (Universe f, SingI as)
=> (forall a. Elem f as a -> Sing a -> p @@ a -> q @@ a)
-> Any f p @@ as
-> Any f q @@ as
ientailAny :: forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k).
(Universe f, SingI as) =>
(forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a)
-> (Any f p @@ as) -> Any f q @@ as
ientailAny forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a
f (WitAny Elem f as a1
i p @@ a1
x) = forall {k} (f :: * -> *) (b :: f k) (a1 :: k) (a :: k ~> *).
Elem f b a1 -> (a @@ a1) -> WitAny f a b
WitAny Elem f as a1
i (forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a
f Elem f as a1
i (forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f as a1
i forall {k} (a :: k). SingI a => Sing a
sing) p @@ a1
x)
entailAny
:: forall f p q. Universe f
=> (p --> q)
-> (Any f p --> Any f q)
entailAny :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
Universe f =>
(p --> q) -> Any f p --> Any f q
entailAny = forall {k1} {k2} (f :: (k1 ~> *) -> k2 ~> *) (p :: k1 ~> *)
(q :: k1 ~> *).
TFunctor f =>
(p --> q) -> f p --> f q
tmap @(Any f)
ientailAll
:: forall f p q as. (Universe f, SingI as)
=> (forall a. Elem f as a -> Sing a -> p @@ a -> q @@ a)
-> All f p @@ as
-> All f q @@ as
ientailAll :: forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k).
(Universe f, SingI as) =>
(forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a)
-> (All f p @@ as) -> All f q @@ as
ientailAll forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a
f All f p @@ as
a = forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> p @@ a) -> WitAll f p as
WitAll forall a b. (a -> b) -> a -> b
$ \Elem f as a
i -> forall (a :: k). Elem f as a -> Sing a -> (p @@ a) -> q @@ a
f Elem f as a
i (forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f as a
i forall {k} (a :: k). SingI a => Sing a
sing) (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll All f p @@ as
a Elem f as a
i)
entailAll
:: forall f p q. Universe f
=> (p --> q)
-> (All f p --> All f q)
entailAll :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
Universe f =>
(p --> q) -> All f p --> All f q
entailAll = forall {k1} {k2} (f :: (k1 ~> *) -> k2 ~> *) (p :: k1 ~> *)
(q :: k1 ~> *).
TFunctor f =>
(p --> q) -> f p --> f q
tmap @(All f)
ientailAnyF
:: forall f p q as h. Functor h
=> (forall a. Elem f as a -> p @@ a -> h (q @@ a))
-> Any f p @@ as
-> h (Any f q @@ as)
ientailAnyF :: forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k) (h :: * -> *).
Functor h =>
(forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a))
-> (Any f p @@ as) -> h (Any f q @@ as)
ientailAnyF forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a)
f = \case WitAny Elem f as a1
i p @@ a1
x -> forall {k} (f :: * -> *) (b :: f k) (a1 :: k) (a :: k ~> *).
Elem f b a1 -> (a @@ a1) -> WitAny f a b
WitAny Elem f as a1
i forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a)
f Elem f as a1
i p @@ a1
x
entailAnyF
:: forall f p q h. (Universe f, Functor h)
=> (p --># q) h
-> (Any f p --># Any f q) h
entailAnyF :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *) (h :: * -> *).
(Universe f, Functor h) =>
(-->#) p q h -> (-->#) (Any f p) (Any f q) h
entailAnyF (-->#) p q h
f Sing a
x Any f p @@ a
a = forall {k} (n :: k) r. Sing n -> (SingI n => r) -> r
withSingI Sing a
x forall a b. (a -> b) -> a -> b
$
forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k) (h :: * -> *).
Functor h =>
(forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a))
-> (Any f p @@ as) -> h (Any f q @@ as)
ientailAnyF @f @p @q (\Elem f a a
i -> (-->#) p q h
f (forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f a a
i Sing a
x)) Any f p @@ a
a
ientailAllF
:: forall f p q as h. (Universe f, Applicative h, SingI as)
=> (forall a. Elem f as a -> p @@ a -> h (q @@ a))
-> All f p @@ as
-> h (All f q @@ as)
ientailAllF :: forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k) (h :: * -> *).
(Universe f, Applicative h, SingI as) =>
(forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a))
-> (All f p @@ as) -> h (All f q @@ as)
ientailAllF forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a)
f All f p @@ as
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) {k} (p :: Predicate k) (g :: k -> *)
(as :: f k).
Universe f =>
(forall (a :: k). g a -> p @@ a) -> Prod f g as -> All f p @@ as
prodAll forall {k1} (p :: k1 ~> *) (a :: k1). Wit p a -> p @@ a
getWit)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: * -> *) (m :: * -> *) (as :: f k) (g :: k -> *)
(h :: k -> *).
(FProd f, Applicative m) =>
(forall (a :: k). Elem f as a -> g a -> m (h a))
-> Prod f g as -> m (Prod f h as)
itraverseProd (\Elem f as a
i Sing a
_ -> forall {k1} (p :: k1 ~> *) (a :: k1). (p @@ a) -> Wit p a
Wit @q forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a)
f Elem f as a
i (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll All f p @@ as
a Elem f as a
i))
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) {k} (as :: f k).
FProd f =>
Sing as -> Prod f Sing as
singProd (forall {k} (a :: k). SingI a => Sing a
sing @as)
entailAllF
:: forall f p q h. (Universe f, Applicative h)
=> (p --># q) h
-> (All f p --># All f q) h
entailAllF :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *) (h :: * -> *).
(Universe f, Applicative h) =>
(-->#) p q h -> (-->#) (All f p) (All f q) h
entailAllF (-->#) p q h
f Sing a
x All f p @@ a
a = forall {k} (n :: k) r. Sing n -> (SingI n => r) -> r
withSingI Sing a
x forall a b. (a -> b) -> a -> b
$
forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k) (h :: * -> *).
(Universe f, Applicative h, SingI as) =>
(forall (a :: k). Elem f as a -> (p @@ a) -> h (q @@ a))
-> (All f p @@ as) -> h (All f q @@ as)
ientailAllF @f @p @q (\Elem f a a
i -> (-->#) p q h
f (forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f a a
i Sing a
x)) All f p @@ a
a
idecideEntailAll
:: forall f p q as. (Universe f, SingI as)
=> (forall a. Elem f as a -> p @@ a -> Decision (q @@ a))
-> All f p @@ as
-> Decision (All f q @@ as)
idecideEntailAll :: forall {k} (f :: * -> *) (p :: Predicate k) (q :: Predicate k)
(as :: f k).
(Universe f, SingI as) =>
(forall (a :: k). Elem f as a -> (p @@ a) -> Decision (q @@ a))
-> (All f p @@ as) -> Decision (All f q @@ as)
idecideEntailAll forall (a :: k). Elem f as a -> (p @@ a) -> Decision (q @@ a)
f All f p @@ as
a = forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Decision (All f p @@ as)
idecideAll (\Elem f as a
i Sing a
_ -> forall (a :: k). Elem f as a -> (p @@ a) -> Decision (q @@ a)
f Elem f as a
i (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll All f p @@ as
a Elem f as a
i)) forall {k} (a :: k). SingI a => Sing a
sing
decideEntailAll
:: forall f p q. Universe f
=> p -?> q
-> All f p -?> All f q
decideEntailAll :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
Universe f =>
(p -?> q) -> All f p -?> All f q
decideEntailAll = forall {k1} {k2} (f :: (k1 ~> *) -> k2 ~> *) (p :: k1 ~> *)
(q :: k1 ~> *).
DFunctor f =>
(p -?> q) -> f p -?> f q
dmap @(All f)
anyImpossible :: Universe f => Any f Impossible --> Impossible
anyImpossible :: forall {k} (f :: * -> *).
Universe f =>
Any f Impossible --> Impossible
anyImpossible Sing a
_ (WitAny Elem f a a1
i Impossible @@ a1
p) = Impossible @@ a1
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f a a1
i
anyNotNotAll :: Any f (Not p) --> NotAll f p
anyNotNotAll :: forall {k} (f :: * -> *) (p :: Predicate k).
Any f (Not p) --> NotAll f p
anyNotNotAll Sing a
_ (WitAny Elem f a a1
i Not p @@ a1
v) WitAll f p a
a = Not p @@ a1
v forall a b. (a -> b) -> a -> b
$ forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll WitAll f p a
a Elem f a a1
i
notAllAnyNot
:: forall f p. (Universe f, Decidable p)
=> NotAll f p --> Any f (Not p)
notAllAnyNot :: forall {k} (f :: * -> *) (p :: k ~> *).
(Universe f, Decidable p) =>
NotAll f p --> Any f (Not p)
notAllAnyNot Sing a
xs NotAll f p @@ a
vAll = forall a. Decision a -> Refuted (Refuted a) -> a
elimDisproof (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @(Any f (Not p)) Sing a
xs) forall a b. (a -> b) -> a -> b
$ \Refuted (Any f (Not p) @@ a)
vAny ->
NotAll f p @@ a
vAll forall a b. (a -> b) -> a -> b
$ forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> p @@ a) -> WitAll f p as
WitAll forall a b. (a -> b) -> a -> b
$ \Elem f a a
i ->
forall a. Decision a -> Refuted (Refuted a) -> a
elimDisproof (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @p (forall {k} (f :: * -> *) (as :: f k) (a :: k).
FProd f =>
Elem f as a -> Sing as -> Sing a
indexSing Elem f a a
i Sing a
xs)) forall a b. (a -> b) -> a -> b
$ \Refuted (p @@ a)
vP ->
Refuted (Any f (Not p) @@ a)
vAny forall a b. (a -> b) -> a -> b
$ forall {k} (f :: * -> *) (b :: f k) (a1 :: k) (a :: k ~> *).
Elem f b a1 -> (a @@ a1) -> WitAny f a b
WitAny Elem f a a
i Refuted (p @@ a)
vP
allNotNone :: All f (Not p) --> None f p
allNotNone :: forall {k} (f :: * -> *) (p :: Predicate k).
All f (Not p) --> None f p
allNotNone Sing a
_ All f (Not p) @@ a
a (WitAny Elem f a a1
i p @@ a1
v) = forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll All f (Not p) @@ a
a Elem f a a1
i p @@ a1
v
noneAllNot
:: forall f p. (Universe f, Decidable p)
=> None f p --> All f (Not p)
noneAllNot :: forall {k} (f :: * -> *) (p :: k ~> *).
(Universe f, Decidable p) =>
None f p --> All f (Not p)
noneAllNot Sing a
xs None f p @@ a
vAny = forall a. Decision a -> Refuted (Refuted a) -> a
elimDisproof (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @(All f (Not p)) Sing a
xs) forall a b. (a -> b) -> a -> b
$ \Refuted (All f (Not p) @@ a)
vAll ->
Refuted (All f (Not p) @@ a)
vAll forall a b. (a -> b) -> a -> b
$ forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> p @@ a) -> WitAll f p as
WitAll forall a b. (a -> b) -> a -> b
$ \Elem f a a
i Apply p a
p -> None f p @@ a
vAny forall a b. (a -> b) -> a -> b
$ forall {k} (f :: * -> *) (b :: f k) (a1 :: k) (a :: k ~> *).
Elem f b a1 -> (a @@ a1) -> WitAny f a b
WitAny Elem f a a
i Apply p a
p
allToAny :: (All f p &&& NotNull f) --> Any f p
allToAny :: forall {k} (f :: * -> *) (p :: Predicate k).
(All f p &&& NotNull f) --> Any f p
allToAny Sing a
_ (WitAll f p a
a, WitAny Elem f a a1
i Evident @@ a1
_) = forall {k} (f :: * -> *) (b :: f k) (a1 :: k) (a :: k ~> *).
Elem f b a1 -> (a @@ a1) -> WitAny f a b
WitAny Elem f a a1
i forall a b. (a -> b) -> a -> b
$ forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitAll f p as -> forall (a :: k). Elem f as a -> p @@ a
runWitAll WitAll f p a
a Elem f a a1
i