clash-lang / clash-prelude

CLaSH prelude library containing datatypes and functions for circuit design
http://www.clash-lang.org/
Other
31 stars 27 forks source link

Comonad instance for Vec #71

Open ggreif opened 8 years ago

ggreif commented 8 years ago

This is a (low-prio) feature request. Comonad interface is very convenient for looking at a vector from all perspectives (indices) at once. It allows tranformations of the entire vector by allowing the mapping to peek into neighbouring elements . Think blurring of pixels 0, or other image transformations 1. Writing down the instance is easy:

{-# language DeriveFunctor, TupleSections #-}

module CoVec where

import CLaSH.Prelude hiding (extend)
import Control.Comonad

newtype CoVec n a = CoVec (Index n, Vec n a) deriving (Eq, Show, Functor)

instance KnownNat n => Comonad (CoVec n) where
  extract (CoVec (i, v)) = v !! i
  duplicate (CoVec (i, v)) = CoVec (i, CoVec . (,v) <$> indicesI)

The question is whether one wants to add the Comonad dependency.

Another problem might be the consumption of logical resources (LUTs). I did not evaluate this issue.