ekmett / adjunctions

Simple adjunctions
http://hackage.haskell.org/package/adjunctions
Other
44 stars 27 forks source link

List vs. Seq for Rep Cofree #50

Open oisdk opened 6 years ago

oisdk commented 6 years ago

The index for Cofree is currently Seq from Data.Sequence:

https://github.com/ekmett/adjunctions/blob/3040e61f1dcae3fb108cc2e0e03239afcb35479d/src/Data/Functor/Rep.hs#L505-L510

But it looks like the sequence is only ever accessed with consing and unconsing. If so, would it be possible to change the implementation to use lists instead? Maybe something like:

instance Representable f => Representable (Cofree f) where
  type Rep (Cofree f) = [Rep f]
  index = flip (foldr f extract) where
    f x xs ys = xs (index (unwrap ys) x)
  tabulate = coiterW (\f -> tabulate (\x -> f . (:) x))

tabulate could alternatively be:

tabulate = unfold (\f -> (f [], tabulate (\x -> f . (:) x)))
-- Or
tabulate = coiterW (collect (flip (.)) (tabulate (:)))

The extra flexibility and lower overhead of lists would be handy, and I haven't seen any of the extra features of sequences being used with this instance.