feat: reducer works
This commit is contained in:
@@ -30,9 +30,9 @@ func main() {
|
|||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression))
|
logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression))
|
||||||
|
|
||||||
lambda.Normalize(&expression)
|
for lambda.ReduceOnce(&expression) {
|
||||||
cli.HandleError(err)
|
logger.Info("Reduction.", "tree", lambda.Stringify(expression))
|
||||||
logger.Info("Evaluated expression.", "tree", expression)
|
}
|
||||||
|
|
||||||
fmt.Println(lambda.Stringify(expression))
|
fmt.Println(lambda.Stringify(expression))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package lambda
|
|||||||
|
|
||||||
type Expression interface {
|
type Expression interface {
|
||||||
Accept(Visitor)
|
Accept(Visitor)
|
||||||
|
Copy() Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
@@ -11,14 +12,18 @@ type Abstraction struct {
|
|||||||
Body Expression
|
Body Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
func (this *Abstraction) Copy() Expression {
|
||||||
return &Abstraction{Parameter: parameter, Body: body}
|
return NewAbstraction(this.Parameter, this.Body.Copy())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Abstraction) Accept(v Visitor) {
|
func (this *Abstraction) Accept(v Visitor) {
|
||||||
v.VisitAbstraction(this)
|
v.VisitAbstraction(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
||||||
|
return &Abstraction{Parameter: parameter, Body: body}
|
||||||
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
@@ -26,28 +31,36 @@ type Application struct {
|
|||||||
Argument Expression
|
Argument Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApplication(function Expression, argument Expression) *Application {
|
func (this *Application) Copy() Expression {
|
||||||
return &Application{Abstraction: function, Argument: argument}
|
return NewApplication(this.Abstraction.Copy(), this.Argument.Copy())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Application) Accept(v Visitor) {
|
func (this *Application) Accept(v Visitor) {
|
||||||
v.VisitApplication(this)
|
v.VisitApplication(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewApplication(function Expression, argument Expression) *Application {
|
||||||
|
return &Application{Abstraction: function, Argument: argument}
|
||||||
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
type Variable struct {
|
type Variable struct {
|
||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVariable(name string) *Variable {
|
func (this *Variable) Copy() Expression {
|
||||||
return &Variable{Value: name}
|
return NewVariable(this.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Variable) Accept(v Visitor) {
|
func (this *Variable) Accept(v Visitor) {
|
||||||
v.VisitVariable(this)
|
v.VisitVariable(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewVariable(name string) *Variable {
|
||||||
|
return &Variable{Value: name}
|
||||||
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
type Visitor interface {
|
type Visitor interface {
|
||||||
|
|||||||
@@ -21,8 +21,3 @@ func ReduceOnce(e *Expression) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Normalize(e *Expression) {
|
|
||||||
for ReduceOnce(e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ func Substitute(e *Expression, target string, replacement Expression) {
|
|||||||
switch typed := (*e).(type) {
|
switch typed := (*e).(type) {
|
||||||
case *Variable:
|
case *Variable:
|
||||||
if typed.Value == target {
|
if typed.Value == target {
|
||||||
*e = replacement
|
*e = replacement.Copy()
|
||||||
}
|
}
|
||||||
case *Abstraction:
|
case *Abstraction:
|
||||||
if typed.Parameter == target {
|
if typed.Parameter == target {
|
||||||
|
|||||||
Reference in New Issue
Block a user