# Derived-term Automata for Extended Weighted Rational Expressions¶

This page is a complement to Derived-term Automata for Extended Weighted Rational Expressions presented at ICTAC 2016. This page exists in several forms:

You may change the cells, and run then. To run a cell in a notebook, hit "Control-Enter" (in which case the focus stays on the same cell) or "Shift-Enter" (focus goes to the next cell). Beware that depending on the requested operations, Vcsn may generate and compile code, which may be a really slow process on small machines (about a minute): be patient! However, the code is compiled only once: successive uses will be way faster.

To run all the cells anew, select "Restart & Run All" in the "Kernel" menu above.

In :
import vcsn


## Valid Expressions¶

Beware that the so-called ''context'' (which, in Vcsn parlance denotes the type of labels (letters or words, etc.) and the semiring of weights) has an extreme importance. In particular, an expression might be valid in a given context, and invalid in another.

As an example, ${a^*}^*$ is valid in $\mathbb{B}$, so we can build its derived-term automaton.

In :
e = vcsn.B.expression('a**'); e.is_valid()

Out:
True
In :
e.derived_term()

Out:

However, the construction will fail in $\mathbb{Q}$, since the constant term for $a^*$ is 1, which is not starrable in $\mathbb{Q}$ (indeed, $1^* = \sum_{k \in \mathbb{N}} 1^k$ is not a value in $\mathbb{Q}$).

In :
e = vcsn.Q.expression('a**'); e.is_valid()

Out:
False
In :
try:
e.derived_term()
except RuntimeError as err:
print("error:", err)

error: Q: value is not starrable: 1
while computing expansion of: a**
while computing derived-term of: a**


## Example $\mathsf{E}_3$: A Lex-like Scanner¶

### Desugaring¶

We introduce the "context" (i.e., the automaton type) that corresponds to labels that are letters (lal_char) and weights that are rational numbers (q).

In :
Q = vcsn.context('lal_char(ab), q')
Q

Out:
$\{a, b\}\to\mathbb{Q}$

Vcsn supports the <+ operator. It is desugared in a combination of conjuction and complement.

In :
e3 = Q.expression('<2>(ab) <+ <3>[ab]{+}')
e3

Out:
$\left\langle 2 \right\rangle \,\left(a \, b\right) + \left(a \, b\right)^{c} \& \left\langle 3 \right\rangle \,\left(\left(a + b\right) \, \left(a + b\right)^{*}\right)$

### The Derived-Term Automaton¶

The expansion and the derived-term automaton of $\mathsf{E}_3$ follow.

In :
e3.expansion()

Out:
$a \odot \left[\left\langle 2\right\rangle b \oplus \left\langle 3\right\rangle {b}^{c} \& \left(a + b\right)^{*}\right] \oplus b \odot \left[\left\langle 3\right\rangle \left(a + b\right)^{*}\right]$
In :
e3.derived_term()

Out:

## Example $\mathsf{E}_1$: A Simple Expression¶

### Polynomials and Derivatives¶

In :
Z = vcsn.context('lal_char, z')
e1 = Z.expression('<5>\e + <2>ace + <6>bce + <4>ade + <3>bde')
e1

Out:
$\left\langle 5 \right\rangle \,\varepsilon + \left\langle 2 \right\rangle \,\left(a \, c \, e\right) + \left\langle 4 \right\rangle \,\left(a \, d \, e\right) + \left\langle 6 \right\rangle \,\left(b \, c \, e\right) + \left\langle 3 \right\rangle \,\left(b \, d \, e\right)$

The derivatives of $\mathsf{E}_1$ with respect to $a$ and $b$ are the following polynomials.

In :
e1.derivation('a')

Out:
$\left\langle 2\right\rangle c \, e \oplus \left\langle 4\right\rangle d \, e$
In :
e1.derivation('b')

Out:
$\left\langle 6\right\rangle c \, e \oplus \left\langle 3\right\rangle d \, e$

The expansion of $\mathsf{E}_1$ is as follows. The notation is slightly different from the paper, where for higher clarity the weights in the polynomials are separated from the expressions by a $\otimes$. In the implementation, since the weight is always displayed, this separation is useless, and not put, for conciseness.

In :
e1.expansion()

Out:
$\left\langle 5\right\rangle \oplus a \odot \left[\left\langle 2\right\rangle c \, e \oplus \left\langle 4\right\rangle d \, e\right] \oplus b \odot \left[\left\langle 6\right\rangle c \, e \oplus \left\langle 3\right\rangle d \, e\right]$

### The Derived-Term Automaton¶

The derived-term automaton can be computed using derivations or expansions.

In :
e1.derived_term('expansion')

Out:
In :
e1.derived_term('derivation')

Out:

### The Deterministic Derived-Term Automaton¶

In :
e1.derived_term(deterministic=True)

Out:

## Section 4.2: An Infinite Automaton¶

There exists no finite weighted deterministic automaton for the following expression.

In :
e = Q.expression('a* + (<2>a)*')
e

Out:
${a}^{*} + \left( \left\langle 2 \right\rangle \,a\right)^{*}$
In :
e.derived_term()

Out:
In :
a = e.derived_term(deterministic=True, lazy=True)
a

Out:

Repeated evaluations uncover parts of this automaton.

In :
print(a('')); a

2

Out:
In :
print(a('aa')); a

5

Out:
In :
print(a('aaaa')); a

17

Out:

The following automata demonstrate the support of the shuffle operator (denoted ':' in ASCII, and '$\between$' in $\LaTeX$), and the infiltrate operator ('&:' and '$\uparrow$'). See also the documentation of the corresponding operations on automata: automaton.shuffle and automaton.infiltrate.

In :
exp = vcsn.context('lal, q').expression
e1 = exp('<2>a<3>a : <5>a<6>a', 'trivial')
e1

Out:
$\left\langle 2 \right\rangle \,a \, \left\langle 3 \right\rangle \,a \between \left\langle 5 \right\rangle \,a \, \left\langle 6 \right\rangle \,a$

The 'trivial' argument above restricts the identities to the trivial ones, otherwise additional identities would defeat our attempt to "taint" the as below:

In :
exp('<2>a<3>a : <5>a<6>a')

Out:
$\left\langle 6 \right\rangle \,\left(a \, a\right) \between \left\langle 30 \right\rangle \,\left(a \, a\right)$
In :
e1.derived_term()

Out:
In :
e2 = exp('<2>a<3>a &: <5>a<6>a', 'trivial')
e2

Out:
$\left\langle 2 \right\rangle \,a \, \left\langle 3 \right\rangle \,a \uparrow \left\langle 5 \right\rangle \,a \, \left\langle 6 \right\rangle \,a$
In :
e2.derived_term()

Out:

### Broken Derived Terms¶

"Breaking" variants "split" the polynomials at each step. In short, it means that no state will be labeled by an addition: rather the addition is split into several states. As a consequence, the automaton might have several initial states.

In :
e3 = exp('[ab]{2}')
e3.derived_term()

Out:
In :
e3.derived_term(breaking=True)

Out: