CS考试辅导 COMP 302: Programming Languages and Paradigms

Week 5: References Prof. Xujie Si
COMP 302: Programming Languages and Paradigms

Survey of lecture material difficulty (so far)

Copyright By PowCoder代写 加微信 powcoder

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*)
let y = x + (let x = 10 in x * x)
let y = 10.0 *. float_of_int x
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*)
let y = x + (let x = 10 in x * x)
let y = 10.0 *. float_of_int x
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*)
let y = x + (let x = 10 in x * x)
let y = 10.0 *. float_of_int x
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*)
let y = x + (let x = 10 in x * x)
let y = 10.0 *. float_of_int x
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*)
let y = x + (let x = 10 in x * x)
let y = 10.0 *. float_of_int x
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)

Recall: Variable Binding and Overshadowing
letx=23 (*xbindsto23*) let y = x + (let x = 10 in x * x)
(* y binds to 123 (23 + 100) *)
(* y binds to 230.0 (10.0 *. 23.) *)
let y = 10.0 *. float_of_int x
There is no state update, just variable bindings to different values of different types

Program with state (imperative style)
let x = ref 42
x : int ref
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
Will this overwrite the memory that x refers to previously?
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
Will this overwrite the memory that x refers to previously? No! ref always creates a new reference cell.
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
let x = ref [1; 2; 3]
Will this overwrite the memory that x refers to previously? No! ref always creates a new reference cell.
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
let x = ref [1; 2; 3] x:
Will this overwrite the memory that x refers to previously? No! ref always creates a new reference cell.
Allocates a reference cell with the name x in memory and initialize it with value 42

Program with state (imperative style)
let x = ref 42
x : int ref
let x = ref “hello”
x : string ref
let x = ref [1; 2; 3] x : int list ref
Will this overwrite the memory that x refers to previously? No! ref always creates a new reference cell.
Allocates a reference cell with the name x in memory and initialize it with value 42

Read a state
let x = ref 42
Allocate a reference cell (or “state”)

Read a state
let x = ref 42
let y = x + 1
Allocate a reference cell (or “state”)

Read a state
let x = ref 42
let y = x + 1
Allocate a reference cell (or “state”) We will get a type error!

Read a state
let x = ref 42
let y = x + 1
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref

Read a state
let x = ref 42
let y = x + 1
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref
Read value that is stored in the reference cell with the name x

Read a state
let x = ref 42
let y = x + 1
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref
Read value that is stored in the reference cell with the name x
(!) : ’a ref -> ’a

Read a state
let x = ref 42
let y = x + 1
let y = !x + 1
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref
Read value that is stored in the reference cell with the name x
(!) : ’a ref -> ’a

Read a state
let x = ref 42
let y = x + 1
let y = !x + 1
x : int ref
(!x) : int
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref
Read value that is stored in the reference cell with the name x
(!) : ’a ref -> ’a

Read a state
let x = ref 42
let y = x + 1
let y = !x + 1
x : int ref
(!x) : int
Allocate a reference cell (or “state”) We will get a type error!
(+) : int -> int -> int
x : int ref
Read value that is stored in the reference cell with the name x
(!) : ’a ref -> ’a let y = 43 (* 42 + 1 = 43 *)

Write a state
let x = ref 42

Write a state
let x = ref 42
let x = 100

Write a state
let x = ref 42
let x = 100
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed

Write a state
let x = ref 42
let x = 100
let x = ref 42
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)

Write a state
let x = ref 42
let x = 100
let x = ref 42
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)

Write a state
let x = ref 42
let x = 100
let x = ref 42
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit What is the value of the reference cell with name X after this?

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit What is the value of the reference cell with name X after this?

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit What is the value of the reference cell with name X after this?
“refer to a unique memory cell”

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit What is the value of the reference cell with name X after this?
“refer to a unique memory cell”
“content of the memory cell”

