-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeral_test.go
More file actions
255 lines (236 loc) · 5.9 KB
/
numeral_test.go
File metadata and controls
255 lines (236 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package lambda
import (
"testing"
)
func TestNumeralString(t *testing.T) {
if s := Numeral(0).String(); s != "[0]" {
t.Errorf("Numeral(0).String() = %q, want [0]", s)
}
if s := Numeral(42).String(); s != "[42]" {
t.Errorf("Numeral(42).String() = %q, want [42]", s)
}
}
func TestNumeralFreeVars(t *testing.T) {
fv := Numeral(5).FreeVars()
if len(fv) != 0 {
t.Errorf("Numeral should have no free vars, got %v", fv)
}
}
func TestNumeralToInt(t *testing.T) {
for _, n := range []int{0, 1, 2, 5, 10, 100} {
got := ToInt(Numeral(uint64(n)))
if got != n {
t.Errorf("ToInt(Numeral(%d)) = %d", n, got)
}
}
}
func TestNumeralExpand(t *testing.T) {
// Numeral(3).Expand() should equal ChurchNumeral(3)
expanded := Numeral(3).Expand()
church := ChurchNumeral(3)
if expanded.String() != church.String() {
t.Errorf("Numeral(3).Expand() = %s, want %s", expanded, church)
}
}
func TestNumeralBetaReduce(t *testing.T) {
// Numeral itself doesn't reduce
_, reduced := Numeral(5).BetaReduce()
if reduced {
t.Error("Numeral should not beta-reduce on its own")
}
}
func TestNumeralApplyPartial(t *testing.T) {
// Numeral(3) f → NumeralApply{3, f}
app := Application{Func: Numeral(3), Arg: Var{Name: "f"}}
result, reduced := app.BetaReduce()
if !reduced {
t.Fatal("Numeral(3) f should reduce")
}
na, ok := result.(NumeralApply)
if !ok {
t.Fatalf("expected NumeralApply, got %T: %s", result, result)
}
if na.N != 3 {
t.Errorf("NumeralApply.N = %d, want 3", na.N)
}
}
func TestNumeralApplyFull(t *testing.T) {
// Numeral(3) f x → f(f(f(x))) in two steps
term := Application{
Func: Application{Func: Numeral(3), Arg: Var{Name: "f"}},
Arg: Var{Name: "x"},
}
// Step 1: Numeral(3) f → NumeralApply
result, reduced := term.BetaReduce()
if !reduced {
t.Fatal("step 1 should reduce")
}
// Step 2: NumeralApply{3, f} x → f(f(f(x)))
result, reduced = result.BetaReduce()
if !reduced {
t.Fatal("step 2 should reduce")
}
// Verify structure: f(f(f(x)))
expected := Application{
Func: Var{Name: "f"},
Arg: Application{
Func: Var{Name: "f"},
Arg: Application{
Func: Var{Name: "f"},
Arg: Var{Name: "x"},
},
},
}
if result.String() != expected.String() {
t.Errorf("got %s, want %s", result, expected)
}
}
func TestNumeralZero(t *testing.T) {
// Numeral(0) f x → x (zero applications of f)
term := Application{
Func: Application{Func: Numeral(0), Arg: Var{Name: "f"}},
Arg: Var{Name: "x"},
}
result, _ := Reduce(term, 10)
if v, ok := result.(Var); !ok || v.Name != "x" {
t.Errorf("Numeral(0) f x = %s, want x", result)
}
}
func TestNumeralArithmetic(t *testing.T) {
tests := []struct {
name string
term Term
want int
}{
{
"SUCC 3",
Application{Func: SUCC, Arg: Numeral(3)},
4,
},
{
"PLUS 3 5",
Application{Func: Application{Func: PLUS, Arg: Numeral(3)}, Arg: Numeral(5)},
8,
},
{
"MULT 3 4",
Application{Func: Application{Func: MULT, Arg: Numeral(3)}, Arg: Numeral(4)},
12,
},
{
"POW 2 3",
Application{Func: Application{Func: POW, Arg: Numeral(2)}, Arg: Numeral(3)},
8,
},
{
"PLUS 0 0",
Application{Func: Application{Func: PLUS, Arg: Numeral(0)}, Arg: Numeral(0)},
0,
},
{
"MULT 0 5",
Application{Func: Application{Func: MULT, Arg: Numeral(0)}, Arg: Numeral(5)},
0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, _ := Reduce(tt.term, 1000)
got := ToInt(result)
if got != tt.want {
t.Errorf("%s = %d, want %d (reduced to: %s)", tt.name, got, tt.want, result)
}
})
}
}
func TestNumeralMixedWithChurch(t *testing.T) {
// Numeral + ChurchNumeral should work together
term := Application{
Func: Application{Func: PLUS, Arg: Numeral(3)},
Arg: ChurchNumeral(5),
}
result, _ := Reduce(term, 1000)
got := ToInt(result)
if got != 8 {
t.Errorf("PLUS Numeral(3) Church(5) = %d, want 8", got)
}
}
func TestNumeralSubstitute(t *testing.T) {
// Numeral should pass through substitution unchanged
n := Numeral(5)
got := n.Substitute("x", Var{Name: "y"})
if got != n {
t.Errorf("Numeral.Substitute changed it: %s", got)
}
}
func TestNumeralApplySubstitute(t *testing.T) {
// NumeralApply should substitute in F
na := NumeralApply{N: 3, Param: "x", F: Var{Name: "f"}}
got := na.Substitute("f", Var{Name: "g"})
na2, ok := got.(NumeralApply)
if !ok {
t.Fatalf("expected NumeralApply, got %T", got)
}
if na2.F.String() != "g" {
t.Errorf("F should be g, got %s", na2.F)
}
}
func TestNumeralDiagram(t *testing.T) {
// Diagram of Numeral(2) should match Church 2
got := Diagram(Numeral(2))
want := Diagram(ChurchNumeral(2))
if got != want {
t.Errorf("Diagram(Numeral(2)):\n%s\nwant:\n%s", got, want)
}
}
func TestNumeralApplyEtaConvert(t *testing.T) {
// NumeralApply{1, f} = λx.f x → f (η-reduction)
na := NumeralApply{N: 1, Param: "x", F: Var{Name: "f"}}
result, converted := na.EtaConvert()
if !converted {
t.Fatal("should η-convert")
}
if v, ok := result.(Var); !ok || v.Name != "f" {
t.Errorf("η-conversion of λx.f x = %s, want f", result)
}
}
func BenchmarkPLUS_Church(b *testing.B) {
term := Application{
Func: Application{Func: PLUS, Arg: ChurchNumeral(10)},
Arg: ChurchNumeral(10),
}
for i := 0; i < b.N; i++ {
result, _ := Reduce(term, 10000)
ToInt(result)
}
}
func BenchmarkPLUS_Numeral(b *testing.B) {
term := Application{
Func: Application{Func: PLUS, Arg: Numeral(10)},
Arg: Numeral(10),
}
for i := 0; i < b.N; i++ {
result, _ := Reduce(term, 10000)
ToInt(result)
}
}
func BenchmarkMULT_Church(b *testing.B) {
term := Application{
Func: Application{Func: MULT, Arg: ChurchNumeral(5)},
Arg: ChurchNumeral(5),
}
for i := 0; i < b.N; i++ {
result, _ := Reduce(term, 10000)
ToInt(result)
}
}
func BenchmarkMULT_Numeral(b *testing.B) {
term := Application{
Func: Application{Func: MULT, Arg: Numeral(5)},
Arg: Numeral(5),
}
for i := 0; i < b.N; i++ {
result, _ := Reduce(term, 10000)
ToInt(result)
}
}