This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
18.02-continuation_passing_style
57 lines (46 loc) · 1.84 KB
/
18.02-continuation_passing_style
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
CONTINUATION PASSING STYLE
============================
Este método de computação é o utilizado dentro do esquema
cliente-servidor (mesmo com suas várias formas de manter o estado). O
conceito chave é quebrar uma certa computação em uma série de passos
menores, em que vamos mantendo o "resto" da computação depois da
operação.
Ex:
; Toda expressão CONSTANTE deve ser quebrada na forma:
; * Constantes cte:
; (λ (resto) (resto cte) )
- Original:
(+ 2 2)
- CPS:
(
(λ (resto) (resto 2))
(λ (x) (+ x 2))
)
Ex:
; Todo LAMBDA deve ser quebrada na forma:
; * Lambdas (λ (arg) (ação arg)):
; (λ (arg resto) (resto (ação arg)) )
- Original:
((λ (a) (+ (* a 2) 5)) 2)
- CPS:
(
; Criamos um lambda com a computação mais interna, (* a 2)
; que é depois aplicada no "resto" da ação.
(λ (a, resto) (resto (* a 2)))
; O argumento para este lambda são dois valores: o x e o resto.
(2 (λ (x) (+ x 5)))
)
Para cada tipo de estrutura, quebraríamos de forma diferentes. Essa é
uma forma de TRANSFORMAÇÃO, em que estamos modificando o programa SEM
alterar o resultado. A implementação poderia ser feita de duas formas:
* Açúcar : Usando macros para transformar as sentenças originais;
* Core : Modificar o core para implementar esta forma.
Se pegássemos uma expressão, teríamos a transformação na forma:
e: 3 * (5 + 2) - 7 * 4
f(x): 3 * x - 7 * 4 e = f(5+2)
g(y): y - 7 * 4 e = g(f(5+2))
... ...
Estamos passando o resultado de uma ação para a próxima. Essa é a ideia
de PILHA DE EXECUÇÃO, como feita em Assembly. Cada ação devolve apenas
uma modificação de estado, e o estado é compartilhada pelas várias ações
necessárias para serem feitas.