[m-dev.] for review: improvements for record syntax
Simon Taylor
stayl at cs.mu.OZ.AU
Fri Dec 8 19:06:03 AEDT 2000
Fergus wrote:
> However, note that `:=' is not referentially transparent either.
> For example, `Y = X^f1, R = (Y^f2 := Z)' is not
> the same as `R = ((X^f1)^f2 := Z)'!
Yes, that's a bit icky, but probably unavoidable.
It is possible to do referentially transparent field
update of nested fields using update functions which apply
a mapping function to the fields, as shown below.
Unfortunately, there is no nice way to determine
which modes of the `map_field' functions should
be generated. The modes to generate would depend
on the mapping functions that the user wanted
to apply to sub-fields of the field to be updated.
Currently we avoid that problem by converting the
compiler-generated `field/1' and `field :=/2' functions
into unifications before mode-checking, but that won't work
for the `map_field' functions.
The restrictions on taking the address of functions with
multiple modes would also cause problems.
Simon.
:- module field_update.
:- interface.
:- import_module io.
:- pred main(io__state::di, io__state::uo) is det.
:- implementation.
:- type has_f
---> has_f(f :: has_g).
:- type has_g
---> has_g(g :: int).
:- func map_f(func(has_g) = has_g, has_f) = has_f.
map_f(Update, has_f(F)) = has_f(Update(F)).
:- func map_g(func(int) = int, has_g) = has_g.
map_g(Update, has_g(G)) = has_g(Update(G)).
:- func set(T, T) = T.
set(T, _) = T.
main -->
{ X = has_f(has_g(1)) },
{ Z = 2 },
io__write(map_f(map_g(set(Z)), X)),
io__nl.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list