Changeset 282061a
- Timestamp:
- Mar 11, 2024, 10:41:33 PM (9 months ago)
- Branches:
- master
- Children:
- a885357
- Parents:
- 446740a
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r446740a r282061a 128 128 Note, 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. 129 129 130 While 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); 132 calling constructors happens at runtime (dynamic). 133 130 134 131 135 \section{Pure Enumerators} -
doc/theses/jiada_liang_MMath/relatedwork.tex
r446740a r282061a 1 1 \chapter{Related Work} 2 2 \label{s:RelatedWork} 3 4 An algebraic data type (ADT) can be viewed as a recursive sum of product types. 5 A sum type lists values as members. 6 A member in a sum type definition is known as a data constructor. 7 For example, C supports sum types union and enumeration (enum). 8 An enumeration in C can be viewed as the creation of a list of zero-arity data constructors. 9 A union instance holds a value of one of its member types. 10 Defining a union does not generate new constructors. 11 The definition of member types and their constructors are from the outer lexical scope. 3 12 4 13 In general, an \Newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types. … … 66 75 \end{ada} 67 76 Like \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)}. 69 78 70 79 \begin{figure} … … 254 263 enum E { A, B, C }; 255 264 E e = A; 256 int i = A; i = e;$\C{// implicit casts to int}$265 int i = A; i = e; $\C{// implicit casts to int}$ 257 266 \end{c++} 258 267 \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. … … 266 275 enum class E { A, B, C }; 267 276 @using enum E;@ 268 E e = A; $\C{// direct access}$ 269 e = B; $\C{// direct access}$ 277 E e = A; e = B; $\C{// direct access}$ 270 278 \end{c++} 271 279 \CC{11} added the ability to explicitly declare the underlying \emph{integral} type for \lstinline[language=c++]{enum class}. … … 2454 2462 2455 2463 2456 \section{OCamal} 2464 \section{OCaml} 2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=ML,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{} 2466 2457 2467 An 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} 2469 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun 2470 \end{ocaml} 2471 Enumerated data types are the simplest subset of variants types. 2472 Because @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 2474 The values defined in an enumerated type are called data constructors. 2475 The data constructors of an enumerated data type takes no value as parameter. 2476 They are known as 0-arity constructor. 2473 2477 2474 2478 A 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} 2480 type colour = Red | Green of string | Blue of int * float 2481 \end{ocaml} 2482 The @colour@ type can be constructed as a combination of a value from an @int@ and a @blue@, using the @Blue@ data constructor. 2483 Mathematically, 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@. 2485 The OCaml variant type can be create as a sum of product of different types. 2485 2486 2486 2487 A 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} 2489 type stringList = Empty | Pair of string * stringList 2490 \end{ocaml} 2491 OCaml's variant types are recusrive sum of product of types, which are known as Algebraic Data Types. 2492 Programming languages follows the functional programming paradigm often supports algebraic data types, and supports pattern matching against algebraic data type. 2493 \begin{ocaml} 2494 let take_class = function 2495 Mon | Wed -> "CS442" | 2496 Tue | Thu -> "CS343" | 2497 Fri -> "Tutorial" | 2498 _ -> "Take a break" 2499 \end{ocaml} 2500 The 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@. 2501 The @_@ 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. 2509 2502 2510 2503 Values 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} 2505 let 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} 2511 A recurisve function is often used to pattern match against a recurisve variant type. 2512 \begin{ocaml} 2513 let 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 89 89 \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt,font=normalsize]{subfig} 90 90 \renewcommand\thesubfigure{(\alph{subfigure})} 91 91 92 % cfa macros used in the document 92 93 \input{common} 93 94 %\usepackage{common} 94 95 \CFAStyle % CFA code-style 95 \lstset{language=cfa,belowskip=-1pt} 96 \lstset{language=cfa,belowskip=-1pt} % set default language to CFA 96 97 97 98 \newcommand{\newtermFont}{\emph} … … 108 109 \usepackage[dvips,pagebackref=true]{hyperref} % with basic options 109 110 %\usepackage[pdftex,pagebackref=true]{hyperref} 110 111 % N.B. pagebackref=true provides links back from the References to the body text. This can cause trouble for printing. 111 112 \hypersetup{ 112 113 plainpages=false, % needed if Roman numbers in frontpages … … 116 117 pdffitwindow=false, % window fit to page when opened 117 118 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! 119 120 pdfauthor={Jiada Liang}, % author: CHANGE THIS TEXT! and uncomment this line 120 121 pdfsubject={Cforall}, % subject: CHANGE THIS TEXT! and uncomment this line
Note: See TracChangeset
for help on using the changeset viewer.