Changeset d734fa1


Ignore:
Timestamp:
Mar 26, 2024, 4:47:29 AM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
d066c5b
Parents:
486caad
Message:

Comment on relatedwork work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r486caad rd734fa1  
    22152215
    22162216% https://ocaml.org/docs/basic-data-types#enumerated-data-types
     2217% https://dev.realworldocaml.org/runtime-memory-layout.html
    22172218
    22182219OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
    22192220The 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)
     2223OCaml provides a variant (union) type, which is an aggregation of heterogeneous types.
     2224A basic variant is a list of nullary datatype constructors, which is like an unscoped, pure enumeration.
    22202225\begin{ocaml}
    22212226type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     
    22302235@CS442@
    22312236\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.
     2237The 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.
     2238OCaml compile store tags as ascending int, starting from 0. Variants @Mon@ to @Sun@ are stored as int value 0 to 6.
    22332239Here, 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@.
    22342240The ``@_@'' 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.
     
    22402246type colour = Red | Green of @string@ | Blue of @int * float@
    22412247\end{ocaml}
     2248A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
    22422249@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
    22432250(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
     
    22522259@Red, abc, 1 1.5@
    22532260\end{ocaml}
     2261
    22542262
    22552263A variant type can have a recursive definition.
Note: See TracChangeset for help on using the changeset viewer.