Files
lambda/pkg/convert/saccharine_to_lambda.go
M.V. Hutz bbe027e9f4 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>
2026-02-07 05:39:32 +00:00

134 lines
3.3 KiB
Go

package convert
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/lambda"
"git.maximhutz.com/max/lambda/pkg/saccharine"
)
func encodeAtom(n *saccharine.Atom) lambda.Expression {
return lambda.NewVariable(n.Name)
}
func encodeAbstraction(n *saccharine.Abstraction) lambda.Expression {
result := encodeExpression(n.Body)
parameters := n.Parameters
// If the function has no parameters, it is a thunk. Lambda calculus still
// requires _some_ parameter exists, so generate one.
if len(parameters) == 0 {
freeVars := result.GetFree()
freshName := lambda.GenerateFreshName(freeVars)
parameters = append(parameters, freshName)
}
for i := len(parameters) - 1; i >= 0; i-- {
result = lambda.NewAbstraction(parameters[i], result)
}
return result
}
func encodeApplication(n *saccharine.Application) lambda.Expression {
result := encodeExpression(n.Abstraction)
arguments := []lambda.Expression{}
for _, argument := range n.Arguments {
encodeedArgument := encodeExpression(argument)
arguments = append(arguments, encodeedArgument)
}
for _, argument := range arguments {
result = lambda.NewApplication(result, argument)
}
return result
}
func reduceLet(s *saccharine.LetStatement, e lambda.Expression) lambda.Expression {
var value lambda.Expression
if len(s.Parameters) == 0 {
value = encodeExpression(s.Body)
} else {
value = encodeAbstraction(saccharine.NewAbstraction(s.Parameters, s.Body))
}
return lambda.NewApplication(
lambda.NewAbstraction(s.Name, e),
value,
)
}
func reduceDeclare(s *saccharine.DeclareStatement, e lambda.Expression) lambda.Expression {
freshVar := lambda.GenerateFreshName(e.GetFree())
return lambda.NewApplication(
lambda.NewAbstraction(freshVar, e),
encodeExpression(s.Value),
)
}
func reduceStatement(s saccharine.Statement, e lambda.Expression) lambda.Expression {
switch s := s.(type) {
case *saccharine.DeclareStatement:
return reduceDeclare(s, e)
case *saccharine.LetStatement:
return reduceLet(s, e)
default:
panic(fmt.Errorf("unknown statement type: %v", s))
}
}
func encodeClause(n *saccharine.Clause) lambda.Expression {
result := encodeExpression(n.Returns)
for i := len(n.Statements) - 1; i >= 0; i-- {
result = reduceStatement(n.Statements[i], result)
}
return result
}
func encodeExpression(s saccharine.Expression) lambda.Expression {
switch s := s.(type) {
case *saccharine.Atom:
return encodeAtom(s)
case *saccharine.Abstraction:
return encodeAbstraction(s)
case *saccharine.Application:
return encodeApplication(s)
case *saccharine.Clause:
return encodeClause(s)
default:
panic(fmt.Errorf("unknown expression type: %T", s))
}
}
func decodeExression(l lambda.Expression) saccharine.Expression {
switch l := l.(type) {
case lambda.Variable:
return saccharine.NewAtom(l.Name())
case lambda.Abstraction:
return saccharine.NewAbstraction(
[]string{l.Parameter()},
decodeExression(l.Body()))
case lambda.Application:
return saccharine.NewApplication(
decodeExression(l.Abstraction()),
[]saccharine.Expression{decodeExression(l.Argument())})
default:
panic(fmt.Errorf("unknown expression type: %T", l))
}
}
func Lambda2Saccharine(l lambda.Expression) (saccharine.Expression, error) {
return decodeExression(l), nil
}
func Saccharine2Lambda(s saccharine.Expression) (lambda.Expression, error) {
return encodeExpression(s), nil
}