feat: new engine format
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
// Package engine defines a general process of reducing a lambda calculus
|
||||
// expression.
|
||||
package engine
|
||||
|
||||
type Engine[T any] interface {
|
||||
Load() Process[T]
|
||||
}
|
||||
|
||||
// A Process handles the reduction of a
|
||||
type Process[T any] interface {
|
||||
Get() (T, error)
|
||||
Set(T) error
|
||||
Step(int) bool
|
||||
}
|
||||
|
||||
// An Engine is an object that handles
|
||||
type Engine[T any] = func(T) (Process[T], error)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Package normalorder contains an engine that reduces a 'lambda.Expression'
|
||||
// in the normal order.
|
||||
package normalorder
|
||||
|
||||
import (
|
||||
@@ -5,20 +7,20 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Process struct {
|
||||
type process struct {
|
||||
expr lambda.Expression
|
||||
}
|
||||
|
||||
func (e Process) Get() (lambda.Expression, error) {
|
||||
func (e process) Get() (lambda.Expression, error) {
|
||||
return e.expr, nil
|
||||
}
|
||||
|
||||
func (e *Process) Set(l lambda.Expression) error {
|
||||
func (e *process) Set(l lambda.Expression) error {
|
||||
e.expr = l
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Process) Step(i int) bool {
|
||||
func (e *process) Step(i int) bool {
|
||||
for range i {
|
||||
next, reduced := ReduceOnce(e.expr)
|
||||
if !reduced {
|
||||
@@ -31,12 +33,10 @@ func (e *Process) Step(i int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
// NewProcess creates a new redution process.
|
||||
func NewProcess(expression lambda.Expression) (engine.Process[lambda.Expression], error) {
|
||||
return &process{expr: expression}, nil
|
||||
}
|
||||
|
||||
func (e Engine) Load() engine.Process[lambda.Expression] {
|
||||
return &Process{}
|
||||
}
|
||||
|
||||
var _ engine.Process[lambda.Expression] = (*Process)(nil)
|
||||
var _ engine.Engine[lambda.Expression] = (*Engine)(nil)
|
||||
var _ engine.Process[lambda.Expression] = (*process)(nil)
|
||||
var _ engine.Engine[lambda.Expression] = NewProcess
|
||||
|
||||
@@ -2,6 +2,11 @@ package normalorder
|
||||
|
||||
import "git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
|
||||
// ReduceOnce attempts to apply a single reduction to a lambda expression.
|
||||
// It returns (1) the final expression (reduced, or not), and (2) whether or not
|
||||
// a reduction was applied.
|
||||
//
|
||||
// If a reduction is not applied, it returns the original expression.
|
||||
func ReduceOnce(e lambda.Expression) (lambda.Expression, bool) {
|
||||
switch e := e.(type) {
|
||||
case lambda.Abstraction:
|
||||
Reference in New Issue
Block a user