Changeset 9a32903
- Timestamp:
- Mar 12, 2024, 1:57:50 PM (9 months ago)
- Branches:
- master
- Children:
- 423c0cd
- Parents:
- 75d789c
- Location:
- doc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/bibliography/pl.bib
r75d789c r9a32903 5985 5985 } 5986 5986 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 5987 5998 @book{Galletly96, 5988 5999 keywords = {occam}, -
doc/theses/jiada_liang_MMath/relatedwork.tex
r75d789c r9a32903 2463 2463 2464 2464 \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 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. 2468 2469 \begin{ocaml} 2469 2470 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun 2471 let day : weekday = Mon $\C{(* bind *)}$ 2472 let 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" 2478 let _ = take_class( Mon ); 2479 @CS442@ 2470 2480 \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. 2481 The only operations are binding a tag and pattern matching (equality). 2482 Here, 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@. 2483 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. 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. 2479 2488 \begin{ocaml} 2480 2489 type colour = Red | Green of string | Blue of int * float 2481 2490 \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@.) 2493 Hence, a variant type creates a sum of product of different types. 2494 \begin{ocaml} 2495 let c : colour = Red $\C{(* 0-ary constructor *)}$ 2496 let _ = match c with Red -> Printf.printf "Red, " 2497 let c : colour = Green( "abc" ) $\C{(* 1-ary constructor *)}$ 2498 let _ = match c with Green g -> Printf.printf "%s, " g 2499 let c : colour = Blue( 1, 1.5 ) $\C{(* 2-ary constructor *)}$ 2500 let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f 2501 @Red, abc, 1 1.5@ 2502 \end{ocaml} 2486 2503 2487 2504 A variant type can have a recursively definition. … … 2489 2506 type stringList = Empty | Pair of string * stringList 2490 2507 \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. 2508 which is a recursive sum of product of types, called an \Newterm{algebraic data-type}. 2509 A recursive function is often used to pattern match against a recursive variant type. 2512 2510 \begin{ocaml} 2513 2511 let rec len_of_string_list(l: stringList): int = … … 2516 2514 Pair(_ , r) -> 1 + len_of_string_list r 2517 2515 \end{ocaml} 2516 2517 Note, matching is logically a dynamic type-check. 2518 Hence, 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.