adjunctions icon indicating copy to clipboard operation
adjunctions copied to clipboard

Editable representable functors

Open treeowl opened this issue 7 years ago • 1 comments

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?

treeowl avatar Nov 22 '16 01:11 treeowl

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

Icelandjack avatar Aug 05 '17 20:08 Icelandjack