diff --git a/.golangci.yml b/.golangci.yml index a9a6f07..9c9494b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -48,7 +48,7 @@ linters: # More information: https://golangci-lint.run/usage/false-positives/#comments # # Please uncomment the following line if your code is not using the godoc format - - comments + # - comments # Common 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. - 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. - name: context-as-argument arguments: diff --git a/Makefile b/Makefile index 2ce49e2..c4ee60d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ BINARY_NAME=lambda 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 .SILENT: @@ -15,6 +15,7 @@ help: echo " docs - Start local godoc server on port 6060" echo " test - Run tests for all samples" echo " bench - Run benchmarks for all samples" + echo " lint - Run golangci-lint on all packages" echo " clean - Remove all build artifacts" build: @@ -45,6 +46,9 @@ test: bench: go test -bench=. -benchtime=10x -cpu=4 ./cmd/lambda +lint: + go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest run ./... + clean: rm -f ${BINARY_NAME} rm -f program.out diff --git a/pkg/codec/codec.go b/pkg/codec/codec.go new file mode 100644 index 0000000..7fc8a7f --- /dev/null +++ b/pkg/codec/codec.go @@ -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) +} diff --git a/pkg/expr/expr.go b/pkg/expr/expr.go deleted file mode 100644 index b45a515..0000000 --- a/pkg/expr/expr.go +++ /dev/null @@ -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 -} diff --git a/pkg/lambda/expression.go b/pkg/lambda/expression.go index dd28fe6..3b78f13 100644 --- a/pkg/lambda/expression.go +++ b/pkg/lambda/expression.go @@ -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 } diff --git a/pkg/normalorder/runtime.go b/pkg/normalorder/runtime.go index 37d38e4..c014e3d 100644 --- a/pkg/normalorder/runtime.go +++ b/pkg/normalorder/runtime.go @@ -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 } diff --git a/pkg/repr/repr.go b/pkg/repr/repr.go new file mode 100644 index 0000000..a1248f0 --- /dev/null +++ b/pkg/repr/repr.go @@ -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 +} diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index c2aafad..5749045 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -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 }