[Haskell-cafe] Re: I just don't get it (data structures and OO)

Arie Peterson ariep at xs4all.nl
Mon Jun 4 21:55:33 EDT 2007


David Menendez wrote:

| That's a neat idiom. I wonder how far one could usefully generalize it.
|
| For example,
|
|     type Ref cx t = forall f. Functor f => (t -> f t) -> cx -> f cx
|
|     newtype Id a = Id { unId :: a }
|     instance Functor Id where fmap f = Id . f . unId
|
|     newtype K t a = K { unK :: t }
|     instance Functor (K t) where fmap = K . unK
|
|
|     select :: Ref cx t -> cx -> t
|    select ref = unK . ref K
|
|     update :: Ref cx t -> (t -> t) -> cx -> cx
|     update ref f = unId . ref (Id . f)
|
|
|     rfst :: Ref (a,b) a
|     rfst f (x,y) = fmap (\x' -> (x',y)) (f x)
|
| In this implementation, composition of Refs is just function
| composition.
|
|     select (rfst . rfst) :: ((a,b),c) -> a

Cool!

Reversely, you can usefully convert "my" Refs to yours:

fRef :: Original.Ref cx t -> David.Ref cx t
fRef r h cx = fmap (($ cx) . update r . const) $ h (select r cx)


Claus Reinke wrote:

| you might find Koji Kagawa's papers interesting:
|
| [...]

Mm, more reading... :-)


Greetings,

Arie



More information about the Haskell-Cafe mailing list