-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
Linguagens: | ||
|
||
LLE => Let + expressões + substituição | ||
|
||
LFLE01 => Funções + Let + substituição | ||
|
||
LFLE02 => Funções + Let + expressões + referência | ||
|
||
LFCF: Funções com valor | ||
Expressões = Valor Int | ||
|Soma Exp Exp | ||
|... | ||
|Let Id Exp Exp | ||
|\ Id Exp | ||
|Aplicacao Exp Exp | ||
|
||
\x -> x+1 | ||
|
||
avaliar :: Expressao -> Expressao | ||
. / \ | ||
. / \ | ||
. {Valor Int} {\expression} | ||
. | ||
. | ||
avaliar (Valor m) = Valor m | ||
avaliar (Soma e d) = Valor(ve + vd) | ||
where | ||
(Valor ve) = avaliar e | ||
(Valor vd) = avaliar d | ||
. | ||
. | ||
. | ||
avaliar(Let v e c) = avaliar(substituicao v e c) | ||
. | ||
. | ||
. | ||
avaliar(\ Id Exp) = \ Id Exp | ||
. | ||
. | ||
. | ||
avaliar(Aplicacao e1 e2) = | ||
Let Lambda = avaliar e1 | ||
in case Lambda of | ||
(\ v e) = avaliar (substituicao v e2 c) | ||
otherwise = ERROR | ||
| Let "F" (\ "x" (Soma (Ref "x") (Valor 1))) | ||
|avaliar (Aplicacao (Ref "F")(Valor 4)) | ||
|
||
-------------------------------------------------------------------------------- | ||
Def Soma x = x+y | ||
|
||
Let y=3 |(a)Escolpo Estático: Erro | ||
in Soma 4 |(b)Escolpo Dinâmiico: Valor 7 | ||
|
||
Let y=3 | | ||
in Let Soma = \x -> x+y |(a)Escolpo Estático: 7 | ||
in Soma 4 | | ||
|
||
Let y=3 | | ||
in Let F = \y -> x+y |(a)Escolpo Estático: 6 => (x é substituido por 3) | ||
in Let X=4 |(b)Escolpo Dinâmico: 7 => (x é substituido por 4) | ||
in F 3 | | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
Lazy Evaluation | ||
Baseado nas notas de aula do Prof. Dr. Graham Hutton | ||
|
||
(Ler cap. 7 do livro) | ||
-------------------------------------------------------------------------------- | ||
zeros = 0:zeros | ||
|
||
timeBonus :: [String] -> [(String, Int)] | ||
timeBonus c = zip c bs | ||
where bs = [20,12,8] ++ zeros | ||
|
||
tB2 :: [String] -> [(String, Int)] | ||
-------------------------------------------------------------------------------- | ||
sqr m = m*m | ||
|
||
sqr (3+4) | Innermost | Outermost | Lazy | | ||
sqr 7 | (3+4)*(3+4) | (3+4)*(3+4) | ||
7*7 | 7*(3+4) | 7*7 | ||
49 | 7*7 | 49 | ||
| 49 | | ||
|
||
Outermost é geralmente menos eficiente, porem permite que trabalhemos | ||
com listas infinitas, de forma em que operações possam terminar. | ||
|
||
From Innermost to Outermost | ||
Type Refs = [(Id, Int)] =====> [(Id, Exp)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module Testes where | ||
-- Em linguagens com suporte a escopo | ||
-- dinamico, esse exemplo deveria funcionar | ||
-- e a avaliacao da expressao "e" deveria | ||
-- retornar o valor 15. | ||
-- | ||
-- Em linguagens com escopo estatico, esse | ||
-- exemplo seria rejeitado | ||
|
||
f x = x + y | ||
------------------------------------------------------------------------------------------------------- | ||
Inc x = x+1 | ||
|
||
Let x=5 |Let x=5 | ||
in Inc 3 | in 3+1 | ||
| └┤Retornar 4 é mais natural | ||
└┤Retornar 6 é pouco natural | ||
|
||
F x = x+y | ||
|
||
Let y=4 | ||
in F 4 | ||
| └┤Valor 7 em scopo dinâmico | ||
└┤Erro escopo estático | ||
|
||
(subId val) | ||
substituicao x 5 (F 3) | ||
╗ | ||
(I) Recuperar a DecFuncao ║ | ||
=> Usando a nossa Funcao "Pesquisar" ║ | ||
(II) Se subId == arg da funcao (mesmo nome) ║ | ||
=> Não substitui no corpo da funcao ╠▬▬▬▬>Escopo Dinâmico | ||
=> Retorna a expressao de Aplicacao ║ | ||
else ║ | ||
substituicao arg f arg (subs subId val corpo) ║ | ||
╝ | ||
------------------------------------------------------------------------------------------------------------------------- | ||
Aula 11/04 | ||
------------------------------------------------------------------------------------------------------------------------- | ||
|
||
Let x=5 | |Let| | ||
in Let y=10 | / \ \ | ||
in Let z = 15 | |x| |5| \ | ||
in x+y+z | |Let| | ||
| / \ \ | ||
| |y| |10| \ | ||
| |Let| | ||
| / \ \ | ||
| |z| |15| \ | ||
| |+| | ||
| / \ | ||
| |5| |+| | ||
| / \ | ||
| |10| |15| | ||
------------------------------------------------------------------------------------------------------------------------- | ||
Aula 18/04 | ||
------------------------------------------------------------------------------------------------------------------------- | ||
|
||
avaliar Let x = 10 | ||
in (Let x = 5 in x + 8) + x | ||
|
||
avlaiar (Let x=5 in x+8) + x [] [(x,10)] | ||
|
||
=Lhs + Rhs [] [(x,10)] | ||
where | ||
I Lhs = avaliar (Ley x=5 in x+8) [] [(x,10)] = 13 | ||
II Rhs = avaliar x [] [(x,10)] = 10 |