Code: Select all
; Description
Text := "
(
I(x) - Identity. Returns x.
K(x, y) - K-combinator. Returns the left value x.
S(x, y, z) - Substitution. Returns x(z) applied to y(z).
Double(x) - Returns xx. Defined as SII. Use M(x).
Identity(x) - Can be redefined as SKK. Use I(x).
Reverse(x, y) - Turns xy into yx. Defined as S(K(SI))K. Use T(x)(y).
M := S(I)(I)
I := S(K)(K)
T := S(K(S(I)))(K)
Edit the bottom of the script file.
eval := S(I)(I)(x)
)"
; String
''.base.call := (p1, p2:="") => p1 . (p2 ? p2.ToString() : "")
''.base.ToString := (a) => a
; Functions
I(x:=unset) {
if !IsSet(&x)
return I
return x
}
I.ToString := (*) => 'I'
K(x:=unset, y:=unset) {
if !IsSet(&x) && !IsSet(&y)
return K
K1 := (y) => K(x, y)
K1.ToString := (*) => 'K' . x.ToString()
if !IsSet(&y)
return K1
return x
}
K.ToString := (*) => 'K'
S(x:=unset, y:=unset, z:=unset) {
if !IsSet(&x) && !IsSet(&y) && !IsSet(&z)
return S
S2 := (y, z:=unset) => IsSet(&z) ? S(x, y, z) : S(x, y)
S2.ToString := (*) => 'S' . x.ToString()
if !IsSet(&y) && !IsSet(&z)
return S2
S1 := (z) => S(x, y, z)
S1.ToString := (*) => 'S' . x.ToString() . y.ToString()
if !IsSet(&z)
return S1
return x(z)(y(z))
}
S.ToString := (*) => 'S'
; Constants
x := (a:="") => ('x' . ((a) ? a() : ""))
x.ToString := (*) => 'x'
y := (a:="") => ('y' . ((a) ? a() : ""))
y.ToString := (*) => 'y'
z := (a:="") => ('z' . ((a) ? a() : ""))
z.ToString := (*) => 'z'
; Eval
M := S(I)(I)
T := S(K(S(I)))(K)
B := S(K(S))(K) ; Turns B(x)(y)(z) into x(y(z)).
C := S(S(K(S(K(S))(K)))(S))(K(K)) ; Turns C(x)(y)(z) into (x)(z)(y).
W := S(S)(S(K)) ; Turns W(x)(y) into (x)(y)(y).
; B, C, K, W represent an earlier system of combinatorial logic.
MsgBox(Text, "SKI Calculus is a turing complete language.")
eval := S(I)(I)(x)
MsgBox eval.ToString()
I first learned of this concept when I was looking at how to swap two variables in Java. Java only passes parameters by value so a simple swap(x,y) has no effect on the original x and y arguments. So the only alternative is to use a K-Combinator.
Code: Select all
int swap(int a, int b) { // usage: y = swap(x, x=y);
return a;
}
y = swap(x, x=y);