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
//! [Lambda terms](https://en.wikipedia.org/wiki/Lambda_calculus#Lambda_terms)

use self::Term::*;
use self::Error::*;
use std::fmt;
use std::borrow::Cow;

/// A lambda term that is either a variable with a De Bruijn index, an abstraction over a term or
/// an applicaction of one term to another.
#[derive(Debug, PartialEq, Clone)]
pub enum Term {
	Var(usize),
	Abs(Box<Term>),
	App(Box<Term>, Box<Term>)
}

/// An error that can be returned when an inapplicable function is applied to a term.
#[derive(Debug, PartialEq, Clone)]
pub enum Error {
	NotAVar,
	NotAnAbs,
	NotAnApp,
	NotANum,
	NotAPair,
	NotAList
}

impl Term {
	/// Applies self to another term.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	/// use lambda_calculus::arithmetic::{zero, succ};
	///
	/// assert_eq!(succ().app(zero()), App(Box::new(succ()), Box::new(zero())));
	/// ```
	pub fn app(self, argument: Term) -> Term { App(Box::new(self), Box::new(argument)) }

	/// Consumes a lambda variable and returns its De Bruijn index.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(1).unvar(), Ok(1));
	/// ```
	pub fn unvar(self) -> Result<usize, Error> {
		match self {
			Var(n) => Ok(n),
			_ => Err(NotAVar)
		}
	}

	/// Consumes an abstraction and returns its underlying term.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	/// use lambda_calculus::term::abs;
	///
	/// assert_eq!(abs(Var(1)).unabs(), Ok(Var(1)));
	/// ```
	pub fn unabs(self) -> Result<Term, Error> {
		match self {
			Abs(x) => Ok(*x),
			_ => Err(NotAnAbs)
		}
	}

	/// Returns a reference to an abstraction's underlying term.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	/// use lambda_calculus::term::abs;
	///
	/// assert_eq!(abs(Var(1)).unabs_ref(), Ok(&Var(1)));
	/// ```
	pub fn unabs_ref(&self) -> Result<&Term, Error> {
		match *self {
			Abs(ref x) => Ok(&*x),
			_ => Err(NotAnAbs)
		}
	}

	/// Returns a mutable reference to an abstraction's underlying term.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	/// use lambda_calculus::term::abs;
	///
	/// assert_eq!(abs(Var(1)).unabs_ref_mut(), Ok(&mut Var(1)));
	/// ```
	pub fn unabs_ref_mut(&mut self) -> Result<&mut Term, Error> {
		match *self {
			Abs(ref mut x) => Ok(&mut *x),
			_ => Err(NotAnAbs)
		}
	}

	/// Consumes an application and returns a pair containing its underlying terms.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).unapp(), Ok((Var(0), Var(1))));
	/// ```
	pub fn unapp(self) -> Result<(Term, Term), Error> {
		match self {
			App(lhs, rhs) => Ok((*lhs, *rhs)),
			_ => Err(NotAnApp)
		}
	}

	/// Returns a pair containing references to an application's underlying terms.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).unapp_ref(), Ok((&Var(0), &Var(1))));
	/// ```
	pub fn unapp_ref(&self) -> Result<(&Term, &Term), Error> {
		match *self {
			App(ref lhs, ref rhs) => Ok((&*lhs, &*rhs)),
			_ => Err(NotAnApp)
		}
	}

	/// Returns a pair containing mutable references to an application's underlying terms.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).unapp_ref_mut(), Ok((&mut Var(0), &mut Var(1))));
	/// ```
	pub fn unapp_ref_mut(&mut self) -> Result<(&mut Term, &mut Term), Error> {
		match *self {
			App(ref mut lhs, ref mut rhs) => Ok((&mut *lhs, &mut *rhs)),
			_ => Err(NotAnApp)
		}
	}

	/// Returns the left-hand side term of an application. Consumes self.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).lhs(), Ok(Var(0)));
	/// ```
	pub fn lhs(self) -> Result<Term, Error> {
		if let Ok((lhs, _)) = self.unapp() { Ok(lhs) } else { Err(NotAnApp) }
	}

