{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Generics.MRSOP.Base.Universe where
import Data.Function (on)
import Data.Type.Equality
import Data.Proxy
import Control.Monad
import Generics.MRSOP.Base.NS
import Generics.MRSOP.Base.NP
import Generics.MRSOP.Util
data Atom kon
= K kon
| I Nat
deriving (Eq, Show)
data NA :: (kon -> *) -> (Nat -> *) -> Atom kon -> * where
NA_I :: (IsNat k) => phi k -> NA ki phi (I k)
NA_K :: ki k -> NA ki phi (K k)
mapNA :: (forall k . ki k -> kj k)
-> (forall ix . IsNat ix => f ix -> g ix)
-> NA ki f a -> NA kj g a
mapNA fk fi (NA_I f) = NA_I (fi f)
mapNA fk fi (NA_K k) = NA_K (fk k)
mapNAM :: (Monad m)
=> (forall k . ki k -> m (kj k))
-> (forall ix . IsNat ix => f ix -> m (g ix))
-> NA ki f a -> m (NA kj g a)
mapNAM fk fi (NA_K k) = NA_K <$> fk k
mapNAM fk fi (NA_I f) = NA_I <$> fi f
elimNA :: (forall k . ki k -> b)
-> (forall k . IsNat k => phi k -> b)
-> NA ki phi a -> b
elimNA kp fp (NA_I x) = fp x
elimNA kp fp (NA_K x) = kp x
zipNA :: NA ki f a -> NA kj g a -> NA (ki :*: kj) (f :*: g) a
zipNA (NA_I fk) (NA_I gk) = NA_I (fk :*: gk)
zipNA (NA_K ki) (NA_K kj) = NA_K (ki :*: kj)
eqNA :: (forall k . ki k -> ki k -> Bool)
-> (forall ix . fam ix -> fam ix -> Bool)
-> NA ki fam l -> NA ki fam l -> Bool
eqNA kp fp x = elimNA (uncurry' kp) (uncurry' fp) . zipNA x
newtype Rep (ki :: kon -> *) (phi :: Nat -> *) (code :: [[Atom kon]])
= Rep { unRep :: NS (PoA ki phi) code }
type PoA (ki :: kon -> *) (phi :: Nat -> *) = NP (NA ki phi)
mapRep :: (forall ix . IsNat ix => f ix -> g ix)
-> Rep ki f c -> Rep ki g c
mapRep = bimapRep id
mapRepM :: (Monad m)
=> (forall ix . IsNat ix => f ix -> m (g ix))
-> Rep ki f c -> m (Rep ki g c)
mapRepM = bimapRepM return
bimapRep :: (forall k . ki k -> kj k)
-> (forall ix . IsNat ix => f ix -> g ix)
-> Rep ki f c -> Rep kj g c
bimapRep fk fi = Rep . mapNS (mapNP (mapNA fk fi)) . unRep
bimapRepM :: (Monad m)
=> (forall k . ki k -> m (kj k))
-> (forall ix . IsNat ix => f ix -> m (g ix))
-> Rep ki f c -> m (Rep kj g c)
bimapRepM fk fi = (Rep <$>) . mapNSM (mapNPM (mapNAM fk fi)) . unRep
zipRep :: (MonadPlus m)
=> Rep ki f c -> Rep kj g c
-> m (Rep (ki :*: kj) (f :*: g) c)
zipRep (Rep t) (Rep u)
= Rep . mapNS (mapNP (uncurry' zipNA) . uncurry' zipNP) <$> zipNS t u
elimRepM :: (Monad m)
=> (forall k . ki k -> m a)
-> (forall k . IsNat k => f k -> m a)
-> ([a] -> m b)
-> Rep ki f c -> m b
elimRepM fk fi cat
= cat <.> elimNS (elimNPM (elimNA fk fi)) . unRep
elimRep :: (forall k . ki k -> a)
-> (forall k . f k -> a)
-> ([a] -> b)
-> Rep ki f c -> b
elimRep kp fp cat
= elimNS (cat . elimNP (elimNA kp fp)) . unRep
eqRep :: (forall k . ki k -> ki k -> Bool)
-> (forall ix . fam ix -> fam ix -> Bool)
-> Rep ki fam c -> Rep ki fam c -> Bool
eqRep kp fp t = maybe False (elimRep (uncurry' kp) (uncurry' fp) and)
. zipRep t
data Constr :: [k] -> Nat -> * where
CS :: Constr xs n -> Constr (x : xs) (S n)
CZ :: Constr (x : xs) Z
instance TestEquality (Constr codes) where
testEquality CZ CZ = Just Refl
testEquality (CS x) (CS y) = apply (Refl :: S :~: S) <$> testEquality x y
testEquality _ _ = Nothing
instance (IsNat n) => Show (Constr xs n) where
show _ = "C" ++ show (getNat (Proxy :: Proxy n))
injNS :: Constr sum n -> PoA ki fam (Lkup n sum) -> NS (NP (NA ki fam)) sum
injNS CZ poa = Here poa
injNS (CS c) poa = There (injNS c poa)
inj :: Constr sum n -> PoA ki fam (Lkup n sum) -> Rep ki fam sum
inj c = Rep . injNS c
matchNS :: Constr sum c -> NS (NP (NA ki fam)) sum -> Maybe (PoA ki fam (Lkup c sum))
matchNS CZ (Here ps) = Just ps
matchNS (CS c) (There x) = matchNS c x
matchNS _ _ = Nothing
match :: Constr sum c -> Rep ki fam sum -> Maybe (PoA ki fam (Lkup c sum))
match c (Rep x) = matchNS c x
data View :: (kon -> *) -> (Nat -> *) -> [[ Atom kon ]] -> * where
Tag :: Constr sum n -> PoA ki fam (Lkup n sum) -> View ki fam sum
sop :: Rep ki fam sum -> View ki fam sum
sop = go . unRep
where
go :: NS (NP (NA ki fam)) sum -> View ki fam sum
go (Here poa) = Tag CZ poa
go (There s) = case go s of
Tag c poa -> Tag (CS c) poa
newtype Fix (ki :: kon -> *) (codes :: [[[ Atom kon ]]]) (n :: Nat)
= Fix { unFix :: Rep ki (Fix ki codes) (Lkup n codes) }
proxyFixIdx :: Fix ki fam ix -> Proxy ix
proxyFixIdx _ = Proxy
mapFixM :: (Monad m)
=> (forall k . ki k -> m (kj k))
-> Fix ki fam ix -> m (Fix kj fam ix)
mapFixM fk = (Fix <$>) . bimapRepM fk (mapFixM fk) . unFix
eqFix :: (forall k. ki k -> ki k -> Bool)
-> Fix ki fam ix -> Fix ki fam ix -> Bool
eqFix p = eqRep p (eqFix p) `on` unFix
heqFixIx :: (IsNat ix , IsNat ix')
=> Fix ki fam ix -> Fix ki fam ix' -> Maybe (ix :~: ix')
heqFixIx fa fb = testEquality (getSNat Proxy) (getSNat Proxy)