Changeset 6337916
- Timestamp:
- Mar 13, 2024, 11:31:29 AM (9 months ago)
- Branches:
- master
- Children:
- 223b631
- Parents:
- 30a1f0c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
r30a1f0c r6337916 2463 2463 2464 2464 \section{OCaml} 2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,more keywords={match},moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}2466 2467 OCaml~\cite{Ocaml} provides a taggedvariant (union) type, where multiple heterogeneously-typed objects share the same storage.2468 The simplest form of the variant type is a list of untyped tags, which is like an unscoped, pure enumeration.2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{} 2466 2467 OCaml~\cite{Ocaml} provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage. 2468 The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, pure enumeration. 2469 2469 \begin{ocaml} 2470 2470 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun … … 2479 2479 @CS442@ 2480 2480 \end{ocaml} 2481 The only operations are binding a tag and pattern matching (equality).2481 The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the vale in the object storage. 2482 2482 Here, function @take_class@ has a @weekday@ parameter, and returns @"CS442"@, if the weekday value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@. 2483 2483 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. 2484 Since the taghas no type, it has a \Newterm{0-arity constructor}, \ie no parameters.2485 Because @weekday@ is a summation of values @Mon@ to @Sun@, it is a \Newterm{sumtype} in turns of the functional-programming paradigm.2486 2487 Each tagcan have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.2484 Since the variant has no type, it has a \Newterm{0-arity constructor}, \ie no parameters. 2485 Because @weekday@ is a union of values @Mon@ to @Sun@, it is a \Newterm{union type} in turns of the functional-programming paradigm. 2486 2487 Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value. 2488 2488 \begin{ocaml} 2489 2489 type colour = Red | Green of @string@ | Blue of @int * float@ … … 2509 2509 A recursive function is often used to pattern match against a recursive variant type. 2510 2510 \begin{ocaml} 2511 let rec len_of_string_list( list : stringList ): int =2511 let rec @len_of_string_list@( list : stringList ): int = 2512 2512 match list with 2513 2513 Empty -> 0 | 2514 Pair( _ , r ) -> 1 + len_of_string_listr2514 Pair( _ , r ) -> 1 + @len_of_string_list@ r 2515 2515 \end{ocaml} 2516 2516 Here, the head of the recursive type is removed and the remainder is processed until the type is empty. … … 2518 2518 2519 2519 Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement. 2520 However, the tag is dynamically set on binding (and possible reset on assignment), so a \lstinline[language=OCaml]{match} statement is effectively doing RTTI to select the matching case clause. 2521 Hence, a tagged variant has no notion of enumerabilty, and therefore is not a real enumeration, except for the simple pure (untyped) case. 2520 However, the union tag is dynamically set on binding (and possible reset on assignment), so a \lstinline[language=OCaml]{match} statement is effectively doing RTTI to select the matching case clause. 2521 2522 In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty. 2523 Therefore it is not an enumeration, except for the simple pure (nullary) case. 2524 2525 \begin{comment} 2526 Date: Wed, 13 Mar 2024 10:52:34 -0400 2527 Subject: Re: OCaml 2528 To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> 2529 From: Gregor Richards <gregor.richards@uwaterloo.ca> 2530 2531 On 3/12/24 18:34, Peter A. Buhr wrote: 2532 > Gregor, attached is a section Jiada wrote on OCaml (1-page). 2533 > Does it reflect our discussion about functional languages and enumerations? 2534 2535 Yeah, I think so. The most important part, i.e., that once they're 2536 parameterized they're not really enumerations at all, is covered clearly 2537 enough. 2538 2539 A couple quibbles: 2540 2541 <<a list of untyped tags>> 2542 2543 This is true, but leaking implementation details. These are nullary datatype 2544 constructors. Indeed, you later talk about "tagged variants", which are really 2545 just parameterized variants, using the term "tag" differently, confusing the 2546 term "tag" further. 2547 2548 <<Because weekday is a summation of values Mon to Sun, it is a sum type in 2549 turns of the functional-programming paradigm>> 2550 2551 It is a *union* of values and is a *union* type. 2552 2553 With valediction, 2554 - Gregor Richards 2555 \end{comment}
Note: See TracChangeset
for help on using the changeset viewer.