 # An impure solution to the problem of matching fans

We present an algorithm to solve the problem of matching fans in interaction net implementations of optimal reduction for the pure untyped lambda calculus without use of any additional agent types. The algorithm relies upon a specific interaction nets reduction strategy and involves side effects in one of interaction rules.

## Code Repositories

### lambda

Macro Lambda Calculus

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Problem

Matching fans is the main problem of implementation of optimal reduction in interaction nets [optimal]. Existing solutions use so-called oracle which is implemented using bracket and croissant agents in BOHM [optimal] and delimiter agents in Lambdascope [lambdascope].

Aiming to eliminate the overhead due to the oracle, we decided to limit our signature to the basic types only (abstraction, application, erase, and fan) and try to achieve the same behavior of fans as if the oracle were still present.

This paper continues our previous work towards optimal reduction without oracle [orwoo]. Specifically, 1) we use the idea of identities attached to fans from [orwoo], 2) restrict the interaction nets reduction strategy to needed reduction [neededred]

, and also 3) allow side effects in one of interaction rules. The main idea is to memorize identities of two different fans at the moment of their first interaction. It has led us to an impure solution we present in this paper.

The presented solution has been implemented in software and is available in the MLC package at https://www.npmjs.com/package/@alexo/lambda (version 0.5.0) where it exists as the abstract encoding which is the default algorithm.

## 2 Solution

We work in the version of interaction calculus described in [neededred].

The signature of our interaction system is

 Σ={p, @, λ, ε}∪{δi | i∈N}∪{aM | M∈Λ}∪{rC[M] | C[M] is a context}.

Intuitively, one can think of in as this agent’s identity. Agents , , and are part of the embedded read-back mechanism described in [termgraph, Section 7].

The interaction rules are as follows:

 rC[M][x] ⋈λ[ay, rC[λy.[M]](x)],where y is fresh; @[x, y] ⋈λ[x, y]; @[rM [M](x), x] ⋈aM; rC[M][aC[M]] ⋈aM; rC[M][δi(x, y)] ⋈δi[rC[M](x), rC[M](y)]; δi[aM, aM] ⋈aM; δi[λ(x, y), λ(v, w)] ⋈λ[δi(x, v), δi(y, w)]; δi[@(x, y), @(v, w)] ⋈@[δi(x, v), δi(y, w)]; δi[x, y] ⋈δi[x, y].

Here, we omit the interaction rule and will define it later explicitly extending reduction relation as it is this interaction rule that involves side effects.

We define a state as

 c/(φ, n),

where is a configuration as in [neededred], , and .

The reduction relation on states is defined in two steps:

1. If as in [neededred], then

 c1/(φ, n)→c2/(φ, n).
2. If , then

 ⟨ !δi(t1, t2)=δj(u1, u2), Δ⟩/(φ, n)→ ⟨ t1=δφ′(j, i)(x, y), t2=δj(v, w), u1=δφ(i, j)(x, v), u2=δi(y, w), Δ⟩/(φ′, n′),

where and if , or

 φ′ =φ∪{(i, j, n′), (j, i, j)}and n′ =n+1

if .

(In MLC, is implemented using a hash table. As long as the latter’s search and insert operations are , the cost of the reduction remains , thus keeping number of reductions an adequate measure of efficiency.)

In order to encode a -term into a state, we first need to distinguish free variables from bound variables in . We do so by marking all free variables using the following operation: , where .

Then the following state is the initial encoding of a -term :

 [M]=⟨r[M](!p)=x, Γn(M∙, x)⟩/(∅, n),

where is the result of enumerating all occurrences in as , and

 Γ(x∙, y) ={ax=y}; Γ(x, y) ={x=y}; Γ(λx.M, y) ={y=λ(ε, z)}∪Γ(M, z),when % x∉FV(M); Γ(λx.M, y) ={y=λ(x, z)}∪Γ(M, z),when x∈%FV(M); Γ(M N, x) ={y=@(x, z)}∪Γ(M[→t:=→t′], y)∪Γ(N[→t:=→t′′], z)∪Ψ(→t),where Ψ(→t) ={ti=δ(t′′i, t′i) | ti∈(→t)}and(→t)=FV(M)∩FV(N).

The algorithm is correct if the following conjecture holds true for any -term .

###### Conjecture.

iff is the normal form of .

## 3 Conclusion

Our impure solution to the problem of matching fans makes use of the mechanism to track identities of fans suggested in [orwoo]. We get rid of the naïve level-tracking part and utilize needed reduction [neededred], blocking previously found counterexamples at the cost of side effects.

We believe that further work should be directed towards a pure version of the presented solution in order to investigate its properties and correctness.