{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
module Data.Type.Universe.Subset (
Subset, WitSubset(..)
, makeSubset
, intersection, union, symDiff, mergeSubset, imergeSubset
, mapSubset, imapSubset
, subsetToList
, subsetToAny, subsetToAll, subsetToNone
, emptySubset, fullSubset
) where
import Control.Applicative
import Data.Kind
import Data.Monoid (Alt(..))
import Data.Singletons
import Data.Singletons.Decide
import Data.Type.Functor.Product
import Data.Type.Predicate
import Data.Type.Predicate.Logic
import Data.Type.Predicate.Quantification
import Data.Type.Universe
newtype WitSubset f p (as :: f k) = WitSubset
{ forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset :: forall a. Elem f as a -> Decision (p @@ a)
}
data Subset f :: (k ~> Type) -> (f k ~> Type)
type instance Apply (Subset f p) as = WitSubset f p as
instance (Universe f, Decidable p) => Decidable (Subset f p)
instance (Universe f, Decidable p) => Provable (Subset f p) where
prove :: Prove (Subset f p)
prove = forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Subset f p @@ as
makeSubset @f @_ @p (\Elem f a a
_ -> forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @p)
makeSubset
:: forall f k p (as :: f k). Universe f
=> (forall a. Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as
-> Subset f p @@ as
makeSubset :: forall (f :: * -> *) k (p :: k ~> *) (as :: f k).
Universe f =>
(forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a))
-> Sing as -> Subset f p @@ as
makeSubset forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ a)
f Sing as
xs = forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> Decision (p @@ a))
-> WitSubset f p as
WitSubset forall a b. (a -> b) -> a -> b
$ \Elem f as a
i -> forall (a :: k). Elem f as a -> Sing a -> Decision (p @@ 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 Sing as
xs)
subsetToList
:: forall f p t. (Universe f, Alternative t)
=> (Subset f p --># Any f p) t
subsetToList :: forall {k} (f :: * -> *) (p :: k ~> *) (t :: * -> *).
(Universe f, Alternative t) =>
(-->#) (Subset f p) (Any f p) t
subsetToList Sing a
xs Subset f p @@ a
s = forall {k} (f :: k -> *) (a :: k). Alt f a -> f a
getAlt forall a b. (a -> b) -> a -> b
$ (forall (f :: * -> *) k (as :: f k) m.
(FProd f, Monoid m) =>
(forall (a :: k). Elem f as a -> Sing a -> m) -> Sing as -> m
`ifoldMapSing` Sing a
xs) forall a b. (a -> b) -> a -> b
$ \Elem f a a
i Sing a
_ -> forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Alt forall a b. (a -> b) -> a -> b
$ case forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ a
s Elem f a a
i of
Proved p @@ a
p -> forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 p @@ a
p
Disproved Refuted (p @@ a)
_ -> forall (f :: * -> *) a. Alternative f => f a
empty
subsetToAny
:: forall f p. Universe f
=> Subset f p -?> Any f p
subsetToAny :: forall {k} (f :: * -> *) (p :: k ~> *).
Universe f =>
Subset f p -?> Any f p
subsetToAny Sing a
xs Subset f p @@ a
s = 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 (\Elem f a a
i Sing a
_ -> forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ a
s Elem f a a
i) Sing a
xs
emptySubset :: forall f as. (Universe f, SingI as) => Subset f Impossible @@ as
emptySubset :: forall {k} (f :: * -> *) (as :: f k).
(Universe f, SingI as) =>
Subset f Impossible @@ as
emptySubset = forall {k1} (p :: k1 ~> *). Provable p => Prove p
prove @(Subset f Impossible) forall {k} (a :: k). SingI a => Sing a
sing
fullSubset :: forall f as. (Universe f, SingI as) => Subset f Evident @@ as
fullSubset :: forall {k} (f :: * -> *) (as :: f k).
(Universe f, SingI as) =>
Subset f Evident @@ as
fullSubset = forall {k1} (p :: k1 ~> *). Provable p => Prove p
prove @(Subset f Evident) forall {k} (a :: k). SingI a => Sing a
sing
subsetToNone :: forall f p. Universe f => Subset f p -?> None f p
subsetToNone :: forall {k} (f :: * -> *) (p :: k ~> *).
Universe f =>
Subset f p -?> None f p
subsetToNone Sing a
xs Subset f p @@ a
s = 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 (\Elem f a a
i Sing a
_ -> forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ a
s Elem f a a
i) Sing a
xs
imergeSubset
:: forall f k p q r (as :: f k). ()
=> (forall a. Elem f as a -> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> Subset f p @@ as
-> Subset f q @@ as
-> Subset f r @@ as
imergeSubset :: forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
imergeSubset forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a)
f Subset f p @@ as
ps Subset f q @@ as
qs = forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> Decision (p @@ a))
-> WitSubset f p as
WitSubset forall a b. (a -> b) -> a -> b
$ \Elem f as a
i ->
forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a)
f Elem f as a
i (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ as
ps Elem f as a
i) (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f q @@ as
qs Elem f as a
i)
mergeSubset
:: forall f k p q r (as :: f k). ()
=> (forall a. Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> Subset f p @@ as
-> Subset f q @@ as
-> Subset f r @@ as
mergeSubset :: forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
mergeSubset forall (a :: k).
Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a)
f = forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
imergeSubset (\(Elem f as a
_ :: Elem f as a) Decision (p @@ a)
p -> forall (a :: k).
Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a)
f @a Decision (p @@ a)
p)
intersection
:: forall f p q. ()
=> ((Subset f p &&& Subset f q) --> Subset f (p &&& q))
intersection :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
(Subset f p &&& Subset f q) --> Subset f (p &&& q)
intersection Sing a
_ = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
imergeSubset forall a b. (a -> b) -> a -> b
$ \(Elem f a a
_ :: Elem f as a) -> forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p &&& q) @@ a)
decideAnd @p @q @a
union
:: forall f p q. ()
=> ((Subset f p &&& Subset f q) --> Subset f (p ||| q))
union :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
(Subset f p &&& Subset f q) --> Subset f (p ||| q)
union Sing a
_ = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
imergeSubset forall a b. (a -> b) -> a -> b
$ \(Elem f a a
_ :: Elem f as a) -> forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ||| q) @@ a)
decideOr @p @q @a
symDiff
:: forall f p q. ()
=> ((Subset f p &&& Subset f q) --> Subset f (p ^^^ q))
symDiff :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
(Subset f p &&& Subset f q) --> Subset f (p ^^^ q)
symDiff Sing a
_ = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) k (p :: k ~> *) (q :: k ~> *) (r :: k ~> *)
(as :: f k).
(forall (a :: k).
Elem f as a
-> Decision (p @@ a) -> Decision (q @@ a) -> Decision (r @@ a))
-> (Subset f p @@ as) -> (Subset f q @@ as) -> Subset f r @@ as
imergeSubset forall a b. (a -> b) -> a -> b
$ \(Elem f a a
_ :: Elem f as a) -> forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ^^^ q) @@ a)
decideXor @p @q @a
subsetToAll
:: forall f p. Universe f
=> Subset f p -?> All f p
subsetToAll :: forall {k} (f :: * -> *) (p :: k ~> *).
Universe f =>
Subset f p -?> All f p
subsetToAll Sing a
xs Subset f p @@ a
s = 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 a a
i Sing a
_ -> forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ a
s Elem f a a
i) Sing a
xs
imapSubset
:: (forall a. Elem f as a -> p @@ a -> q @@ a)
-> (forall a. Elem f as a -> q @@ a -> p @@ a)
-> Subset f p @@ as
-> Subset f q @@ as
imapSubset :: forall {k} (f :: * -> *) (as :: f k) (p :: k ~> *) (q :: k ~> *).
(forall (a :: k). Elem f as a -> (p @@ a) -> q @@ a)
-> (forall (a :: k). Elem f as a -> (q @@ a) -> p @@ a)
-> (Subset f p @@ as)
-> Subset f q @@ as
imapSubset forall (a :: k). Elem f as a -> (p @@ a) -> q @@ a
f forall (a :: k). Elem f as a -> (q @@ a) -> p @@ a
g Subset f p @@ as
s = forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
(forall (a :: k). Elem f as a -> Decision (p @@ a))
-> WitSubset f p as
WitSubset forall a b. (a -> b) -> a -> b
$ \Elem f as a
i ->
forall a b. (a -> b) -> (b -> a) -> Decision a -> Decision b
mapDecision (forall (a :: k). Elem f as a -> (p @@ a) -> q @@ a
f Elem f as a
i) (forall (a :: k). Elem f as a -> (q @@ a) -> p @@ a
g Elem f as a
i) (forall k (f :: * -> *) (p :: k ~> *) (as :: f k).
WitSubset f p as
-> forall (a :: k). Elem f as a -> Decision (p @@ a)
runWitSubset Subset f p @@ as
s Elem f as a
i)
mapSubset
:: Universe f
=> (p --> q)
-> (q --> p)
-> (Subset f p --> Subset f q)
mapSubset :: forall {k} (f :: * -> *) (p :: k ~> *) (q :: k ~> *).
Universe f =>
(p --> q) -> (q --> p) -> Subset f p --> Subset f q
mapSubset p --> q
f q --> p
g Sing a
xs = forall {k} (n :: k) r. Sing n -> (SingI n => r) -> r
withSingI Sing a
xs forall a b. (a -> b) -> a -> b
$
forall {k} (f :: * -> *) (as :: f k) (p :: k ~> *) (q :: k ~> *).
(forall (a :: k). Elem f as a -> (p @@ a) -> q @@ a)
-> (forall (a :: k). Elem f as a -> (q @@ a) -> p @@ a)
-> (Subset f p @@ as)
-> Subset f q @@ as
imapSubset (\Elem f a a
i -> p --> q
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
xs))
(\Elem f a a
i -> q --> p
g (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))