feat: repr, codec

This commit is contained in:
2026-01-30 16:24:17 -05:00
parent f2c8d9f7d2
commit 0ec52008bb
8 changed files with 51 additions and 23 deletions

12
pkg/codec/codec.go Normal file
View File

@@ -0,0 +1,12 @@
package codec
import "git.maximhutz.com/max/lambda/pkg/repr"
type String string
func (s String) Id() string { return "string" }
type Codec[T, U repr.Repr] interface {
Encode(T) (U, error)
Decode(U) (T, error)
}

View File

@@ -1,15 +0,0 @@
// Package expr provides the abstract Expression interface for all evaluatable
// expression types in the lambda runtime.
package expr
import (
"fmt"
)
// Expression is the base interface for all evaluatable expression types.
// Different evaluation modes (lambda calculus, SKI combinators, typed lambda
// calculus, etc.) implement this interface with their own concrete types.
type Expression interface {
// The expression should have a human-readable representation.
fmt.Stringer
}

View File

@@ -1,14 +1,17 @@
package lambda
import (
"git.maximhutz.com/max/lambda/pkg/expr"
"fmt"
"git.maximhutz.com/max/lambda/pkg/repr"
"git.maximhutz.com/max/lambda/pkg/set"
)
// Expression is the interface for all lambda calculus expression types.
// It embeds the general expr.Expression interface for cross-mode compatibility.
type Expression interface {
expr.Expression
repr.Repr
fmt.Stringer
// Substitute replaces all free occurrences of the target variable with the
// replacement expression. Alpha-renaming is performed automatically to
@@ -37,6 +40,8 @@ type Abstraction struct {
var _ Expression = Abstraction{}
func (a Abstraction) Id() string { return "lambda" }
func (a Abstraction) Parameter() string {
return a.parameter
}
@@ -62,6 +67,8 @@ type Application struct {
var _ Expression = Application{}
func (a Application) Id() string { return "lambda" }
func (a Application) Abstraction() Expression {
return a.abstraction
}
@@ -86,6 +93,8 @@ type Variable struct {
var _ Expression = Variable{}
func (a Variable) Id() string { return "lambda" }
func (v Variable) Name() string {
return v.name
}

View File

@@ -2,8 +2,8 @@ package normalorder
import (
"git.maximhutz.com/max/lambda/pkg/emitter"
"git.maximhutz.com/max/lambda/pkg/expr"
"git.maximhutz.com/max/lambda/pkg/lambda"
"git.maximhutz.com/max/lambda/pkg/repr"
"git.maximhutz.com/max/lambda/pkg/runtime"
)
@@ -23,7 +23,7 @@ func NewRuntime(expression lambda.Expression) *Runtime {
}
// Expression returns the current expression state.
func (r *Runtime) Expression() expr.Expression {
func (r *Runtime) Expression() repr.Repr {
return r.expression
}

15
pkg/repr/repr.go Normal file
View File

@@ -0,0 +1,15 @@
// Package repr defines a general definition of a representation of lambda
// calculus.
package repr
import "fmt"
// Repr is a representation of lambda calculus.
type Repr interface {
fmt.Stringer
// Id returns to name of the objects underlying representation. If is
// assumed that if two Repr objects have the same Id(), they share the same
// representation.
Id() string
}

View File

@@ -4,7 +4,7 @@ package runtime
import (
"git.maximhutz.com/max/lambda/pkg/emitter"
"git.maximhutz.com/max/lambda/pkg/expr"
"git.maximhutz.com/max/lambda/pkg/repr"
)
// Runtime defines the interface for expression reduction strategies.
@@ -23,5 +23,5 @@ type Runtime interface {
Run()
// Copy the state of the runtime.
Expression() expr.Expression
Expression() repr.Repr
}