Skip to content
Snippets Groups Projects
Commit 48a4ed85 authored by 2269767's avatar 2269767
Browse files

fromstring.py weg, fromstring met Var+Abst in main

parent 38260ba2
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment