<jupyter_start><jupyter_text>Datatypes      CS3100 Fall 2019   ReviewPreviously* Function definition and application* Anonymous and recursive functions* Tail call optimisationThis lecture,* Data types* Pattern matching  Type aliasesOCaml support the definition of aliases for existing types. For example,<jupyter_code>type int_float_pair = int * float
let x = (10, 3.14)
let y : int_float_pair = x<jupyter_output><empty_output><jupyter_text>Records* Records in OCaml represent a collection of named elements. * A simple example is a point record containing x, y and z fields:<jupyter_code>type point = {
  x : int;
  y : int;
  z : int;
}<jupyter_output><empty_output><jupyter_text>Records: Creation and accessWe can create instances of our point type using `{ ... }`, and access the elements of a point using the `.` operator:<jupyter_code>let origin = { y = 0; x = 0;z = 0 }

let get_y (r : point) = r.y<jupyter_output><empty_output><jupyter_text>Records: Functional update* New records can also be created from existing records using the `with` keyword.<jupyter_code>let p = { origin with z = 10 }<jupyter_output><empty_output><jupyter_text>* `p` is a new record with the same fields as `origin` except `z`.* `origin` remains unchanged!<jupyter_code>origin<jupyter_output><empty_output><jupyter_text>Records: Field punningAnother useful trick with records is field punning, which allows you to replace:<jupyter_code>let mk_point x y z = { x = x; y = y; z = z }<jupyter_output><empty_output><jupyter_text>with<jupyter_code>let mk_point x y z = { x; y; z }<jupyter_output><empty_output><jupyter_text>Product Types* Records and tuples are known as **product types**.   + Each value of a product type includes all of the types that constitute the product.   ```ocamltype person_r = {name: string; age: int; height: float}type person_t = string * int * float```* Records are indexed by *names* whereas *tuples* are indexed by positions (1st, 2nd, etc.).  what is the sum type?  VARIANTS   Defining variantsThe type definition syntax is:```ocamltype t = | C1 of t1| C2 of t2| C3 of t2| ...```* C1, C2, C2 are known as constructors* t1, t2 and t3 are optional data carried by constructor* Also known as **Algebraic Data Types**<jupyter_code>type color = 
  | Red
  | Green
  | Blue
let v = (Green , Red)
type point = {x : int; y : int}

type shape = 
  | Circle of point * float (* center, radius *)
  | Rect of point * point   (* lower-left, upper-right *)
  | ColorPoint of point * color
Circle ({x=4;y=3}, 2.5)
Rect ({x=3;y=4}, {x=7;y=9})<jupyter_output><empty_output><jupyter_text>Recursive variant typesLet's define an integer list<jupyter_code>type intlist = 
  | INil
  | ICons of int * intlist
ICons (1, ICons (2, ICons (3, INil)))<jupyter_output><empty_output><jupyter_text>* `Nil` and `Cons` originate from Lisp.  String List```ocamltype stringlist =  | SNil  | Scons of string * stringlist``` * Now what about `pointlist`, `shapelist`, etc?  Parameterized Variants<jupyter_code>type 'a lst = 
    Nil
  | Cons of 'a * 'a lst
Cons (1, Cons (2, Nil))
Cons ("Hello", Cons("World", Nil))<jupyter_output><empty_output><jupyter_text>Type Variable* **Variable**: name standing for an unknown value* **Type Variable**: name standing for an unknown type * Java example is `List` * OCaml syntax for type variable is a single quote followed by an identifier  + '`foo`, `'key`, '`value`* Most often just `'a`, `'b`.   + Pronounced "alpha", "beta" or "quote a", "quote b".  Polymorphism* The type `'a lst` that we had defined earlier is a **polymorphic data type**.* poly = many, morph = change. * write functionality that works for many data types.* Related to Java Generics and C++ template instantiation.* In `'a lst`, `lst` is known as a **type constructor**.  + constructs types such as `int lst`, `string lst`, `shape lst`, etc.  OCaml built-in lists are just variantsOCaml effectively codes up lists as variants:```ocamltype 'a list = [] | :: of 'a * 'a list```* `[]` and `::` are constuctors. * Just a bit of syntactic magic to use `[]` and `::` as constructors rather than alphanumeric identifiers.<jupyter_code>[]
1::2::[]<jupyter_output><empty_output><jupyter_text>Null"I call it my billion-dollar mistake. It was the inventionof the null reference in 1965. At that time, I wasdesigning the first comprehensive type system forreferences in an object-oriented language. My goal wasto ensure that all use of references should beabsolutely safe, with checking performed automaticallyby the compiler. **But I couldn’t resist the temptation toput in a null reference, simply because it was so easy toimplement.** This has led to innumerable errors,vulnerabilities, and system crashes, which haveprobably caused a billion dollars of pain and damage inthe last forty years." - <NAME>   Option: A Built-in Variant* OCaml does not have a null value. ```ocamltype 'a option = None | Some of 'a```<jupyter_code>None
Some 10
Some "Hello"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11028225806451612, 'fraction_numerical': 0.010685483870967741, 'mean_word_length': 5.0426309378806335, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '354648', 'license': 'MIT', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1553, 'n_words': 620, 'path': 'lectures/lec5/lec5.ipynb', 'repo_name': 'gowthamk/csci7000_f20', 'size': '4968'}