如何使用单例库为存在的量化类型定义HasRep实例?

时间:2018-11-14 18:44:24

标签: haskell existential-type singleton-type representable

出于以下两个原因,我想使用现有尺寸的图像窗口:

  • 我想将不同大小的窗口打包到同一列表中。
  • 我想为我的窗口类型创建一个Zip实例。

我正在使用此代码执行此操作:

-- | A "matrix" represented as a composition of sized vectors.
type Mat h w = Vector h :.: Vector w

-- | A handy alias for locating a window in the 2D plane.
type Orig = (Integer,Integer)

-- | An attempt at using `singletons` to define windows.
data Wind :: Nat -> Nat -> Type -> Type where  -- height -> width
  Wind
    :: (KnownNat h, KnownNat w)
    => { img  :: Mat h w a  -- ^ image data matrix
       , ll   :: Orig       -- ^ position of lower-left corner
       , dflt :: a          -- ^ out-of-bounds value
       } -> Wind h w a

data SomeWind a :: Type where
  MkSomeWind :: Sing h -> Sing w -> Wind h w a -> SomeWind a

现在,我需要为我的HasRep类型创建一个SomeWind实例:

import qualified ConCat.Rep as R

instance R.HasRep (SomeWind a) where
  type Rep (SomeWind a) = ???
  repr (MkSomeWind _ _ (Wind im orig def)) = (im, (orig, def))
  abst (im :: (Vector h :.: Vector w) a, (orig, def)) =
    MkSomeWind (SNat :: Sing h) (SNat :: Sing w) (Wind im orig def)

我有点hun愧,应该能够使用 singletons 库来帮助我替换“ ???”。上面有正确的定义,但我不知道怎么做。

有人可以帮忙吗?

0 个答案:

没有答案