Dependent sample applications "ZipVector"

I made myself a style ZipVector Applicativeon the finite Vector, which uses the sum type to glue the finite vectors in Unit, which model the "infinite" vectors.

data ZipVector a = Unit a | ZipVector (Vector a)
             deriving (Show, Eq)

instance Functor ZipVector where
  fmap f (Unit a)  = Unit (f a)
  fmap f (ZipVector va) = ZipVector (fmap f va)

instance Applicative ZipVector where
  pure = Unit
  Unit f   <*> p        = fmap f p
  pf       <*> Unit x   = fmap ($ x) pf
  ZipVector vf <*> ZipVector vx = ZipVector $ V.zipWith ($) vf vx

This will probably be enough for my needs, but I just wanted the "Fixed Size" to be modeled in applicative instances that you can get with the dependent "Vector" types.

data Point d a = Point (Vector a) deriving (Show, Eq)

instance Functor (Point d) where
  fmap f (Point va) = Point (fmap f va)

instance Applicative Point where
  pure = Vector.replicate reifiedDimension
  Point vf <*> Point vx = Point $ V.zipWith ($) vf vx

where the dphantom parameter is a type Nat. How can I (if possible) write reifiedDimensionin Haskell? Moreover, if possible, provided (Point v1) :: Point d1 aand (Point v2) :: Point d2 ahow can I receive length v1 == length v2, can I receive d1 ~ d2?

+5
1

( ) reifiedDimension Haskell?

GHC.TypeLits ScopedTypeVariables:

instance SingI d => Applicative (Point d) where
  pure = Point . Vector.replicate reifiedDimension
    where reifiedDimension = fromInteger $ fromSing (sing :: Sing d)
  ...

. .

, , (Point v1) :: Point d1 a (Point v2) :: Point d2 a, length v1 == length v2, d1 ~ d2?

Data.Vector, no. , . , , , Point.

+4

All Articles