-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinator.go
211 lines (180 loc) · 4.05 KB
/
combinator.go
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
package combinator
import (
"context"
"slices"
)
type (
Combinator struct {
Name string
Arguments []string
Definition string
}
Basis []Combinator
)
// From section 3 of the paper
var (
// Identity
I = Combinator{
Name: "I",
Arguments: []string{"x"},
Definition: "x",
}
// Constancy
K = Combinator{
Name: "K",
Arguments: []string{"x", "y"},
Definition: "x",
}
// Interchange
T = Combinator{
Name: "T",
Arguments: []string{"x", "y", "z"},
Definition: "xzy",
}
// Composition
Z = Combinator{
Name: "Z",
Arguments: []string{"x", "y", "z"},
Definition: "x(yz)",
}
// Fusion
S = Combinator{
Name: "S",
Arguments: []string{"x", "y", "z"},
Definition: "xz(yz)",
}
// All of Schönfinkel's defined combinators
Schonfinkel = Basis{I, K, T, Z, S}
)
// SK and SKI (https://en.wikipedia.org/wiki/SKI_combinator_calculus)
var (
SK = Basis{S, K}
SKI = Basis{S, K, I}
)
// BCKW (https://en.wikipedia.org/wiki/B,_C,_K,_W_system)
var (
B = Combinator{
Name: "B",
Arguments: []string{"x", "y", "z"},
Definition: "x(yz)",
}
C = Combinator{
Name: "C",
Arguments: []string{"x", "y", "z"},
Definition: "xzy",
}
W = Combinator{
Name: "W",
Arguments: []string{"x", "y"},
Definition: "xyy",
}
BCKW = Basis{B, C, K, W}
)
// Iota (https://en.wikipedia.org/wiki/Iota_and_Jot)
var (
Iota = Basis{
S,
K,
Combinator{
Name: "i",
Arguments: []string{"x"},
// Note the use of other combinators in the definition
// makes Iota "improper"
Definition: "xSK",
},
}
)
// Church Encoding (https://en.wikipedia.org/wiki/Church_encoding)
var (
Zero = Combinator{
Name: "0",
Arguments: []string{"f", "x"},
Definition: "x",
}
Succ = Combinator{
Name: "S",
Arguments: []string{"n", "f", "x"},
Definition: "f(nfx)",
}
Plus = Combinator{
Name: "P",
Arguments: []string{"m", "n", "f", "x"},
Definition: "mf(nfx)",
}
Mult = Combinator{
Name: "M",
Arguments: []string{"m", "n", "f", "x"},
Definition: "m(nf)x",
}
Exp = Combinator{
Name: "E",
Arguments: []string{"m", "n", "f", "x"},
Definition: "nmfx",
}
Church = Basis{Zero, Succ, Plus, Mult, Exp}
)
// Transforms the statement using the Basis `b`
func (b Basis) Transform(ctx context.Context, statement string) (string, error) {
if err := isWellDefined(statement); err != nil {
return "", err
}
tree := parse(statement)
reducedTree, err := reduce(ctx, tree, b, false, 0)
if err != nil {
return "", err
}
return unparse(reducedTree), nil
}
// Adds an additional Combinator to the Basis
func (b Basis) With(combinator Combinator) Basis {
return append(b, combinator)
}
// Transforms the statement using the Combinator `c`
func (c Combinator) Transform(ctx context.Context, statement string) (string, error) {
if err := isWellDefined(statement); err != nil {
return "", err
}
tree := parse(statement)
reducedTree, err := reduce(ctx, tree, Basis{c}, false, 0)
if err != nil {
return "", err
}
return unparse(reducedTree), nil
}
// Returns whether the statement is well defined or not
func WellDefined(statement string) (bool, error) {
err := isWellDefined(statement)
return err == nil, err
}
// Parses the statement into a Tree
func Parse(statement string) *Tree {
return parse(statement)
}
// Parses the statement into a Tree
func Unparse(tree *Tree) string {
return unparse(tree)
}
// Reduces the Tree using the Basis `b`
func (b Basis) Reduce(ctx context.Context, tree *Tree) (*Tree, error) {
reduced, err := reduce(ctx, tree, b, false, 0)
return reduced, err
}
// Joins the trees together under a new root
func Join(trees ...*Tree) *Tree {
return join(trees)
}
// Produces a copy of the tree
func Copy(t *Tree) *Tree {
return copy(t)
}
// Attempts to find a Combinator named `name` in the Basis `b`
func findCombinator(name string, b Basis) (Combinator, bool) {
index := slices.IndexFunc(b, func(c Combinator) bool {
return c.Name == name
})
if index >= 0 {
return b[index], true
} else {
return Combinator{}, false
}
}