or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
Concatenative Combinators abstraction algorithm
Abstract
In [1], Kerby discuss abstraction by converting expressions containg concatenative combinators to lambda expressions with variables and then proceeds by abstracting variables from the lambda expressions.
This article follows a more direct approach and provides abstraction rules that are aimed to make the algorithm easier to apply and implement.
Even if we are mostly interested in showing that such algorithm exists we have made an effort to choose a base of combinators that will keep the resulting expression as short as possible (abstraction usually leads to very long and complex expressions). Further improvements can be done while implementing the algorithm, but we won't discuss them here.
Expressions
The language of concatenative combinators is defined by the following EBNF:
That can be summarized as:
()
is a termNote that variables are notated with a different set of symbols.
Some examples:
The evaluation of an expression proceeds from left to right by finding the first combinator that can be successfully reduced (i.e. the one that has enough quotes at its left) and replacing it and its arguments with the result of its application to the arguments.
We assume that, as in classical Combinatory Logic, the Church-Rosser theorem holds. While not a real proof, at the end of this paper we'll provide a reasoning that reassure us on the validity of the Church-Rosser theorem.
Concatenative Combinators
Let's use an intuitive definition of what a concatenative combinator is:
The behaviour of a combinator is defined as an equivalence like this:
You can interpret the definition of
swap
above by saying that in any given expression you can replace any occurence of:with:
For the sake of reasoning (and as a possible strategy of implementation) we can think of a combinator as a program that operates on a stack:
(Non minimal) Base for concatenative combinators
To simplify the abstraction algorithm we'll use the following combinators:
which are a superset of the bases defined in [1] and, hence, ensure that every possible expression can be represented using those combinators.
We'll extend the concept of combinator defintions by allowing a combinator to add other elements that were not in the original arguments.
For example:
Abstraction
The abstraction of an expression
𝓕
with respect to the variable𝑥
is denoted with{𝓕}[(𝑥)]
.The result of the abstraction of the variable
𝑥
from the expression𝓕
is an expression𝓖
that does not contain𝑥
and that, when applied to(𝑥)
. will return𝓕
:In a sense, abstraction is similar to compilation. Given an expression
𝓕
containing a variable𝑥
(the source code) we can see{𝓕}[(𝑥)]
as a program that when applied to a quoted expression(𝓡)
will result in the original expression where any occurence of the variable𝑥
is replaced by𝓡
.When abstracting multiple variable we'll have:
In other words, we first abstract wrt
𝑥
, then𝑦
and the result𝓖
is such that(𝑦) (𝑥) 𝓖 = 𝓕
.Note that abstraction is defined with respect to a quoted variable as concatenative combinators are defined to only operate on quotes.
Abstraction rules
We'll use the following definitions:
𝑥
is a generic variable𝓖
is a non empty expression that does not contain𝑥
(i.e.
𝑥
does not occur in𝓖
)𝓜
is an expressions that may contain𝑥
(
𝑥
may occur in𝓜
)𝓝
is an expressions that do contain𝑥
(
𝑥
occurs in𝓝
)Given an expression, looking at the list of terms from left to right there can only be the following cases:
The expression is empty;
The expression can be split in two parts with first terms containing the variable
𝑥
followed by other terms not containing𝑥
;The expression can be split in two parts with first terms not containing the variable
𝑥
followed by other terms containing𝑥
;The first term is a quote containing
𝑥
(otherwise it would have been accounted for in case 3) followed by an expression containing𝑥
(otherwise it would have been accounted for in case 0);The first term is the variable
𝑥
(unquoted).This leads to the following abstraction rules:
Note that the expressions on the right side can't be reduced further without being applied to a quote.
It's easy to prove, by induction on the length of the expressions, that the algorithm converges: at each step the expressions to be abstracted become smaller and smaller.
In the following subsection we'll show that the rules do hold by applying them to
(𝑥)
and checking that the result is, indeed, the original expression.Rule 1
This is the base case for when the expression is empty.
Let's check that applying the result to (𝑥) we got the empty expression:
Rule 2
This rule allows us to stop earlier in the abstraction process: trailing terms not containing
𝑥
can be left untouched.This is implied by the fact that the combinators are concatenative.
Let's check that rule
2
holds:Rule 3
This rule is to be applied when the expression consists of a list of terms which do not contain
𝑥
followed by a list of terms which contain𝑥
.To prove that this rule holds, let's apply it to
(𝑥)
and check that the result is𝓖 𝓝
.Rule 4
This rule is to be applied when the expression consist of a quote that contains
𝑥
followed by a list of terms which contain𝑥
(if they didn't we would have used rule 0).Let's apply it to
(𝑥)
:Rule 5
This is the rule to apply when the first term is
𝑥
.To show that rules
5
holds, let's apply it to(𝑥)
:Optimization
The only optimization we mention here is the possibility of simplifying some special cases:
They can be easily checked as we did in the previous section.
Note that those special cases are included in the general case when one of the expressions is empty or has a special form.
Let's give just one example that shows that rule
4b
is implied in rule4
when the expression 𝓜 is empty.It's easy to see that, under the assumption of 𝓜 being empty, the two are equivalent:
Quotes
As said at the beginning. combinators only operate on quotes.
This is needed since (as in CL and 𝜆-calculus) there is no distinction between functions (programs) and data. Quotes are what make this distinction.
Note that we have assumed that quotes are transparent. i.e. that reductions may happen within a quote. This has the advantage that all expressions are reduced to their minimal form but also has the disadvantage of having to compute any single quotes even if they might be discarded at a later time. Opaque quotes allow for lazy evaluation and the content of a quote is evaluated only when (and if) it is really needed, not before.
From the abstraction algorithm there is no difference as the two type of quotes are equivalent.
The Church-Rosser theorem
The Church-Rosser theorem, plays a key role in the evaluation of an expression and the fact that it holds for concatenative combinators is an assumption for the abstraction algorithm to work. It is also essential for the discussion on transparent/opaque quotes in the preceding section.
It can be formulated in many different ways, this is one of them:
In plain words, if an expression 𝓤 can be reduced to two different expressions 𝓧 and 𝓨, then there is an expression 𝓩 to whom both 𝓧 and 𝓨 can be reduced.
Which means that the strategy of reductions is irrelevant as any of them will lead to the same expression 𝓩.
While the general proof of this theorem is quite complex, in our specific case (concatenative combinators that only act on quotes) it's pretty straightforwad to convince ourselves that the theorem holds.
Let's consider the following expression:
where
A
andB
are combinators and𝓊
and𝓋
are generic expressions.The only interesting case is when both of them can be reduced (i.e. they both are a redex). Let's consider one step of reduction.
If we reduce
(𝓊) A
, there will be no consequence on(𝓋) B
, since it is already a redex.If we reduce
(𝓋) B
the result can be:(𝓈) (𝓉) C
.The resulting expression
(𝓊) A (𝓈) (𝓉) C
now contains only one redex ((𝓊) A
) becase the combinatorC
only operates on quotes andA
is unquoted.This reasoning can be repeated for a more general case but it's easy to see that the redex in an expressions to do not interfere with each other and, hence, the order in which they are reduced is irrelevant for the end result.
Conclusion
We have defined an abstraction algorithm for Concatenative combinators that is simple enough to be implemented and even being applied by hand
This frees us from the need of handling variables when using Concatenative Combinators.
We have also argumented around the validity of the Church-Rosser theorem that is an assumption for the abstraction algorithm to work.
Here are the list of all the abstraction rules (including the special cases):
Bibliography
[1] The Theory of Concatenative Combinators,
Brent Kerby (bkerby at byu dot net).
Completed June 19, 2002. Updated February 5, 2007.
(link)
[2] Lambda-Calculus and Combinators, an introduction,
J. Roger Hindley, Jonathan P. Seldin
(link)