Changeset dcfcf368 for doc/theses/jiada_liang_MMath/intro.tex
- Timestamp:
- Sep 14, 2024, 5:07:55 PM (2 months ago)
- Branches:
- master
- Children:
- 8c79dc3c
- Parents:
- 3733643
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/intro.tex
r3733643 rdcfcf368 39 39 For example, the week, the weekdays, the weekend, and every second day of the week. 40 40 \begin{cfa}[morekeywords={in}] 41 for ( cursor in Mon, Tue, Wed, Thu, Fri, Sat, Sun }... $\C[3.75in]{// week}$42 for ( cursor in Mon, Tue, Wed, Thu, Fri }... $\C{// weekday}$43 for ( cursor in Sat, Sun }... $\C{// weekend}$44 for ( cursor in Mon, Wed, Fri, Sun }... $\C{// every second day of week}\CRT$41 for ( cursor in Mon, Tue, Wed, Thu, Fri, Sat, Sun ) ... $\C[3.75in]{// week}$ 42 for ( cursor in Mon, Tue, Wed, Thu, Fri ) ... $\C{// weekday}$ 43 for ( cursor in Sat, Sun ) ... $\C{// weekend}$ 44 for ( cursor in Mon, Wed, Fri, Sun ) ... $\C{// every second day of week}\CRT$ 45 45 \end{cfa} 46 46 A set can have a partial or total ordering, making it possible to compare set elements, \eg Monday is before Tuesday and Tuesday is after. … … 73 73 74 74 The term \newterm{enumeration} defines a type with a set of new constants, and the term \newterm{enumerator} represents an arbitrary alias name \see{\VRef{s:CEnumeration} for the name derivations}. 75 An enumerated type can have th ree fundamental properties,\newterm{label} (name), \newterm{order} (position), and \newterm{value} (payload).75 An enumerated type can have the following properties: \newterm{label} (name), \newterm{order} (position), and \newterm{value} (payload). 76 76 \begin{cquote} 77 77 \sf\setlength{\tabcolsep}{3pt} … … 234 234 % The association between ADT and enumeration occurs if all the constructors have a unit (empty) type, \eg @struct unit {}@. 235 235 % Note, the unit type is not the same as \lstinline{void}. 236 In terms of functional programming linguistics, enumerations often refers to a @unit type@ ADT, where @unit type@ is a type 237 that carry no information. The unit type is different than @void@ in C, because @void@ is type has no value, i.e., it is a empty set. 238 In constract, unit type has exactly one value, often called a @nil@ value. 239 Because of this distinction, it is not possbile to have a variable to have type @void@ or to be assigned with a value @void@. 240 In practice, @void@ in C is more like an annotation that nothing is expected in this place. A function takes @void@ as parameter 241 is essentially a function that expects no parameter. A function that return @void@ cannot be used as a parameter of a function that expects no 242 parameter. Therefore, the following code is illegal in C: 236 In terms of functional programming linguistics, enumerations often refer to a @unit type@ ADT, which is a set with the @nil@ value carrying no information. 237 The unit type is different from type @void@ in C, because @void@ has no value, which is an empty set. 238 Hence, @void@ is a C annotation that nothing is expected in this place. 239 For example, a function that takes a @void@ parameter and returns a @void@ is a function that expects no parameters and returns nothing. 240 \begin{cfa} 241 void foo( void ); 242 foo(); $\C{// no arguments and no result}$ 243 \end{cfa} 244 Because of this distinction, it is impossible to have a variable of type @void@, to assign a @void@ value, or have a function taking and returning multiple @void@s. 245 \begin{cfa} 246 void v; $\C{// disallowed}$ 247 v = void; 248 [ void, void ] bar( void, void ); 249 \end{cfa} 250 Programming languages often use an empty parameter list to imply no value and no return type for empty return. 251 \begin{cfa} 252 [] bar(); $\C{// \CFA empty/empty prototype}$ 253 \end{cfa} 254 However, C is saddled with an empty parameter list meaning a list of unknown type parameters, \ie @var_arg@, which is changed to @void@ in \CC/\CFA. 255 As a result, a function that returns @void@ cannot be used as a parameter of a function that expects no parameter. 243 256 \begin{cfa} 244 257 void foo( void ); … … 246 259 \end{cfa} 247 260 248 This is a more notably issue when to use @variant@ to simulate @ADT@: @void@ cannot be used as an empty variant. To solve this problem, 249 \CC introduced @monstate@, a type that can be instantiated as a value, but holds no information. There is no standard representation of 250 @unit@ in C, and it is often approximated by a user-defined type that has no field, for example: 251 \begin{cfa} 252 struct Empty {} e; $\C{// empty type}$ 253 Empty bar( Empty ); 254 bar(@bar(e)@); 255 \end{cfa} 256 @Empty@ is a close approximation to @unit@ if and only if @Empty@ is the only representation of @unit@ in the program. If a program has 257 a second type, say @Nothing@, that also tries to resemble @unit@, then @unit@ concepts falls apart. 261 This issue arose when simulating an ADT using a \CC @variant@: @void@ cannot be used as an empty variant. 262 To solve this problem, \CC introduced @std::monstate@~\cite{C++monstate}, a type that can be instantiated as a value but holds no information. 263 A similar approximation in C is to define a @struct@ type with no fields. 264 \begin{cfa} 265 struct Unit {} e; $\C{// empty type}$ 266 Unit bar( Unit ); 267 bar( @bar( e )@ ); 268 \end{cfa} 269 Because @std::monostate@ and @Unit@ are user-defined types versus part of the type system, they are only an approximation to @unit@ because other @unit@ types can be defined. 258 270 259 271 In the Haskell ADT:
Note: See TracChangeset
for help on using the changeset viewer.