简体   繁体   English

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

[英]How to use the singletons library to define a HasRep instance for existentially quantified types?

I'd like to use existentially sized image windows for a couple of reasons: 我想使用存在大小的图像窗口有几个原因:

  • I'd like to pack different size windows into the same list. 我想将不同大小的窗口打包到同一个列表中。
  • I'd like to make a Zip instance for my window type. 我想为我的窗口类型创建一个Zip实例。

I'm using this code to do this: 我正在使用此代码执行此操作:

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

Now, I need to make a HasRep instance for my SomeWind type: 现在,我需要为SomeWind类型创建一个HasRep实例:

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)

I have a nagging hunch that I ought to be able to use the singletons library to help me replace the "???" 我有一种唠叨的预感,我应该能够使用单身人士库来帮助我取代“???” above with a correct definition, but I can't figure out how. 上面有正确的定义,但我无法弄清楚如何。

Can anyone help? 有人可以帮忙吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM