Docsity

Introduction to OCaml Programming - Prof. Elsa Gunter, Study notes of Computer Science

Partial preview of the text

Download Introduction to OCaml Programming - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

CS421 Lecture 2: Introduction to OCaml

Mark Hills

[email protected]

University of Illinois at Urbana-Champaign

May 31, 2006

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

Agha, and Elsa Gunter

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Objectives for Today

Introducing OCaml

Writing OCaml Programs

Running OCaml

Types

Environments

Patterns

Lists

Other Syntax

Activity Solutions

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

OCaml

I The OCaml system in installed on the EWS machines

I Code for the examples run in this lecture is available on the

lectures page

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

OCaml Online

I Main CAML home: http://caml.inria.fr/index.en.html

I To install OCAML on your computer see:

http://caml.inria.fr/ocaml/release.en.html

I Current release (as of May 27): 3.09.

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Some History...

I CAML is European descendant of original ML

I American/British version is SML

I O is for object-oriented extension

I ML stands for Meta-Language

I ML family designed for implementing theorem provers

I It was the meta-language for programming the object language

of the theorem prover

I Despite obscure original application area, OCAML is a full

general-purpose programming language

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Features of OCaml

Language Features:

I higher order functional language

I call-by-value parameter passing style

I modern syntax

I parametric polymorphism (aka structural polymorphism)

I automatic garbage collection

I user-defined algebraic data types

I strongly typed, uses type inference

I It’s very fast — the winners of the 2000 and 1999 ICFP

Programming Contests used OCaml.

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Starting OCaml Immediate Mode

I Immediate Mode statements must be terminated by a ;;

I Types are inferred if not explicitly provided

I Output can refer to either values returned by expressions or

text printed by print calls – we will usually mean the former

1 $ ocaml

2 Objective Caml version 3.09.

5 - : int = 42

7 - : float = 36.

8 # "hello";;

9 - : string = "hello"

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Hello, World!

I This is the Standard First ProgramTM

I The type unit is like void in C/C++; it represents

commands.

1 # print_string "Hello, world!\n";;

2 Hello, world!

3 - : unit = ()

I The type unit has one value, () (pronounced “unit”).

I Note:

I “Hello, world!” has been output (I/O) to the screen, but

I the result of this function (the value returned) is ().

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Type Errors

If you try to combine things with incompatible types, you get a

type error.

1 # 1 + "hello";;

2 ^^^^^^^

3 This expression has type string but is here used

4 with type int

6 ^

7 This expression has type int but is here used

8 with type float

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Environments

Environments play a central role in languages.

ρ = {name 1 7 → value 1 ; name 2 7 → value 2 ; · · · }

I Mathematically, an environment is a partial function

I Technically a set, but can be represented as a list (find first

match) or a stack

I The environment is usually represented by letter ρ.

I The 7 → symbol is pronounced “maps to”.

I Evaluating an expression always requires an environment

I Some operations create a new environment from an old one:

I global let, local let

I function call (invocation, not definition!)

I match/with

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Variable Creation 2: Local Variables

let name = expression in body

I This creates ρ 2 from ρ 1 , with name having value expression.

I ρ 2 is created after expression is evaluated, used only

within body, and then destroyed.

1 # let i = 10;; (* ρ 1 = { i 7 → 10 } *)

2 val i : int = 10

3 # let a = 20;; (* ρ 1 = { a 7 → 20; i 7 → 10 } *)

4 val a : int = 20

5 # let i = a + a (* ρ 1 = { a 7 → 20; i 7 → 10 } *)

6 in i * a;; (* ρ 2 = { i 7 → 40; a 7 → 20 } *)

7 - : int = 800

8 # i * a;; (* ρ 1 = { a 7 → 20; i 7 → 10 } *)

9 - : int = 200

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Variable Creation 3: Function Definition

let name parameters = body

1 # let i = 10;;

2 val i : int = 10

3 # let f n = i + 10;; // Creates a function

4 val f : ’a -> int = // note the arrow!

5 # f i;; // Function calls are denoted

6 - : int = 20 // by juxtaposition

7 # let i = 100;;

8 val i : int = 100

9 # f i;;

10 - : int = ...? (* What should this be? *)

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Save the Environment!

I A function is a value in this language.

I It is stored internally as a closure....

Cv → exp; ρf B

I The function is stored in the first half, the environment as it

existed when the function was created is stored in the second

half as ρf.

I When the function is called, the variable is created in a copy

of ρf.

I This keeps the behavior of functions from changing when we

redefine a variable.

Outline Objectives for Today Introducing OCaml Writing OCaml Programs

Running OCaml Types Environments Patterns Lists Other Syntax Activity Solutions

Function Calls

1 # let i = 10;; // ρ 1 = {i 7 → 10 }

2 val i : int = 10

3 # let f n = i + 10;; // ρ 2 = {f 7 → Cn → i + 10 , ρ 1 B}

4 val f : ’a -> int = // ∪ ρ 1

5 # f i;;

6 - : int = 20 // ρ 1 a = {n 7 → 10; i 7 → 10 }

7 # let i = 100;; // ρ 3 = {i 7 → 100; f 7 → Cn → i + 10 , ρ 1 B}

8 val i : int = 100

9 # f i;;

10 - : int = ...? // ρ 1 b = {n 7 → 100; i 7 → 10 }

When f is called, we use ρ 3 to look up parameter i, and

assign that to n in a copy of ρ 1. We use this new environment

(ρ 1 a or ρ 1 b) to look up the value of i inside the function.