Changes in / [a3525c4:30a1f0c]


Ignore:
Location:
doc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/LaTeXmacros/lstlang.sty

    ra3525c4 r30a1f0c  
    88%% Created On       : Sat May 13 16:34:42 2017
    99%% Last Modified By : Peter A. Buhr
    10 %% Last Modified On : Sat Mar  9 17:03:58 2024
    11 %% Update Count     : 41
     10%% Last Modified On : Tue Mar 12 17:29:58 2024
     11%% Update Count     : 42
    1212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1313
     
    9999}
    100100
    101 \lstdefinelanguage{pseudo}{
    102         morekeywords={string,uint,int,bool,float},
    103         sensitive=true,
    104         morecomment=[l]{//},
    105         morecomment=[s]{/*}{*/},
    106         morestring=[b]',
    107         morestring=[b]",
    108         morestring=[s]{`}{`},
    109 }
     101\lstdefinelanguage{OCaml}%
     102        {morekeywords={
     103                and, as, asr, assert, begin, class, constraint do, done, downto, else, end, exception, external,
     104                false, for, fun, function, functor, if, in, include, inherit, initializer, land, lazy, let, lor, lsl, lsr, lxor,
     105                match, method, mod, module, open, mutable, new, nonrec, object, of, open, open!, or, private, rec,
     106                sig, struct, then, to, true, try, type, val, virtual, when, while, with
     107        },
     108   sensitive,%
     109   morecomment=[n]{(*}{*)},%
     110   morestring=[d]"%
     111}[keywords,comments,strings]%
    110112
    111113% CFA programming language, based on ANSI C (with some gcc additions)
     
    135137}
    136138
     139% pseudo code
     140\lstdefinelanguage{pseudo}{
     141        morekeywords={string,uint,int,bool,float},
     142        sensitive=true,
     143        morecomment=[l]{//},
     144        morecomment=[s]{/*}{*/},
     145        morestring=[b]',
     146        morestring=[b]",
     147        morestring=[s]{`}{`},
     148}
     149
    137150% Local Variables: %
    138151% tab-width: 4 %
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    ra3525c4 r30a1f0c  
    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.