Sunteți pe pagina 1din 4

Características de Lenguajes de Programación

Primer parcial
Licenciatura en Informática • Universidad Nacional de Quilmes

2 de octubre de 2018

Nombre y apellido: Jano & Juan

1. El siguiente algoritmo toma dos números positivos a y b y devuelve el par (q, r) donde q es el
resultado de la divión entera de a por b y r el resto:
a, b, q, r ∈ N
read(a, b);
if a = 0
then q = 0, r = 0;
else{
q = 0, r = a;
while (r ≥ b) do r = r − b, q = q + 1;
}
devolver (q, r)

a) Determinar si el siguiente término de PCF con pares implementa dicho algoritmo (explicar
si hace algo diferente, y modificarlo si fuera necesario):

div = λa.λb.ifz a then (0, 0) else fix f.λr.λq.ifz r − b then (q, r) else f (q + 1)(r − b)

Recordar que si x < y, x − y = 0.


b) Dar la traza de div 5 2, utilizando el término del punto anterior o la modificación realizada, y
eligiendo alguna estrategia vista en clase (CBN o CBV, débil o fuerte) e indicar la estrategia
elegida.

Solución:
a) El fix debería estar aplicado a a y a 0, que es el equivalente a la línea q = 0, r = a del
algoritmo en pseudo-código. Además, los parámetros pasados a la f están al revés (primero
recibe el resto y luego el cociente). Por lo tanto, el término correcto es

div = λa.λb.ifz a then (0, 0) else (fix f.λr.λq.ifz r − b then (q, r) else f (r − b)(q + 1))a 0
b) Traza de div 5 2 (llamamos divFb a fix f.λr.λq.ifz r − b then (q, r) else f (r − b)(q + 1)):
div 5 2 → (λb.ifz 5 then (0, 0) else divFb 5 0)2
→ ifz 5 then (0, 0) else divF2 5 0
→ divF2 5 0
→ (λr.λq.ifz r − 2 then (q, r) else divF2 (r − 2)(q + 1)) 5 0
→ (λq.ifz 5 − 2 then (q, 5) else divF2 (5 − 2)(q + 1)) 0
→ ifz 5 − 2 then (0, 5) else divF2 (5 − 2)(0 + 1)
→ ifz 3 then (0, 5) else divF2 (5 − 2)(0 + 1)
→ divF2 (5 − 2)(0 + 1)
Hasta aquí, CBN y CBV, fuerte y débil eran iguales. A partir de aquí cambian las estrategias
(fuerte y débil, sin embargo, siguen siendo iguales), así que damos las dos.
CBV
→ divF2 (5 − 2) 1
→ divF2 3 1
→ (λr.λq.ifz r − 2 then (q, r) else divF2 (r − 2)(q + 1)) 3 1
→ (λq.ifz 3 − 2 then (q, 3) else divF2 (3 − 2)(q + 1)) 1
→ ifz 3 − 2 then (1, 3) else divF2 (3 − 2)(1 + 1)
→ ifz 1 then (1, 3) else divF2 (3 − 2)(1 + 1)
→ divF2 (3 − 2)(1 + 1)
→ divF2 (3 − 2) 2
→ divF2 1 2
→ (λr.λq.ifz r − 2 then (q, r) else divF2 (q + 1)(r − 2)) 1 2
→ (λq.ifz 1 − 2 then (q, 1) else divF2 (q + 1)(1 − 2)) 2
→ ifz 1 − 2 then (2, 1) else divF2 (2 + 1)(1 − 2)
→ ifz 0 then (2, 1) else divF2 (2 + 1)(1 − 2)
→ (2, 1)
CBN
→ (λr.λq.ifz r − 2 then (q, r) else divF2 (r − 2)(q + 1)) (5 − 2) (0 + 1)
→ (λq.ifz (5 − 2) − 2 then (q, (5 − 2)) else divF2 ((5 − 2) − 2)(q + 1)) (0 + 1)
→ ifz (5 − 2) − 2 then ((0 + 1), (5 − 2)) else divF2 ((5 − 2) − 2)((0 + 1) + 1)
→ ifz 3 − 2 then ((0 + 1), (5 − 2)) else divF2 ((5 − 2) − 2)((0 + 1) + 1)
→ ifz 1 then ((0 + 1), (5 − 2)) else divF2 ((5 − 2) − 2)((0 + 1) + 1)
→ divF2 ((5 − 2) − 2)((0 + 1) + 1)
→ (λr.λq.ifz r − 2 then (q, r) else divF2 (q + 1)(r − 2))((5 − 2) − 2)((0 + 1) + 1)
→ (λq.ifz ((5 − 2) − 2) − 2 then (q, (5 − 2) − 2) else divF2 (q + 1)(((5 − 2) − 2) − 2))((0 + 1) + 1)
→ ifz ((5 − 2) − 2) − 2 then (((0 + 1) + 1), (5 − 2) − 2) else divF2 (((0 + 1) + 1) + 1)(((5 − 2) − 2) − 2)
→ ifz (3 − 2) − 2 then (((0 + 1) + 1), (5 − 2) − 2) else divF2 (((0 + 1) + 1) + 1)(((5 − 2) − 2) − 2)
→ ifz 1 − 2 then (((0 + 1) + 1), (5 − 2) − 2) else divF2 (((0 + 1) + 1) + 1)(((5 − 2) − 2) − 2)
→ ifz 0 then (((0 + 1) + 1), (5 − 2) − 2) else divF2 (((0 + 1) + 1) + 1)(((5 − 2) − 2) − 2)
→ (((0 + 1) + 1), (5 − 2) − 2)
→ ((1 + 1), (5 − 2) − 2)
→ (2, (5 − 2) − 2)
→ (2, 3 − 2)
→ (2, 1)

