Stck.Console handles loading of external STCK-code. You could e.g. load the standard library by using:
load Stck/stdlib.stck
rot -> [[] swap << swap << swap >> app] rot #
over -> [swap dup rot rot] over #
2dup -> [over over] 2dup #
Booleans uses the classic lambda calculus encoding.
true = λa . λb . a / false = λa . λb . b
true -> [[.]] true #
false -> [[swap .]] false #
From there we can define boolean operators.
if = λp . λt . λe . p t e
if -> [rot app app] ? #
not -> [false true rot app] not #
and -> [dup app] and #
or -> [not swap not and not] or #
xor -> [2dup not swap not and rot rot and or not] xor #
We can compare booleans with implication and equivalence.
Left implication -> [not or] <- #
Right implication -> [swap <-] -> #
Equivalence -> [2dup -> rot rot <- and] <-> #
Numerals is also encoded using Church encoding. We start with the successor function.
successor = (n) -> (f) -> (x) -> f(n(f)(x))
pick -> [swap dup rot swap ||] pick #
succ -> [| [pick] rot || ||] succ #
Then we can define some numbers.
[[[.] app]] 0 #[0 succ] 1 #[1 succ] 2 #[2 succ] 3 #[3 succ] 4 #[4 succ] 5 #[5 succ] 6 #[6 succ] 7 #[7 succ] 8 #[8 succ] 9 #[9 succ] 10 #[10 10 *] 100 #[100 10 *] 1000 #[1000 1000 *] 1M #
We can multiply and add the numbers together.
multiplication -> [[swap rot swap [app] swap << swap << swap app] swap << swap <<] * #
addition -> [[app] swap << swap [app] swap << [rot dup rot swap << rot rot << || app] swap << swap <<] + #
Then for the tricky part, defining the predecessor function. The general ide is to group together a number and boolean.
pred-first -> [0 false] pred-first #
Then you want a function that increments the number so it's one less than the number of times the function has been called. It goes something like this:
pred-first->0 falsepred-first pred-next->0 truepred-first pred-next pred-next->1 true- ...
pred-next -> [[succ true] [true] ?] pred-next #
Now we can construct the predecessor function by applying the number to pred-first and pred-next, and then drop the boolean at the end.
pred -> [pred-first rot [pred-next] swap app .] pred #
With a predecessor function we can define subtraction.
subtraction -> [[pred] swap app] - #
We might also want some predicates to convert numbers to booleans.
is-zero -> [true [. false] rot app] is-zero #
less-or-equal -> [swap - is-zero] <= #
greater-or-equal -> [- is-zero] >= #
equal -> [2dup >= rot rot <= and] = #
Finally we can make a remainder/modulo operation.
remainder -> [2dup <= [dup rot swap - swap %] [.] ?] % #
You should probably not use these operators before they're out of beta...
error -> [err app] error #
empty -> [emp app] empty #
clear -> [empty [] [. clear] ?] clear #