====== Operators ======
A special case of [[intro_example:pure_struct|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
deftype signed_int(bool neg; int 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
deftype signed_int(bool neg; int val)
{
methods {
function plus (signed_int x) : signed_int
{
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 [[language:types2#operator_overloading|overloaded]] in a similar fashion.