Strange function

I was toying with an object system in Haskell and I encountered (created?) the following function:

f :: Contains r r' => (r' -> (a,r')) -> (r -> (a,r))

where any value of r contains a value of r': the function is the best answer I have found so far to the problem of "casting" a value of r to its supertype r', doing some work on it that gives a value of type a and a new value of r' and then replacing the resulting r' into the original value of type r. To me this reminds some sort of binding, but I was wondering if anyone here more expert than me could suggest a better interpretation of this operation.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Off Topic

Note: you really should take a question like the above to a Haskell forum. If there was something related to PL you want to point out about a strange function, then by all means it is welcome. If you need help interpreting the function, though, that belongs elsewhere.

Regarding content: your interpretation looks accurate. It looks like r'->(a,r') is representing (on the RHS) a return value and updated object. 'f' is doing an unwrap/update/rewrap and fetching the return value for the update.

I'm not sure I'd consider this part of an object system. You do not have (meaningful) object identity without support for aliasing, and you do not have an object system without object identity, and you cannot have aliasing with the sort of updates implied by the function you described above...

Thanks for the input. I

Thanks for the input. I believe the question is in no way related to Haskell: it is quite general, and it is mostly related to subtyping and its nuances.

Your interpretation is of course accurate in that the above function does an unwrap/update/rewrap. About the OOP, my intuition is that to represent aliasing you need to bind values of a supertype to values of the subtype for operations on the supertype, and if this is done automatically (inside a monad, for example) then you get code that looks very much like the imperative equivalent.

Aliasing

About the OOP, my intuition is that to represent aliasing you need to bind values of a supertype to values of the subtype for operations on the supertype, and if this is done automatically (inside a monad, for example) then you get code that looks very much like the imperative equivalent.

For object aliasing, you need the ability two objects to 'share' reference to a third. I.e. Alice and Charlie each contain a reference to Bob. If Alice effects an update on Bob, then Charlie can observe this update, even if Charlie is not aware of Alice. With the setup you describe, however, Alice would produce a new Bob' that is not shared by Charlie. This has absolutely nothing to do with supertype vs. subtype bindings. This has to do with how updates are expressed.

I lack the context to know whether 'f' is intended for something other than 'object' update. Aliasing (and even mutable 'pointers') has been achieved in Haskell in at least three different ways (that I've read about), and I suspect anyone really interested in Haskell object systems would eventually poke around at prior art.

That's great: could you

That's great: could you provide pointers to these three approaches?

Thanks

For prior art, I would

For prior art, I would suggest studying Yampa (reactive and stateful) and OOHaskell (object-oriented and stateful). If those interest you, they have bibliographies for even more prior art.