Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
language:types2:data [2024/07/18 16:14] rajitlanguage:types2:data [2024/07/18 16:23] (current) – [Structures] rajit
Line 1: Line 1:
-====== Data types ======+====== User-defined data types ======
  
-A data type is defined using ''deftype''. A data type corresponds +user-defined data type is defined using ''deftype''These are classified into two categories: 
-to an integer or Boolean value, although it could also be a composite +   * //data types//, which correspond to representations of the built-in ''int'' and ''bool'' types. 
-construct like a record or structure (from software programming +   * //structures//, which are a collection of data types. 
-languages). The syntax is similar to a process, and the constraints + 
-about declarations/etc. apply here as well.+===== Data types ===== 
 + 
 +A data type corresponds to an integer or Boolean value, even though it 
 +could also be a composite construct like a record or structure (from software 
 +programming languages). The syntax is similar to a process, and the constraints 
 +about declarations/etc. apply here as well.  These data types are handled as 
 +the underlying built-in ''int'' or ''bool'' that they correspond to at the CHP 
 +level of abstraction.
  
 Often data types have some additional structure that is not required for a Often data types have some additional structure that is not required for a
Line 49: Line 56:
 type is supposed to represent a circuit structure that is used to type is supposed to represent a circuit structure that is used to
 represent a data value. represent a data value.
 +
 +==== Data methods ====
 +
 +There are two methods that can be specified for a data type:
 +   - a //set method//, used to write a value to the type;
 +   - a //get method//, used to read the value of the type.
 +One can think of these as type conversion methods invoked
 +automatically to read or write the data type. When a normal
 +data type is used, the special variable ''self'' is implicitly
 +defined to be the built-in type that is implemented by the
 +user-defined data type.
 +
 +<code act>
 +deftype d1of2 <: int<1> (bool d0,d1)
 +{
 +   spec {
 +    exclhi(d0,d1)
 +   }
 +   methods {
 +     set {
 +       [self=1->d1-;d0+ [] self=0->d0-;d1+]
 +     }
 +     get {
 +       [d0->self:=1 [] d1->self:=0]
 +     }
 +   }
 +}
 +</code>
 +
 +In the example above, the ''set'' method says that the way to set a
 +''d1of2'' data type to the value ''0'' is to set ''d0'' to
 +''false'' and ''d1'' to ''true''. The special variable
 +''self'' is used to specify the ''int<1>'' value of the type, and
 +the methods specify conversion operations.
 +
 +The selection statement in the ''get'' method uses the deterministic
 +selection operator ''[]'' (see [[language:langs:hse|the hse sublanguage]]). This is an implicit check
 +that when the ''get'' method is invoked, signals ''d0'' and ''d1''
 +cannot both be ''true''. We have also made this explicit in the
 +specification body. Also, if both ''d0'' and ''d1'' are false
 +(i.e. an illegal state in which to execute a get operation), the
 +variable ''self'' is not assigned; the operation waits for at least
 +one of ''d0'' or ''d1'' to be true. This is viewed as an error for a
 +data type. (This is different in the case of a channel, where the
 +semantics of the channel permit waiting.)
  
  
Line 67: Line 119:
 The distinction between a structure and another data type is that other data types are The distinction between a structure and another data type is that other data types are
 implementations of one of the built-in types like ''int'' or ''bool.''  implementations of one of the built-in types like ''int'' or ''bool.'' 
 +
 +==== Pure structures ====
 +
 +A pure structure is one that only contains other pure structures or ''int'' and ''bool'' components. 
 +In other words, they are structures that don't include channels. Structures like this correspond 
 +to what is normally viewed as a record/struct in a normal software programming language.
 +