Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| language:types2 [2025/04/21 15:52] – [Functions] rajit | language:types2 [2026/08/09 13:10] (current) – [Non-strict template parameters] rajit | ||
|---|---|---|---|
| Line 153: | Line 153: | ||
| the type. Hence, when checking for type compatibility, | the type. Hence, when checking for type compatibility, | ||
| parameters are also taken into account. Hence, the full type for | parameters are also taken into account. Hence, the full type for | ||
| - | instance '' | + | instance '' |
| '' | '' | ||
| parameters are more completely specified as '' | parameters are more completely specified as '' | ||
| Line 199: | Line 199: | ||
| Note that ACT is very strict about type-checking; | Note that ACT is very strict about type-checking; | ||
| + | |||
| + | ==== Grouping parameters ==== | ||
| + | |||
| + | Parameters can be combined into [[language: | ||
| + | |||
| ===== Direction flags ===== | ===== Direction flags ===== | ||
| Line 280: | Line 285: | ||
| <code act> | <code act> | ||
| - | defproc stack (chan(int)? in; chan(int)!out ) | + | defproc stack (chan(int)? in; chan(int)! out) |
| { | { | ||
| ... | ... | ||
| Line 306: | Line 311: | ||
| | | ||
| ... | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Macros can also use the '' | ||
| + | |||
| + | <code act> | ||
| + | defproc stack (chan(int)? in; chan(int)! out) | ||
| + | { | ||
| + | ... | ||
| + | methods { | ||
| + | macro push(int val) chp-txt { | ||
| + | send(in, | ||
| + | } | ||
| + | macro pop(int res) chp-txt { | ||
| + | recv(out, | ||
| + | } | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 347: | Line 369: | ||
| </ | </ | ||
| - | === Operator overloading === | + | Note that functions cannot have any side-effects; |
| + | any of the members of the pure structure. Macros can be used to change those. | ||
| + | |||
| + | ==== Operator overloading ==== | ||
| + | |||
| + | Functions within pure structures are also used to support operator overloading. In particular, the following function methods are interpreted to be the definition of operator overloading for arithmetic operators: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | An example of a fixed-point arithmetic datatype is provided in the [[https:// | ||
| + | In the linked example, '' | ||
| + | |||
| + | ==== Example user-defined type with macros and methods ==== | ||
| + | |||
| + | Combining some of these ideas, the following is an example of a user-defined type corresponding to an N-bit signed integer. Only some methods are defined. | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | deftype signed_int (int< | ||
| + | { | ||
| + | { W > 1 : "Need sign bit plus one bit at a minumum" | ||
| + | methods { | ||
| + | function plus (signed_int< | ||
| + | { | ||
| + | chp { | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | macro display() | ||
| + | { | ||
| + | [ x{W-1} = 1 -> log_p(" | ||
| + | [] else -> log_p(x) | ||
| + | ] | ||
| + | } | ||
| + | macro set(int< | ||
| + | | ||
| + | x := v | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In this scenario, the following CHP adds two numbers, and then displays the result using a '' | ||
| + | <code act> | ||
| + | defproc test() | ||
| + | { | ||
| + | | ||
| + | chp { | ||
| + | | ||
| + | | ||
| + | v := v + u; | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | This should display '' | ||
| + | |||
| + | ===== Non-strict template parameters | ||
| + | |||
| + | The following ACT program is a buffer that adds a constant value to its input: | ||
| + | <code act> | ||
| + | template< | ||
| + | defproc addbuf(chan? | ||
| + | { | ||
| + | | ||
| + | chp { | ||
| + | *[ L?x; R!(x+17) ] | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | addbuf< | ||
| + | ... | ||
| + | </ | ||
| + | Since '' | ||
| + | A parameterized version of this buffer is: | ||
| + | <code act> | ||
| + | template< | ||
| + | defproc addbuf(chan? | ||
| + | { | ||
| + | | ||
| + | chp { | ||
| + | *[ L?x; R!(x+VAL) ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ... | ||
| + | addbuf< | ||
| + | ... | ||
| + | </ | ||
| + | This will also result in the same circuit as the earlier example, because the value '' | ||
| + | |||
| + | What if we wanted to create an array of these adders? While | ||
| + | <code act> | ||
| + | ... | ||
| + | addbuf< | ||
| + | </ | ||
| + | is fine, the problem arises if we want each '' | ||
| + | <code act> | ||
| + | addbuf< | ||
| + | addbuf< | ||
| + | </ | ||
| + | will result in the error message | ||
| + | < | ||
| + | -[ERROR]-> | ||
| + | Orig type: addbuf< | ||
| + | New type: addbuf< | ||
| + | </ | ||
| + | This is because elements of an array must have the same type. As this is a useful use-case, ACT has support for a special type of template parameter introduced for this scenario. | ||
| + | |||
| + | Template parameters for processes can be of two types: | ||
| + | * //strict// parameters, which are the ones we have been considering so far. Elements of an array must have the same strict template parameters. | ||
| + | * // | ||
| + | |||
| + | |||
| + | Non-strict parameters are separated from strict parameters by a vertical bar. The example above would be written as follows: | ||
| + | <code act> | ||
| + | template< | ||
| + | defproc addbuf(chan? | ||
| + | { | ||
| + | | ||
| + | chp { | ||
| + | *[ L?x; R!(x+VAL) ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ... | ||
| + | addbuf< | ||
| + | addbuf< | ||
| + | ... | ||
| + | </ | ||
| + | ACT will allow this syntax. This comes with a few caveats: | ||
| + | * Any ports for the type can only use strict parameters. | ||
| + | template< | ||
| + | defproc oddbuf(chan? | ||
| + | { | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | -[ERROR]-> | ||
| + | </ | ||
| + | * Even if the array is dense, it will be treated as a sparse array with multiple chunks where each chunk can have different non-strict parameters. | ||
| + | * Sub-array expressions cannot be used where different elements have different types. | ||
| + | |||
| + | Since the general ACT syntax can be used, the following would also be valid: | ||
| + | <code act> | ||
| + | ... | ||
| + | (i:8: addbuf< | ||
| + | ... | ||
| + | </ | ||