-
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
9 changed files
with
513 additions
and
20 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,107 @@ | ||
module LFCFT where | ||
|
||
-- A linguagem LFCF suporta tanto | ||
-- expressoes identificadas (LET) quanto | ||
-- identificadores e funcoes de alta ordem | ||
-- (com o mecanismo de expressoes lambda). | ||
|
||
type Id = String | ||
|
||
data Expressao = Valor Int | ||
| Soma Expressao Expressao | ||
| Subtracao Expressao Expressao | ||
| Multiplicacao Expressao Expressao | ||
| Divisao Expressao Expressao | ||
| Let Id Expressao Expressao | ||
| Ref Id | ||
| Lambda Id Expressao | ||
| App Expressao Expressao | ||
deriving(Show, Eq) | ||
|
||
-- O interpretador da linguagem LFCF | ||
-- precisa ser melhor discutido, uma vez que | ||
-- o tipo de retorno nao pode ser simplesmente | ||
-- um inteiro. O que seria a avaliacao de uma | ||
-- expressao lambda (\x -> x + 1)? | ||
|
||
toInt :: Expressao -> Int | ||
toInt (Valor n) = n | ||
|
||
avaliar :: Expressao -> Expressao | ||
avaliar (Valor n) = (Valor n) | ||
|
||
avaliar (Soma e d) = Valor (ve + vd) | ||
where | ||
(Valor ve) = avaliar e | ||
(Valor vd) = avaliar d | ||
|
||
avaliar (Subtracao e d) = Valor (ve - vd) | ||
where | ||
(Valor ve) = avaliar e | ||
(Valor vd) = avaliar d | ||
|
||
avaliar (Multiplicacao e d) = Valor (ve * vd) | ||
where | ||
(Valor ve) = avaliar e | ||
(Valor vd) = avaliar d | ||
|
||
avaliar (Divisao e d) = Valor (ve `div` vd) | ||
where | ||
(Valor ve) = avaliar e | ||
(Valor vd) = avaliar d | ||
|
||
avaliar (Lambda argFormal corpo) = (Lambda argFormal corpo) | ||
|
||
avaliar (App exp1 exp2) = | ||
let expLambda = avaliar exp1 | ||
in case expLambda of | ||
(Lambda argFormal corpo) -> avaliar (substituicao argFormal exp2 corpo) | ||
otherwise -> error "aplicando uma expressao que nao eh lambda" | ||
|
||
avaliar (Let subId expNomeada corpoExp) = avaliar (App exp1 exp2) | ||
-- | corpoExp /= Let boundId namedExp bodyExp = substituicao subId expNomeada corpoExp | ||
-- | otherwise = avaliar (App exp1 exp2) | ||
where | ||
Let boundId namedExp bodyExp = corpoExp | ||
exp1 = Lambda subId corpoExp | ||
exp2 = expNomeada | ||
|
||
avaliar (Ref var) = error "avaliando uma variavel livre." | ||
|
||
substituicao :: Id -> Expressao -> Expressao -> Expressao | ||
substituicao subId val (Valor n) = Valor n | ||
substituicao subId val (Soma e d) | ||
| d == Let boundId namedExp bodyExp = Soma (substituicao subId val e)(avaliar d) | ||
| otherwise = Soma (substituicao subId val e)(substituicao subId val d) | ||
where | ||
Let boundId namedExp bodyExp = d | ||
|
||
substituicao subId val (Subtracao e d) | ||
| d == Let boundId namedExp bodyExp = Subtracao (substituicao subId val e)(avaliar d) | ||
| otherwise = Subtracao (substituicao subId val e)(substituicao subId val d) | ||
where | ||
Let boundId namedExp bodyExp = d | ||
|
||
substituicao subId val (Multiplicacao e d) | ||
| d == Let boundId namedExp bodyExp = Multiplicacao (substituicao subId val e)(avaliar d) | ||
| otherwise = Multiplicacao (substituicao subId val e)(substituicao subId val d) | ||
where | ||
Let boundId namedExp bodyExp = d | ||
|
||
substituicao subId val (Divisao e d) | ||
| d == Let boundId namedExp bodyExp = Divisao (substituicao subId val e)(avaliar d) | ||
| otherwise = Divisao (substituicao subId val e)(substituicao subId val d) | ||
where | ||
Let boundId namedExp bodyExp = d | ||
|
||
substituicao subId val (Let boundId namedExp bodyExp) = | ||
substRef subId val (Let boundId namedExp bodyExp) | ||
|
||
substituicao subId val (Ref var) | ||
| subId == var = val | ||
| otherwise = (Ref var) | ||
|
||
substRef :: Id -> Expressao -> Expressao -> Expressao | ||
substRef subId val (Let boundId namedExp bodyExp) | ||
| namedExp == (Ref subId) = Let boundId val bodyExp | ||
| otherwise = Let boundId namedExp (substituicao subId val bodyExp) |
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,107 @@ | ||
module LFCFTTestes where | ||
|
||
import LFCFT | ||
|
||
import Test.HUnit | ||
|
||
v5 = Valor 5 | ||
v3 = Valor 3 | ||
|
||
s1 = Soma v5 v3 | ||
|
||
s2 = Soma s1 v3 | ||
|
||
-- let x = 5 in x + x | ||
let01 = Let "x" (Valor 5) (Soma (Ref "x") (Ref "x")) | ||
-- (\x -> x+x)5 | ||
-- 10 | ||
|
||
-- let x = 5 in let y = 10 in x + y | ||
let02 = Let "x" (Valor 5) | ||
(Let "y" (Valor 10) (Soma (Ref "x") (Ref "y"))) | ||
|
||
-- let x = 5 in x + let x = 10 in x + 3 | ||
let03 = Let "x" (Valor 5) | ||
(Soma (Ref "x") (Let "x" (Valor 10) (Soma (Ref "x") (Valor 3)))) | ||
|
||
--avaliar ( Let "x" (Valor 5) (Soma (Ref "x") (Let "x" (Valor 10) (Soma (Ref "x") (Valor 3)))) ) | ||
--avaliar ( App (Lambda "x" (Soma (Ref "x") (Let "x" (Valor 10) (Soma (Ref "x") (Valor 3))))) (Valor 5)) | ||
----e---- ---------------------d---------------------- | ||
--avaliar ( substituicao "x" (Valor 5) (Soma (Ref "x") (Let "x" (Valor 10) (Soma (Ref "x") (Valor 3) ) ) ) ) | ||
--avaliar ( Soma (substituicao "x" (Valor 5) (Ref "x") (substituicao "x" (Valor 5) (Let "x" (Valor 10) (Soma (Ref "x") (Valor 3)))))) | ||
--avaliar ( Soma (Valor 5) ) | ||
|
||
-- let x = 5 in let y = x in y | ||
let04 = Let "x" (Valor 5) | ||
(Let "y" (Ref "x") (Ref "y")) | ||
-- let x=5 | ||
-- in let y=x | ||
-- in y | ||
|
||
-- let x = 5 in let x = x in x | ||
let05 = Let "x" (Valor 5) | ||
(Let "x" (Ref "x") (Ref "x")) | ||
|
||
teste1 = TestCase (assertEqual "avaliar 5" 5 (toInt v5)) | ||
|
||
teste2 = TestCase (assertEqual "avaliar 5 + 3" 8 (toInt (avaliar s1))) | ||
|
||
teste3 = TestCase (assertEqual "avaliar (5 + 3) + 3" 11 (toInt (avaliar s2))) | ||
|
||
teste4 = TestCase (assertEqual "avaliar let x = 5 in x + x" 10 (toInt(avaliar let01))) | ||
|
||
teste5 = TestCase (assertEqual "avaliar let x = 5 in let y = 10 in x + y" 15 (toInt(avaliar let02))) | ||
|
||
teste6 = TestCase (assertEqual "avaliar let x = 5 in x + let x = 10 in x + 3" 18 (toInt(avaliar let03))) | ||
|
||
teste7 = TestCase (assertEqual "avaliar let x = 5 in let y = x in y" 5 (toInt(avaliar let04))) | ||
|
||
teste8 = TestCase (assertEqual "avaliar let x = 5 in let x = x in x" 5 (toInt(avaliar let05))) | ||
|
||
todosOsTestes = TestList [ teste1 | ||
, teste2 | ||
, teste3 | ||
, teste4 | ||
, teste5 | ||
, teste6 | ||
, teste7 | ||
, teste8 | ||
] | ||
|
||
executarTestes = runTestTT todosOsTestes | ||
|
||
|
||
-- argFor exp2 ---------corpo--------- | ||
-- subId expNom -------corpoExp------- | ||
--avaliar (Let "x" (Valor 5)(Soma (Ref "x")(Valor 1))) =======> (\x -> x+x)5 | ||
--avaliar (App ("x" (Soma(Ref "x")(Valor 1))) (Valor 5)) | ||
-- -------------exp1------------ ---exp2--- | ||
--avaliar (Let subId expNomeada corpoExp) = avaliar(App (Lambda subId (corpoExp)) expNomeada) | ||
|
||
--avaliar (App (Lambda "x" (Soma(Ref"x")(Valor 1)))()) | ||
--let expLambda = avaliar exp1 | ||
--in case expLambda of | ||
-- (Lambda arg corpo) -> | ||
|
||
--let01 = Let "x" (Valor 5) (Soma (Ref "x") (Ref "x")) | ||
-- (\x -> x+x)5 | ||
-- 10 | ||
--avaliar (Let "x"(Valor 5)(Soma(Ref "x")(Ref "x"))) | ||
-- | ||
|
||
--definindo expressoes lambda: | ||
--incre = \x -> x+1 | ||
--incre x = x+1 | ||
-- | ||
--somar = \x -> \y -> x+y | ||
--somar x y = x+y | ||
-- | ||
--(\x -> x+4)4 | ||
--8 | ||
-- | ||
-- Let "x" (Valor 5) (Soma(Ref "x")(Ref "y")) | ||
-- (\x -> x+x)5 | ||
-- 10 | ||
-- | ||
--(\x -> 5+5)1 | ||
--10 |
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
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
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
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
Oops, something went wrong.