# Operator resolution

**URL:** <https://discourse.ats-lang.org/t/operator-resolution/734>\
**Category:** Google Group\
**Created:** [March 8, 2013, 4:21pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734 "2013-03-08T16:21:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [March 8, 2013, 4:21pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/1 "2013-03-08T16:21:22Z")

</div>

I don’t doubt that I have a few more type checking errors in store for me  
in the current version of list\_min below (which ideally I would implement  
in a more polymorphic manner). Right now I’m not sure about this error  
about operator overloading (code highlighted in yellow).

This more simple case works fine, so I haven’t captured the essence of the  
problem:  
var a: double = 1.00  
var b: double = 2.79

val x = if a \< b then 1 else 0

error(3): the symbol [\<] cannot be resolved: there are too many matches:  
lt\_string\_string, lt\_ssize1\_int1, …  
exit(ATS): uncaught exception:  
\_2fhome\_2ffac2\_2fhwxi\_2fresearch\_2fATS\_2fIMPLEMENT\_2fGeizella\_2fAnairiats\_2fsvn\_2fats\_2dlang\_2fsrc\_2fats\_error\_2esats\_\_FatalErrorException(1233)

fn find (  
SDmap: &map (string, double), k: string, res: &double?  
) : void = () where {  
val b = linmap\_search (SDmap, k, lam(x,y) =\> compare\_string\_string(x,y),  
res)  
val () = if b then let  
prval () = opt\_unsome {double} (res)  
in  
print “Some(”; print res; print ")“  
end else let  
prval () = opt\_unnone {double} (res) in ($raise InvalidCase; print  
"None()”)  
end // end of [val]  
val () = print\_newline ()  
} // end of [find]

fun list\_min (inlist: List string, emap: &map(string,double)): Option\_vt  
string = let  
fun loop(cmin: string, rlist: List string, emap: &map(string,double)):  
string = case+ rlist of  
| list\_cons (x, xs) =\> let  
var xval: double  
val \_ = find(emap, x, xval)  
var cminval: double  
val \_ = find(emap, cmin, cminval)  
in  
if cminval \< xval then loop(cmin,xs,emap) else loop(x,xs,emap)  
end  
| list\_nil () =\> cmin  
in case+ inlist of  
| list\_cons (x, xs) =\> Some\_vt(loop(x,xs))  
| list\_nil () =\> None\_vt ()  
end // end of [list\_min]

---

<div class="post-metadata">

**Author:** ![Chris\_Double](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@Chris\_Double](https://discourse.ats-lang.org/u/Chris_Double)\
**Post date:** [March 8, 2013, 9:22pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/2 "2013-03-08T21:22:36Z")

</div>

> I admit I’m not sure why it isn’t what you suggest, but when I change it  
> to that I get a new error:

It needs to be changed as Hongwei suggests otherwise ‘cminval’ and ‘xval’  
are of type ‘double?’ which is an uninitialized double. There would be no  
comparison operator defined for that. Making the changes results in the  
other type error you paste. This is because the ‘find’ function now expects  
the ‘res’ parameter to be an initialized ‘double’ on completion. But the  
case where the ‘linmap\_search’ fails results in an unintialized double (a  
‘double?’).

You either need to propogate the ‘linmap\_search’ failure through the result  
of ‘find’ and handle it by the caller, or handle it in ‘find’ by setting it  
to some sentinel value in the case where linmap\_search fails. This will  
probably also necessitate adding a type hint to the ‘if’. Something like:

if :(res: double) =\> b then let  
prval () = opt\_unsome {double} (res)  
in  
print “Some(”; print res; print “)”  
end else let  
prval () = opt\_unnone {double} (res)  
val () = res := -1.0  
in ($raise InvalidCase; print “None()”)  
end

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 10, 2013, 12:19am UTC](https://discourse.ats-lang.org/t/operator-resolution/734/3 "2013-03-10T00:19:10Z")

</div>

‘-’ -\> ‘~’

In ATS, ‘~’ stands for the negative sign.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 8, 2013, 5:44pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/4 "2013-03-08T17:44:58Z")

</div>

fn find (  
SDmap: &map (string, double), k: string, res: &double?  
) : void

Should it be ‘res: &double? \>\> double’ ?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 9, 2013, 1:14pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/5 "2013-03-09T13:14:38Z")

</div>

prval () = linset\_free (…)  
^^^^^

‘prval’ should be replaced with ‘val’; otherwise, there will be memory leak.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [July 11, 2013, 2:53pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/6 "2013-07-11T14:53:01Z")

</div>

Would it be better for this sort of mistake to emit an error? While in my  
case it was originally done out of ignorance, I can see it being a  
plausible typo.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [March 9, 2013, 11:52pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/7 "2013-03-09T23:52:13Z")

</div>

Thanks for all the feedback; I’ve mostly gone through with abstract  
interfaces for set and map (working on finishing map now).

I’m still having a bit of trouble with find, changing it around a bit:  
extern  
fun gDMap\_find(mp: &gDMap, k: string): double

implement  
gDMap\_find (mp, k): double = let  
var res: double?  
val b = $LM.linmap\_search (mp, k, lam(x,y)  
=\> compare\_string\_string(x,y), res)  
in  
if: (res:double) =\> b then let  
prval () = opt\_unsome {double} (res)  
val \_ = $showtype res  
in res end  
else let  
prval () = opt\_unnone {double} (res)  
in ($raise InvalidCase; -1.0) end  
end // end of [gDMap\_find]

Now i’m getting:

error(1): fixity cannot be resolved.

(highlighted above)

Also if you know of any good examples for ref-counted absvtypes, I’ll try  
and have a look (thanks for the abstract type examples).

One other thing, not sure I understand “(res:double) =\> b” completely, but  
I assume this means something like “assert res is a double”.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [March 8, 2013, 6:15pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/8 "2013-03-08T18:15:41Z")

</div>

I basically copied the function “find” from  
./ats-lang-anairiats-0.2.9/doc/EXAMPLE/TEST/libats\_linmap\_avltree.dats.

I admit I’m not sure why it isn’t what you suggest, but when I change it to  
that I get a new error:  
/media/RAID5/share/ATS\_learning/toCNF4.dats: 14846(line=554, offs=14) –  
15195(line=564, offs=2): error(3): mismatch of static terms (tyleq):  
The needed term is: S2Eapp(S2Ecst(at\_viewt0ype\_addr\_view); S2Ecst(double),  
S2Evar(res(11138)))  
The actual term is: S2Eapp(S2Ecst(at\_viewt0ype\_addr\_view); S2Etop(0;  
S2Ecst(double\_t0ype)), S2Evar(res(11138)))  
/media/RAID5/share/ATS\_learning/toCNF4.dats: 14846(line=554, offs=14) –  
15195(line=564, offs=2): error(3): mismatch of static terms (tyleq):  
The needed term is: S2Ecst(double\_t0ype)  
The actual term is: S2Etop(0; S2Ecst(double\_t0ype))

If I call $showtype on xval before and after using find, it reports  
S2Etop(0; S2Ecst(double\_t0ype))

So the book-keeping seems to be in order as far as that goes.

---

<div class="post-metadata">

**Author:** ![Chris\_Double](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@Chris\_Double](https://discourse.ats-lang.org/u/Chris_Double)\
**Post date:** [March 8, 2013, 10:50pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/9 "2013-03-08T22:50:19Z")

</div>

local

assume myset = linset (…)  
val cmp = lam (x, y) = …

in  
…  
end

Is there a similar idiom to this that works with ‘cloptr’ to avoid use of  
the garbage collector, assuming a ‘linset’ that takes a cloptr instead of a  
cloref?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [April 28, 2013, 9:29pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/10 "2013-04-28T21:29:55Z")

</div>

Please add no space between ‘:’ and ‘\<’.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [July 11, 2013, 2:53pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/11 "2013-07-11T14:53:52Z")

</div>

I was referring to:

prval () = linset\_free (…)  
^^^^^

‘prval’ should be replaced with ‘val’; otherwise, there will be memory leak.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [March 8, 2013, 6:22pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/12 "2013-03-08T18:22:44Z")

</div>

Full code attached, if it helps.

[cnf4.zip](https://us1.discourse-cdn.com/flex019/uploads/ats_lang/original/1X/54d081d891a8cdd2f65cbb75e753c6313a1c638a.zip) (7.5 KB)

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 9, 2013, 1:10pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/13 "2013-03-09T13:10:18Z")

</div>

I remember seeing something like:

prval () = linset\_free (…)

in your code. This will lead to memory leak. Please use ‘val’ for 'prval’  
instead.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 8, 2013, 9:50pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/14 "2013-03-08T21:50:11Z")

</div>

I took a look at your code.

It is great to see that you have made so much progress.

There is one issue that I would like to point out now, which I think is  
vital for you to  
continue making progress.

First, this is the time to introduce some abstract types. You use linmap  
and linset directly,  
which is a style that I suggest you to avoid as much as possible.

Please introduce abstract types like myset and mymap, and then implement  
them based on  
linset and linap. Please take a look how this is done in the following code:

> <https://github.com/githwxi/ATS-Postiats/blob/master/src/pats_dynexp2.sats>

  

> <https://github.com/githwxi/ATS-Postiats/blob/master/src/pats_dynexp2_dvar.dats>

If you use ATS properly, the language can really do wonders for you.

Hongwei

PS: I also have the feeling that GRexp should not be defined as a  
dataviewtype; it should be  
abstract and its values should be ref-counted.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [April 28, 2013, 9:13pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/15 "2013-04-28T21:13:14Z")

</div>

I see now. When using , why would something like the following:

fun loopCOV {n:nat} (COV: matrix(double,n,n), rlist: List(string), imap:  
&gIdxMap n, ccov: double): double = let…

result in:

error(1): fixity cannot be resolved.

I thought the “1” in cloref1 would allow any effect, or does fixity mean  
something else here?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [July 11, 2013, 3:12pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/16 "2013-07-11T15:12:05Z")

</div>

Right now, this typo causes the compiler to put the token PMVerr in the  
emitted code (if it is not stopped earlier).  
By looking at the location info associated with PMVerr, you can readily  
identify the source of the problem.

Yes, we can probably do better later.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [March 8, 2013, 9:56pm UTC](https://discourse.ats-lang.org/t/operator-resolution/734/17 "2013-03-08T21:56:41Z")

</div>

Declaration:

absviewtype myset  
fun myset\_union (xs: myset, ys: myset): myset  
overload + with myset\_union

Implementation:

local

assume myset = linset (…)  
val cmp = lam (x, y) = …

in (\* in of [local] \*)

implement  
myset\_union (xs, ys) = linset\_union (xs, ys, cmp)

end // …