	/// Returns a reference to the left-hand side term of an application.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).lhs_ref(), Ok(&Var(0)));
	/// ```
	pub fn lhs_ref(&self) -> Result<&Term, Error> {
		if let Ok((lhs, _)) = self.unapp_ref() { Ok(lhs) } else { Err(NotAnApp) }
	}

	/// Returns a mutable reference to the left-hand side term of an application.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).lhs_ref_mut(), Ok(&mut Var(0)));
	/// ```
	pub fn lhs_ref_mut(&mut self) -> Result<&mut Term, Error> {
		if let Ok((lhs, _)) = self.unapp_ref_mut() { Ok(lhs) } else { Err(NotAnApp) }
	}

	/// Returns the right-hand side term of an application. Consumes self.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).rhs(), Ok(Var(1)));
	/// ```
	pub fn rhs(self) -> Result<Term, Error> {
		if let Ok((_, rhs)) = self.unapp() { Ok(rhs) } else { Err(NotAnApp) }
	}

	/// Returns a reference to the right-hand side term of an application.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).rhs_ref(), Ok(&Var(1)));
	/// ```
	pub fn rhs_ref(&self) -> Result<&Term, Error> {
		if let Ok((_, rhs)) = self.unapp_ref() { Ok(rhs) } else { Err(NotAnApp) }
	}

	/// Returns a mutable reference to the right-hand side term of an application.
	///
	/// # Example
	/// ```
	/// use lambda_calculus::term::Term::*;
	///
	/// assert_eq!(Var(0).app(Var(1)).rhs_ref_mut(), Ok(&mut Var(1)));
	/// ```
	pub fn rhs_ref_mut(&mut self) -> Result<&mut Term, Error> {
		if let Ok((_, rhs)) = self.unapp_ref_mut() { Ok(rhs) } else { Err(NotAnApp) }
	}
}

/// Wraps a term in an abstraction. Consumes its argument.
///
/// # Example
/// ```
/// use lambda_calculus::term::Term::*;
/// use lambda_calculus::term::abs;
///
/// assert_eq!(abs(Var(1)), Abs(Box::new(Var(1))));
/// ```
pub fn abs(term: Term) -> Term { Abs(Box::new(term)) }

/// Produces an application of its arguments, consuming them in the process.
///
/// # Example
/// ```
/// use lambda_calculus::term::Term::*;
/// use lambda_calculus::term::app;
///
/// assert_eq!(app(Var(0), Var(1)), App(Box::new(Var(0)), Box::new(Var(1))));
/// ```
pub fn app(lhs: Term, rhs: Term) -> Term { App(Box::new(lhs), Box::new(rhs)) }

fn parenthesize_if(condition: bool, input: &str) -> Cow<str> {
	if condition {
		format!("({})", input).into()
	} else {
		input.into()
	}
}

/// Set to `true` for λ or `false` for \ when displaying lambda terms. The default is `true`.
pub const DISPLAY_PRETTY: bool = true;

fn show_precedence(context_precedence: usize, term: &Term) -> String {
	match *term {
		Var(i) => format!("{:X}", i), // max. index = 15
		Abs(ref t) => {
			let ret = format!("{}{}", if DISPLAY_PRETTY { 'λ' } else { '\\' }, t);
			parenthesize_if(context_precedence > 1, &ret).into()
		},
		App(ref t1, ref t2) => {
			let ret = format!("{}{}", show_precedence(2, t1), show_precedence(3, t2));
			parenthesize_if(context_precedence == 3, &ret).into()
		}
	}
}

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

#[cfg(test)]
mod test {
	use arithmetic::*;

	#[test]
	fn displaying_terms() {
		assert_eq!(&format!("{}", zero()), "λλ1");
		assert_eq!(&format!("{}", succ()), "λλλ2(321)");
		assert_eq!(&format!("{}", pred()), "λλλ3(λλ1(24))(λ2)(λ1)");
	}
}