[mercury-users] Cautionary Tale About Currying

Ralph Becket rbeck at microsoft.com
Tue Apr 4 21:18:21 AEST 2000


It's easy to forget that Mercury doesn't have full currying,
unlike many other languages from the same stable.  This one
just bit me in the pants...

I'd quite innocently written

	SupportSetOps = list__foldl(
		list__merge_and_remove_dups `compose` snd,
		OpSupportSetPairs,
		[]
	)

where the various object types were (using more concise MLish
type notation):

	compose:                     (Y -> Z) x (X -> Y) x X -> Z
	snd:                         pair(_, W) -> W
	list__merge_and_remove_dups: list(V) x list(V) -> list(V)

The problem is not immediately obvious, but if we work out the
type of the composed function we get:

	list__merge_and_remove_dups `compose` snd: X -> Z
	|                                     |
	|                                     X = pair(_, W)
	|                                     Y = W
      Y = list(Z)
      Z = list(Z) -> list(Z)

i.e. `X -> Z' resolves to `pair(_, list(Z)) -> list(Z) -> list(Z)'
whereas what list__foldl/3 is expecting here is
`pair(_, list(Z)) x list(Z) -> list(Z)'.

The solution is to use an explicit lambda rather than compose/3.
[I wonder whether it's worth extending compose/3 to cover first
arguments of arity greater than one?]  Either way, it's one to
watch out for.

Cheers,

Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list