Changeset 9a32903


Ignore:
Timestamp:
Mar 12, 2024, 1:57:50 PM (9 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
423c0cd
Parents:
75d789c
Message:

update OCaml text and add OCaml citation

Location:
doc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/bibliography/pl.bib

    r75d789c r9a32903  
    59855985}
    59865986
     5987@manual{Ocaml,
     5988    keywords    = {OCaml programming language},
     5989    contributer = {pabuhr@plg},
     5990    key         = {OCaml},
     5991    title       = {The {OC}aml system, release 5.1},
     5992    optaddress  = {Rust Project Developers},
     5993    year        = 2023,
     5994    note        = {\url{https://v2.ocaml.org/manual/}},
     5995}
     5996
     5997
    59875998@book{Galletly96,
    59885999    keywords    = {occam},
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r75d789c r9a32903  
    24632463
    24642464\section{OCaml}
    2465 \lstnewenvironment{ocaml}[1][]{\lstset{language=ML,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    2466 
    2467 An enumerated data types is a list of named values.
     2465\lstnewenvironment{ocaml}[1][]{\lstset{language=ML,escapechar=\$,morekeywords={match},moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     2466
     2467OCaml~\cite{Ocaml} provides a tagged variant (union) type, where multiple heterogeneously-typed objects share the same storage.
     2468The simplest form of the variant type is a list of untyped tags, which is like an unscoped, pure enumeration.
    24682469\begin{ocaml}
    24692470type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     2471let day : weekday = Mon                                 $\C{(* bind *)}$
     2472let take_class( d : weekday ) =
     2473        match d with                                            $\C{(* matching *)}$
     2474                Mon | Wed -> Printf.printf "CS442\n" |
     2475                Tue | Thu -> Printf.printf "CS343\n" |
     2476                Fri -> Printf.printf "Tutorial\n" |
     2477                _ -> Printf.printf "Take a break\n"
     2478let _ = take_class( Mon );
     2479@CS442@
    24702480\end{ocaml}
    2471 Enumerated data types are the simplest subset of variants types.
    2472 Because @weekday@ is a summantion of values @Mon@ to @Sun@, enumerated data types are often call a sum type in turns of functional programming paradigm.
    2473 
    2474 The values defined in an enumerated type are called data constructors.
    2475 The data constructors of an enumerated data type takes no value as parameter.
    2476 They are known as 0-arity constructor.
    2477 
    2478 A more generic variant type has n-ary constructors.
     2481The only operations are binding a tag and pattern matching (equality).
     2482Here, 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@.
     2483The ``@_@'' 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.
     2484Since the tag has no type, it has a \Newterm{0-arity constructor}, \ie no parameters.
     2485Because @weekday@ is a summation of values @Mon@ to @Sun@, it is a \Newterm{sum type} in turns of the functional-programming paradigm.
     2486
     2487Each tag can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
    24792488\begin{ocaml}
    24802489type colour = Red | Green of string | Blue of int * float
    24812490\end{ocaml}
    2482 The @colour@ type can be constructed as a combination of a value from an @int@ and a @blue@, using the @Blue@ data constructor.
    2483 Mathematically, a @Blue@ value is a Cartesian product of the @int@ type and the @bool@.
    2484 @Colour@ type is a summation of a nullary type, a unary product type, and a cross product of @int@ and @bool@.
    2485 The OCaml variant type can be create as a sum of product of different types.
     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@.)
     2493Hence, a variant type creates a sum of product of different types.
     2494\begin{ocaml}
     2495let c : colour = Red                                    $\C{(* 0-ary constructor *)}$
     2496let _ = match c with Red -> Printf.printf "Red, "
     2497let c : colour = Green( "abc" )                 $\C{(* 1-ary constructor *)}$
     2498let _ = match c with Green g -> Printf.printf "%s, " g
     2499let c : colour = Blue( 1, 1.5 )                 $\C{(* 2-ary constructor *)}$
     2500let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
     2501@Red, abc, 1 1.5@
     2502\end{ocaml}
    24862503
    24872504A variant type can have a recursively definition.
     
    24892506type stringList = Empty | Pair of string * stringList
    24902507\end{ocaml}
    2491 OCaml's variant types are recusrive sum of product of types, which are known as Algebraic Data Types.
    2492 Programming languages follows the functional programming paradigm often supports algebraic data types, and supports pattern matching against algebraic data type.
    2493 \begin{ocaml}
    2494 let take_class = function
    2495         Mon | Wed -> "CS442" |
    2496         Tue | Thu -> "CS343" |
    2497         Fri -> "Tutorial" |
    2498         _ -> "Take a break"
    2499 \end{ocaml}
    2500 The function has a @weekday@ as parameter, and returns @"CS442"@, if the weekday value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@.
    2501 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.
    2502 
    2503 Values of a product type can be named.
    2504 \begin{ocaml}
    2505 let check_colour (c: colour): string =
    2506          match c with
    2507                 Red -> "Red" |
    2508                 Green g -> g |
    2509                 Blue(i, f) -> string_of_int i ^ string_of_float f
    2510 \end{ocaml}
    2511 A recurisve function is often used to pattern match against a recurisve variant type.
     2508which is a recursive sum of product of types, called an \Newterm{algebraic data-type}.
     2509A recursive function is often used to pattern match against a recursive variant type.
    25122510\begin{ocaml}
    25132511let rec len_of_string_list(l: stringList): int =
     
    25162514                Pair(_ , r) -> 1 + len_of_string_list r
    25172515\end{ocaml}
     2516
     2517Note, matching is logically a dynamic type-check.
     2518Hence, a tagged variant has no notion of enumerabilty, and therefore is not an enumeration, except for the simple pure (untyped) case.
Note: See TracChangeset for help on using the changeset viewer.