Write a state
let x = ref 42
let x = 100
let x = ref 42
let y = x in
Note that this has nothing to do with writing a state!!
We just created a new binding and the previous x is overshadwed
“different names of the same memory cell”
“refer to a unique memory cell”
“content of the memory cell”
Write the value 123 into the reference cell with the name x (The previously stored value 42 is overwritten)
(:=) : ’a ref -> ’a -> unit What is the value of the reference cell with name X after this?

Comparison
let x = ref 42
let z = ref 42
x = y (* true *)
x = z (* true *)
y = z (* true *)
x == y (* true *)
x == z (* false *)
y == z (* false *)
(=) tests whether two values are “logically” equal
(==) tests whether two values are “physically” the same

Can “Montreal” be a person’s name?

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.
let int = 42

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.
let int = 42

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.
let int = 42

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.
let int = 42
function name

Can “Montreal” be a person’s name?
Yes, city names and person names are two completely different categories. There will be no confusion when the context is clear.
A person and a city can have the same name.
let int = 42
function name

The ref function and data type
let x = ref 42 x : int ref
This ref is a function
This ref is a data type

The ref function and data type
let x = ref 42 x : int ref
This ref is a function
ref : ’a -> ’a ref
This ref is a data type

The ref function and data type
let x = ref 42 x : int ref
This ref is a function
This ref is a data type
ref : ’a -> ’a ref type ’a ref = {mutable contents : ‘a}

The ref function and data type
let x = ref 42 x : int ref
This ref is a function
This ref is a data type
ref : ’a -> ’a ref type ’a ref = {mutable contents : ‘a}

The ref function and data type
let x = ref 42 This ref is a function
ref : ’a -> ’a ref
x : int ref This ref is a data type
type ’a ref = {mutable contents : ‘a}
let y = !x
let y = x.contents

The ref function and data type
let x = ref 42 This ref is a function
ref : ’a -> ’a ref
x : int ref This ref is a data type
type ’a ref = {mutable contents : ‘a}
let y = !x
let y = x.contents
let {contents = y} = x

The ref function and data type
let x = ref 42 This ref is a function
ref : ’a -> ’a ref
x : int ref This ref is a data type
type ’a ref = {mutable contents : ‘a}
let y = !x
let y = x.contents
let {contents = y} = x
let {contents = y} = {contents = 42}

The ref function and data type
let x = ref 42 This ref is a function
ref : ’a -> ’a ref
x : int ref This ref is a data type
type ’a ref = {mutable contents : ‘a}
let y = !x
let y = x.contents
let {contents = y} = x
let {contents = y} = {contents = 42}
x.contents <- 123 Imperative vs Functional Programming in OCaml Imperative vs Functional Programming in OCaml Imperative vs Functional Programming in OCaml Imperative vs Functional Programming in OCaml More complicated than the functional way Harder to reason about the correctness (Considered bad style, please do NOT use for-loops in HWs) Reasonable Uses of State: Global Counter Reasonable Uses of State: Global Counter Reasonable Uses of State: Global Counter Reasonable Uses of State: Global Counter Reasonable Uses of State: Global Counter (e1 : unit) ; e2 The first expression must have the type of unit Reasonable Uses of State: shared state Reasonable Uses of State: shared state type point = {x : int; y : int} let p = {x=2; y=3} Reasonable Uses of State: shared state type point = {x : int; y : int} let p = {x=2; y=3} Reasonable Uses of State: shared state type point = {x : int; y : int} let p = {x=2; y=3} Reasonable Uses of State: shared state type point = {x : int; y : int} let p = {x=2; y=3} Reasonable Uses of State: shared state type point = {x : int; y : int} let p = {x=2; y=3} • Programming with mutable state using references • Understanding the difference between variable bindings and mutable • Declarative programming is less error prone • Objects can be modeled using records with function values and shared states • Define a data type representing (mutable) linked list • Construct the following linked lists 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com