# Homework 4
- Chiao Lu
- UID: 204848946
- Email: josephlu85@engineering.ucla.edu
- Aishni Parab
- UID: 905526328
- Email: aishni@cs.ucla.edu
## Problem 1
### (a)
<pre>
function p:((A ∧ B) ∧ C) →
let a = fst (fst p) in
let b = snd (fst p) in
let c = snd p in
(a, (b, c))
</pre>
### (b)
<pre>
function p:((A ∨ B) ∨ C) →
match p with
| left aorb →
match aorb with
| left a → left a
| right b → right (left b)
| right c → right (right c)
</pre>
### (c\)
<pre>
function p:((A ∨ B) → C) →
(
function p1:A → p (left p1),
function p2:B → p (right p2)
)
</pre>
### (d)
<pre>
function p1:(A → B) →
function p2:(B → False) →
function p3:A →
p2 (p1 p3)
</pre>
## Problem 2
### (a)
Reference: [https://piazza.com/class_profile/get_resource/h82u3exc6351np/h860gvgnm2e1dr](https://piazza.com/class_profile/get_resource/h82u3exc6351np/h860gvgnm2e1dr)
If we have this rule
<pre>
t1 → t1'
------------------------------
while t1 do t2 → while t1' do t2
</pre>
We would also need something like the following
<pre>
------------------------------
while true do t2 → ???
</pre>
This is basically telling us to keep evaluating `t1` until it is `true` or `false`, and then we must either enter an _infinite loop_ or _not enter the loop at all_, which is definitely not the desired behaviour of `while`. We need the `while` loop to keep looking at `t1` until it evaluates to `false`, instead of just once.
### (b)
Online reference above is suggesting something like <code>if t1 then (t2; while t1 do t2) else ()</code>. We convert the sequencing (`;`) to `let` as discussed in the lecture (`t1;t2 --> let x=t1 in t2`). After that, as pointed out in the Campuswire hint by Todd, we can't use `let` so we convert it further to function call (`E-LetSugar`).
<pre>
fresh x
------------------------------
while t1 do t2 →
if t1 then ((function x:True → while t1 do x) t2) else ()
</pre>
### (c\)
<pre>
Γ ⊢ t1:Bool Γ ⊢ t2:True
------------------------------
Γ ⊢ while t1 do t2:True
</pre>
### (d)
<pre>
function whileFun f1:(True → Bool) f2:(True → True) →
let rec loop =
function p1:(True → Bool) p2:(True → True) →
if (p1 ()) then (let x=(p2 ()) in (loop p1 p2)) else ()
in
(loop f1 f2)
</pre>
## Problem 3
### (a)
<pre>
------------------ (I-Var) ------------------ (I-Var)
tinfer({f:X1,x:X2}, f) = ➀ tinfer({f:X1,x:X2}, x) = ➁
-------------------- (I-Var) --------------------------------------------- (I-App)
tinfer({f:X1,x:X2}, f) = ➂ tinfer({f:X1,x:X2}, f x) = ➃
--------------------------------------------------------------------------- (I-App)
tinfer({f:X1,x:X2}, f (f x)) = ➄
--------------------------------------------------------------------------- (I-Fun)
tinfer({f:X1}, function x → f (f x)) = ➅
--------------------------------------------------------------------------- (I-Fun)
tinfer({}, function f → function x → f (f x)) = ➆
</pre>
Where
<pre>
➀ = (X3, {X3=X1})
➁ = (X4, {X4=X2})
➂ = (X5, {X5=X1})
➃ = (X6, {X3=X1}⋃{X4=X2}⋃{X3=X4→X6})
➄ = (X7, {X5=X1}⋃{X3=X1}⋃{X4=X2}⋃{X3=X4→X6}⋃{X5=X6→X7})
➅ = (X8, {X5=X1}⋃{X3=X1}⋃{X4=X2}⋃{X3=X4→X6}⋃{X5=X6→X7}⋃{X8=X2→X7})
➆ = (X9, {X5=X1}⋃{X3=X1}⋃{X4=X2}⋃{X3=X4→X6}⋃{X5=X6→X7}⋃{X8=X2→X7}⋃{X9=X1→X8})
</pre>
### (b)
<pre>
x:Int
f:Int→Int
X1=Int→Int
X2=Int
X3=Int→Int
X4=Int
X5=Int→Int
X6=Int
X7=Int
X8=Int→Int
X9=(Int→Int)→Int→Int
</pre>
Putting this into ocaml shell gives
```ocaml=
─( 05:13:55 )─< command 0 >────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # function f -> function x -> f (f x);;
- : ('a -> 'a) -> 'a -> 'a = <fun>
─( 05:13:55 )─< command 1 >────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # function (f:int->int) -> function (x:int) -> f (f x);;
- : (int -> int) -> int -> int = <fun>
```
## Problem 4
<pre>
tinfer(G, s) = (S1, C1)
fresh T2
fresh X
--------------------------- (I-Left)
tinfer(G, left s) = (X, C1 ⋃ {X=S1∨T2})
tinfer(G, s) = (S2, C2)
fresh T1
fresh X
--------------------------- (I-Right)
tinfer(G, right s) = (X, C2 ⋃ {X=T1∨S2})
fresh X
fresh X1
fresh X2
tinfer((G, x1:X1), s1) = (S1, C1)
tinfer((G, x2:X2), s2) = (S2, C2)
tinfer(G, s) = (S, C)
------------------------------------(I-Match)
tinfer(G, match s with left x1→s1 |
right x2→s2)
= (X, C ⋃ C1 ⋃ C2 ⋃ {S=S1∨S2, X=S})
</pre>