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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Definition of the lambda calculus term.
//!
//! This implementation of the lambda calculus uses the classic notation.
//! Currently the De Bruin index notation is not supported.

#[cfg(test)]
use proptest::strategy::Strategy;

use std::collections::HashSet;
use std::fmt::{self, Display};
use std::ops::{Deref, DerefMut};

use self::Term::*;

/// Constructs a variable of the given name.
///
/// This is a convenience function for constructing a [`Term`](enum.Term.html)
/// of variant `Term::Var` in a readable form with minimal keystrokes. It takes
/// any value that can be converted into a `String` and returns
/// `Term::Var(VarName(name))`.
///
/// This function combined with the functions [`lam`](fn.lam.html) and
/// [`app`](fn.app.html) let us construct any [`Term`](enum.Term.html) in the
/// untyped lambda calculus.
///
/// # Examples
///
/// ```
/// # extern crate lamcal;
/// # use lamcal::{var, Term, VarName};
/// let variable = var("x");
///
/// assert_eq!(variable, Term::Var(VarName("x".to_string())));
/// ```
pub fn var(name: impl Into<String>) -> Term {
    Var(VarName(name.into()))
}

/// Constructs a lambda abstraction with given parameter and body.
///
/// This is a convenience function for constructing a [`Term`](enum.Term.html)
/// of variant `Term::Lam` in a readable form with minimal keystrokes. It takes
/// any value that can be converted into a `String` to form a bound variable
/// (the parameter of the abstraction) and a `Term` for the body of the
/// abstraction.
///
/// This function combined with the functions [`var`](fn.var.html) and
/// [`app`](fn.app.html) let us construct any [`Term`](enum.Term.html) in the
/// untyped lambda calculus.
///
/// # Examples
///
/// ```
/// # extern crate lamcal;
/// # use lamcal::{lam, var, Term, VarName};
/// let abstraction = lam("x", var("x"));
///
/// assert_eq!(
///     abstraction,
///     Term::Lam(
///         VarName("x".to_string()),
///         Box::new(Term::Var(VarName("x".to_string())))
///     )
/// );
/// ```
pub fn lam(param: impl Into<String>, body: Term) -> Term {
    Lam(VarName(param.into()), Box::new(body))
}

/// Constructs a function application with the `lhs` term to be applied to the
/// `rhs` term.
///
/// This is a convenience function for constructing a [`Term`](enum.Term.html)
/// of variant `Term::App` in a readable form with minimal keystrokes. It takes
/// two `Term`s as its input and returns a `Term::App` with the first `Term` to
/// be applied to the second `Term`.
///
/// This function combined with the functions [`var`](fn.var.html) and
/// [`lam`](fn.lam.html) let us construct any [`Term`](enum.Term.html) in the
/// untyped lambda calculus.
///
/// # Examples
///
/// ```
/// # extern crate lamcal;
/// # use lamcal::{app, lam, var, Term, VarName};
/// let application = app(lam("x", var("x")), var("y"));
///
/// assert_eq!(
///     application,
///     Term::App(
///         Box::new(Term::Lam(
///             VarName("x".to_string()),
///             Box::new(Term::Var(VarName("x".to_string())))
///         )),
///         Box::new(Term::Var(VarName("y".to_string())))
///     )
/// );
/// ```
pub fn app(lhs: Term, rhs: Term) -> Term {
    App(Box::new(lhs), Box::new(rhs))
}

/// A term in the lambda calculus.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Term {
    /// A variable (x)
    ///
    /// A character or string representing a parameter or mathematical/logical
    /// value.
    Var(VarName),
    /// An abstraction (λx.M)
    ///
    /// Function definition (M is a lambda term). The variable x becomes bound
    /// in the expression.
    Lam(VarName, Box<Term>),
    /// An application (M N)
    ///
    /// Applying a function to an argument. M and N are lambda terms.
    App(Box<Term>, Box<Term>),
}

enum TermFormat<'a> {
    LParen,
    RParen,
    Space,
    ToFormat(&'a Term),
}

impl Display for Term {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::fmt::Write;
        use self::TermFormat::*;
        let mut to_format = Vec::with_capacity(7);
        to_format.push(ToFormat(self));
        while let Some(item) = to_format.pop() {
            match item {
                LParen => f.write_char('(')?,
                RParen => f.write_char(')')?,
                Space => f.write_char(' ')?,
                ToFormat(term) => match term {
                    Var(name) => f.write_str(&name)?,
                    Lam(param, body) => {
                        write!(f, "λ{}.", &param)?;
                        to_format.push(ToFormat(&**body));
                    },
                    App(lhs, rhs) => match (&**lhs, &**rhs) {
                        (App(_, _), App(_, _)) => {
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(LParen);
                            to_format.push(Space);
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**lhs));
                            to_format.push(LParen);
                        },
                        (Lam(_, _), App(_, _)) => {
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(LParen);
                            to_format.push(Space);
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**lhs));
                            to_format.push(LParen);
                        },
                        (Lam(_, _), Lam(_, _)) => {
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(LParen);
                            to_format.push(Space);
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**lhs));
                            to_format.push(LParen);
                        },
                        (_, App(_, _)) => {
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(LParen);
                            to_format.push(Space);
                            to_format.push(ToFormat(&**lhs));
                        },
                        (_, Lam(_, _)) => {
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(LParen);
                            to_format.push(Space);
                            to_format.push(ToFormat(&**lhs));
                        },
                        (Lam(_, _), _) => {
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(Space);
                            to_format.push(RParen);
                            to_format.push(ToFormat(&**lhs));
                            to_format.push(LParen);
                        },
                        (_, _) => {
                            to_format.push(ToFormat(&**rhs));
                            to_format.push(Space);
                            to_format.push(ToFormat(&**lhs));
                        },
                    },
                },
            }
        }
        Ok(())
    }
}

