# Effects detected in a seemingly pure function

**URL:** <https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272>\
**Category:** Google Group\
**Created:** [March 29, 2014, 3:52am UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272 "2014-03-29T03:52:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahab\_Tasharrofi](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@Shahab\_Tasharrofi](https://discourse.ats-lang.org/u/Shahab_Tasharrofi)\
**Post date:** [March 29, 2014, 3:52am UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272/1 "2014-03-29T03:52:29Z")

</div>

I have implemented a comparison function to be used in the merge sort on  
lists as below

implement list\_mergesort$cmp\<Tuple0(arity)\> (x1, x2) =  
let  
fun loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) : int  
case+ cols of  
| list\_nil() =\> 0  
| list\_cons( (\_, indices), rest) =\>  
case+ indices of  
| list\_cons(index, \_) =\>  
if (x1[index] \> x2[index]) then 1  
else if (x1[index] \< x2[index]) then ~1  
else loop(rest)  
| \_ =/=\> loop(rest)  
in  
loop(extracted\_cols)  
end

The ATS compiler reports that, in the first call to loop (i.e., the line  
before end), some  
disallowed effects may be incurred. By searching in the mailing list, I  
found that  
$effmask\_all(…) can be used to mask all the effects but I could not find  
how it is  
used. So, my first question is how $effmask\_all is used.

My second question is what effect is the ATS compiler detecting? The exact  
error  
message I am getting is:  
the\_effenv\_check\_set: some disallowed effects may be incurred: 1  
which does not seem to contain any information about the kind of effect I  
should be  
looking for.

Thanks,  
Shahab

---

<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 29, 2014, 1:38pm UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272/2 "2014-03-29T13:38:16Z")

</div>

| \_ =/=\> loop(rest)

should probably be changed to

| \_ =/=\> ()

Actually, it can be eliminated as the compiler knows  
this is an unreachable case.On Saturday, March 29, 2014 9:35:08 AM UTC-4, gmhwxi wrote:

> The following declaration essentially indicates to the compiler that no  
> effect-tracking  
> is needed for ‘loop’:
> 
> loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) : int
> 
> If you change it to
> 
> loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) :\<\> int
> 
> then the code should type-check.
> 
> On Friday, March 28, 2014 11:52:29 PM UTC-4, Shahab Tasharrofi wrote:
> 
> > I have implemented a comparison function to be used in the merge sort on  
> > lists as below
> > 
> > implement list\_mergesort$cmp\<Tuple0(arity)\> (x1, x2) =  
> > let  
> > fun loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) :  
> > int =  
> > case+ cols of  
> > | list\_nil() =\> 0  
> > | list\_cons( (\_, indices), rest) =\>  
> > case+ indices of  
> > | list\_cons(index, \_) =\>  
> > if (x1[index] \> x2[index]) then 1  
> > else if (x1[index] \< x2[index]) then ~1  
> > else loop(rest)  
> > | \_ =/=\> loop(rest)  
> > in  
> > loop(extracted\_cols)  
> > end
> > 
> > The ATS compiler reports that, in the first call to loop (i.e., the line  
> > before end), some  
> > disallowed effects may be incurred. By searching in the mailing list, I  
> > found that  
> > $effmask\_all(…) can be used to mask all the effects but I could not  
> > find how it is  
> > used. So, my first question is how $effmask\_all is used.
> > 
> > My second question is what effect is the ATS compiler detecting? The  
> > exact error  
> > message I am getting is:  
> > the\_effenv\_check\_set: some disallowed effects may be incurred: 1  
> > which does not seem to contain any information about the kind of effect I  
> > should be  
> > looking for.
> > 
> > Thanks,  
> > Shahab

---

<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 29, 2014, 1:35pm UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272/3 "2014-03-29T13:35:08Z")

</div>

The following declaration essentially indicates to the compiler that no  
effect-tracking  
is needed for ‘loop’:

loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) : int

If you change it to

loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) :\<\> int

then the code should type-check.On Friday, March 28, 2014 11:52:29 PM UTC-4, Shahab Tasharrofi wrote:

> I have implemented a comparison function to be used in the merge sort on  
> lists as below
> 
> implement list\_mergesort$cmp\<Tuple0(arity)\> (x1, x2) =  
> let  
> fun loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) :  
> int =  
> case+ cols of  
> | list\_nil() =\> 0  
> | list\_cons( (\_, indices), rest) =\>  
> case+ indices of  
> | list\_cons(index, \_) =\>  
> if (x1[index] \> x2[index]) then 1  
> else if (x1[index] \< x2[index]) then ~1  
> else loop(rest)  
> | \_ =/=\> loop(rest)  
> in  
> loop(extracted\_cols)  
> end
> 
> The ATS compiler reports that, in the first call to loop (i.e., the line  
> before end), some  
> disallowed effects may be incurred. By searching in the mailing list, I  
> found that  
> $effmask\_all(…) can be used to mask all the effects but I could not  
> find how it is  
> used. So, my first question is how $effmask\_all is used.
> 
> My second question is what effect is the ATS compiler detecting? The exact  
> error  
> message I am getting is:  
> the\_effenv\_check\_set: some disallowed effects may be incurred: 1  
> which does not seem to contain any information about the kind of effect I  
> should be  
> looking for.
> 
> Thanks,  
> Shahab

---

<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 29, 2014, 7:34pm UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272/4 "2014-03-29T19:34:36Z")

</div>

Effect-tracking is designed for something else. For your code, I would  
suggest:

implement list\_mergesort$cmp\<Tuple0(arity)\> (x1, x2) =  
let  
fun loop {n:nat} .. (cols:list(@(Nat, List1(NatLt(arity))), n)) : int  
case+ cols of  
| list\_nil() =\> 0  
| list\_cons( (\_, indices), rest) =\>  
case+ indices of  
| list\_cons(index, \_) =\>  
if (x1[index] \> x2[index]) then 1  
else if (x1[index] \< x2[index]) then ~1  
else loop(rest)  
| \_ =/=\> loop(rest)  
in  
$effmask\_all(loop(extracted\_cols))  
endOn Saturday, March 29, 2014 3:15:28 PM UTC-4, Shahab Tasharrofi wrote:

> Changing the code “:\<\> int” as you pointed out, moved the type-checking  
> error  
> to comparisons (i.e., x1[index] and x2[index]) which makes sense since they  
> are overloaded with method arrayref\_get\_at\_gint which has a reference  
> effect in  
> its output.
> 
> Now, my question is that, since I am using x1[index] and x2[index] as right  
> values, how can I make sure that the reference effects are not passed  
> along to  
> the loop function?

---

<div class="post-metadata">

**Author:** ![Shahab\_Tasharrofi](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@Shahab\_Tasharrofi](https://discourse.ats-lang.org/u/Shahab_Tasharrofi)\
**Post date:** [March 29, 2014, 7:15pm UTC](https://discourse.ats-lang.org/t/effects-detected-in-a-seemingly-pure-function/272/5 "2014-03-29T19:15:28Z")

</div>

Changing the code “:\<\> int” as you pointed out, moved the type-checking  
error  
to comparisons (i.e., x1[index] and x2[index]) which makes sense since they  
are overloaded with method arrayref\_get\_at\_gint which has a reference  
effect in  
its output.

Now, my question is that, since I am using x1[index] and x2[index] as right  
values, how can I make sure that the reference effects are not passed along  
to  
the loop function?
