Docsity

User-Defined Datatypes in OCaml: Records, Variants, and Recursive Types - Prof. Elsa Gunte, Study notes of Computer Science

Partial preview of the text

Download User-Defined Datatypes in OCaml: Records, Variants, and Recursive Types - Prof. Elsa Gunte and more Study notes Computer Science in PDF only on Docsity!

Outline Introduction Records Variants Recursive Types

CS421 Topic 5: User-Defined Datatypes in

OCaml

Sameer Sundresh

[email protected]

University of Illinois at Urbana-Champaign

June 4, 2007

Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul

Agha, Elsa Gunter, and Mark Hills

Outline Introduction Records Variants Recursive Types

Objectives

User-defined datatypes in OCaml will be heavily used in upcoming

MPs and are a fundamental feature of the ML family of languages.

I basics of types

I different kinds of user-defined data types

I how to create new data types

I manipulating data types with functions and pattern matching

Outline Introduction Records Variants Recursive Types

Introduction Type Built-in types User-defined types

Introduction: why have data types?

Data types play a key role in:

I the design of programs (through data abstraction);

I the analysis of programs (through type checking and type

inference);

I the translation and execution of programs (through

compile-time code generation).

Outline Introduction Records Variants Recursive Types

Introduction Type Built-in types User-defined types

Type

A type t defines a set of possible data values sharing some

common property

I e.g., short in C ≡ {i : − 215 ≤ i ≤ 215 − 1 }

I A value in this set is said to be of type t.

Outline Introduction Records Variants Recursive Types

Introduction Type Built-in types User-defined types

User-defined types

Sometimes it’s useful to define your own data types. OCaml

provides:

I records—similar to C structs

I variants—when you want “a number or a string,” etc.

I enumerations—similar to C enums, but unordered

I disjoint unions—similar to C unions, but typesafe

I polymorphic variants—make your own type constructors!

I recursive data types allow arbitrary nesting

Outline Introduction Records Variants Recursive Types

Overview Record Types Record Values Pattern Matching Access and Creation

Records

I Records serve then same programming purpose as tuples

I Provide better documentation, more readable code

I Allow components to be accessed by label instead of position

I Labels (aka field names must be unique)

I Fields accessed by suffix dot notation

Outline Introduction Records Variants Recursive Types

Overview Record Types Record Values Pattern Matching Access and Creation

Record Values

Records built with labels; order does not matter

1 # let hello = {text = "Hello";

2 font size = 72;

3 coordinate = (123,45)};;

4 val hello : label =

5 {text = "Hello"; coordinate = (123, 45); font size = 72}

Outline Introduction Records Variants Recursive Types

Overview Record Types Record Values Pattern Matching Access and Creation

Record Values

1 # let good bye = {coordinate=(987,65);

2 text="good bye.";

3 font size=3};;

4 val good bye : label =

5 {text = "good bye."; coordinate = (987, 65); font size = 3}

7 # hello = good bye;;

8 - : bool = false

Outline Introduction Records Variants Recursive Types

Overview Record Types Record Values Pattern Matching Access and Creation

Record Field Access

1 # let xy = hello.coordinate;;

2 val xy : int * int = (123, 45)

Outline Introduction Records Variants Recursive Types

Overview Record Types Record Values Pattern Matching Access and Creation

New Records from Old

1 # let bigger label =

2 {label with font size = label.font size + 1};;

3 val bigger : label -> label =

5 # bigger hello;;

6 - : label = {text = "Hello"; coordinate = (123, 45);

7 font size = 73}

Outline Introduction Records Variants Recursive Types

Overview Enumerations Disjoint Unions Polymorphism and Option Maps and Folds

Variants

I Single data type, multiple forms

I Represent several different types of data with one type name

Common examples:

I lists

I trees

I syntactic elements in a programming language

Outline Introduction Records Variants Recursive Types

Overview Enumerations Disjoint Unions Polymorphism and Option Maps and Folds

Variant Syntax

Variants are declared as types with constructors, which take zero

or more data items; constructor names always start with CAPS

I no data with constructor name, constructor is a constant

I data types can include polymorphic types (more on this later)

I each constructor essentially a function from constructor’s data

to variant type

fun x 1 · · · xn-> C x 1 · · · xn (type t 1 · · · tn-> name)

I constructors the basis of almost all pattern matching

Outline Introduction Records Variants Recursive Types

Overview Enumerations Disjoint Unions Polymorphism and Option Maps and Folds

Enumerations

I Enumerations are variants where all constructors are constants

I Essentially forms a collection of distinct elements (days of the

week, months of the year, etc)

I Unlike in C, enumerations in OCaml are unordered

Outline Introduction Records Variants Recursive Types

Overview Enumerations Disjoint Unions Polymorphism and Option Maps and Folds

Enumerations: An Example

1 # type weekday = Monday | Tuesday | Wednesday

2 | Thursday | Friday | Saturday | Sunday;;

3 type weekday =

4 Monday

5 | Tuesday

6 | Wednesday

7 | Thursday

8 | Friday

9 | Saturday

10 | Sunday