A Golang runtime and compilation backend for Delta Interaction Nets.
1package lambda
2
3import (
4 "github.com/vic/godnet/pkg/deltanet"
5 "os"
6 "testing"
7)
8
9// helper: roundtrip a term through ToDeltaNet -> FromDeltaNet (no reduction)
10func roundtrip(t *testing.T, term Term) Term {
11 net := deltanet.NewNetwork()
12 rootNode, rootPort, varNames := ToDeltaNet(term, net)
13 // ensure deterministic tiny timeout for network workers
14 // Ensure any pending interactions are processed (no-op if none)
15 net.ReduceAll()
16 res := FromDeltaNet(net, rootNode, rootPort, varNames)
17 return res
18}
19
20func TestRoundtripIdentity(t *testing.T) {
21 orig := Abs{Arg: "x", Body: Var{Name: "x"}}
22 res := roundtrip(t, orig)
23 if _, ok := res.(Abs); !ok {
24 t.Fatalf("Identity roundtrip: expected Abs, got %T: %#v", res, res)
25 }
26}
27
28func TestRoundtripNestedApp(t *testing.T) {
29 // (x. (y. (z. ((x y) z))))
30 orig := Abs{Arg: "x", Body: Abs{Arg: "y", Body: Abs{Arg: "z", Body: App{Fun: App{Fun: Var{Name: "x"}, Arg: Var{Name: "y"}}, Arg: Var{Name: "z"}}}}}
31 res := roundtrip(t, orig)
32 // We expect an Abs at top-level
33 if _, ok := res.(Abs); !ok {
34 t.Fatalf("NestedApp roundtrip: expected Abs, got %T: %#v", res, res)
35 }
36}
37
38func TestRoundtripFreeVar(t *testing.T) {
39 orig := Var{Name: "a"}
40 res := roundtrip(t, orig)
41 // free variables lose name (deltanet doesn't store names) but structure should be Var
42 if _, ok := res.(Var); !ok {
43 t.Fatalf("FreeVar roundtrip: expected Var, got %T: %#v", res, res)
44 }
45}
46
47func TestRoundtripSharedVar(t *testing.T) {
48 //
49 // (. (f f)) where f is bound and shared
50 orig := Abs{Arg: "f", Body: App{Fun: Var{Name: "f"}, Arg: Var{Name: "f"}}}
51 res := roundtrip(t, orig)
52 if _, ok := res.(Abs); !ok {
53 t.Fatalf("SharedVar roundtrip: expected Abs, got %T: %#v", res, res)
54 }
55}
56
57func TestTranslatorDiagnostics(t *testing.T) {
58 // This ensures DELTA_DEBUG can be toggled without breaking behavior.
59 old := os.Getenv("DELTA_DEBUG")
60 defer os.Setenv("DELTA_DEBUG", old)
61 os.Setenv("DELTA_DEBUG", "1")
62 orig := Abs{Arg: "x", Body: Var{Name: "x"}}
63 _ = roundtrip(t, orig)
64}