feat: repr, codec
This commit is contained in:
@@ -48,7 +48,7 @@ linters:
|
|||||||
# More information: https://golangci-lint.run/usage/false-positives/#comments
|
# More information: https://golangci-lint.run/usage/false-positives/#comments
|
||||||
#
|
#
|
||||||
# Please uncomment the following line if your code is not using the godoc format
|
# Please uncomment the following line if your code is not using the godoc format
|
||||||
- comments
|
# - comments
|
||||||
|
|
||||||
# Common false positives
|
# Common false positives
|
||||||
# feel free to remove this if you don't have any false positives
|
# feel free to remove this if you don't have any false positives
|
||||||
@@ -126,6 +126,9 @@ linters:
|
|||||||
# Blank import should be only in a main or test package, or have a comment justifying it.
|
# Blank import should be only in a main or test package, or have a comment justifying it.
|
||||||
- name: blank-imports
|
- name: blank-imports
|
||||||
|
|
||||||
|
# Packages should have comments of the form "Package x ...".
|
||||||
|
- name: package-comments
|
||||||
|
|
||||||
# context.Context() should be the first parameter of a function when provided as argument.
|
# context.Context() should be the first parameter of a function when provided as argument.
|
||||||
- name: context-as-argument
|
- name: context-as-argument
|
||||||
arguments:
|
arguments:
|
||||||
|
|||||||
6
Makefile
6
Makefile
@@ -1,7 +1,7 @@
|
|||||||
BINARY_NAME=lambda
|
BINARY_NAME=lambda
|
||||||
TEST=simple
|
TEST=simple
|
||||||
|
|
||||||
.PHONY: help build run profile explain graph docs test bench clean
|
.PHONY: help build run profile explain graph docs test bench lint clean
|
||||||
.DEFAULT_GOAL := help
|
.DEFAULT_GOAL := help
|
||||||
.SILENT:
|
.SILENT:
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ help:
|
|||||||
echo " docs - Start local godoc server on port 6060"
|
echo " docs - Start local godoc server on port 6060"
|
||||||
echo " test - Run tests for all samples"
|
echo " test - Run tests for all samples"
|
||||||
echo " bench - Run benchmarks for all samples"
|
echo " bench - Run benchmarks for all samples"
|
||||||
|
echo " lint - Run golangci-lint on all packages"
|
||||||
echo " clean - Remove all build artifacts"
|
echo " clean - Remove all build artifacts"
|
||||||
|
|
||||||
build:
|
build:
|
||||||
@@ -45,6 +46,9 @@ test:
|
|||||||
bench:
|
bench:
|
||||||
go test -bench=. -benchtime=10x -cpu=4 ./cmd/lambda
|
go test -bench=. -benchtime=10x -cpu=4 ./cmd/lambda
|
||||||
|
|
||||||
|
lint:
|
||||||
|
go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest run ./...
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f ${BINARY_NAME}
|
rm -f ${BINARY_NAME}
|
||||||
rm -f program.out
|
rm -f program.out
|
||||||
|
|||||||
12
pkg/codec/codec.go
Normal file
12
pkg/codec/codec.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,17 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.maximhutz.com/max/lambda/pkg/expr"
|
"fmt"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/repr"
|
||||||
"git.maximhutz.com/max/lambda/pkg/set"
|
"git.maximhutz.com/max/lambda/pkg/set"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Expression is the interface for all lambda calculus expression types.
|
// Expression is the interface for all lambda calculus expression types.
|
||||||
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
||||||
type Expression interface {
|
type Expression interface {
|
||||||
expr.Expression
|
repr.Repr
|
||||||
|
fmt.Stringer
|
||||||
|
|
||||||
// Substitute replaces all free occurrences of the target variable with the
|
// Substitute replaces all free occurrences of the target variable with the
|
||||||
// replacement expression. Alpha-renaming is performed automatically to
|
// replacement expression. Alpha-renaming is performed automatically to
|
||||||
@@ -37,6 +40,8 @@ type Abstraction struct {
|
|||||||
|
|
||||||
var _ Expression = Abstraction{}
|
var _ Expression = Abstraction{}
|
||||||
|
|
||||||
|
func (a Abstraction) Id() string { return "lambda" }
|
||||||
|
|
||||||
func (a Abstraction) Parameter() string {
|
func (a Abstraction) Parameter() string {
|
||||||
return a.parameter
|
return a.parameter
|
||||||
}
|
}
|
||||||
@@ -62,6 +67,8 @@ type Application struct {
|
|||||||
|
|
||||||
var _ Expression = Application{}
|
var _ Expression = Application{}
|
||||||
|
|
||||||
|
func (a Application) Id() string { return "lambda" }
|
||||||
|
|
||||||
func (a Application) Abstraction() Expression {
|
func (a Application) Abstraction() Expression {
|
||||||
return a.abstraction
|
return a.abstraction
|
||||||
}
|
}
|
||||||
@@ -86,6 +93,8 @@ type Variable struct {
|
|||||||
|
|
||||||
var _ Expression = Variable{}
|
var _ Expression = Variable{}
|
||||||
|
|
||||||
|
func (a Variable) Id() string { return "lambda" }
|
||||||
|
|
||||||
func (v Variable) Name() string {
|
func (v Variable) Name() string {
|
||||||
return v.name
|
return v.name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package normalorder
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.maximhutz.com/max/lambda/pkg/emitter"
|
"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/lambda"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/repr"
|
||||||
"git.maximhutz.com/max/lambda/pkg/runtime"
|
"git.maximhutz.com/max/lambda/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ func NewRuntime(expression lambda.Expression) *Runtime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Expression returns the current expression state.
|
// Expression returns the current expression state.
|
||||||
func (r *Runtime) Expression() expr.Expression {
|
func (r *Runtime) Expression() repr.Repr {
|
||||||
return r.expression
|
return r.expression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
pkg/repr/repr.go
Normal file
15
pkg/repr/repr.go
Normal 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
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ package runtime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.maximhutz.com/max/lambda/pkg/emitter"
|
"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.
|
// Runtime defines the interface for expression reduction strategies.
|
||||||
@@ -23,5 +23,5 @@ type Runtime interface {
|
|||||||
Run()
|
Run()
|
||||||
|
|
||||||
// Copy the state of the runtime.
|
// Copy the state of the runtime.
|
||||||
Expression() expr.Expression
|
Expression() repr.Repr
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user