Creating a closure function for use with linset

I’m trying to create a closure “wrapper” function for strcmp, so that I can
use the function linset_make_nil to make an empty AVL based linset. First,
I’d like to ask why it is necessary to specify that cmp be a cloref, since
I believe there is no data being needed from its environment other than its
two arguments. I believe I may be misunderstanding something fundamental
here.

Here is my attempt:

val cmpstr:cmp(string) = lam(x:string,y:string):int = (strcmp(x,y))

The first error below makes me think I’m almost (exactly) right with types,
but the second error I do not understand ( vs ).

/media/RAID5/share/ATS_learning/stringtest.dats: 1909(line=80, offs=7) –
1927(line=80, offs=25): error(3): mismatch of static terms (tyleq):
The needed term is: S2Efun(clo(-1); 0; 0; S2Ecst(string), S2Ecst(string);
S2Ecst(int))
The actual term is: S2Efun(clo(-1); 0; 0; S2Ecst(string), S2Ecst(string);
S2Ecst(int))
/media/RAID5/share/ATS_learning/stringtest.dats: 1909(line=80, offs=7) –
1927(line=80, offs=25): error(3): mismatch of effects:
The needed term is:
The actual term is:

Thanks,

I think the default is actually non-pure. I use ‘compare_string_string’
when using ‘linset’ on strings. It has the correct return type (strcmp will
give an error on this when you get past the effects issue) and is declared
pure:

fun compare_string_string (s1: string, s2: string):<> Sgn

Chris.
http://www.bluishcoder.co.nz

The needed term is:
The actual term is:

This is referring to the ‘effects’ modifier. That’s the ‘1’ you have at the
end of ‘cloref1’. The ‘1’ states all effects are allowed. It’s expcting no
effects allowed so you want ‘cloref0’. You can let ‘lam’ infer almost
everything btw:

val found = linset_insert (set, key, lam (x,y) => strcmp(x,y))

I agree it’d be nice if it didn’t require a cloref. In every case I’ve used
I’ve used a function. Even better would be for it not to be needed on every
insert, and is_member call. I can’t imagine that function varying over the
lifetime of the set. If it could be a template value (is that possible) or
set on init it’d be easier.

Chris.
http://www.bluishcoder.co.nz

That clears up a lot, I should have noticed (still getting used to effects
notation, and closure notation). When I try your example there are still
problems:

val found = linset_insert (set, key, lam (x,y) => strcmp(x,y))

–>
error(3): the effects [exn, ntm, ref] are disallowed.

Below, I see strcmp does not specify any effects (I thought the default was
pure (i.e. nothing == == ).

fun strcmp (
str1: string, str2: string
) : int = “mac#atslib_strcmp” // end of [strcmp]

Sorry, I had a typo above: I’m trying to use linset_insert here, not
linset_make_nil.

All your wishes will be granted in ATS2 :slight_smile:

By the way, I did encounter a couple cases where the comparison function is
a closure.