- Timestamp:
- Sep 11, 2024, 1:21:03 PM (3 months ago)
- Branches:
- master
- Children:
- 025f9c5, c494b84
- Parents:
- 08e0d65
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/intro.tex
r08e0d65 ra35e342 235 235 % Note, the unit type is not the same as \lstinline{void}. 236 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. 238 % \begin{cfa} 239 % void foo( void ); 240 % struct unit {} u; $\C[1.5in]{// empty type}$ 241 % unit bar( unit ); 242 % foo( @foo()@ ); $\C{// void argument does not match with void parameter}$ 243 % bar( bar( u ) ); $\C{// unit argument does match with unit parameter}\CRT$ 244 % \end{cfa} 245 246 For example, in the Haskell ADT: 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: 243 \begin{cfa} 244 void foo( void ); 245 foo( @foo()@ ); $\C{// void argument does not match with void parameter}$ 246 \end{cfa} 247 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. 258 259 In the Haskell ADT: 247 260 \begin{haskell} 248 261 data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show)
Note: See TracChangeset
for help on using the changeset viewer.