1. O documento discute a representação abstrata do cálculo pointfree e como ele é análogo ao cálculo algébrico.
2. As expressões no cálculo pointfree são representadas como árvores onde os nós são operadores e as folhas são funções.
3. Regras como a definição de operadores como "swap" e propriedades como associatividade podem ser aplicadas às expressões para simplificá-las de forma análoga ao cálculo algébrico.
1. Calculador Pointfree
(Jo˜o
a Ulisses) (Moura Costa)
Departamento de Inform´tica, Universidade do Minho
a
Julho 21, 2008
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
2. Sum´rio
a
1 Contextualiza¸˜o
ca
2 Representa¸˜o
ca
3 Calculador
4 Trabalho futuro
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
3. Sum´rio
a
1 Contextualiza¸˜o
ca
2 Representa¸˜o
ca
3 Calculador
4 Trabalho futuro
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
4. An´logo ao c´lculo alg´brico
a a e
(a + b) + c + d × 0 = a + (b + c)
⇔ {Elemento absorvente da multiplica¸˜o}
ca
(a + b) + c + 0 = a + (b + c)
⇔ {Elemento neutro da adi¸˜o}
ca
(a + b) + c = a + (b + c)
⇔ {Associatividade da soma}
a + (b + c) = a + (b + c)
. . . e o c´lculo Pointfree?
a
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
5. An´logo ao c´lculo alg´brico
a a e
(a + b) + c + d × 0 = a + (b + c)
⇔ {Elemento absorvente da multiplica¸˜o}
ca
(a + b) + c + 0 = a + (b + c)
⇔ {Elemento neutro da adi¸˜o}
ca
(a + b) + c = a + (b + c)
⇔ {Associatividade da soma}
a + (b + c) = a + (b + c)
. . . e o c´lculo Pointfree?
a
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
6. An´logo ao c´lculo alg´brico
a a e
(a + b) + c + d × 0 = a + (b + c)
⇔ {Elemento absorvente da multiplica¸˜o}
ca
(a + b) + c + 0 = a + (b + c)
⇔ {Elemento neutro da adi¸˜o}
ca
(a + b) + c = a + (b + c)
⇔ {Associatividade da soma}
a + (b + c) = a + (b + c)
. . . e o c´lculo Pointfree?
a
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
7. An´logo ao c´lculo alg´brico
a a e
(a + b) + c + d × 0 = a + (b + c)
⇔ {Elemento absorvente da multiplica¸˜o}
ca
(a + b) + c + 0 = a + (b + c)
⇔ {Elemento neutro da adi¸˜o}
ca
(a + b) + c = a + (b + c)
⇔ {Associatividade da soma}
a + (b + c) = a + (b + c)
. . . e o c´lculo Pointfree?
a
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
8. An´logo ao c´lculo alg´brico
a a e
(a + b) + c + d × 0 = a + (b + c)
⇔ {Elemento absorvente da multiplica¸˜o}
ca
(a + b) + c + 0 = a + (b + c)
⇔ {Elemento neutro da adi¸˜o}
ca
(a + b) + c = a + (b + c)
⇔ {Associatividade da soma}
a + (b + c) = a + (b + c)
. . . e o c´lculo Pointfree?
a
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
9. C´lculo Pointfree manual
a
Entediante
N˜o h´ “undo”
aa
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
10. Sum´rio
a
1 Contextualiza¸˜o
ca
2 Representa¸˜o
ca
3 Calculador
4 Trabalho futuro
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
11. Representa¸˜o abstracta do c´lculo
ca a
f ◦ swap ◦ swap = f
Regras dispon´
ıveis
=
swap = snd fst
3 h) ◦ f = g ◦ f h◦f
(g
◦r f fst ◦ (f g ) = f , snd ◦ (f g) = g
fst snd = id
Ø quot;
u f ◦ id = f = id ◦ f
swap swap
f
Os nodos s˜o operadores e as folhas fun¸˜es
a co
Operador (◦) ´ “flat”, i.e., n-´rio
e a
fun¸˜o f ´ vari´vel
ca e a
fun¸˜o swap ´ constante (sabe-se a sua defini¸˜o)
ca e ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
12. Passo 1/9
f ◦ swap ◦ swap = f
Regras aplic´veis:
a
w7 =
w7 w7 swap = snd fst
w7
w7 w7 3
s{ h) ◦ f = g ◦ f h◦f
(g
◦ f fst ◦ (f g ) = f , snd ◦ (f g) = g
O O
O O fst snd = id
quot; f ◦ id = f = id ◦ f
u swap swap
f
Def-swap
Instˆncia¸˜o da regra
a ca
=
/0/1/[]
quot;
{
swap swap !
x
snd fst
!
x
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
13. ´
Arvore resultante
=
3
◦r f
Ù quot;
u swap
f
quot;
y
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
14. Passo 2/9
f ◦ (snd fst) ◦ swap = f
w7 = w7 w7 Regras aplic´veis:
a
w7 w7
w7 w7 3 swap = snd fst
s{
◦?
f h) ◦ f = g ◦ f h◦f
(g
?
?1 fst ◦ (f g ) = f , snd ◦ (f g) = g
u fst snd = id
swap
f f ◦ id = f = id ◦ f
quot;
y
snd fst
Fus˜o-×
a
Instˆncia¸˜o da regra
a ca
=
/0/[1, 2] 1
◦o %
(snd fst) ◦
◦p
◦
v ◦t $ '
swap ◦
snd v
f swap fst swap
ht
gt
gt h f f
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
15. ´
Arvore resultante
=
quot;
◦u f
#
u
f
quot;
◦r ◦
quot; quot;
y w
swap swap
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
16. Passo 3/9
f ◦ (snd ◦ swap fst ◦ swap) = f
Regras aplic´veis:
a
y9 = quot;
u} y9 swap = snd fst
◦ $d f
$d 2@ h) ◦ f = g ◦ f h◦f
(g
r
u5
f fst ◦ (f g ) = f , snd ◦ (f g) = g
u5 u5 u5
qy u5
fst snd = id
◦ $d ◦ f ◦ id = f = id ◦ f
2@ $
v
fst u
swap swap
snd
Def-swap
Instˆncia¸˜o da regra
a ca
=
/0/1/0/1/[]
quot;
{
swap swap !
x
snd fst
!
x
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
17. ´
Arvore resultante
=
quot;
◦u f
#
u
f
quot;
◦r ◦
# quot;
y w swap
snd fst
quot;
y
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
18. Passo 4/9
f ◦ (snd ◦ (snd fst ◦ swap) = f
fst)
=
{; Regras aplic´veis:
a
quot;
w
◦ !a f swap = snd fst
)7 h) ◦ f = g ◦ f h◦f
(g
fr u5 fst ◦ (f g) = f ,
u5 u5
qy u5
snd ◦ (f g) = g
◦ ◦
$ fst snd = id
$
v
t f ◦ id = f = id ◦ f
swap
snd fst
quot;
v
snd fst
Cancelamento-×
=
! Instˆncia¸˜o da
a ca
/0/1/0/[] q g
◦ regra
!
w
snd ◦ (snd fst)
snd fst
fs g
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
19. ´
Arvore resultante
=
quot;
◦u f
#
u
f
quot;
w
◦
fst
quot;
w swap
fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
20. Passo 5/9
f ◦ (fst fst ◦ swap) = f
= Regras aplic´veis:
a
{; quot;
w
◦ !a swap = snd fst
f
)7 h) ◦ f = g ◦ f h◦f
(g
ft fst ◦ (f g ) = f , snd ◦ (f g) = g
_ _ '5 fst snd = id
u ◦ #c
fst f ◦ id = f = id ◦ f
19
v swap
fst
Def-swap
Instˆncia¸˜o da regra
a ca
=
/0/1/1/1/[]
quot;
{
swap swap !
x
snd fst
!
x
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
21. ´
Arvore resultante
=
quot;
◦u f
#
u
f
quot;
w
◦
fst
#
w
fst
quot;
y
snd fst
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
22. Passo 6/9
f ◦ (fst fst ◦ (snd fst)) = f
=
{; % Regras aplic´veis:
a
w
◦ !a f swap = snd fst
)7 h) ◦ f = g ◦ f h◦f
(g
ft
quot;b fst ◦ (f g) = f ,
08
u snd ◦ (f g) = g
◦
fst fst snd = id
#
fst t
f ◦ id = f = id ◦ f
quot;
v
snd fst
Cancelamento-×
=
! Instˆncia¸˜o da
a ca
/0/1/1/[] ◦r f regra
fst ◦ (snd v
fst)
fst snd
fs g
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
23. ´
Arvore resultante
=
quot;
◦u f
#
u
f
quot;
w
fst snd
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
24. Passo 7/9
f ◦ (fst snd) = f
Regras aplic´veis:
a
=
? ? ? ? swap = snd fst
!
{Ó h) ◦ f = g ◦ f h◦f
(g
◦ f
_ _ fst ◦ (f g ) = f , snd ◦ (f g) = g
'5
fu
fst snd = id
f ◦ id = f = id ◦ f
!
w
fst snd
Reflex˜o-×
a
Instˆncia¸˜o da
a ca
=
/0/1/[] # regra
r id
fst snd
# id
fst u snd
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
25. ´
Arvore resultante
=
!
◦u f
!
fu id
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
26. Passo 8/9
f ◦ (fst snd) = f Regras aplic´veis:
a
= swap = snd fst
? ? ? ? h) ◦ f = g ◦ f h◦f
(g
!
{Ó fst ◦ (f g ) = f , snd ◦ (f g) = g
◦ f fst snd = id
! f ◦ id = f = id ◦ f
fu id
Natural-Id
= Instˆncia¸˜o da
a ca
/0/[] ! regra
◦u f
f ◦ id
f
!
fu id
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
27. ´
Arvore resultante
=
quot;
u
f f
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
28. Passo 9/9
Provado f ◦ swap ◦ swap = f
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
29. Sum´rio
a
1 Contextualiza¸˜o
ca
2 Representa¸˜o
ca
3 Calculador
4 Trabalho futuro
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
30. Aspecto geral
1 Input
2 Regras aplic´veis
a
3 Estado da prova
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
31. Input
Introdu¸˜o da prova
ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
32. Regras aplic´veis
a
Navega¸˜o
ca
PgUp / PgDn
enter
Instˆncia¸˜o da regra em 1
a ca
Carregar regras
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
33. Regras aplic´veis
a
Navega¸˜o
ca
PgUp / PgDn
enter
Instˆncia¸˜o da regra em 1
a ca
Carregar regras
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
34. Regras aplic´veis
a
Navega¸˜o
ca
PgUp / PgDn
enter
Instˆncia¸˜o da regra em 1
a ca
Carregar regras
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
35. Regras aplic´veis
a
Navega¸˜o
ca
PgUp / PgDn
enter
Instˆncia¸˜o da regra em 1
a ca
Carregar regras
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
36. Estado da prova
Navega¸˜o
ca
up / down - n´ ıveis
left / right - filhos
backspace - undo
Load / Save em XML
Exporta¸˜o PDF
ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
37. Estado da prova
Navega¸˜o
ca
up / down - n´ ıveis
left / right - filhos
backspace - undo
Load / Save em XML
Exporta¸˜o PDF
ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
38. Estado da prova
Navega¸˜o
ca
up / down - n´ ıveis
left / right - filhos
backspace - undo
Load / Save em XML
Exporta¸˜o PDF
ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
39. Estado da prova
Navega¸˜o
ca
up / down - n´ ıveis
left / right - filhos
backspace - undo
Load / Save em XML
Exporta¸˜o PDF
ca
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
40. Sum´rio
a
1 Contextualiza¸˜o
ca
2 Representa¸˜o
ca
3 Calculador
4 Trabalho futuro
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
41. C´lculo sobre fun¸oes recursivas
a c˜
O estado actual do programa j´ o permite, no entanto
a
temos que ter uma regra para cada tipo recurisvo
Ficheiro de regras
cataList g = g ◦ (id + id × cataList g ) ◦ outList
anaList h = inList ◦ (id + id × cataList g ) ◦ h
cataLTree g = g ◦ (id + id × cataLTree g × cataLTree g ) ◦ outLTree
anaBTree h = inBTree ◦ (id + id × cataBTree g × cataBTree g ) ◦ h
cataRoseTree g = g ◦ (id × map(cataRoseTree g )) ◦ outRoseTree
Solu¸˜o: inferˆncia de tipos para ter apenas a defini¸˜o geral de
ca e ca
cata:
Ficheiro de regras com inferˆncia de tipos
e
cataT g FT outT = g ◦ FT ◦ outT
anaT h FT inT = inT ◦ FT ◦ h
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
42. C´lculo sobre fun¸oes recursivas
a c˜
O estado actual do programa j´ o permite, no entanto
a
temos que ter uma regra para cada tipo recurisvo
Ficheiro de regras
cataList g = g ◦ (id + id × cataList g ) ◦ outList
anaList h = inList ◦ (id + id × cataList g ) ◦ h
cataLTree g = g ◦ (id + id × cataLTree g × cataLTree g ) ◦ outLTree
anaBTree h = inBTree ◦ (id + id × cataBTree g × cataBTree g ) ◦ h
cataRoseTree g = g ◦ (id × map(cataRoseTree g )) ◦ outRoseTree
Solu¸˜o: inferˆncia de tipos para ter apenas a defini¸˜o geral de
ca e ca
cata:
Ficheiro de regras com inferˆncia de tipos
e
cataT g FT outT = g ◦ FT ◦ outT
anaT h FT inT = inT ◦ FT ◦ h
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
43. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
44. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
45. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
ht7 = ftt2 ◦ gtt34 ◦ roundtt56
t8
1
⇔ {Regra do ◦}
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
46. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
ht7 = (ftt2 ◦ gtt34 ◦ roundtt56 )t2
t8
t5
1
⇔ {t2 ∼ t8 , t5 ∼ t7 }
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
47. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
ht5 = (ftt2 ◦ gtt34 ◦ roundtt56 )t2
t2
t5
1
⇔ {t1 ∼ t4 , t3 ∼ t6 }
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
48. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
ht5 = (ftt2 ◦ gtt31 ◦ roundtt53 )t2
t2
t5
1
⇔ {t5 ∼ R, t3 ∼ N}
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
49. Inferˆncia de tipos
e
Algoritmo Damas-Milner
round : R → N ⇔ roundR
N
Inferˆncia de tipos para h = f ◦ g ◦ round
e
hR = (ftt2 ◦ gN1 ◦ roundR )t2
t2 t N
1 R
Conclus˜o:
a
h : R → t2
f : t1 → t2
g : N → t1
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree
50. Formul´rio para inferˆncia de tipos no c´lculo Pointfree
a e a
ftt2 gtt32
ftt2 gtt34 1
1
+
g )t2 +t3
(f
(f + g )t2 +t4 t1
t1 +t2
ftt2 gtt12
ftt2 gtt34 1
1
=
×
TRUE
(f × g )t2 ×t4
t1 ×t2
n
ftt2 gtt13 zttn+1
ftt2 gtt31 n
...
1
1
◦
g )t2 ×t3
(f z)t2
t1 (f ◦ g ◦ . . . ◦ tn+1
. . . quest˜es?
o
FIM
(Jo˜o
a Ulisses) (Moura Costa) Calculador Pointfree