Changeset 56a8eb8


Ignore:
Timestamp:
Mar 12, 2024, 6:08:15 PM (2 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
30a1f0c
Parents:
8bdc9705
Message:

more updates on OCaml

File:
1 edited

Legend:

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

    r8bdc9705 r56a8eb8  
    24632463
    24642464\section{OCaml}
    2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=ML,escapechar=\$,morekeywords={match},moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     2465\lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,morekeywords={match},moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    24662466
    24672467OCaml~\cite{Ocaml} provides a tagged variant (union) type, where multiple heterogeneously-typed objects share the same storage.
     
    24692469\begin{ocaml}
    24702470type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
    2471 let day : weekday = Mon                                 $\C{(* bind *)}$
     2471let day : weekday @= Mon@                               $\C{(* bind *)}$
    24722472let take_class( d : weekday ) =
    2473         match d with                                            $\C{(* matching *)}$
     2473        @match@ d with                                          $\C{(* matching *)}$
    24742474                Mon | Wed -> Printf.printf "CS442\n" |
    24752475                Tue | Thu -> Printf.printf "CS343\n" |
     
    24872487Each tag can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
    24882488\begin{ocaml}
    2489 type colour = Red | Green of string | Blue of int * float
     2489type colour = Red | Green of @string@ | Blue of @int * float@
    24902490\end{ocaml}
    2491 @colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @bool@.
    2492 (Mathematically, a @Blue@ value is a Cartesian product of the @int@ type and the @bool@.)
     2491@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
     2492(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
    24932493Hence, a variant type creates a sum of product of different types.
    24942494\begin{ocaml}
    2495 let c : colour = Red                                    $\C{(* 0-ary constructor *)}$
     2495let c = Red                                                             $\C{(* 0-ary constructor, set tag *)}$
    24962496let _ = match c with Red -> Printf.printf "Red, "
    2497 let c : colour = Green( "abc" )                 $\C{(* 1-ary constructor *)}$
     2497let c = Green( "abc" )                                  $\C{(* 1-ary constructor, set tag *)}$
    24982498let _ = match c with Green g -> Printf.printf "%s, " g
    2499 let c : colour = Blue( 1, 1.5 )                 $\C{(* 2-ary constructor *)}$
     2499let c = Blue( 1, 1.5 )                                  $\C{(* 2-ary constructor, set tag *)}$
    25002500let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
    25012501@Red, abc, 1 1.5@
     
    25042504A variant type can have a recursive definition.
    25052505\begin{ocaml}
    2506 type stringList = Empty | Pair of string * stringList
     2506type @stringList@ = Empty | Pair of string * @stringList@
    25072507\end{ocaml}
    25082508which is a recursive sum of product of types, called an \Newterm{algebraic data-type}.
     
    25172517Each recursion is counted to obtain the number of elements in the type.
    25182518
    2519 Note, the compiler statically guarantees that only the correct kind of type can be used in the \lstinline[language=ML]{match} statement.
    2520 However, the tag is dynamically set on binding (and possible reset on assignment), so the \lstinline[language=ML]{match} statement is effectively doing RTTI to select the matching case clause.
     2519Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement.
     2520However, 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.
    25212521Hence, a tagged variant has no notion of enumerabilty, and therefore is not a real enumeration, except for the simple pure (untyped) case.
Note: See TracChangeset for help on using the changeset viewer.