Skip to content

Commit a577cea

Browse files
committed
Add function for finding all beta redexes in a lambda term
1 parent 4149682 commit a577cea

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

lambda-parser.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ executable lambda-parser
2929
-- Modules included in this executable, other than Main.
3030
other-modules:
3131
Examples.Terms
32+
Interaction
3233
Parser
3334
Transformer
35+
Generator
3436
Substitution.Named
3537
Substitution.Nameless
36-
Generator
37-
Stack
38-
Interaction
38+
Libs.Stack
39+
Libs.BetaRedexes
3940

4041
-- LANGUAGE extensions used by modules in this package.
4142
-- other-extensions:

src/Examples/Terms.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ simpleApplication :: String
77
simpleApplication = "xy"
88

99
complexApplication :: String
10-
complexApplication = "(xy)z"
10+
complexApplication = "xyz"
1111

1212
simpleAbstraction :: String
1313
simpleAbstraction = "λx.x"
@@ -19,14 +19,23 @@ brackets :: String
1919
brackets = "(xy)"
2020

2121
composed :: String
22-
composed = "(λx.(ab)x)(yz)"
22+
composed = "(λx.abx)(yz)"
2323

2424
currySubstitution1 :: String
2525
currySubstitution1 = "λy.yλa.ax"
2626

2727
currySubstitution2 :: String
2828
currySubstitution2 = "λy.λa.yax"
2929

30+
betaRedexes :: String
31+
betaRedexes = "(λx.x)((λy.y)z)u"
32+
33+
singleBetaRedex :: String
34+
singleBetaRedex = "λz.(λx.λy.x)(λu.u)(λu.uzu)z"
35+
36+
complexBetaRedexes :: String
37+
complexBetaRedexes = "(λx.xz)((λy.zy)z)"
38+
3039
allExamples :: [String]
3140
allExamples = [
3241
variable,
@@ -37,4 +46,7 @@ allExamples = [
3746
brackets,
3847
composed,
3948
currySubstitution1,
40-
currySubstitution2 ]
49+
currySubstitution2,
50+
betaRedexes,
51+
singleBetaRedex,
52+
complexBetaRedexes ]

src/Interaction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Interaction where
33
import Prelude hiding (lookup)
44
import Generator (generateTerm, generateNamelessTerm)
55
import Parser (parseTerm)
6-
import Stack (emptyStack)
6+
import Libs.Stack (emptyStack)
77
import Substitution.Named (substitute)
88
import Transformer (toNameless, toNamed)
99
import Data.Map (empty, lookup)

src/Libs/BetaRedexes.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Libs.BetaRedexes where
2+
import Parser (Term (Variable, Application, Abstraction))
3+
import Generator (generateTerm)
4+
5+
extractBetaRedexes :: Term -> [Term]
6+
extractBetaRedexes (Variable _) = []
7+
extractBetaRedexes term@(Application lhs@(Abstraction _ body) rhs) =
8+
term : extractBetaRedexes body ++ extractBetaRedexes rhs
9+
extractBetaRedexes (Application lhs rhs) =
10+
extractBetaRedexes lhs ++ extractBetaRedexes rhs
11+
extractBetaRedexes (Abstraction _ body) = extractBetaRedexes body
12+
13+
displayBetaRedexes :: Term -> IO ()
14+
displayBetaRedexes term =
15+
mapM_ (putStrLn . generateTerm) $ extractBetaRedexes term

src/Stack.hs renamed to src/Libs/Stack.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Stack where
1+
module Libs.Stack where
22

33
type Stack t = [t]
44

src/Main.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
module Main where
22

3-
import Data.Map (empty, lookup)
4-
import Generator (generateNamelessTerm, generateTerm)
5-
import Parser (Term (Variable), parseTerm)
6-
import Substitution.Named (substitute)
7-
import Substitution.Nameless (substituteNameless)
83
import System.IO (BufferMode (BlockBuffering), hSetBuffering, stdout)
9-
import Transformer (toNamed, toNameless)
10-
import Prelude hiding (lookup)
11-
import Stack (emptyStack)
124
import Interaction (getSubstitutionType, getOutputTermType, doSubstituteNameless, TermType (Named), doSubstituteNamed)
135

146
main :: IO ()

src/Parser.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Parser (parseTerm, Term(Variable, Application, Abstraction)) where
2+
23
import Data.Char (isAlpha)
3-
import Debug.Trace (trace)
4-
import Stack (Stack, peek, pop, emptyStack, push)
4+
import Libs.Stack (Stack, peek, pop, emptyStack, push)
55

66
data Term =
77
Variable Char |

0 commit comments

Comments
 (0)