# Homework 6
- Chiao Lu
- UID: 204848946
- Email: josephlu85@engineering.ucla.edu
- Aishni Parab
- UID: 905526328
- Email: aishni@cs.ucla.edu
## Problem 1
### (a)
```ocaml=
let r = ref 41 in let x = (r:=!r +1) in !r
```
### (b)
```ocaml=
let r = ref 41 in
let x = ((function (r: int ref) -> (r := 41; 500))
(r:=!r+1;ref 404)) in !r;;
```
### (c\)
```ocaml=
let f = (let r = ref 5 in function()
-> (r:=!r+1;!r)) in
(f ()) * (f ());;
```
## Problem 2
### (a)
<pre>
<let x = ref 0 in let y = x in let z = x:=5 in !y, {}> →
<let x = l1 in let y = x in let z = x:=5 in !y, {(l1,0)}> →
<let y = l1 in let z = l1:=5 in !y, {(l1,0)}> →
<let z = l1:=5 in !l1, {(l1,0)}> →
<let z = () in !l1, {(l1, 5)}> →
<!l1, {(l1, 5)}> →
<5, {(l1, 5)}>
</pre>
### (b)
Let <code>Σ={(l1, Int), (l2, Int→Int)}</code>
<pre>
Σ(l1)=Int
----------- (T-LOC) -------- (T-NUM)
{};Σ ⊢ l1:(Ref Int) {};Σ ⊢ 5:Int ➀
---------------------------------(T-ASSIGN) ------------------- (T-APP)
{};Σ ⊢ (l1:=5):True {x:True};Σ ⊢ !l2 !l1 : Int
----------------------------------------------------------------- (T-LET)
{};Σ ⊢ let x = l1:=5 in !l2 !l1 : Int
</pre>
Where ➀ is shown below
<pre>
Σ(l2) = (Int→Int) Σ(l1) = Int
---------------------- (T-LOC) ----------------- (T-LOC)
{x:True};Σ ⊢ l2:(Ref Int→Int) {x:True};Σ ⊢ l1:(Ref Int)
---------------------- (T-DEREF) ------------ (T-DEREF)
{x:True};Σ ⊢ !l2:(Int→Int) {x:True};Σ ⊢ !l1:Int
</pre>
### (c\)
The following program,
```ocaml=
let a = ref 0 in
let b = ref (function x -> x+1) in
let x = a := 5 in !b !a
```
eventually steps to
```ocaml=
let x = l1 := 5 in !l2 !l1
```
Evaluation steps:
<pre>
<let a = ref 0 in
let b = ref (function x → x+1) in
let x = a := 5 in !b !a
, {}>
→
<let a = l1 in
let b = ref(function x → x+1) in
let x = a :=5 in !b !a
, {(l1, 0)}>
→
<let b = ref(function x → x+1) in
let x = l1 :=5 in !b !l1
, {(l1, 0)}>
→
<let b = l2 in
let x = l1 := 5 in !b !l1
, {(l2, function x → x+1), (l1, 0)}>
→
<let x = l1 :=5 in !l2 !l1,
{(l2, function x → x+1), (l1, 0)}>
</pre>
## Problem 3
### (a)
The following program typechecks and is eventually stuck by the following steps:
<pre>
<let x = ref 0 in
let y = x in
let z = free x in
free y, {}>
→
<let x = l1 in
let y = x in
free x in
free y, {(l1, 0)}>
→
<let y = l1 in
free l1 in
free y, {(l1, 0)}>
→
<free l1 in
free l1, {(l1, 0)}>
→
<free l1, {}>
→
<b style="color: #ff0000">stuck</b>
</pre>
### (b)
Program in (a) is a counter example.
### (c\)
This statement holds.