A special case of pure structures are those that are used to hold arithmetic types. For example, an example pure structure corresponding to a sign-magnitude integer is:
template<pint N> deftype signed_int(bool neg; int<N> val) { }
It is convenient to be able to define arithmetic on signed_int
types. In particular, it would be helpful to be able to write
signed_int<8> a, b; ... chp { ... a := a + b; ... }
If a plus
function is defined for a pure structure, then the addition operator gets mapped to a call to this function. In other words, if we had:
template<pint N> deftype signed_int(bool neg; int<N> val) { methods { function plus (signed_int<N> x) : signed_int<N> { chp { [ neg & x.neg | ~neg & ~x.neg -> self.neg := neg; self.val := x.val + val [] else -> [ x.val > val -> self.val := x.val - val; self.neg := x.neg [] else -> self.val := val - x.val; self.neg := neg ] ] } } } }
Given this defintion, the CHP statement a := a + b
would be translated to a := a.plus (b)
, and then the plus()
function would be used to compute the result of the addition. Other expression operators can also be overloaded in a similar fashion.