style: restructure cli and registry packages (#43)

## Description

The `internal/cli` package had grown to contain both CLI utilities (source/destination I/O) and registry-level abstractions (repr, conversion, engine, marshaler).
This PR separates concerns by moving registry types into `internal/registry` and keeping only CLI I/O types in `internal/cli`.
It also simplifies several core abstractions and aligns naming conventions.

- Move `Source`, `Destination` from `internal/config` to `internal/cli`.
- Move `Repr`, `Conversion`, `Engine`, `Process`, `Codec` from `internal/cli` to `internal/registry`.
- Rename "marshalers" to "codecs" throughout the codebase.
- Simplify `codec.Codec[T, U]` to `codec.Codec[T]` (string-based marshaling only).
- Add `codec.Conversion[T, U]` as a function type alias.
- Change `engine.Engine[T]` from an interface to a function type.
- Merge `Engine.Load()` + `Process.Set()` into a single `Engine.Load(Repr)` call.
- Convert `Saccharine2Lambda` from a struct to standalone conversion functions.
- Replace registry methods (`MustAddMarshaler`, `MustAddEngine`, `MustAddConversions`) with generic free functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`).
- Remove unused `internal/config` package (`Config`, `GetLogger`, `ParseFromArgs`).
- Remove unused `pkg/emitter` package.
- Rename `Id()` to `ID()` per Go conventions.
- Add documentation comments and enable `checkPublicInterface` lint rule.
- Rename `reduce_one.go` to `reduce_once.go`.

### Decisions

- `Engine[T]` is now a function type (`func(T) (Process[T], error)`) rather than an interface, since the only method was `Load`.
- `Codec[T, U]` was split into `Codec[T]` (string marshaling) and `Conversion[T, U]` (type-to-type conversion function), which better reflects how they are actually used.
- Registration uses free generic functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`) instead of methods on `Registry`, enabling type inference at the call site.

## Benefits

- Clearer separation of concerns between CLI I/O and the registry's internal type system.
- Simpler abstractions: fewer interfaces, fewer wrapper types, fewer indirections.
- Removing unused packages (`config`, `emitter`) reduces maintenance burden.
- Naming conventions (`ID`, codecs, `reduce_once`) are more idiomatic.

## Checklist

- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`<type>/<description>`).
- [x] Tests pass (if applicable).
- [x] Documentation updated (if applicable).

Reviewed-on: #43
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #43.
This commit is contained in:
2026-02-07 05:39:32 +00:00
committed by Maxim Hutz
parent 58d0823069
commit bbe027e9f4
29 changed files with 250 additions and 453 deletions

View File

@@ -1,8 +1,8 @@
package codec
type Codec[T, U any] interface {
Encode(T) (U, error)
Decode(U) (T, error)
}
type Conversion[T, U any] = func(T) (U, error)
type Marshaler[T any] = Codec[T, string]
type Codec[T any] interface {
Encode(T) (string, error)
Decode(string) (T, error)
}

View File

@@ -3,7 +3,6 @@ package convert
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/codec"
"git.maximhutz.com/max/lambda/pkg/lambda"
"git.maximhutz.com/max/lambda/pkg/saccharine"
)
@@ -125,14 +124,10 @@ func decodeExression(l lambda.Expression) saccharine.Expression {
}
}
type Saccharine2Lambda struct{}
func (c Saccharine2Lambda) Decode(l lambda.Expression) (saccharine.Expression, error) {
func Lambda2Saccharine(l lambda.Expression) (saccharine.Expression, error) {
return decodeExression(l), nil
}
func (c Saccharine2Lambda) Encode(s saccharine.Expression) (lambda.Expression, error) {
func Saccharine2Lambda(s saccharine.Expression) (lambda.Expression, error) {
return encodeExpression(s), nil
}
var _ codec.Codec[saccharine.Expression, lambda.Expression] = (*Saccharine2Lambda)(nil)

View File

@@ -1,46 +0,0 @@
package emitter
import "git.maximhutz.com/max/lambda/pkg/set"
type Emitter[E comparable] interface {
On(E, func()) Listener[E]
Off(Listener[E])
Emit(E)
}
type BaseEmitter[E comparable] struct {
listeners map[E]set.Set[Listener[E]]
}
func (e *BaseEmitter[E]) On(kind E, fn func()) Listener[E] {
if e.listeners[kind] == nil {
e.listeners[kind] = set.New[Listener[E]]()
}
listener := &BaseListener[E]{kind, fn}
e.listeners[kind].Add(listener)
return listener
}
func (e *BaseEmitter[E]) Off(listener Listener[E]) {
kind := listener.Kind()
if e.listeners[kind] != nil {
e.listeners[kind].Remove(listener)
}
}
func (e *BaseEmitter[E]) Emit(event E) {
if e.listeners[event] == nil {
e.listeners[event] = set.New[Listener[E]]()
}
for listener := range e.listeners[event].Items() {
listener.Run()
}
}
func New[E comparable]() *BaseEmitter[E] {
return &BaseEmitter[E]{
listeners: map[E]set.Set[Listener[E]]{},
}
}

View File

@@ -1,19 +0,0 @@
package emitter
type Listener[E comparable] interface {
Kind() E
Run()
}
type BaseListener[E comparable] struct {
kind E
fn func()
}
func (l BaseListener[E]) Kind() E {
return l.kind
}
func (l BaseListener[E]) Run() {
l.fn()
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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:

View File

@@ -16,4 +16,4 @@ func (m Marshaler) Encode(e Expression) (string, error) {
return e.String(), nil
}
var _ codec.Marshaler[Expression] = (*Marshaler)(nil)
var _ codec.Codec[Expression] = (*Marshaler)(nil)

View File

@@ -21,4 +21,4 @@ func (m Marshaler) Encode(e Expression) (string, error) {
return stringifyExpression(e), nil
}
var _ codec.Marshaler[Expression] = (*Marshaler)(nil)
var _ codec.Codec[Expression] = (*Marshaler)(nil)