{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE TypeInType          #-}
{-# LANGUAGE TypeOperators       #-}

-- |
-- Module      : Data.Type.Universe.Subset
-- Copyright   : (c) Justin Le 2018
-- License     : BSD3
--
-- Maintainer  : justin@jle.im
-- Stability   : experimental
-- Portability : non-portable
--
-- Represent a decidable subset of a type-level collection.
--
module Data.Type.Universe.Subset (
  -- * Subset
    Subset, WitSubset(..)
  , makeSubset
  -- ** Subset manipulation
  , intersection, union, symDiff, mergeSubset, imergeSubset
  , mapSubset, imapSubset
  -- ** Subset extraction
  , subsetToList
  -- ** Subset tests
  , subsetToAny, subsetToAll, subsetToNone
  -- ** Subset construction
  , 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

-- | A @'WitSubset' f p @@ as@ describes a /decidable/ subset of type-level
-- collection @as@.
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)
    }

-- | A @'Subset' f p@ is a predicate that some decidable subset of an input
-- @as@ is true.
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)

-- | Create a 'Subset' from a predicate.
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)

-- | Turn a 'Subset' into a list (or any 'Alternative') of satisfied
-- predicates.
--
-- List is meant to include no duplicates.
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

-- | Restrict a 'Subset' to a single (arbitrary) member, or fail if none
-- exists.
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

-- | Construct an empty subset.
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

-- | Construct a full subset
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

-- | Test if a subset is empty.
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

-- | Combine two subsets based on a decision function
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)

-- | Combine two subsets based on a decision function
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)

-- | Subset intersection
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

-- | Subset union (left-biased)
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

-- | Symmetric subset difference
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

-- | Test if a subset is equal to the entire original collection
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

-- | 'mapSubset', but providing an 'Elem'.
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)

-- | Map a bidirectional implication over a subset described by that
-- implication.
--
-- Implication needs to be bidirectional, or otherwise we can't produce
-- a /decidable/ subset as a result.
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))