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
//! [Standard term]s and [combinator]s
//!
//! This module defines [standard term]s and [combinator]s with commonly
//! accepted names. A combinator is a closed lambda expression, meaning that it
//! has no free variables.
//!
//! The combinators are collected from these sources:
//!
//! * [SKI] system by Moses Schönfinkel and Haskell Curry
//! * [BCKW] system by Haskell Curry
//! * [fixed-point combinator]s by Haskell Curry
//! * the self-application combinator ω
//! * the divergent combinator Ω
//! * the reverse application (thrush) combinator
//!
//! The standard terms and combinators defined in this module are also
//! predefined named constants in the default environment which can be created
//! by calling `Environment::default()`.
//!
//! [standard term]: https://en.wikipedia.org/w/index.php?title=Lambda_calculus#Standard_terms
//! [combinator]: https://en.wikipedia.org/wiki/Combinatory_logic#Combinatory_calculi
//! [fixed-point combinator]: https://en.wikipedia.org/wiki/Fixed-point_combinator
//! [SKI]: https://en.wikipedia.org/wiki/SKI_combinator_calculus
//! [BCKW]: https://en.wikipedia.org/wiki/B,_C,_K,_W_system
//! [Iota]: https://en.wikipedia.org/wiki/Iota_and_Jot

#![allow(non_snake_case)]

use std::collections::HashSet;

use environment::Binding;
use term::{app, lam, var, Term};

/// Creates a set of bindings for all combinators implemented in this module.
pub fn default_bindings() -> HashSet<Binding> {
    binds! {
        I => I(),
        K => K(),
        S => S(),
        B => B(),
        C => C(),
        W => W(),
        ω => M(),
        M => M(),
        Ω => MM(),
        MM => MM(),
        T => T(),
        U => U(),
        V => V(),
        Y => Y(),
        Z => Z(),
        Θ => UU(),
        UU => UU(),
    }
}

/// Creates a set of bindings for all combinators of the [SKI] system.
///
/// The returned `HashSet` contains a binding for the combinators:
///
/// * S - Starling - Substitution
/// * K - Kestrel  - Constant
/// * I - Idiot    - Identity
///
/// [SKI]: https://en.wikipedia.org/wiki/SKI_combinator_calculus
pub fn ski_set() -> HashSet<Binding> {
    binds! {
        S => S(),
        K => K(),
        I => I(),
    }
}

/// I - Idiot - Identity combinator
///
/// I ≡ λa.a ≡ S K K
pub fn I() -> Term {
    lam("a", var("a"))
}

/// K - Kestrel - Constant combinator (TRUE)
///
/// K ≡ λab.a
pub fn K() -> Term {
    lam("a", lam("b", var("a")))
}

/// S - Starling - Substitution combinator
///
/// S ≡ λabc.ac(bc)
pub fn S() -> Term {
    lam(
        "a",
        lam(
            "b",
            lam("c", app![var("a"), var("c"), app(var("b"), var("c"))]),
        ),
    )
}

/// Creates a set of bindings for all combinators of the [BCKW] system.
///
/// The returned `HashSet` contains a binding for the combinators:
///
/// * B - Bluebird - Composition
/// * C - Cardinal - Swapping
/// * K - Kestrel  - Constant
/// * W - Warbler  - Duplication
///
/// [BCKW]: https://en.wikipedia.org/wiki/B,_C,_K,_W_system
pub fn bckw_set() -> HashSet<Binding> {
    binds! {
        B => B(),
        C => C(),
        K => K(),
        W => W(),
    }
}

/// B - Bluebird - Composition combinator
///
/// B ≡ λabc.a(bc) ≡ S (K S) K
pub fn B() -> Term {
    lam(
        "a",
        lam("b", lam("c", app(var("a"), app(var("b"), var("c"))))),
    )
}

/// C - Cardinal - Swapping combinator
///
/// C ≡ λabc.acb ≡ S (B B S) (K K)
pub fn C() -> Term {
    lam("a", lam("b", lam("c", app![var("a"), var("c"), var("b")])))
}

/// W - Warbler - Duplication combinator
///
/// W ≡ λab.abb ≡ C (B M R)
pub fn W() -> Term {
    lam("a", lam("b", app![var("a"), var("b"), var("b")]))
}

/// ω - M - Mockingbird - Self-application combinator
///
/// ω ≡ M ≡ λa.aa ≡ S I I
pub fn M() -> Term {
    lam("a", app![var("a"), var("a")])
}

/// Ω - Omega - Divergent combinator
///
/// Ω ≡ ω ω ≡ M M
pub fn MM() -> Term {
    app(
        lam("a", app![var("a"), var("a")]),
        lam("a", app![var("a"), var("a")]),
    )
}

/// T - Thrush - Reverse application combinator
///
/// T ≡ λab.ba ≡ C I
pub fn T() -> Term {
    lam("a", lam("b", app![var("b"), var("a")]))
}

/// U - Turing
///
/// U ≡ λab.b(aab) ≡ L O
pub fn U() -> Term {
    lam(
        "a",
        lam("b", app(var("b"), app![var("a"), var("a"), var("b")])),
    )
}

/// V - Vireo - Pairing combinator (PAIR)
///
/// V ≡ λabc.cab ≡ B C T
pub fn V() -> Term {
    lam("a", lam("b", lam("c", app![var("c"), var("a"), var("b")])))
}

/// Y - lazy fixed-point combinator
///
/// Y ≡ λf.(λa.f(aa))(λa.f(aa))
///
/// discovered by Haskell Curry.
pub fn Y() -> Term {
    lam(
        "f",
        app(
            lam("a", app(var("f"), app(var("a"), var("a")))),
            lam("a", app(var("f"), app(var("a"), var("a")))),
        ),
    )
}

/// Z - strict fixed-point combinator
///
/// Z ≡ λf.(λa.f(λb.aab))(λa.f(λb.aab))
pub fn Z() -> Term {
    lam(
        "f",
        app(
            lam(
                "a",
                app(var("f"), lam("b", app![var("a"), var("a"), var("b")])),
            ),
            lam(
                "a",
                app(var("f"), lam("b", app![var("a"), var("a"), var("b")])),
            ),
        ),
    )
}

/// Θ - Turing fixed-point combinator
///
/// Θ ≡ (λab.b(aab))(λab.b(aab)) ≡ U U
///
/// discovered by Alan Turing.
pub fn UU() -> Term {
    app(
        lam(
            "a",
            lam("b", app(var("b"), app![var("a"), var("a"), var("b")])),
        ),
        lam(
            "a",
            lam("b", app(var("b"), app![var("a"), var("a"), var("b")])),
        ),
    )
}