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.
- Passing a constant into a function requires a cast like u8(10) or
u73(12) - 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?