ekmett / adjunctions

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

Editable representable functors #30

Open treeowl opened 8 years ago

treeowl commented 8 years ago

In general,

ixSureDef   :: (Representable f, Eq (Rep f)) => Rep f -> Lens' (f a) a
ixSureDef i f fa = (\x' -> Rep.tabulate (\k -> if k == i then x' else Rep.index fa k)) <$> f (Rep.index fa i)

Of course, this is horrifically inefficient. Is there a good place here for a subclass supporting such an operation? Alternatively (or additionally), is there room in lens for an IxSure class similar to Ixed but providing a Lens instead of a Traversal?

Icelandjack commented 7 years ago

You mention the type class Ixed with a Lens, this page on the wiki Varying lens properties by instance shows a funny approach

class Ixed_ xs where
  type IxedConstraint_ xs :: (Type -> Type) -> Constraint
  type IxedConstraint_ xs = Applicative

  ix_ :: Index xs -> IxedConstraint_ xs f => LensLike' f xs (IxValue xs)

indicated by a newtype

newtype WrappedIxedRep r a = WrapIxedRep (r a)
  deriving newtype
    (Functor, Representable)

instance Representable r => Distributive (WrappedIxedRep r) where
  distribute = distributeRep

type instance IxValue (WrappedIxedRep r a) = a
type instance Index   (WrappedIxedRep r a) = Rep r

instance (Representable r, Eq (Rep r)) => Ixed_ (WrappedIxedRep r a) where
  type IxedConstraint_ (WrappedIxedRep r a) = Functor

  ix_ :: Rep r -> Lens' (WrappedIxedRep r a) a
  ix_ = ixSureDef