Function Repository Resource:

BinaryCombinator

Source Notebook

Perform bitwise operations through SK combinators

Contributed by: Austin Jiang

ResourceFunction["BinaryCombinator"][n, length]

converts the integer n into its representative combinator in binary form of length bits.

ResourceFunction["BinaryCombinator"][cmb]

converts the binary combinator cmb into the integer.

ResourceFunction["BinaryCombinator"][, cmb]

displays the binary combinator cmb as a string.

ResourceFunction["BinaryCombinator"][, length]

gives a combinator that can convert the binary combinator cmb of length bits into the Church numeral.

ResourceFunction["BinaryCombinator"][op, length]

gives a combinator corresponding to the operator op for binary numbers of length bits.

Details and Options

Church numerals are used in lambda calculus, which is a way of encoding primitive data types. ResourceFunction["ChurchCombinator"] is a related encoder.
In ResourceFunction["BinaryCombinator"][n, length], n must be a non-negative integer.
In ResourceFunction["BinaryCombinator"][n, length] and ResourceFunction["BinaryCombinator"][n, length], length must be a non-negative integer.
In ResourceFunction["BinaryCombinator"][op, length], op can be any of the operators BitNot, BitAnd, BitOr, BitXor, BitShiftLeft, BitShiftRight, Increment, Decrement, Plus, and Subtract.
BinaryCombinator can take the following option:
"SKGlyphs"{CombinatorS,CombinatorK}symbols used to specify combinators

Examples

Basic Examples (5) 

Generate a combinator that represent 5 in binary form:

In[1]:=
five = ResourceFunction["BinaryCombinator"][5, 3]
Out[1]=

Convert the combinator of 5 in binary form back into an integer:

In[2]:=
ResourceFunction["BinaryCombinator"][five]
Out[2]=

Display the combinator of 5 in binary form as a string:

In[3]:=
ResourceFunction["BinaryCombinator"][ToString, five]
Out[3]=

Convert the combinator of 5 in binary form into the Church numeral:

In[4]:=
ResourceFunction["BinaryCombinator"][ResourceFunction[
ResourceObject[<|"Name" -> "ChurchCombinator", "ShortName" -> "ChurchCombinator", "UUID" -> "0d655a5b-138f-4cea-9b61-d8f72754e2fb", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Evaluate the Church combinator", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$ca72fcb51a844aba9dc00f5c4e7fee33`ChurchCombinator", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/8a1dfea1-ec88-4b49-94fb-c38f6b7252be"]|>, ResourceSystemBase -> Automatic]], 3][five]
Out[4]=
In[5]:=
%[a][b] //. {CombinatorS[x_][y_][z_] -> x[z][y[z]], CombinatorK[x_][y_] -> x}
Out[5]=

Apply the operation BitNot to 5:

In[6]:=
ResourceFunction["BinaryCombinator"][BitNot, 3][
 ResourceFunction["BinaryCombinator"][5, 3]]
Out[6]=
In[7]:=
ResourceFunction["BinaryCombinator"][%]
Out[7]=

Scope (10) 

Bitwise-not a binary combinator:

In[8]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitNot, 3][
  ResourceFunction["BinaryCombinator"][5, 3]]]
Out[8]=

Bitwise-and two binary combinators:

In[9]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitAnd, 3][
   ResourceFunction["BinaryCombinator"][5, 3]][
  ResourceFunction["BinaryCombinator"][3, 3]]]
Out[9]=

Bitwise-or two binary combinators:

In[10]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitOr, 3][
   ResourceFunction["BinaryCombinator"][5, 3]][
  ResourceFunction["BinaryCombinator"][3, 3]]]
Out[10]=

Bitwise-xor two binary combinators:

In[11]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitXor, 3][
   ResourceFunction["BinaryCombinator"][5, 3]][
  ResourceFunction["BinaryCombinator"][3, 3]]]
Out[11]=

Left-shift a binary combinator:

In[12]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitShiftLeft, 4][
  ResourceFunction["BinaryCombinator"][5, 4]]]
Out[12]=

Right-shift a binary combinator:

In[13]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][BitShiftRight, 4][
  ResourceFunction["BinaryCombinator"][5, 4]]]
Out[13]=

Increment a binary combinator:

In[14]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][Increment, 3][
  ResourceFunction["BinaryCombinator"][5, 3]]]
Out[14]=

Decrement a binary combinator:

In[15]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][Decrement, 3][
  ResourceFunction["BinaryCombinator"][5, 3]]]
Out[15]=

Plus two binary combinators:

In[16]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][Plus, 4][
   ResourceFunction["BinaryCombinator"][5, 4]][
  ResourceFunction["BinaryCombinator"][6, 4]]]
Out[16]=

Subtract a binary combinator from another:

In[17]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][Subtract, 4][
   ResourceFunction["BinaryCombinator"][11, 4]][
  ResourceFunction["BinaryCombinator"][6, 4]]]
Out[17]=

Options (2) 

SKGlyphs (2) 

Compute a binary combinator using the default glyphs S and K:

In[18]:=
ResourceFunction["BinaryCombinator"][5, 3]
Out[18]=

Change the glyphs:

In[19]:=
ResourceFunction["BinaryCombinator"][5, 3, "SKGlyphs" -> {s, k}]
Out[19]=

Possible Issues (3) 

The number of bits for all binary combinators in an evaluation should be the same:

In[20]:=
ResourceFunction["BinaryCombinator"][ToString, ResourceFunction["BinaryCombinator"][BitNot, 6][
  ResourceFunction["BinaryCombinator"][3, 4]]]
Out[20]=

If the number of bits of a binary combinator is too small, the result may be incorrect, wrapping around and starting over (mod 2length):

In[21]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][8, 3]]
Out[21]=

Negative numbers resulting from subtraction will also wrap around:

In[22]:=
ResourceFunction["BinaryCombinator"][
 ResourceFunction["BinaryCombinator"][Subtract, 3][
   ResourceFunction["BinaryCombinator"][5, 3]][
  ResourceFunction["BinaryCombinator"][6, 3]]]
Out[22]=

Publisher

Austin Jiang

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 21 August 2024

Related Resources

Author Notes

This submission is an outcome of the author's 2024 WSRP project.

License Information