tree-calculus.definitions
Exploring Tree Calculus discovered by Professor Barry Jay.
See Reflective Programs in Tree Calculus (2001) and Johannes Bader’s Tree Calculus website.
This Hacker News post has many useful comments, as is Timur Latypoff’s visualization.
And
Logical conjunction. Returns True if and only if both operands are True
.
(And True True) ;; => True
(And False True) ;; => False
Append
(Append xs ys)
Append list xs
to the head of list ys
.
(Append (List x y z) (List a b c)) ;; => (List x y z a b c)
Apply
(Apply L R)
Apply tree left-hand tree L
to right-hand tree R
. L
and R
are “alternate function invoke” vectors, which, when at the head of an evaluated S-expression, invoke a custom function.
Following reductions listed at Barry Jay’s Reflective Programs in Tree Calculus (2021, pages 5 and 28).
Rules:
- Rule 0a:
Δ ␣b → Δb
- Rule 0b:
Δa ␣b → Δab
- Rule 1:
ΔΔ y␣z → y
Kernel Rule, or K-Rule - Rule 2:
Δ(Δx) y␣z → (y␣z)␣(x␣z)
Stem Rule, or S-Rule - Rule 3:
Δ(Δwx)y␣z → (z␣w)␣x
Fork Rule, or F-Rule
Δ is the tree operator, which represents a node in an unlabelled, binary tree.
a
, b
, w
, x
, y
, z
are arbitrary sub-trees. The letters convey no semantics, but are chosen to help track of their original position.
␣
is the space operator, which represents application of one tree to another.
Note the structural progression. Rules 0a and 0b concern applying a plain leaf and plain stem, respectively. Rules 1, 2, and 3 concern applying a fork whose left value is a plain leaf, plain stem, or fork, respectively.
B
B combinator. When applied to three arguments, subverts the left-associative application ordering.
(B x y z) ;; => (x (y z))
BF
Branch-First self-evaluator. Apply x
to y
using a strategy that evaluates all branches before evaluating the root.
(BF Not False) ;; => True
Bite
(Bite b0 b1 b2 b3 b4 b5 b6 b7)
Given eight tree calculus bits b0
to b7
, returns a tree calculus byte.
Note: Convert Clojure/Java bits with (map bit->Bit bits)
D
D combinator. When applied to three arguments, reverses order of of first two arguments and broadcasts their application to the third argument.
(D x y z) ;; => ((y z) (x z))
See also d.
d
(d x)
Shortcut function for D combinator. The D combinator applied to the first, x
, of its three arguments always reduces to (Δ (Δ x))
.
(D x y z) ;; => ((Δ (Δ x)) y z)
is equivalent to
((d x) y z)
See page 29 of Reflective Programs in Tree Calculus.
Equal?-Variant
error
Value returned when Type-Check determines that two tags fail to conform. See also Typed-App.
Extension
(Extension p s r)
Pattern matching. Returns a function accepting a single argument x
, that when applied to x
returns various patterns below. Extension
requires a pattern p
, body s
, and a ‘always-applied’ functions r
.
Use when you have a pattern (Δ :a :b)
and a test (Δ :c :d)
, and want to ask What is the thing in test
at the same location as :a
in pattern
? Answer: :c
.
See unit tests for example usage.
Extension-2
(Extension-2 p s r)
Variant of Extension that uses native tree calculus predicates, i.e., does not rely on Clojure host’s cond
itional form.
False
Logical false. Opposite of True.
(Not False) => True
In the operator position, returns the second of two arguments.
First
(First pair)
Fold-Left
(Fold-Left f x y)
Apply a function f
to an initial value x
and the first element of a list y
, then apply the function to that result and next element of the list, etc.
(Fold-Left Divide sixty (List three four)) ;; => five
Fold-Right
(Fold-Right f x y)
Apply function f
to an initial value x
and a value obtained by applying f
to the first element of list y
and the value obtained by applying f
to the second element, etc.
(Fold-Right Divide one (List sixty one-hundred-twenty four)) ;; => two
Fork?
Fork?-2
When argument is a:
- leaf, returns
(K K)
- stem of form
(Δ x)
, returns(K (Δ x (K (K Δ))))
- fork, returns
Δ
.
Used by Triage.
Get-Tag
I
I combinator. When applied to a single argument, x
, returns exactly x
, i.e., its identity.
(I x) ;; => x
Iff
Biconditional. Returns True if both operands are True
or both operands are False
.
(Iff True True) ;; => True
(Iff True False) ;; => False
(Iff False True) ;; => False
(Iff False False) ;; => True
Implies
Material implication. Returns True if and only if first operand is True
and the second operand is False
.
(Implies True False) ;; => True
(Implies True True) ;; => False
(Implies True False) ;; => False
(Implies False False) ;; => False
K
K combinator. When supplied with two arguments, returns the first, discarding the second.
(K x y) ;; => x
Leaf?
Map
Apply a function to each element of a list.
(Map Successor (List x y z)) ;; => (List (Successor x) (Successor y) (Successor z))
Not
Logical negation. Returns opposite Boolean.
(Not True) ;; => False
(Not False) ;; => True
Or
Logical disjunction. Returns True if either of two operands are True
.
(Or True False) ;; => True
(Or False False) ;; => False
Pair
(Pair x y)
Predecessor
Quote
RB
Root-Branch self-evaluator. Performs root evaluation then recursively evaluates the branches to produce a normal form. Consumes a single tree, a fork whose branches are each Quoted.
(RB (Δ (Quote Not) (Quote False))) ;; => True
RF
(RF f z)
Root-First self-evaluator. Applies f
to z
, quotation implicit.
(RF Not False) ;; => True
Root
Root self-evaluator. Apply m
to n
, doing only enough computation to determine the nature of the root. Requires Quote-ing the arguments.
Relationship between the input and output are not straightforward. E.g.,
(Root (Δ (Quote (Δ y)) (Quote z))) ;; => (Δ (Quote y) (Quote z))
S
S combinator. When applied to three arguments, broadcasts the third argument to each of the first two arguments.
(S x y z) ;; => ((x z) (y z))
Second
(Second pair)
Size-Variant
Stem?
Stem?-2
Returns Δ
if argument is a leaf or a fork, or returns (Δ (K (K Δ)) (x (K (K Δ))))
for a stem of form (Δ x)
. Used by Triage.
Successor
T-Cons
(T-Cons h t)
Construct a list by appending h
to list t
.
(T-Cons h (List t1 t2 t3)) ;; => (h t1 t2 t3)
Tag
(Tag t f)
Triage
(Triage f0 f1 f2)
Applies a specified function, f
n, depending on whether argument is a leaf, stem, or fork.
If argument is a:
- leaf
Δ
, returns(f0)
- stem
(Δ x)
, returns(f1 x)
- fork
(Δ x y)
, returns(f2 x y)
True
Logical true. Opposite of False.
(Not True) ;; => False
In the operator position, returns the first of two arguments.
Type-Check
(Type-Check x u)
Typed-App
(Typed-App f x)
Typed application. Apply tagged function f
to tagged argument x
, returning a tagged value.
(Typed-App (Tag True Successor) (Tag True n)) ;; => (Tag true (+ n 1))
Y-t
(Y-t t f)
Tagged Y combinator. Apply tag t
to fixpoint function f
.
(Y-t t f) ;; => (f (Y-t t f))
Tags can not be added after the fixpoint has been formed, so the usual Y combinator can not be used.
Zero?
Δ
Node operator. Both a value and a function. By itself, equivalent to an empty Clojure vector. When at the head of an evaluated list, behaves as a function, invokes Apply, the tree calculus application rules.
(Δ) ;; => Δ, or equivalently []
(Δ x) ;; returns a stem of x
(Δ x y) ;; returns a fork of x and y```