+++ title = "`struct`" weight = 1 +++ Defines a record type (a "pure product"). This is a specialized form of a `data` definition. The same field name may occur in more than one type. ``` topDefn ::= struct typeId {tyVarId }= { { fieldDef ; }} [ derive ] fieldDef ::= fieldId :: type ``` Example: ``` struct Proc = { pc :: Addr; rf :: RegFile; mem :: Memory } struct Coord = { x :: Int; y :: Int } ``` Section [5.6](fixme) describes how to construct values of a `struct` type. A field of a `struct` type can be extracted either directly using "dot" notation (section [5.7](fixme)) or using pattern matching (section [6.3](fixme)). ### Tuples {#sec-tuple-type} One way to group multiple values together is to use a `data` definition in which a constructor has multiple fields. However, there is a built-in notation for a common form of grouping, called "tuples". To group two (or more) values together the Prelude contains a type, `PrimPair`, for which there is syntactic sugar for type expressions, value expressions, and patterns. The type has the following definition ```hs struct PrimPair a b = { fst :: a; snd :: b } deriving (Eq, Bits, Bounded) ``` For type expressions the following shorthand can be used: (a, b) $\equiv$ PrimPair a b Or, more generally, ($t_1$, $t_2$, $\cdots$, $t_n$) $\equiv$ PrimPair $t_1$ (PrimPair $t_2$ ($\cdots$ $t_n$)) There is a corresponding shorthand for value expressions and patterns: (a, b) $\equiv$ PrimPair { fst = a; snd = b } There is also special syntax for the empty tuple. It is written "`()`" for types, expressions, and patterns. The real type has the following definition ```hs struct PrimUnit = { } deriving (Eq, Bits, Bounded) ```