[mercury-users] Better syntax for currying and functions (was Re: Mercury syntax?

Lee Naish lee at cs.mu.oz.au
Tue Sep 9 12:50:30 AEST 1997

>Funny this should come up.  I just happened to be thinking today that
>data constructors are conceptually functions, so that
>  :- type tree(T) ---> empty ; tree(tree(T), T, tree(T)).
>is the same as declaring functions `tree__empty()' and
>`tree__tree(tree(T), T, tree(T))' and that

This is certainly useful in functional languages.  For example:

|| O(N) list reversal
reverse = foldl (converse (:)) []	|| : is "cons"

I'm sure the problem with error messages mentioned by Andrew Bromage can
be overcome.  Eg, if the declared type of the argument of a predicate is
first order and the supplied argument is a function then a more
intuitive message (like the current ones) could be given.  I don't think
the nasty error message argument really stands up very well.

>If anything were done with type declarations then I'd prefer that it
>be allowing something like:
>  istype(X, tree(T)):-
>	(
>		X = tree:empty
>	;
>		X = tree:tree(L, V, R),
>		istype(L, tree(T)),
>		istype(V, T),
>		istype(R, tree(T))
>	).

You can also make a natural association between a type and a predicate.
Lets assume the declaration is

:- type tree_type(T) ---> empty ; tree(tree_type(T), T, tree_type(T)).

to avoid overloading tree.  This corresponds to the predicate

tree_type(T, empty).
tree_type(T, tree(A, B, C)) :-
	tree_type(T, A),
	call(T, B),
	tree_type(T, C).

Note the argument order allows us to use call(Type, Term) instead of
a separate is_type predicate.  This predicate gives us a useful
"skeleton" on which to base other predicates over trees (by adding extra
arguments and calls).  Its possible to automatically generate versions
of map, foldr and foldl from the type definition using recent work on
"shape"/"polytypism".  See http://www.cs.mu.oz.au/~lee/papers/hose/
for an extended abstract of a recent paper (a full version will be there
soon) plus some links.


More information about the users mailing list