2
2. Considerar la siguiente función:
r = λn.ifz n then 0 else (fix f.λn.ifz n then 1 else n × f (n − 1))n
Mostrar que ` r : nat ⇒ nat (es decir, dar el árbol de derivación de tipo).
Solución:
..
. axv
fix
axv axc n : nat ` Fact : nat ⇒ nat n : nat ` n : nat ⇒e
n : nat ` n : nat n : nat ` 0 : nat n : nat ` (fix f.λn.ifz n then 1 else n × f (n − 1))n : nat
ifz
n : nat ` ifz n then 0 else fix f.λn.ifz n then 1 else n × f (n − 1) : nat
⇒i
` λn.ifz n then 0 else fix f.λn.ifz n then 1 else n × f (n − 1) : nat ⇒ nat

3. Sea ω = fix f.let x = 3 in ifz x then 1 else x × f .


a) Usar inferencia de tipos simples (Hindley y Robinson) para mostrar que ` ω : nat.
b) Mostrar que ω no termina (por ejemplo, dando una traza de ejecución infinita).
c) Explicar porqué siendo ω un término tipado y que no termina, no contradice el teorema de
Tait (normalización fuerte).
Solución:
a) Hindley: Sea Γ = f : A, x : B. Entonces

Γ`x B, ∅ Γ ` f A, ∅
Γ`x B, ∅ Γ ` 1 nat, ∅ Γ ` x × f nat, {A = nat, B = nat}
f :A`3 nat, ∅ Γ ` ifz x then 1 else x × f nat, {A = nat, B = nat, nat = nat}
f : A ` let x = 3 in ifz x then 1 else x × f nat, {A = nat, B = nat, nat = nat}
` fix f.let x = 3 in ifz x then 1 else x × f nat, {A = nat, B = nat, nat = nat}
Robinson: 
 A = nat 
A = nat
B = nat =⇒
B = nat
nat = nat

Por lo tanto, el tipo principal es nat.


b) ω → let x = 3 in ifz x then 1 else x×ω → ifz 3 then 1 else 3×ω → 3×ω →× 3×3×· · ·×3×ω
c) El teorema de Tait sólo aplica a términos que no contienen fix.

4. Dar el árbol de derivación de tipo polimórfico para el siguiente término:


(λn.let x = λy.y in xxn)2
Dar también la traza de ejecución de este término usando la estrategia CBN fuerte.
Solución:
Sea ∆ = n : [nat] y Γ = ∆, x : ∀X.[X ⇒ X].
axv axv
Γ ` x : ∀X.[X ⇒ X] Γ ` x : ∀X.[X ⇒ X]
axv ∀e ∀e
∆, y : [X] ` y : [X] Γ ` x : [(nat ⇒ nat) ⇒ (nat ⇒ nat)] Γ ` x : [nat ⇒ nat]
⇒i ⇒e axc
∆ ` λy.y : [X ⇒ X] Γ ` xx : [nat ⇒ nat] Γ ` n : [nat]
∀i ⇒e
∆ ` λy.y : ∀X.[X ⇒ X] Γ ` xxn : [nat]
let
∆ ` let x = λy.y in xxn : [nat]
⇒i axc
` λn.let x = λy.y in xxn : [nat ⇒ nat] ` 2 : [nat]
⇒e
` (λn.let x = λy.y in xxn)2 : [nat]

(λn.let x = λy.y in xxn)2 → let x = λy.y in xx2 → ((λy.y)λy.y)2 → (λy.y)2 → 2

3
5. Dar el árbol de derivación de tipo polimórfico del término del ejercicio anterior, utilizando el
sistema dirigido por sintaxis.
Solución:
axv axv
Γ ` x : [(nat ⇒ nat) ⇒ (nat ⇒ nat)] Γ ` x : [nat ⇒ nat]
axv ⇒e axc
∆, y : [X] ` y : [X] Γ ` xx : [nat ⇒ nat] Γ ` n : [nat]
⇒i ⇒e
∆ ` λy.y : [X ⇒ X] Γ ` xxn : [nat]
let
∆ ` let x = λy.y in xxn : [nat]
⇒i axc
` λn.let x = λy.y in xxn : [nat ⇒ nat] ` 2 : [nat]
⇒e
` (λn.let x = λy.y in xxn)2 : [nat]

6. Utilizar el intérprete (en CBN o CBV, indicando cuál se utiliza) para interpretar el siguiente
término:
(λn.let x = λy.y in xn)2

Solución:
CBV
Sean
Γ:n=2
∆ : Γ, x = hy, y, n = 2i.

∆ ` n ,→ 2 ∆ ` x ,→ hy, y, n = 2i ∆, y = 2 ` y ,→ 2
Γ ` λy.y ,→ hy, y, n = 2i ∆ ` xn ,→ 2
` 2 ,→ 2 ` λn.let x = λy.y in xn ,→ hn, let x = λy.y in xn, ∅i Γ ` let x = λy.y in xn ,→ 2
` (λn.let x = λy.y in xn)2 ,→ 2

CBN
Sean
Γ : n = h2, ∅i
∆ : Γ, x = hλy, y, Γi.

` 2 ,→ 2
Γ ` λy.y ,→ hy, y, Γi ∆ ` n ,→ 2
∆ ` x ,→ hy, y, Γi ∆, y = hn, ∆i ` y ,→ 2
∆ ` xn ,→ 2
` λn.let x = λy.y in xn ,→ hn, let x = λy.y in xn, ∅i Γ ` let x = λy.y in xn ,→ 2
` (λn.let x = λy.y in xn)2 ,→ 2

S-ar putea să vă placă și