Changeset 56a8eb8
- Timestamp:
- Mar 12, 2024, 6:08:15 PM (9 months ago)
- Branches:
- master
- Children:
- 30a1f0c
- Parents:
- 8bdc9705
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
r8bdc9705 r56a8eb8 2463 2463 2464 2464 \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}}{} 2466 2466 2467 2467 OCaml~\cite{Ocaml} provides a tagged variant (union) type, where multiple heterogeneously-typed objects share the same storage. … … 2469 2469 \begin{ocaml} 2470 2470 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun 2471 let day : weekday = Mon$\C{(* bind *)}$2471 let day : weekday @= Mon@ $\C{(* bind *)}$ 2472 2472 let take_class( d : weekday ) = 2473 matchd with $\C{(* matching *)}$2473 @match@ d with $\C{(* matching *)}$ 2474 2474 Mon | Wed -> Printf.printf "CS442\n" | 2475 2475 Tue | Thu -> Printf.printf "CS343\n" | … … 2487 2487 Each tag can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value. 2488 2488 \begin{ocaml} 2489 type colour = Red | Green of string | Blue of int * float2489 type colour = Red | Green of @string@ | Blue of @int * float@ 2490 2490 \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@.) 2493 2493 Hence, a variant type creates a sum of product of different types. 2494 2494 \begin{ocaml} 2495 let c : colour = Red $\C{(* 0-ary constructor*)}$2495 let c = Red $\C{(* 0-ary constructor, set tag *)}$ 2496 2496 let _ = match c with Red -> Printf.printf "Red, " 2497 let c : colour = Green( "abc" ) $\C{(* 1-ary constructor*)}$2497 let c = Green( "abc" ) $\C{(* 1-ary constructor, set tag *)}$ 2498 2498 let _ = match c with Green g -> Printf.printf "%s, " g 2499 let c : colour = Blue( 1, 1.5 ) $\C{(* 2-ary constructor*)}$2499 let c = Blue( 1, 1.5 ) $\C{(* 2-ary constructor, set tag *)}$ 2500 2500 let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f 2501 2501 @Red, abc, 1 1.5@ … … 2504 2504 A variant type can have a recursive definition. 2505 2505 \begin{ocaml} 2506 type stringList = Empty | Pair of string * stringList2506 type @stringList@ = Empty | Pair of string * @stringList@ 2507 2507 \end{ocaml} 2508 2508 which is a recursive sum of product of types, called an \Newterm{algebraic data-type}. … … 2517 2517 Each recursion is counted to obtain the number of elements in the type. 2518 2518 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.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 2521 Hence, 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.