[Haskell-cafe] Views of data types

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Thu Aug 4 11:23:19 UTC 2016


On Thu, Aug 04, 2016 at 12:16:51PM +0200, Corentin Dupont wrote:
> 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...

Why not just write a function 'Input a -> Input Int' and serialise the
result?


More information about the Haskell-Cafe mailing list