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: class LambdaTerm:
"""Abstract Base Class for lambda terms.""" """Abstract Base Class for lambda terms."""
def fromstring(self): def fromstring(self):
"""Construct a lambda term from a string.""" """Construct a lambda term from a string."""
raise NotImplementedError if "." in self:
return Abstraction.fromstring(self)
else:
return Variable(self)
def substitute(self, rules): def substitute(self, rules):
"""Substitute values for keys where they occur.""" """Substitute values for keys where they occur."""
...@@ -32,6 +33,9 @@ class Variable(LambdaTerm): ...@@ -32,6 +33,9 @@ class Variable(LambdaTerm):
newsymbol = rules newsymbol = rules
return Variable(newsymbol) return Variable(newsymbol)
def fromstring(self):
return eval(f"Variable('{self}')")
class Abstraction(LambdaTerm): class Abstraction(LambdaTerm):
"""Represents a lambda term of the form (λx.M).""" """Represents a lambda term of the form (λx.M)."""
...@@ -44,14 +48,22 @@ class Abstraction(LambdaTerm): ...@@ -44,14 +48,22 @@ class Abstraction(LambdaTerm):
return f"Abstraction({repr(self.var)}, {repr(self.body)})" return f"Abstraction({repr(self.var)}, {repr(self.body)})"
def __str__(self): def __str__(self):
return f"λ{self.var}.{self.body}" return f"λ{self.var}. {self.body}"
def __call__(self, argument): raise NotImplementedError def __call__(self, argument): raise NotImplementedError
def substitute(self, rules): 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 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): class Application(LambdaTerm):
"""Represents a lambda term of the form (M N).""" """Represents a lambda term of the form (M N)."""
...@@ -74,10 +86,10 @@ class Application(LambdaTerm): ...@@ -74,10 +86,10 @@ class Application(LambdaTerm):
ans = self.function.substitute(self.arg) ans = self.function.substitute(self.arg)
return ans return ans
x = Variable('x') def fromstring(self):
a = Variable('a') raise NotImplementedError
id = Abstraction(x, Variable('x x'))
id_x = Application(id, Abstraction(a, a))
red = id_x.reduce()
print(id_x, "-->", red, "-->", type(red)) print(str(LambdaTerm.fromstring(r"\a b. a")))
\ No newline at end of file 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