Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
lambda calculus
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stijn en pjotr
lambda calculus
Commits
48a4ed85
Commit
48a4ed85
authored
2 years ago
by
2269767
Browse files
Options
Downloads
Patches
Plain Diff
fromstring.py weg, fromstring met Var+Abst in main
parent
38260ba2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+23
-11
23 additions, 11 deletions
main.py
with
23 additions
and
11 deletions
main.py
+
23
−
11
View file @
48a4ed85
class
LambdaTerm
:
"""
Abstract Base Class for lambda terms.
"""
def
fromstring
(
self
):
"""
Construct a lambda term from a string.
"""
raise
NotImplementedError
if
"
.
"
in
self
:
return
Abstraction
.
fromstring
(
self
)
else
:
return
Variable
(
self
)
def
substitute
(
self
,
rules
):
"""
Substitute values for keys where they occur.
"""
...
...
@@ -32,6 +33,9 @@ class Variable(LambdaTerm):
newsymbol
=
rules
return
Variable
(
newsymbol
)
def
fromstring
(
self
):
return
eval
(
f
"
Variable(
'
{
self
}
'
)
"
)
class
Abstraction
(
LambdaTerm
):
"""
Represents a lambda term of the form (λx.M).
"""
...
...
@@ -44,14 +48,22 @@ class Abstraction(LambdaTerm):
return
f
"
Abstraction(
{
repr
(
self
.
var
)
}
,
{
repr
(
self
.
body
)
}
)
"
def
__str__
(
self
):
return
f
"
λ
{
self
.
var
}
.
{
self
.
body
}
"
return
f
"
λ
{
self
.
var
}
.
{
self
.
body
}
"
def
__call__
(
self
,
argument
):
raise
NotImplementedError
def
substitute
(
self
,
rules
):
newbody
=
Variable
(
str
(
self
.
body
).
replace
(
str
(
self
.
var
),
str
(
rules
)))
newbody
=
LambdaTerm
.
fromstring
(
str
(
self
.
body
).
replace
(
str
(
self
.
var
),
str
(
rules
)))
return
newbody
def
fromstring
(
self
):
lijst
=
self
.
split
(
"
.
"
)
if
len
(
lijst
[
0
])
>
2
:
return
Abstraction
(
Variable
.
fromstring
(
lijst
[
0
][
1
]),
Abstraction
.
fromstring
(
self
[
2
:]))
else
:
newvar
=
Variable
.
fromstring
(
lijst
[
0
][
1
])
newbody
=
Variable
.
fromstring
(
lijst
[
1
])
return
Abstraction
(
newvar
,
newbody
)
class
Application
(
LambdaTerm
):
"""
Represents a lambda term of the form (M N).
"""
...
...
@@ -74,10 +86,10 @@ class Application(LambdaTerm):
ans
=
self
.
function
.
substitute
(
self
.
arg
)
return
ans
x
=
Variable
(
'
x
'
)
a
=
Variable
(
'
a
'
)
id
=
Abstraction
(
x
,
Variable
(
'
x x
'
))
id_x
=
Application
(
id
,
Abstraction
(
a
,
a
))
red
=
id_x
.
reduce
()
def
fromstring
(
self
):
raise
NotImplementedError
print
(
id_x
,
"
-->
"
,
red
,
"
-->
"
,
type
(
red
))
\ No newline at end of file
print
(
str
(
LambdaTerm
.
fromstring
(
r
"
\a b. a
"
)))
print
(
type
(
LambdaTerm
.
fromstring
(
r
"
\a b. a
"
)))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment