Open lastland opened 9 years ago
This may not be the best approach for the following reasons:
Unsigned[N+1]
or Unsigned[X+Y]
. This might be too complicated for a compiler.Unsigned[N+1]
but also Unsigned[N+2]
and registers of many other types must accept the addition of Unsigned[N]
s. This means that N+1
must be a subtype or supertype of N
, which does not make much sense.N
of Unsigned[N]
to be known at Scala compile time. This forbids programs from setting the bit-length at Scala runtime (e.g. from user inputs or from files).An alternative strategy might be using refinement types and SMT solver. LiquidHaskell may be able to provide some inspirations for us.
For example, assigning the result of an addition between two 4-bit unsigned numbers to a 4-bit unsigned number would be dangerous, but assigning it to a 5-bit or even bigger unsigned number should be all right. It would be ideal if this sort of rules can be statically enforced at compile time by utilizing the Scala type system.
So instead of
val a: Unsigned = Unsigned(0, 16)
, we may want something like this:The following code will result in type error:
If the users are sure that
a + b
will not overflow in a 4-bit unsigned register, or they intend to let it overflow, they must cast it to 4-bit explicitly.SIP-23 may be able to help in this scenario. However, to achieve what we want, we may need something like this:
It is unclear that if SIP-23 would support expressions like
N+1
to be a type as shown in this case.Indexing and slicing may become another challenge, because it is required to statically infer the type of
a(x, y)
, where the value ofx
or/andy
can only be dynamically determined.