Changeset a35e342 for doc


Ignore:
Timestamp:
Sep 11, 2024, 1:21:03 PM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
025f9c5, c494b84
Parents:
08e0d65
Message:

update the discussion of unit type

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/intro.tex

    r08e0d65 ra35e342  
    235235% Note, the unit type is not the same as \lstinline{void}.
    236236In 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:
     237that 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.
     238In constract, unit type has exactly one value, often called a @nil@ value.
     239Because of this distinction, it is not possbile to have a variable to have type @void@ or to be assigned with a value @void@.
     240In practice, @void@ in C is more like an annotation that nothing is expected in this place. A function takes @void@ as parameter
     241is essentially a function that expects no parameter. A function that return @void@ cannot be used as a parameter of a function that expects no
     242parameter. Therefore, the following code is illegal in C:
     243\begin{cfa}
     244void foo( void );
     245foo( @foo()@ );         $\C{// void argument does not match with void parameter}$
     246\end{cfa}
     247
     248This 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}
     252struct Empty {} e;              $\C{// empty type}$
     253Empty bar( Empty );
     254bar(@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
     257a second type, say @Nothing@, that also tries to resemble @unit@, then @unit@ concepts falls apart.
     258
     259In the Haskell ADT:
    247260\begin{haskell}
    248261data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show)
Note: See TracChangeset for help on using the changeset viewer.