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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
use std::fmt::{self, Display};
use proc_macro2::Span;
use super::Name;
pub mod error;
pub mod substitute;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
pub struct Epsilon;
pub type Literal = String;
#[derive(Clone, Debug)]
pub struct Cat {
pub first: Box<NamedExpression>,
pub rest: Vec<(Option<Span>, NamedExpression)>,
}
impl Display for Cat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}", self.first)?;
for (_, other) in &self.rest {
write!(f, " . {}", other)?;
}
write!(f, ")")
}
}
impl PartialEq for Cat {
fn eq(&self, other: &Self) -> bool {
self.first == other.first
&& self.rest.len() == other.rest.len()
&& self
.rest
.iter()
.zip(other.rest.iter())
.all(|((_, me), (_, them))| me == them)
}
}
impl Eq for Cat {}
#[derive(Clone, Debug)]
pub struct Alt {
pub first: Box<NamedExpression>,
pub rest: Vec<(Option<Span>, NamedExpression)>
}
impl Display for Alt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}", self.first)?;
for (_, other) in &self.rest {
write!(f, " | {}", other)?;
}
write!(f, ")")
}
}
impl PartialEq for Alt {
fn eq(&self, other: &Self) -> bool {
self.first == other.first
&& self.rest.len() == other.rest.len()
&& self
.rest
.iter()
.zip(other.rest.iter())
.all(|((_, me), (_, them))| me == them)
}
}
impl Eq for Alt {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Fix {
pub inner: Box<NamedExpression>,
}
impl Display for Fix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "!{}", self.inner)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Variable {
pub index: usize,
}
impl Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "'{}", self.index)
}
}
/// A function invocation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Call {
pub on: Box<NamedExpression>,
pub args: Vec<NamedExpression>,
}
impl Display for Call {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}", self.on)?;
for arg in self.args {
write!(f, " {}", arg)?;
}
write!(f, ")")
}
}
/// A function abstraction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Lambda {
pub args: Vec<Name>,
pub inner: Box<NamedExpression>,
}
impl Display for Lambda {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "/")?;
let mut iter = self.args.iter();
if let Some(arg) = iter.next() {
write!(f, "{}", arg)?;
for arg in iter {
write!(f, ", {}", arg)?;
}
}
write!(f, "/ {}", self.inner)
}
}
#[derive(Clone, Debug)]
pub enum Expression {
/// Matches the empty string.
Epsilon(Epsilon),
/// Matches the literal string.
Literal(Literal),
/// Matches one term followed by another.
Cat(Cat),
/// Matches either one term or another.
Alt(Alt),
/// The least fix point of a term.
Fix(Fix),
/// A fixed point variable.
Variable(Variable),
/// A function invocation.
Call(Call),
/// A function abstraction.
Lambda(Lambda),
}
impl Expression {
pub fn try_into_lambda(self) -> Result<Lambda, Self> {
if let Self::Lambda(l) = self {
Ok(l)
} else {
Err(self)
}
}
}
impl Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Epsilon(_) => write!(f, "_"),
Self::Literal(l) => l.fmt(f),
Self::Cat(c) => c.fmt(f),
Self::Alt(a) => a.fmt(f),
Self::Fix(x) => x.fmt(f),
Self::Variable(v) => v.fmt(f),
Self::Lambda(p) => p.fmt(f),
Self::Call(c) => c.fmt(f),
}
}
}
impl PartialEq for Expression {
fn eq(&self, other: &Self) -> bool {
match self {
Self::Epsilon(_) => matches!(other, Self::Epsilon(_)),
Self::Literal(l) => {
if let Self::Literal(them) = other {
l == them
} else {
false
}
}
Self::Cat(c) => {
if let Self::Cat(them) = other {
c == them
} else {
false
}
}
Self::Alt(a) => {
if let Self::Alt(them) = other {
a == them
} else {
false
}
}
Self::Fix(f) => {
if let Self::Fix(them) = other {
f == them
} else {
false
}
}
Self::Variable(v) => {
if let Self::Variable(them) = other {
v == them
} else {
false
}
}
Self::Lambda(p) => {
if let Self::Lambda(them) = other {
p == them
} else {
false
}
}
Self::Call(c) => {
if let Self::Call(them) = other {
c == them
} else {
false
}
}
}
}
}
impl Eq for Expression {}
impl From<Epsilon> for Expression {
fn from(eps: Epsilon) -> Self {
Self::Epsilon(eps)
}
}
impl From<Literal> for Expression {
fn from(lit: Literal) -> Self {
Self::Literal(lit)
}
}
impl From<Cat> for Expression {
fn from(cat: Cat) -> Self {
Self::Cat(cat)
}
}
impl From<Alt> for Expression {
fn from(alt: Alt) -> Self {
Self::Alt(alt)
}
}
impl From<Fix> for Expression {
fn from(fix: Fix) -> Self {
Self::Fix(fix)
}
}
impl From<Variable> for Expression {
fn from(var: Variable) -> Self {
Self::Variable(var)
}
}
impl From<Lambda> for Expression {
fn from(lambda: Lambda) -> Self {
Self::Lambda(lambda)
}
}
impl From<Call> for Expression {
fn from(call: Call) -> Self {
Self::Call(call)
}
}
#[derive(Clone, Debug)]
pub struct NamedExpression {
pub name: Option<Name>,
pub expr: Expression,
pub span: Option<Span>,
}
impl Display for NamedExpression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.name {
Some(name) => write!(f, "({} : {})", self.expr, name),
None => self.expr.fmt(f),
}
}
}
impl PartialEq for NamedExpression {
fn eq(&self, other: &Self) -> bool {
self.expr == other.expr
}
}
impl Eq for NamedExpression {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Let {
pub name: Name,
pub val: NamedExpression,
pub inner: Box<TopLevel>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TopLevel {
Let(Let),
Goal(NamedExpression),
}
|