Changeset 282061a for doc


Ignore:
Timestamp:
Mar 11, 2024, 10:41:33 PM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
a885357
Parents:
446740a
Message:

update OCaml text

Location:
doc/theses/jiada_liang_MMath
Files:
3 edited

Legend:

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

    r446740a r282061a  
    128128Note, the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures.
    129129
     130While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are not used to evaluate an enumerator's value.
     131\CFA enumeration constants are compile-time values (static);
     132calling constructors happens at runtime (dynamic).
     133
    130134
    131135\section{Pure Enumerators}
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r446740a r282061a  
    11\chapter{Related Work}
    22\label{s:RelatedWork}
     3
     4An algebraic data type (ADT) can be viewed as a recursive sum of product types.
     5A sum type lists values as members.
     6A member in a sum type definition is known as a data constructor.
     7For example, C supports sum types union and enumeration (enum).
     8An enumeration in C can be viewed as the creation of a list of zero-arity data constructors.
     9A union instance holds a value of one of its member types.
     10Defining a union does not generate new constructors.
     11The definition of member types and their constructors are from the outer lexical scope.
    312
    413In general, an \Newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types.
     
    6675\end{ada}
    6776Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names.
    68 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}.
     77\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
    6978
    7079\begin{figure}
     
    254263enum E { A, B, C };
    255264E e = A;
    256 int i = A;   i = e;                                             $\C{// implicit casts to int}$
     265int i = A;    i = e;                                    $\C{// implicit casts to int}$
    257266\end{c++}
    258267\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), where the enumerators are accessed using type qualification.
     
    266275enum class E { A, B, C };
    267276@using enum E;@
    268 E e = A;                                                                $\C{// direct access}$
    269 e = B;                                                                  $\C{// direct access}$
     277E e = A;    e = B;                                              $\C{// direct access}$
    270278\end{c++}
    271279\CC{11} added the ability to explicitly declare the underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
     
    24542462
    24552463
    2456 \section{OCamal}
     2464\section{OCaml}
     2465\lstnewenvironment{ocaml}[1][]{\lstset{language=ML,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     2466
    24572467An enumerated data types is a list of named values.
    2458 \begin{python}
    2459 >>> type weekday =
    2460 ... | Monday
    2461 ... | Tuesday
    2462 ... | Wednesday
    2463 ... | Thursday
    2464 ... | Friday
    2465 ... | Saturday
    2466 ... | Sunday
    2467 \end{python}
    2468 Enumerated data types are the simplest subset of Variants types. Because weekday is a summantion of values Monday to Sunday,
    2469 enumerated data types are often call a sum type in turns of functional programming paradigm.
    2470 
    2471 The values defined in an enumerated type are called data constructors. The data constructors of an enumerated data type
    2472 takes no value as parameter. They are known as 0-arity constructor.
     2468\begin{ocaml}
     2469type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     2470\end{ocaml}
     2471Enumerated data types are the simplest subset of variants types.
     2472Because @weekday@ is a summantion of values @Mon@ to @Sun@, enumerated data types are often call a sum type in turns of functional programming paradigm.
     2473
     2474The values defined in an enumerated type are called data constructors.
     2475The data constructors of an enumerated data type takes no value as parameter.
     2476They are known as 0-arity constructor.
    24732477
    24742478A more generic variant type has n-ary constructors.
    2475 \begin{python}
    2476 >>> type color =
    2477 ... | Red
    2478 ... | Green of string
    2479 ... | Blue of int * float
    2480 \end{python}
    2481 The color type can be constructed as a combination of a value from an int and a blue, using
    2482 the Blue data constructor. Mathematically, a Blue value is a Cartesian product of the int type and the bool.
    2483 Color type is a summation of a nullary type, an unary product type, and a cross product of int and bool. The
    2484 OCamal variant type can be create as a sum of product of different types.
     2479\begin{ocaml}
     2480type colour = Red | Green of string | Blue of int * float
     2481\end{ocaml}
     2482The @colour@ type can be constructed as a combination of a value from an @int@ and a @blue@, using the @Blue@ data constructor.
     2483Mathematically, a @Blue@ value is a Cartesian product of the @int@ type and the @bool@.
     2484@Colour@ type is a summation of a nullary type, a unary product type, and a cross product of @int@ and @bool@.
     2485The OCaml variant type can be create as a sum of product of different types.
    24852486
    24862487A variant type can have a recursively definition.
    2487 \begin{python}
    2488 >>> type stringList =
    2489 ... | Empty
    2490 ... | Pair of string * stringList
    2491 \end{python}
    2492 
    2493 OCaml's variant types are recusrive sum of product of types, which are known as Algebraic Data Types. Programming languages
    2494 follows the functional programming paradigm often supports algebraic data types, and supports Pattern Matching against
    2495 algebraic data type.
    2496 
    2497 \begin{python}
    2498 >>> let take_class = functio
    2499 ... | Monday | Wednesday -> "CS442"
    2500 ... | Tuesday | Thursday -> "CS343"
    2501 ... | Friday -> "Tutorial"
    2502 ... | _ -> "Take a break";;
    2503 \end{python}
    2504 
    2505 The function a weekday as parameter, and returns "CS442" if the weekday value is Monday or Wednesday, "CS343"
    2506 if the value is Tuesday or Thursday, "Tutorial" if it is Friday. The @_@ is wildcard, and it can match any weekday value.
    2507 If the value is Saturday or Sunday, which are not matched by the previous cases, it will be matched by the @_@ and the
    2508 function returns "Take a break".
     2488\begin{ocaml}
     2489type stringList = Empty | Pair of string * stringList
     2490\end{ocaml}
     2491OCaml's variant types are recusrive sum of product of types, which are known as Algebraic Data Types.
     2492Programming languages follows the functional programming paradigm often supports algebraic data types, and supports pattern matching against algebraic data type.
     2493\begin{ocaml}
     2494let take_class = function
     2495        Mon | Wed -> "CS442" |
     2496        Tue | Thu -> "CS343" |
     2497        Fri -> "Tutorial" |
     2498        _ -> "Take a break"
     2499\end{ocaml}
     2500The function has a @weekday@ as parameter, and returns @"CS442"@, if the weekday value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@.
     2501The @_@ is a wildcard matching any @weekday@ value, so the function returns @"Take a break"@ for values @Sat@ or @Sun@, which are not matched by the previous cases.
    25092502
    25102503Values of a product type can be named.
    2511 \begin{python}
    2512 >>> let check_color (c: color): string =
    2513 ... match c with
    2514 ... | Red -> "Red"
    2515 ... | Green g -> g
    2516 ... | Blue (i, f) -> string_of_int i ^ string_of_float f;;
    2517 \end{python}
    2518 
    2519 Recurisve function are often used to pattern match against a recurisve variant type.
    2520 \begin{python}
    2521 >>> let rec len_of_string_list(l: stringList): int =
    2522 ... match l with
    2523 ... | Empty -> 0
    2524 ... | Pair (_ , r) -> 1 + len_of_string_list r
    2525 \end{python}
     2504\begin{ocaml}
     2505let check_colour (c: colour): string =
     2506         match c with
     2507                Red -> "Red" |
     2508                Green g -> g |
     2509                Blue(i, f) -> string_of_int i ^ string_of_float f
     2510\end{ocaml}
     2511A recurisve function is often used to pattern match against a recurisve variant type.
     2512\begin{ocaml}
     2513let rec len_of_string_list(l: stringList): int =
     2514        match l with
     2515                Empty -> 0 |
     2516                Pair(_ , r) -> 1 + len_of_string_list r
     2517\end{ocaml}
  • doc/theses/jiada_liang_MMath/uw-ethesis.tex

    r446740a r282061a  
    8989\usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt,font=normalsize]{subfig}
    9090\renewcommand\thesubfigure{(\alph{subfigure})}
     91
    9192% cfa macros used in the document
    9293\input{common}
    9394%\usepackage{common}
    9495\CFAStyle                                               % CFA code-style
    95 \lstset{language=cfa,belowskip=-1pt}                                    % set default language to CFA
     96\lstset{language=cfa,belowskip=-1pt} % set default language to CFA
    9697
    9798\newcommand{\newtermFont}{\emph}
     
    108109\usepackage[dvips,pagebackref=true]{hyperref} % with basic options
    109110%\usepackage[pdftex,pagebackref=true]{hyperref}
    110                 % N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing.
     111% N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing.
    111112\hypersetup{
    112113    plainpages=false,       % needed if Roman numbers in frontpages
     
    116117    pdffitwindow=false,     % window fit to page when opened
    117118    pdfstartview={FitH},    % fits the width of the page to the window
    118     pdftitle={Type Resolution in \CFA}, % title: CHANGE THIS TEXT!
     119    pdftitle={\CFA Enumerations}, % title: CHANGE THIS TEXT!
    119120    pdfauthor={Jiada Liang},    % author: CHANGE THIS TEXT! and uncomment this line
    120121    pdfsubject={Cforall},  % subject: CHANGE THIS TEXT! and uncomment this line
Note: See TracChangeset for help on using the changeset viewer.