Changeset d734fa1
- Timestamp:
- Mar 26, 2024, 4:47:29 AM (9 months ago)
- Branches:
- master
- Children:
- d066c5b
- Parents:
- 486caad
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
r486caad rd734fa1 2215 2215 2216 2216 % https://ocaml.org/docs/basic-data-types#enumerated-data-types 2217 % https://dev.realworldocaml.org/runtime-memory-layout.html 2217 2218 2218 2219 OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage. 2219 2220 The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, pure enumeration. 2221 2222 (I think the value of a ocaml variants are types not object, so I am not sure about this line) 2223 OCaml provides a variant (union) type, which is an aggregation of heterogeneous types. 2224 A basic variant is a list of nullary datatype constructors, which is like an unscoped, pure enumeration. 2220 2225 \begin{ocaml} 2221 2226 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun … … 2230 2235 @CS442@ 2231 2236 \end{ocaml} 2232 The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the vale in the object storage. 2237 The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the value in the object storage. 2238 OCaml compile store tags as ascending int, starting from 0. Variants @Mon@ to @Sun@ are stored as int value 0 to 6. 2233 2239 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@. 2234 2240 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. … … 2240 2246 type colour = Red | Green of @string@ | Blue of @int * float@ 2241 2247 \end{ocaml} 2248 A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block. 2242 2249 @colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@. 2243 2250 (Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.) … … 2252 2259 @Red, abc, 1 1.5@ 2253 2260 \end{ocaml} 2261 2254 2262 2255 2263 A variant type can have a recursive definition.
Note: See TracChangeset
for help on using the changeset viewer.