SKI combinator interpreter

Post your working scripts, libraries and tools.
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

SKI combinator interpreter

08 Mar 2021, 17:32

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()
SKI Calculus is a simple programming concept. This uses partial functions, i.e. given fn(x, y), fn(x) returns a partially applied function that can then be given the final argument y to return.

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);
https://stackoverflow.com/a/20600020/5978864
Last edited by iseahound on 08 Mar 2021, 23:01, edited 10 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: SKI calculus interpreter

08 Mar 2021, 20:19

cool. thanks for sharing
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: SKI combinator interpreter

08 Mar 2021, 23:02

I did some more research and stumbled upon this page that lists all the different combinators as birds:

https://www.angelfire.com/tx4/cus/combinator/birds.html

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 16 guests