[Haskell-cafe] Views of data types

Corentin Dupont corentin.dupont at gmail.com
Thu Aug 4 10:16:51 UTC 2016


Hi guys,
I have a typeclass like that:

class Signal s where
   type SignalData s :: *


The idea is that when some signal "s" fires, it returns a data of type
(SignalData s).
This is all fine except when I instantiate the class with:

data Input a = Input [(a, String)]

instance Signal (Input a)
   type SignalData (Input a) = a


The concrete type of Input a is not known, so its difficult to store in
lists, to serialize...
So I need to create a "view" for the it:

class (Signal s, Signal v) => SignalView s v where
   view :: s -> v

data InputView = InputView [(Int, String)]

instance Signal InputView
   type SignalData InputView = Int

instance SignalView (Input a) InputView
   view (Input as) = InputView (zip [0…] (snd <$> as))

Is this a common pattern?
I'm not sure I get it right...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160804/40c9f045/attachment.html>


More information about the Haskell-Cafe mailing list