# Driving out the casts

**URL:** <https://discourse.ats-lang.org/t/driving-out-the-casts/556>\
**Category:** Google Group\
**Created:** [November 2, 2015, 2:25pm UTC](https://discourse.ats-lang.org/t/driving-out-the-casts/556 "2015-11-02T14:25:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [November 2, 2015, 2:25pm UTC](https://discourse.ats-lang.org/t/driving-out-the-casts/556/1 "2015-11-02T14:25:05Z")

</div>

I’m trying to drive out all the casts in my code related to uint8 and  
similar. Basically, I have types based on:

{n:int | n \> …

and

uint8

And when I mix them I have a couple of problems.

1. Passing a constant into a function requires a cast like u8(10) or  
u73(12)
2. Passing a uint8 into a function with a parameter like {n:int | …)  
requires a cast like u73(u8(20))

In some cases the constraints, as I like to call them, are equivalent in  
that they they represent a number from 0 to 0xFF, but from a type point of  
view they are not the same.

So, for example, with a function like this:

macdef packetOutCount =  
$extval(ref(uint8),“packetOutCount”)

extern fun{} set\_n {n0: nat | n0 \>= 3 && n0 \<= 127} (count: int(n0)): void  
implement{} set\_n {n0} (count) = let  
val () = !packetOutCount := count  
…  
in  
…  
end

the line below fails to compile:

val () = !packetOutCount := count

but this compiles:

val () = !packetOutCount := u8(count)

count is restricted to values between 3 and 127, so it can’t violate a  
uint8 which is restricted to values between 0 and 255.

So I tried to pick apart uint8 to see what might be the problem:

sortdef uint8 =  
{ i:int | 0 \<= i; i \< 256 }

tkindef  
uint8\_kind = "atstype\_uint8"  
typedef  
uint8\_0 = g0uint (uint8\_kind)  
typedef  
uint8\_1  
(i:int) = g1uint (uint8\_kind, i)  
//  
stadef uint8 = uint8\_1 // 2nd-select  
stadef uint8 = uint8\_0 // 1st-select  
stadef uInt8 = [i:nat] uint8\_1 (i)

I think it has something to do with the meaning of uint8 in:

macdef packetOutCount =  
$extval(ref(uint8),“packetOutCount”)

In the above, what overloaded uint8 is getting used? Is it the uint8\_kind  
or uint8\_0?

Is there a way to setup the macdef in such a way that I don’t have to keep  
casting things?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [November 3, 2015, 2:54am UTC](https://discourse.ats-lang.org/t/driving-out-the-casts/556/2 "2015-11-03T02:54:57Z")

</div>

I’m having problems compile the example. I also tried:

castfn  
u8\_safe {n: nat | n \< 256} (i: int(n)):\<\> uint8(n)

Then I copied an example from the distribution and it failed.

Is there a compiler option or an include I need to use to make castfn  
compile? Anything about the arduino\_ats system that might cause a problem?

---

<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:** [November 2, 2015, 4:48pm UTC](https://discourse.ats-lang.org/t/driving-out-the-casts/556/3 "2015-11-02T16:48:42Z")

</div>

In the following code, a cast is necessary as ‘count’ and ‘u8(count)’ have  
different C-types: int vs. uint8.

Casting can be safely done. For instance, the following cast function  
u8\_safe  
is completely safe:

castfn u8\_safe: {n: nat | n \< 256} int(n) → uint8(n)

Maybe what you really want to do is to replace u8 with u8\_safe.

val () = !packetOutCount := u8(count)On Monday, November 2, 2015 at 9:25:05 AM UTC-5, Mike Jones wrote:

> I’m trying to drive out all the casts in my code related to uint8 and  
> similar. Basically, I have types based on:
> 
> {n:int | n \> …
> 
> and
> 
> uint8
> 
> And when I mix them I have a couple of problems.
> 
> 1. Passing a constant into a function requires a cast like u8(10) or  
> u73(12)
> 2. Passing a uint8 into a function with a parameter like {n:int | …)  
> requires a cast like u73(u8(20))
> 
> In some cases the constraints, as I like to call them, are equivalent in  
> that they they represent a number from 0 to 0xFF, but from a type point of  
> view they are not the same.
> 
> So, for example, with a function like this:
> 
> macdef packetOutCount =  
> $extval(ref(uint8),“packetOutCount”)
> 
> extern fun{} set\_n {n0: nat | n0 \>= 3 && n0 \<= 127} (count: int(n0)): void  
> implement{} set\_n {n0} (count) = let  
> val () = !packetOutCount := count  
> …  
> in  
> …  
> end
> 
> the line below fails to compile:
> 
> val () = !packetOutCount := count
> 
> but this compiles:
> 
> val () = !packetOutCount := u8(count)
> 
> count is restricted to values between 3 and 127, so it can’t violate a  
> uint8 which is restricted to values between 0 and 255.
> 
> So I tried to pick apart uint8 to see what might be the problem:
> 
> sortdef uint8 =  
> { i:int | 0 \<= i; i \< 256 }
> 
> tkindef  
> uint8\_kind = “atstype\_uint8”  
> typedef  
> uint8\_0 = g0uint (uint8\_kind)  
> typedef  
> uint8\_1  
> (i:int) = g1uint (uint8\_kind, i)  
> //  
> stadef uint8 = uint8\_1 // 2nd-select  
> stadef uint8 = uint8\_0 // 1st-select  
> stadef uInt8 = [i:nat] uint8\_1 (i)
> 
> I think it has something to do with the meaning of uint8 in:
> 
> macdef packetOutCount =  
> $extval(ref(uint8),“packetOutCount”)
> 
> In the above, what overloaded uint8 is getting used? Is it the uint8\_kind  
> or uint8\_0?
> 
> Is there a way to setup the macdef in such a way that I don’t have to keep  
> casting things?

---

<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:** [November 3, 2015, 3:02am UTC](https://discourse.ats-lang.org/t/driving-out-the-casts/556/4 "2015-11-03T03:02:33Z")

</div>

You need to have the ‘extern’ keyword if the code is in a DATS file:

extern  
castfn  
u8\_safe {n:nat | n \< 256} (int(n)):\<\> uint8(n)On Monday, November 2, 2015 at 9:54:57 PM UTC-5, Mike Jones wrote:

> I’m having problems compile the example. I also tried:
> 
> castfn  
> u8\_safe {n: nat | n \< 256} (i: int(n)):\<\> uint8(n)
> 
> Then I copied an example from the distribution and it failed.
> 
> Is there a compiler option or an include I need to use to make castfn  
> compile? Anything about the arduino\_ats system that might cause a problem?