impl<'a> From<&'a Term> for Term {
    fn from(expr: &Term) -> Self {
        expr.to_owned()
    }
}

impl From<VarName> for Term {
    fn from(var: VarName) -> Self {
        Var(var)
    }
}

impl Term {
    /// Returns a set of references to all free variables in this term.
    pub fn free_vars(&self) -> HashSet<&VarName> {
        let mut free_vars = HashSet::with_capacity(4);
        let mut to_check: Vec<(&Term, HashSet<&VarName>)> = Vec::with_capacity(2);
        to_check.push((self, HashSet::with_capacity(4)));
        while let Some((term, mut bound_vars)) = to_check.pop() {
            match *term {
                Var(ref name) => {
                    if !bound_vars.contains(name) {
                        free_vars.insert(name);
                    }
                },
                Lam(ref param, ref body) => {
                    bound_vars.insert(param);
                    to_check.push((&*body, bound_vars));
                },
                App(ref lhs, ref rhs) => {
                    to_check.push((&*rhs, bound_vars.clone()));
                    to_check.push((&*lhs, bound_vars));
                },
            }
        }
        free_vars
    }
}

#[cfg(test)]
#[allow(dead_code)]
pub fn any_application() -> impl Strategy<Value = Term> {
    (any_term(), any_term()).prop_map(|(lhs, rhs)| App(lhs.into(), rhs.into()))
}

#[cfg(test)]
#[allow(dead_code)]
pub fn any_abstraction() -> impl Strategy<Value = Term> {
    (any_short_var_name(), any_term()).prop_map(|(param, body)| Lam(param, body.into()))
}

#[cfg(test)]
pub fn any_term() -> impl Strategy<Value = Term> {
    any_short_variable().prop_recursive(23, 500, 3, |inner| {
        prop_oneof!{
            inner.clone(),
            (any_short_var_name(), inner.clone()).prop_map(|(param, body)| Lam(param, body.into())),
            (inner.clone(), inner.clone()).prop_map(|(lhs, rhs)| App(lhs.into(), rhs.into())),
        }
    })
}

#[cfg(test)]
#[allow(dead_code)]
pub fn any_variable() -> impl Strategy<Value = Term> {
    any_var_name().prop_map(Var)
}

#[cfg(test)]
pub fn any_short_variable() -> impl Strategy<Value = Term> {
    any_short_var_name().prop_map(Var)
}

/// A name of a variable.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct VarName(pub String);

impl Display for VarName {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Deref for VarName {
    type Target = String;

    fn deref(&self) -> &<Self as Deref>::Target {
        &self.0
    }
}

impl DerefMut for VarName {
    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
        &mut self.0
    }
}

impl AsRef<str> for VarName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl VarName {
    /// Constructs a new variable name.
    pub fn new(name: impl Into<String>) -> Self {
        VarName(name.into())
    }

    /// Unwraps the `String` out of this variable name.
    pub fn unwrap(self) -> String {
        self.0
    }
}

#[cfg(test)]
pub fn any_var_name() -> impl Strategy<Value = VarName> {
    any_identifier_().prop_map(VarName)
}

#[cfg(test)]
pub fn any_short_var_name() -> impl Strategy<Value = VarName> {
    any_short_identifier_().prop_map(VarName)
}

#[cfg(test)]
pub fn any_identifier() -> impl Strategy<Value = String> {
    any_identifier_()
}

#[cfg(test)]
prop_compose! {
    fn any_identifier_()(
        name in "[A-Za-z0-9][A-Za-z0-9_']*",
    ) -> String {
        name
    }
}

#[cfg(test)]
prop_compose! {
    fn any_short_identifier_()(
        name in "[A-Za-z0-9][A-Za-z0-9_']?",
    ) -> String {
        name
    }
}

/// Constructs a `Term` from a sequence of function applications.
///
/// The app! macro can be used to conveniently construct a `Term` from a
/// sequence of function applications without all the nested calls of the `app`
/// functions which would be the alternative way.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate lamcal;
/// use lamcal::{app, lam, var};
///
/// # fn main() {
/// let expr = app![
///     lam("x", var("x")),
///     lam("y", app!(var("x"), var("y"))),
///     var("z")
/// ];
///
/// assert_eq!(
///     expr,
///     app(
///         app(lam("x", var("x")), lam("y", app(var("x"), var("y")))),
///         var("z")
///     )
/// );
/// # }
/// ```
#[macro_export]
macro_rules! app {
    ($term1:expr, $($term2:expr),+) => {
        {
            let mut __term = $term1;
            $(__term = $crate::Term::App(Box::new(__term), Box::new($term2));)*
            __term
        }
    }
}
//
//#[macro_export]
//macro_rules! lam {
//    ($var1:expr $(, $var2:expr)*, $term:expr) => {
//        {
//            use std::mem;
//            let mut __term = Term::Lam(Var($var1.into()),
// Term::Var(String::new()));            $(if let Term::Lam(_, mut ref body) =
// __term {                let mut __term2 = Term::Lam(Var($var2.into(),
// Term::Var(String::new())));                mem::swap(&mut **body, &mut
// __term2);            })*
//            if let Term::Lam(_, mut ref body) = __term {
//                mem::swap(&mut **body, &mut $term);
//            }
//            __term
//        }
//    }
//}

#[cfg(test)]
mod tests;