Changeset 6337916 for doc


Ignore:
Timestamp:
Mar 13, 2024, 11:31:29 AM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
223b631
Parents:
30a1f0c
Message:

fold in Gregor's comments on OCaml

File:
1 edited

Legend:

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

    r30a1f0c r6337916  
    24632463
    24642464\section{OCaml}
    2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,morekeywords={match},moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    2466 
    2467 OCaml~\cite{Ocaml} provides a tagged variant (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
     2467OCaml~\cite{Ocaml} provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
     2468The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, pure enumeration.
    24692469\begin{ocaml}
    24702470type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     
    24792479@CS442@
    24802480\end{ocaml}
    2481 The only operations are binding a tag and pattern matching (equality).
     2481The 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.
    24822482Here, 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@.
    24832483The ``@_@'' 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 tag has 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{sum type} in turns of the functional-programming paradigm.
    2486 
    2487 Each tag can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
     2484Since the variant has no type, it has a \Newterm{0-arity constructor}, \ie no parameters.
     2485Because @weekday@ is a union of values @Mon@ to @Sun@, it is a \Newterm{union type} in turns of the functional-programming paradigm.
     2486
     2487Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
    24882488\begin{ocaml}
    24892489type colour = Red | Green of @string@ | Blue of @int * float@
     
    25092509A recursive function is often used to pattern match against a recursive variant type.
    25102510\begin{ocaml}
    2511 let rec len_of_string_list( list : stringList ): int =
     2511let rec @len_of_string_list@( list : stringList ): int =
    25122512        match list with
    25132513                Empty -> 0 |
    2514                 Pair( _ , r ) -> 1 + len_of_string_list r
     2514                Pair( _ , r ) -> 1 + @len_of_string_list@ r
    25152515\end{ocaml}
    25162516Here, the head of the recursive type is removed and the remainder is processed until the type is empty.
     
    25182518
    25192519Note, 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.
     2520However, 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
     2522In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty.
     2523Therefore it is not an enumeration, except for the simple pure (nullary) case.
     2524
     2525\begin{comment}
     2526Date: Wed, 13 Mar 2024 10:52:34 -0400
     2527Subject: Re: OCaml
     2528To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     2529From: Gregor Richards <gregor.richards@uwaterloo.ca>
     2530
     2531On 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
     2535Yeah, I think so. The most important part, i.e., that once they're
     2536parameterized they're not really enumerations at all, is covered clearly
     2537enough.
     2538
     2539A couple quibbles:
     2540
     2541<<a list of untyped tags>>
     2542
     2543This is true, but leaking implementation details. These are nullary datatype
     2544constructors. Indeed, you later talk about "tagged variants", which are really
     2545just parameterized variants, using the term "tag" differently, confusing the
     2546term "tag" further.
     2547
     2548<<Because weekday is a summation of values Mon to Sun, it is a sum type in
     2549turns of the functional-programming paradigm>>
     2550
     2551It is a *union* of values and is a *union* type.
     2552
     2553With valediction,
     2554  - Gregor Richards
     2555\end{comment}
Note: See TracChangeset for help on using the changeset viewer.