module Data.HList.HArray where
import Data.HList.FakePrelude
import Data.HList.HList
class HLookupByHNat (n :: HNat) (l :: [*]) where
type HLookupByHNatR (n :: HNat) (l :: [*]) :: *
hLookupByHNat :: Proxy n -> HList l -> HLookupByHNatR n l
instance HLookupByHNat HZero (e ': l) where
type HLookupByHNatR HZero (e ': l) = e
hLookupByHNat _ (HCons e _) = e
instance HLookupByHNat n l => HLookupByHNat (HSucc n) (e ': l) where
type HLookupByHNatR (HSucc n) (e ': l) = HLookupByHNatR n l
hLookupByHNat n (HCons _ l) = hLookupByHNat (hPred n) l
class HDeleteAtHNat (n :: HNat) (l :: [*]) where
type HDeleteAtHNatR (n :: HNat) (l :: [*]) :: [*]
hDeleteAtHNat :: Proxy n -> HList l -> HList (HDeleteAtHNatR n l)
instance HDeleteAtHNat HZero (e ': l) where
type HDeleteAtHNatR HZero (e ': l) = l
hDeleteAtHNat _ (HCons _ l) = l
instance HDeleteAtHNat n l => HDeleteAtHNat (HSucc n) (e ': l) where
type HDeleteAtHNatR (HSucc n) (e ': l) = e ': (HDeleteAtHNatR n l)
hDeleteAtHNat n (HCons e l) = HCons e (hDeleteAtHNat (hPred n) l)
class HUpdateAtHNat (n :: HNat) e (l :: [*]) where
type HUpdateAtHNatR (n :: HNat) e (l :: [*]) :: [*]
hUpdateAtHNat :: Proxy n -> e -> HList l -> HList (HUpdateAtHNatR n e l)
instance HUpdateAtHNat HZero e1 (e ': l) where
type HUpdateAtHNatR HZero e1 (e ': l) = e1 ': l
hUpdateAtHNat _ e1 (HCons _ l) = HCons e1 l
instance HUpdateAtHNat n e1 l => HUpdateAtHNat (HSucc n) e1 (e ': l) where
type HUpdateAtHNatR (HSucc n) e1 (e ': l) = e ': (HUpdateAtHNatR n e1 l)
hUpdateAtHNat n e1 (HCons e l) = HCons e (hUpdateAtHNat (hPred n) e1 l)
instance Fail (FieldNotFound (Proxy n, e1)) => HUpdateAtHNat n e1 '[] where
type HUpdateAtHNatR n e1 '[] = '[]
hUpdateAtHNat _ _ _ = error "Data.HList.HArray.HUpdateAtHNat: Fail must have no instances"
hProjectByHNats' ns l = hMap (FHLookupByHNat l) ns
newtype FHLookupByHNat (l :: [*]) = FHLookupByHNat (HList l)
instance HLookupByHNat n l =>
Apply (FHLookupByHNat l) (Proxy (n :: HNat)) where
type ApplyR (FHLookupByHNat l) (Proxy n) = HLookupByHNatR n l
apply (FHLookupByHNat l) n = hLookupByHNat n l
hProjectByHNats (_ :: Proxy (ns :: [HNat])) l =
hUnfold (FHUProj :: FHUProj True ns) (l,hZero)
data FHUProj (sel :: Bool) (ns :: [HNat]) = FHUProj
instance Apply (FHUProj sel ns) (HList '[],n) where
type ApplyR (FHUProj sel ns) (HList '[],n) = HNothing
apply _ _ = HNothing
instance (ch ~ Proxy (HBoolEQ sel (KMember n ns)),
Apply (ch, FHUProj sel ns) (HList (e ': l),Proxy (n :: HNat))) =>
Apply (FHUProj sel ns) (HList (e ': l),Proxy (n :: HNat)) where
type ApplyR (FHUProj sel ns) (HList (e ': l),Proxy n) =
ApplyR (Proxy (HBoolEQ sel (KMember n ns)), FHUProj sel ns)
(HList (e ': l),Proxy n)
apply fn s = apply (Proxy::ch,fn) s
instance Apply (Proxy True, FHUProj sel ns)
(HList (e ': l),Proxy (n::HNat)) where
type ApplyR (Proxy True, FHUProj sel ns) (HList (e ': l),Proxy n) =
(HJust (e, (HList l,Proxy (HSucc n))))
apply _ (HCons e l,n) = (HJust (e,(l,hSucc n)))
instance (Apply (FHUProj sel ns) (HList l, Proxy (HSucc n))) =>
Apply (Proxy False, FHUProj sel ns)
(HList (e ': l),Proxy (n::HNat)) where
type ApplyR (Proxy False, FHUProj sel ns) (HList (e ': l),Proxy n) =
ApplyR (FHUProj sel ns) (HList l, Proxy (HSucc n))
apply (_,fn) (HCons _ l,n) = apply fn (l,hSucc n)
type family KMember (n :: HNat) (ns :: [HNat]) :: Bool
type instance KMember n '[] = False
type instance KMember n (n1 ': l) = HOr (HNatEq n n1) (KMember n l)
type HProjectByHNatsR (ns :: [HNat]) (l :: [*]) =
HUnfold (FHUProj True ns) (HList l, Proxy 'HZero)
type HProjectByHNatsCtx ns l =
(Apply (FHUProj True ns) (HList l, Proxy 'HZero),
HUnfold' (FHUProj True ns)
(HList l, Proxy 'HZero)
)
hProjectAwayByHNats (_ :: Proxy (ns :: [HNat])) l =
hUnfold (FHUProj :: FHUProj False ns) (l,hZero)
type HProjectAwayByHNatsR (ns :: [HNat]) (l :: [*]) =
HUnfold (FHUProj False ns) (HList l, Proxy 'HZero)
type HProjectAwayByHNatsCtx ns l =
(Apply (FHUProj False ns) (HList l, Proxy 'HZero),
HUnfold' (FHUProj False ns) (HList l, Proxy 'HZero)
)
hSplitByHNats ns l = (hProjectByHNats ns l,
hProjectAwayByHNats ns l)