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
//! Untyped lambda calculus [Term]s.
use Display;
use Formatter;
use Result as FmtResult;
pub use *;
/// A lambda calculus term, which is either a variable, an abstraction, or an application.
///
/// [Term]s can be constructed in multiple ways:
/// - The [lambda!](crate::lambda) macro
/// - The [var!](crate::var), [abs!](crate::abs), and [app!](crate::app) macros
/// - The [Term::var], [Term::abs], and [Term::app] associated functions
/// - [Term]'s enum variants
///
/// The macros offer some syntactic sugar for the construction of [Term]s, and will suffice for the vast majority of cases.
/// For greater control over how [Term]s are constructed, consider using the associated functions and enum variants.
/// Constructs a variable using an identifier.
///
/// While the actual [Term::Var] variant has no constraint on what is and isn't an identifier, this macro only accepts a regular Rust identifier, and stringifies it.
///
/// # Examples
///
/// ```
/// use lamb::*;
///
/// let var_term = var!(x);
/// assert_eq!(var_term, Term::var("x"));
/// ```
/// Constructs an abstraction with one or more formal parameters and a body.
///
/// This macro supports syntax sugar for multiple formal parameters.
/// There must be at least one parameter, and each parameter must be delimited by whitespace, ending with a dot (`.`).
/// The abstraction body comes after all parameters have been declared.
///
/// Abstraction bodies extend as far as possible to the right, i.e. `λa. b c a` is interpreted as `λa. (b c a)`.
///
/// # Examples
///
/// ```
/// use lamb::*;
///
/// let term_a = abs!(x. var!(x));
/// assert_eq!(term_a, Term::abs("x", Term::var("x")));
///
/// let term_b = abs!(x y z. app!(var!(x), var!(y), var!(z)));
/// assert_eq!(term_b, Term::abs(
/// "x",
/// Term::abs(
/// "y",
/// Term::abs(
/// "z",
/// Term::app(
/// Term::app(
/// Term::var("x"),
/// Term::var("y"),
/// ),
/// Term::var("z"),
/// ),
/// ),
/// ),
/// ));
/// ```
/// Constructs an application from multiple [Term]s.
///
/// This macro supports syntax sugar for multiple application.
/// There must be at least two [Term]s to apply together, and each [Term] must be delimited by a comma (`,`).
/// Trailing commas are not permitted.
///
/// Application is left-associative, i.e. `a b c d` is interpreted as `((a b) c) d`.
///
/// # Examples
///
/// ```
/// use lamb::*;
///
/// let term_a = app!(var!(x), var!(y));
/// assert_eq!(term_a, Term::app(
/// Term::var("x"),
/// Term::var("y"),
/// ));
///
/// let term_b = app!(var!(a), abs!(b. var!(a)), var!(d));
/// assert_eq!(term_b, Term::app(
/// Term::app(
/// Term::var("a"),
/// Term::abs("b", Term::var("a"))),
/// Term::var("d"),
/// ));
/// ```
/// Constructs a [Term] using standard untyped lambda calculus notation.
///
/// This macro allows constructing arbitrary [Term]s using syntax that closely matches the standard syntax of untyped lambda calculus.
/// It supports syntax sugar for multiple formal parameter and multiple application.
///
/// Abstraction bodies extend as far as possible to the right, i.e. `λa. b c a` is interpreted as `λa. (b c a)`.
/// Application is left-associative, i.e. `a b c d` is interpreted as `((a b) c) d`.
///
/// Some whitespace is necessary after each `λ`, otherwise Rust will process the `λ` as part of an identifier.
/// i.e. `λx. x` will produce invalid syntax, but `λ x. x` will parse correctly.
///
/// Whitespace is also necessary between two [Term]s being applied together, unless one or both of the [Term]s are enclosed in parentheses (`()`).
/// i.e. `xy` will be parsed as a variable `xy`, but `x y` or `x(y)` will be parsed as `x` applied to `y`.
///
/// # Examples
///
/// ```
/// use lamb::*;
///
/// let term_a = lambda!(x);
/// assert_eq!(term_a, Term::var("x"));
///
/// let term_b = lambda!(λ x. x);
/// assert_eq!(term_b, Term::abs("x", Term::var("x")));
///
/// let term_c = lambda!((λ x y. y (x x y)) (λ x y. y (x x y)));
/// assert_eq!(term_c, Term::app(
/// Term::abs(
/// "x",
/// Term::abs(
/// "y",
/// Term::app(
/// Term::var("y"),
/// Term::app(
/// Term::app(Term::var("x"), Term::var("x")),
/// Term::var("y"),
/// ),
/// ),
/// ),
/// ),
/// Term::abs(
/// "x",
/// Term::abs(
/// "y",
/// Term::app(
/// Term::var("y"),
/// Term::app(
/// Term::app(Term::var("x"), Term::var("x")),
/// Term::var("y"),
/// ),
/// ),
/// ),
/// ),
/// ));
/// ```
;
=> ;
=> ;
=> ;
=> ;
=> ;
}