On Learning Polynomial Recursive Programs

We introduce the class of P-finite automata. These are a generalisation of weighted automata, in which the weights of transitions can depend polynomially on the length of the input word. P-finite automata can also be viewed as simple tail-recursive programs in which the arguments of recursive calls can non-linearly refer to a variable that counts the number of recursive calls. The nomenclature is motivated by the fact that over a unary alphabet P-finite automata compute so-called P-finite sequences, that is, sequences that satisfy a linear recurrence with polynomial coefficients. Our main result shows that P-finite automata can be learned in polynomial time in Angluin's MAT exact learning model. This generalises the classical results that deterministic finite automata and weighted automata over a field are respectively polynomial-time learnable in the MAT model.


INTRODUCTION
A central problem in computational learning is to determine a representation of a function through information about its behaviour on specific inputs.This problem encapsulates one of the main challenges in the analysis and verification of systems and protocols-namely, inferring an abstract model of a black-box system from a specification or a log of its behaviour.
In the case of functions represented by automata, one of most influential and well-known formalisations of the learning problem is the minimally adequate teacher (MAT) model, introduced by Dana Angluin [Angluin 1987].In this framework a learning problem is specified by a semantic class of functions and a syntactic class of representations (e.g., the class regular languages, represented by deterministic finite automata) and the goal of the learner is to output a representation of a given target function by making membership and equivalence queries to a teacher.In a membership query the algorithm asks the teacher the value of the target function on a specific argument, whereas in an equivalence query the algorithm asks whether its current hypothesis represents the target function and, if not, receives as counterexample an argument on which the hypothesis and target differ.This framework is sometimes referred to as active learning, since the learner actively gathers information rather than passively receiving randomly chosen examples, as in Valiant's PAC learning model.Another difference with the PAC model is that in the latter the hypothesis output by the learner is only required to be approximately correct, while in the MAT model it should be an exact representation of the target function.
In the MAT model, we say that a given learning algorithm runs in polynomial time if its running time is polynomial in the shortest representation of the target concept and the length of the longest counterexample output by the teacher.The running time is, by construction, an upper bound on the total number of membership and equivalence queries.Among other contributions [Angluin 1987] introduced the * algorithm: a polynomial-time exact learning algorithm for regular languages, using the representation class of deterministic finite automata.The * algorithm essentially tries to discover and distinguish the different Myhill-Nerode equivalence classes of the target language.By now there are several highly optimized implementations of the basic algorithm, including in the LearnLib26 and Libalf packages [Bollig et al. 2010;Isberner et al. 2015].
For many applications, such as interface synthesis, network protocols, and compositional verification, deterministic finite-state automata are too abstract and inexpressive to capture much of the relevant behaviour.This has motivated various extensions of Angluin's * algorithm to more expressive models, such as non-deterministic, visibly pushdown, weighted, timed, register, and nominal automata [Bollig et al. 2009;Howar et al. 2019;Michaliszyn and Otop 2022;Moerman et al. 2017].The current paper considers an extension of weighted automata.The class of weighted automata over a field was introduced by Schützenberger [Schützenberger 1961] and has since been widely studied in the context of probabilistic automata, ambiguity in non-deterministic automata, and formal power series.A weighted automaton is a non-deterministic finite automaton whose transitions are decorated with constants from a weight semiring.Here we focus on the case that the weight semiring is the field Q of rational numbers.Although weighted automata over a field are strictly more expressive and exponentially more succinct than deterministic automata, the class remains learnable in polynomial time in the MAT model [Beimel et al. 1999].By contrast, subject to standard cryptographic assumptions [Angluin and Kharitonov 1995] non-deterministic finite automata are not learnable in the MAT model with polynomially many queries.
Contributions of this paper.We introduce and study a generalisation of weighted automata, which we call P-finite automata, in which each transition weight is a polynomial function of the length of the input word.Over a unary alphabet, whereas weighted automata represent -finite sequences (sequences that satisfy linear recurrences with constant coefficients), P-finite automata represent so-called P-finite sequences (those that satisfy linear recurrences with polynomial coefficients).P-finite sequences are a classical object of study in combinatorics and the complexity analysis of algorithms [Kauers and Paule 2011].P-finite automata can thus be considered as a common generalisation of P-finite sequences and Q-weighted automata.In Section 2 we also view weighted and P-finite automata as simple tail-recursive programs.
The main results of the paper involve two different developments of the problem of learning Qweighted automata, respectively involving more general and more specific representation classes.
• The most important contribution concerns a generalisation of the algorithm of [Beimel et al. 1999] for learning Q-weighted automata.We give a polynomial-time learning algorithm for the class of P-finite automata in the MAT model.As a stepping stone to this result we show that the equivalence problem for P-finite automata is solvable in polynomial time.• In a second direction we consider the special case of the learning problem for Q-weighted automata in which the target function is assumed to be integer valued.Clearly the algorithm of [Beimel et al. 1999] can be applied in this case, but its final output and intermediate equivalence queries may be Q-weighted automata.On the other hand, it was shown in [Fliess 1974] that if a Q-weighted automaton gives an integer weight to every word then it has a minimal representation that is a Z-weighted automaton.Thus, in the case of an integervalued target it is natural to ask for a learning algorithm that uses Z-weighted automata as representation class.We give such an algorithm, running in polynomial time, and show how it can be implemented using division-free arithmetic.The heart of this construction is to give a polynomial-time procedure to decide whether a Q-weighted automaton is Z-valued and, if yes, to output an equivalent minimal Z-weighted automaton.
Related Work.In the case of a unary alphabet, P-finite automata are closely related to the matrix representations of P-finite sequences considered in [Reutenauer 2012].Over general alphabets P-finite automata can be seen as a very special case of the polynomial automata of [Benedikt et al. 2017].However, while determining equivalence of P-finite automata is in polynomial time, checking equivalence of polynomial automata is non-primitive recursive.The key difference is that in the case of P-finite automata one works with modules over univariate polynomial rings, which are principal ideal domains, rather than general polynomial rings, which are merely Noetherian.The former setting yields much smaller bounds on the length of increasing chains of modules (compare, e.g., Proposition 3.1 herein with [Benedikt et al. 2017, Theorem 2]).
The problems of learning automata with weights in principal ideal domains (such as the ring Z of integers and the ring Q[ ] of univariate polynomials with rational coefficients) was investigated in [van Heerdt et al. 2020].That paper relies on the fact that finitely generated modules over principal ideal domains are Noetherian for the termination of the learning algorithm.The methods of the paper do not address the question of the query and computational complexity of the learning problem.The paper also leaves open the question of learning minimal representations of a given target function.Here we give a method that runs in polynomial time in the case of automata with weights in Z and Q[ ] and that learns minimal representations.

OVERVIEW
Linear Tail-Recursive Programs.The weighted extensions of automata that are currently studied in the literature are able to model simple classes of tail-recursive programs, including linear recurrences.Consider Program 1, which reads a string of 's letter-by-letter from the input, and computes the function 1 : Program 1: A linear tail-recursive program computing 1 .
The above program can be modelled by a weighted automaton with two states 1 and 2 , as depicted on the right above.The states represent the output of , based on the congruence classes modulo 2. Intuitively speaking, the weight of a word is the sum of the weights of all runs of the automaton over the word, where the weight of a run is the product of weights of its starting state, of each transition taken along the run, and of its last state.For the automaton of Program 1 the non-zero initial weights are shown by incoming arrows to the states, whereas final weights are shown by outgoing arrows; each transition is also labelled by the letter and its weights.The automaton of Program 1 is a unary(-alphabet) automaton over Q; it is well-known that unary weighted automata over a field coincide with linear recurrence sequences [Berstel and Reutenauer 2010] over the field.Recall that a rational sequence { } ∞ =1 is a linear recurrence sequence of order if it satisfies a recurrence relation of the form where ∈ Q and 1 ≠ 0. The Fibonacci sequence, for example, is given by 0 = 1 = 1 and = −1 + −2 for all ≥ 2. The corresponding Fibonacci automaton is defined by := 1 1 The automaton computes the -th Fibonacci number as the weight of the input −1 through its semantics ( ) −1 .In the general setting, a recursive program computing a function : Σ * → Q can be realised by a Q-weighted automaton if its so-called Hankel matrix has finite rank [Berstel and Reutenauer 1988].This characterization encompasses a rich class of linear tail-recursive programs, where all assignments are linear updates of the form ← , where := ( 1 , . . ., ) is a tuple of variables and ∈ Q × .See Program 2 for a schematic illustration of such linear recursive programs.An Q-weighted automaton for such programs is defined accordingly as ( , , ) over the alphabet Σ.
Before we proceed, we note that in weighted automata the weight growth of each word is bounded by | | for a fixed positive constant ∈ Z.In the next section, we will see that, in our proposed extension of weighted automata, the weight growth of each word can be of magnitude Polynomial Tail-Recursive Programs.Our proposed P-recursive programs will have a program counter , that initially is set to zero and monotonously increases by one after each input letter, in order to store the length of the word.The updates on each variable is now in the form ← Program 3: A P-recursive program computing 2 .We show that such -recursive programs can be realised by our proposed extension of weighted automata, which we call P-finite automata.This extension can be thought of as a symbolic weighted automata where transition weights, as well as final weights, are parameterized by an indeterminate .Along the execution of a P-finite automaton over an input word, the value of indeterminate stores the length of the input read so far.
In the P-finite automaton representing Program 3 there are two states corresponding to the variables 1 and 2 .As is the case for weighted automata, the weight of a word is the sum of the weight of all runs of the automaton over the word, where the weight of a run is the product of weights of its starting state, of each transition taken along the run, and of its last state.The main difference is that the transition and final weights change in every step, as the value of gets updated after every new input letter.For instance, the weight of is 3! computed by final weight of 2 : 3 Formally, a P-finite automaton A = ( , , ( )) of dimension over an alphabet Σ is defined by the initial weight vector ∈ Q , a transition function : Σ → Q[ ] × , and the final weight vector ( ) ∈ Q[ ] .In the sequel, for simplicity of notations we use ( , ) instead of ( ) ( ), with ∈ Σ and ∈ N. The semantics of A, denoted by A : Although the initial vector is a vector of rationals, one can also look at it as a vector of polynomials (similar to the final vector ( )) that is always evaluated on 0 as it would lead to an equivalent semantics (0) ( 1 , 1) . . .( , ) ( + 1).The automaton of Program 3 is formally defined as := 1 0 ( ) := 0 0 ( ) := 0 0 := 0 .

=1
is a (monic) -recursive sequence of order if it satisfies a recurrence relation of the form where ∈ Q[ ] and 1 ≠ 0. Another example of monic -recursive sequences comes from the famous recurrence for the number of involutions, found by Heinrich August Rothe in 1800.An  | Some -> prog( ( ), + 1) involution on a set {1, 2, . . ., } is a self-inverse permutation.The number of involutions, including the identity involution, is given by 0 = 1 = 1 and = −1 + ( − 1) −2 for ≥ 2. The corresponding P-finite automaton is defined by The P-finite automaton computes the number of involutions of {1, . . ., } as the weight of the input −1 through its semantics See Program 4 for a schematic illustration of a class of polynomial tail-recursive programs that can be realized by a P-finite automata.

P-Solvable Loops and Extensions
The model of P-recursive programs (or P-finite automata) bears similarities with the notion of -solvable loops [Kovács 2008] and its extensions [Humenberger et al. 2017a,b].The latter are studied in the context of program analysis and invariant synthesis in particular.
The class of P-solvable loops is subsumed by that of linear tail-recursive programs, as P-solvable loops allow only linear updates of program variables [Kovács 2008].We have also the class of extended P-solvable loops [Humenberger et al. 2017a,b], in which the sequence of values assumed by a program variable is a sum of hypergeometric sequences.A hypergeometric sequence ( ) ∞ =0 is one that satisfies a polynomial recurrence = ( ) −1 for all ≥ 1, where ( ) ∈ Q( ) is a rational function.The class of extended P-solvable loops is thus incomparable with P-finite automata.On the one hand, hypergeometric recurrences allow multiplication by rational functions (such as ( ) above), not just polynomials.On the other hand P-finite automata over a unary alphabet can define sequences that are not sums of hypergeometric sequences (see [Reutenauer 2012, Section 10]).
The program shown above on the left is an example of an extended P-solvable loop, taken from [Humenberger et al. 2017b].The corresponding P-recursive program is shown on the right.Since the focus of [Humenberger et al. 2017b] is on invariant generation they consider loops that run forever.In our P-recursive programs any input letter invokes the recursive call.

Learning Algorithm
The high-level structure of the algorithm for learning -finite automata is shown in the diagram below.The algorithm consists of a main procedure exact_learnerand a subroutine partial_learner.
It is also not assumed to know a priori an upper bound on the number of states of the target automaton nor a degree bound on the polynomials appearing therein.Hence the procedure exact_learner searches through pairs of possible values of and and for each such pair it calls partial_learner that tries to learn a target automaton subject to these bounds.
The subroutine partial_learner can be seen as a generalisation of the algorithm of [Beimel et al. 1999] for learning Q-weighted automata.As in [Beimel et al. 1999], the basic data structure, which we call the table, is a finite fragment of the Hankel matrix of the target function : Σ * → Q. Formally speaking, the table is a finite matrix whose rows and columns are labelled by words and such that the entry with index ( , ) ∈ Σ * × Σ * is ( ).We will later on denote this table by (R, C) where R and C are the sequences of words labelling the rows and columns of the table.The table is used to construct a hypothesis automaton.This involves making membership queries to interpolate polynomials that label the state-to-state transitions of the automaton.Since there is a bound on the maximum degree of the polynomials, the process of interpolation is reduced to solving a system of linear equations.
Once constructed, the hypothesis automaton is passed to the teacher.If the hypothesis is correct, the algorithm terminates and returns the hypothesis automaton; if it is incorrect, the counterexample given by the teacher is used to augment the table by adding a new row and at most one column (using membership queries to fill in the missing table entries).After augmenting the table, a new hypothesis automaton can be constructed.
For any given run of partial_learner, since the degree bound may not be sufficient to learn the target automaton, there is a timeout ℓ (a function of and ) on the run of partial_learner.If the timeout is reached then the run is abandoned and control returns to exact_learner.
Going back to the case of Q-weighted automata, the termination (and polynomial-time bound) of the learning algorithm of [Beimel et al. 1999] relies on the classical result of Carlyle and Paz that a function : Σ * → Q is recognisable by a Q-weighted automaton if and only if its Hankel matrix has finite rank.The idea is that every unsuccessful equivalence query results in the rank of the table increasing by one, and so the number of equivalence queries is at most the rank of the Hankel matrix of the target function.Such a result is not available in the case of P-finite automata.
The termination proof and polynomial complexity bound for exact_learner rely on an analysis of the timeout.For this we associate with a run of partial_learner an increasing chain of submodules over the polynomial ring Q[ ], whose length is the number of equivalence queries.Since Q[ ] is a Noetherian ring, such a chain must have finite length.We give a novel fine-grained analysis of the maximum length of an increasing chain of modules over Q[ ] to guarantee that we will learn the target automaton within the timeout if the degree bound parameter is sufficiently large.(This analysis even allows us to obtain a polynomial bound on the overall computational and query complexity of our learning algorithm.)We highlight that in contrast to the case of Q-weighted automata, the length of this chain of modules depends on the length of the counterexamples returned by the teacher.As an intermediate result, we use this analysis to show that equivalence of P-finite automata is decidable in polynomial time.
Our analysis of increasing chains of modules over Q[ ] applies equally well to Z.We use the version for Z to give a polynomial-time algorithm to decide whether or not the function recognised by a given Q-weighted automaton is Z-valued.We then observe that such an algorithm can be used to reduce the problem of learning Z-weighted automata to that of learning Q-weighted automata.

BACKGROUND ON MODULE THEORY
Let be a commutative ring with unity.An ideal of is an additive subgroup ⊆ such that ∈ for all ∈ and ∈ .The ring is said to be a principal ideal domain (PID) if every ideal is generated by a single element, that is, there is some ∈ such that = { : ∈ }.We will mainly work with Z and Q[ ], which are both PIDs.
An -module is an abelian group together with a scalar multiplication (•) for all scalars , 1 , 2 ∈ , and for all elements , 1 , 2 ∈ .A key example of an -module is , where ∈ N, in which addition and scalar multiplication act pointwise.
Let be an -module.A submodule of is a subgroup that is closed under scalar multiplication.A subset { : ∈ } ⊆ is said to be linearly independent if an -linear combination ∈ is only zero if all the are zero.We write : ∈ for the -span of the , defined by We say that { : ∈ } generates if = : ∈ .If the are, in addition, linearly independent, then we say that { : ∈ } is a basis of .If is a PID then all submodules of have a basis and all bases have the same cardinality.
A key difference between modules and vector spaces is that one can have proper inclusions between modules of the same rank.For example, we have that 15Z 3Z Z are all rank-1 submodules of Z. However it remains true that all finitely generated -modules are Noetherian: every strictly increasing chain of submodules of is finite.A crucial ingredient in the analysis of our algorithms is an upper bound on the length of strictly increasing chains of modules in .For this, we use the Smith Normal Form.
In the above-mentioned decomposition of as ˜ , the columns of are in fact the -basis 1 , . . ., of , and ˜ is a matrix with columns 1 1 , . . ., , representing an -basis of .P 3.1.Let , ∈ N and 0 1 • • • be a strictly increasing chain of submodules of Z , all having the same rank ≤ .Assume that 0 is generated by a collection of vectors whose entries have absolute value at most .Then ≤ log + 2 log .

P S
. By assumption, there are vectors 1 , . . ., ∈ Z that generate 0 and whose entries have absolute value at most .Using Smith Normal Form, there exists a basis 1 , . . ., of Z and positive integers 1 , . . ., , such that 1 1 , . . ., is a basis of 0 .Furthermore, by Equation (1) it follows that 1 • • • is the greatest common divisor of all × minors of the × matrix with columns 1 , . . ., .By Hadamard's inequality it follows that 1 • • • ≤ /2 .Let be the module generated by 1 , . . ., .Since the modules 0 , . . ., all have rank , they are all contained in .Recall that the index [ : 0 ] of a subgroup 0 in the group , is the number of cosets of 0 in .Observe that the index some positive integer .The strictly increasing chain of modules of rank 1: has length , which is the bound given by Proposition 3.1.
In the next Proposition, we generalize Proposition 3.1 to the PIDs for which there exists a welldefined greatest common divisor function.A detailed proof can be found in Appendix A. . . .be a strictly increasing chain of -submodules of , all having the same rank ≤ .Then is bounded by the number of (not necessarily distinct) prime factors of ( ).

Z-WEIGHTED AUTOMATA
In this section, we start by giving a procedure to decide in polynomial time whether a Q-weighted automaton computes an integer-valued function.For every "yes" instance our procedure returns an equivalent Z-weighted automaton and for every "no" instance it returns a word whose weight is non-integer.This algorithm can be regarded as an effective (and computationally efficient) version of the well-known fact that Q is a Fatou extension of Z [Berstel and Reutenauer 2010, Chapter 7].As a corollary of the above procedure, we give a polynomial-time reduction of the exact learning problem for Z-automata to the exact learning problem for Q-automata.(One can similarly reduce the exact learning problem for automata with weights in the ring Q[ ] to that for automata with weights in the quotient field Q( ).)

Z-valuedness of Q-automata
Let A = ( , , ) be a Q-weighted automaton of dimension over alphabet Σ.Here ∈ Q 1× , ( ) ∈ Q × for all ∈ Σ, and ∈ Q ×1 .We say that such an automaton A is Z-weighted if all entries of , and those of the matrices ( ) are integers.Let be the × identity matrix.We extend to a map : Σ * → Q × by writing ( ) := and ( ) := ( ) ( ) for all ∈ Σ and ∈ Σ * .The semantics of A, that is, the function computed by A, is given by A : with A ( ) := ( ) .Automata A 1 , A 2 over the same alphabet Σ are said to be equivalent if A 1 = A 2 .An automaton A is minimal if there is no equivalent automaton with fewer states.
Define the forward reachability set of A to be { ( ) : ∈ Σ * } and define the backward reachability set to be { ( ) : ∈ Σ * }.The forward space and forward module of A are respectively the Q-subspace of Q and Z-submodule of Q spanned by the forward reachability set, viz., The backward space and backward module are defined analogously.The forward space is the smallest (with respect to inclusion) vector space that contains and is closed under post-multiplication by ( ).The forward module is likewise the smallest module that contains and is closed under post-multiplication by ( ).Analogous statements apply to the backward space and backward module.
Let ∈ Q × with ≤ be a matrix whose rows form a basis of the forward space of A. It is known that there are unique (2) Similarly, let ∈ Q × with ≤ be a matrix whose columns form a basis of the backward space of A. It is known that there are unique ) is a forward conjugate of A, and the automaton A = ( , , ) is a backward conjugate of A. These automata are equivalent to A, meaning that The procedure to decide Z-valuedness of Q-automata is a variant of the classical minimisation algorithm for Q-weighted automata and it is described in Figure 6.Below, we first work through a subroutine used in the algorithm.
It is classical that given an automaton A we can compute in polynomial time a Q-basis of the forward vector space that is comprised of vectors in the forward reachability set.An analogous result holds for the backward space [Kiefer 2020;Tzeng 1992].The forward module need not be finitely generated in general, but it will be finitely generated if the forward reachability set is contained in Z .
The procedure compute_Z_generators, shown in Figure 5, is a polynomial-time algorithm that, for an input Q-automaton, either outputs a finite basis of the forward module of A or a non-integer vector in the forward reachability set.Intuitively, it builds a set of words , starting from { }, by adding words that augment the module ( ) : ∈ Z .When no such word can be found, the set { ( ) : ∈ } will form a generating set for the forward module.
Notice that the procedure is based on a two-pass search: first we search for words that increase the rank of the forward module and then for words that augment the forward module while the rank is stable.This allows us to obtain a polynomial-time running bound through a single application of Proposition 3.1 to the second phase of the search.We do not know if it is possible to obtain a polynomial bound under arbitrary search orders.

P
. Write the entries of , and ( ), with ∈ Σ, as fractions over a common denominator and let be an upper bound of the numerators and denominator of the resulting fractions.Note that the bit size of is polynomially bounded in the length of the encoding of A.
The first while-loop, in Line 3 computes a set of words 0 ⊆ Σ ≤ such that { ( ) : ∈ 0 } is a Q-basis of the forward space of A. By construction, the dimension of the space spanned by the set { ( ) : ∈ 0 } ≤ | 0 | ≤ , which shows that the first while-loop terminates after at most iterations.
Below, we prove that the second while-loop, in Line 6, terminates in polynomial time in the length of the encoding of A. Let 0 , 1 , 2 , . . .be the successive values of the variable during the second loop.For all ∈ N, let be the Z-module ( ) : ∈ Z .Then 0 1 • • • is a strictly increasing sequence of Z-modules, all having the same rank (namely the size of 0 , that is the dimension of the forward space).
Recall that the length of words in 0 is at most .A simple induction on the length of words allows us to show that for all ∈ Σ * , the entries of ( ) have numerators and denominators bounded by | |−1 | | .In particular, we obtain that the entries in { ( ) : ∈ 0 } are bounded by −1 .
Let 0 := ( − 1 2 ) log + 2 log .Suppose that all modules 0 , 1 , • • • contain only integer vectors.Then Proposition 3.1 shows that the above sequence modules has length at most 0 .The only other possibility is that for some ≤ 0 we have Z and hence ( ) ∉ Z for some word ∈ . In either case, the number of iterations of the while loop is at most 0 .It follows that each set consists of at most 0 + words, each of length at most 0 + .Thus the set of vectors { ( ) : The procedure to compute an equivalent Z-automaton from a Q-automaton is illustrated in Figure 6.It starts by computing a Q-basis of the backward space and by building an equivalent Qautomaton A ′ , where each entry of a forward reachability vector is an evaluation of the function computed by A, that is ′ ′ ( ) = A ( 1 ) . . .

A (
) .We then apply compute_Z_generators(A to either deduce the existence of a word such that A ( ) ∉ Z for some ∈ {1, . . ., }, or to obtain a generator of the forward reachability set consisting of integer vectors.Form these generators, an equivalent Z-automaton is built.
T 4.2.The procedure compute_Z_automaton, described in Figure 6, is a polynomial-time algorithm that given a Q-weighted automaton A of dimension over Σ, either outputs an equivalent Z-automaton (that is in fact minimal as a Q-weighted automaton), or a word such that A ( ) ∉ Z.

P
. The procedure is a variant of the classical minimisation algorithm for weighted automata over fields.
The first step is to compute a basis { 1 , . . ., } of the backward space of A. Lines 2-4 correspond to Tzeng's procedure and, as noted previously, this is done in polynomial time.The matrix ∈ Q × has columns corresponding to the vectors in the above-mentioned basis, that are ( ) .
The next step defines a new dimensional Q-automaton A ′ that is a conjugate of A, so that A = A ′ .From the fact that the columns of form a basis of the backward space of A it can be seen that A ′ is well-defined.Furthermore, for all ∈ Σ * we have ′ ′ ( ) = ( ) , so, the -th entry of ′ ′ ( ) has the form ( ) = A ( ).Thus, the forward reachability set of A ′ consists exclusively of integer vectors when A is integer-valued.Applying Proposition 4.1, the computation of compute_Z_generators(A ′ ) yields either a word ∈ Σ * such that ( ) ∉ Z or else a set of words generating the forward reachability set of A ′ .In the former case, there exists ∈ {1, . . ., } such that ( ( )) ∉ Z and so A ( ) ∉ Z.In the latter case, we use the Smith Normal Form to generate a Z-basis of ′ ′ ( ) : ∈ Z .As is comprised of the Z-vectors 1 , . . ., ℓ , the ℓ dimensional automaton A ′′ is a conjugate automaton of A ′ , that is A ′ = A ′′ .Note that A ′′ is a well-defined Z-automaton by the fact that the rows of form a Z-basis forward module of A ′ , which entails that Equation (2) has a solution , ( ), in integers.We conclude by noting that ℓ is the dimension of the forward space of A ′ as well as the rank of the forward module.It follows that A ′′ is a minimal Q-weighted automaton.

Exact Learning
In this subsection, we describe how the exact learning problem for Z-weighted automata can be reduced to the exact learning problem for Q-weighted automata.Such a reduction is non-trivial since the equivalence oracle in the former setting is more restrictive: it requires a Z-weighted automaton as input rather than a Q-weighted automaton.The key to the reduction is thus a procedure Q_equivalence_oracle that implements an equivalence oracle for Q-weighted automata using an equivalence oracle for Z-weighted automata.This procedure inputs a Q-weighted automaton H and returns either Some( ) or None: • In the first case, it returns Some( ) with being a counterexample, witnessing that A ( ) ≠ H ( ).This counterexample is given by compute_Z_automaton(H ) in case H is not integer valued and otherwise it is given by equivalence_oracle.
• In the second case the procedure returns None, meaning that H is equivalent to A.

T 4.3.
There is a procedure that learns the target Z-weighted automaton A, by outputting a minimal Z-weighted automaton equivalent to A, which runs in polynomial time in the length of the encoding of A and in the length of the longest counterexample given by the teacher.

P
. Denote by the size of the encoding of the target automaton A. As is the case for Q-weighted automata learning, the algorithm maintains the invariant that the dimension of the hypothesis automata H constructed during the learning procedure is less than .By Theorem 4.2, the procedure compute_Z_automaton(H ) runs in time polynomial in .This implies that the built-in Q_equivalence_oracle(H ) also runs in time polynomial in .We know that there is a procedure L that learns Q-weighted automata, and runs in time polynomial in and in the length of the longest counterexample given by the teacher [Beimel et al. 1999].As such, L only calls such equivalence oracle a polynomial number of times.Therefore, using Q_equivalence_oracle as an oracle for L yields a polynomial time procedure that outputs a Q-weighted automaton H equivalent to A. We conclude by calling compute_Z_automaton(H ) which runs, as already mentioned, in time polynomial in .
In this section, we tackle the zeroness, equivalence, and exact learning problems for P-finite automata.The equivalence problem is the problem of deciding whether two automata compute the same function, while the zeroness problem aims to check whether the input automaton computes the zero function.In Section 5.1 we observe that the zeroness and equivalence problems for Pfinite automata are polynomial-time interreducible and we show that zeroness can be solved in polynomial time.Meanwhile, in Section 5.2 we show that the P-finite automata can be exactly learned in polynomial time in the MAT model.

Equivalence
We can reduce the equivalence problem to the zeroness problem.Indeed, two automata A 1 and A 2 are equivalent if and only if the difference automaton A − (such that A − = A 1 − A 2 ) computes the zero function.We refer to Appendix B for details.
Consider the P-finite automaton of Program 3, one can show that the backward module B A 1 of this automaton is defined as: where ( ) := =1 ( + ).By a simple computation, we have that We remark that the backward function can be defined recursively as A ( ) = ( ), and for all ∈ Σ and ∈ Σ * , A ( ) = ( , ) A ( ) ( + 1), where A ( ) ( + 1) is obtained by substituting + 1 for in the vector A ( ).By definition, the result of the computation of a P-finite automaton A on a word is A ( ) = A ( ) (1).
From backward module to zeroness.Formally speaking, the zeroness problem asks, given an automaton A over Σ, whether A ( ) = 0 for all words ∈ Σ * .The following proposition describes how we can decide zeroness by inspecting a finite generating set of the backward module.(1) = 0.

P .
Let A be over Σ.The proof is straightforward by unfolding the definitions of backward function, backward module, and A : By the definition of B A ⇐⇒ ∀ ∈ , (1) = 0 Since is a generating set of B A The previous proposition indicates that, in order to verify zeroness, it is enough to check if is orthogonal to a generating set of the backward module.
5.1.2Computing a generating set for B A .Our algorithm for computing a generating set of the backward module is displayed in Figure 7.It bears a strong resemblance to our algorithm for computing generators for the backward and forward modules in Z-weighted automata (Figure 5).The main distinction lies in the soundness proof, which is more involved due to the necessity to work with Q[ ]-modules.In particular, we will need the following corollary of Proposition 3.3. . . .be a strictly increasing chain of submodules of Q[ ] , all having the same rank ≤ .Assume that 0 is generated by a collection of vectors whose entries have degree at most .Then ≤ • .

P
. From Proposition 3.3, it follows that is bounded by the number of prime factors of ( ) where is the matrix whose columns contain generators of 0 .Since the number of prime factors of a univariate polynomial is at most its degree, is bounded by deg( ( )).This can also be upper-bounded by the maximum degree of all × minors of , which, by the triangle inequality and the determinant formula involving permutations, is at most • .
We are now ready to present the polynomial-time membership of the equivalence problem of P-finite automata.
T 5.4.The procedure generators_backward_module in Figure 7, on an input P-finite automaton A, terminates and outputs a set of vectors such that B A = Q[ ] .The procedure executes in polynomial time in the length of encoding of A.

P
. Let A = ( , , ( )) be an automaton of dimension and over alphabet Σ. Write 1 , 2 , . . .for the successive instantiations of the variable during the execution of the function generators_backward_module(A).Since 1 = { }, and for all > 0, = −1 ∪ { } for some ∈ Σ and ∈ −1 , it follows that the maximum length of words in is at most the size of .
The first while-loop, in Line 3, terminates after at most iterations since the backward module, being a submodule of Q[ ] , has rank at most .The second while-loop, in Line 5, terminates by virtue of Q[ ] being Noetherian.Below, we write for some ℓ ≤ , for the instantiations of upon exiting the first and second while-loops, respectively.
We show a similar result concerning the second while-loop termination.We claim that A ( ) ∈ A ( ) : ∈ Q[ ] for all words ∈ Σ * .Intuitively speaking, once exiting the second whileloop, no words in Σ * that could augment the module can be added.The proof is again by induction on the length of the words.The base case (| | = 0) trivially holds as ∈ .For the inductive step (| | > 0), rewrite as ′ for some ∈ Σ and ′ ∈ Σ * .By the induction hypothesis, for some polynomials ( ) ∈ Q[ ] where ∈ {1, . . ., }.Following similar reasoning as in the first loop case, we obtain that A ( ) ∈ A ( ) exit-condition of the second while-loop ensures that concluding the proof of the claim.
It remains to show that the execution of generators_backward_module(A) can be carried out in time polynomial in the length of encoding of A. Recall that, given a word = 1 . . ., the backward reachable vector is computed as A ( ) ( ) = ( 1 , ) . . .( , + − 1) ( + ).Denote by and , respectively, the maximal degree and largest coefficient of the polynomials occurring as entries of ( ) and ( ), for ∈ Σ.It follows that the degree of polynomial entries of A ( ) ( ) is at most (| | + 1).We will argue that the largest coefficient of the polynomials occurring as entries of |+1) .Indeed, this comes from the observation that the coefficients of the monomial ( + | |) are bounded by (| | ) .The length of the encoding of A ( ) ( ) is therefore polynomial in the length of encoding of A and in | |.
Using [Kannan 1985], we deduce that testing whether is polynomial in the length of encoding of A, and in the maximum length of words in , and in the size of .Recall that the maximum length of words in is at most the size of .We conclude the proof by arguing that the size of is polynomial in the length of encoding of A. As a result of the two claims on the termination of the loops, the two backward modules induced by ℓ and have the same rank.Since ℓ ≤ , the degree of the polynomials in the entries of A ( ) for ∈ ℓ is at most ( +1).By Corollary 5.3, the length of the strictly increasing sequence of modules induced by ℓ . . . is at most − ℓ + 1 ≤ ( + 1), implying that the size of is at most ( + 1) + − 1.
By a direct application of Theorem 5.4, Proposition 5.2 and Proposition 5.1, we have: T 5.5.The zeroness and equivalence problems for P-finite automata are both in polynomial time.We can furthermore suppose that the polynomial-time procedure for testing equivalence returns a word of polynomial length that witnesses in-equivalence on negative instances.

Learning
We first introduce some notation and terminology.Below, we fix : Σ * → Q to be a function.The Hankel matrix of is an infinite matrix with rows and columns indexed by words in Σ * such that ( , ) := ( ), where ( , ) is the entry of matrix with row index ∈ Σ * and column index ∈ Σ * .
Given two sequences R, C of words from Σ * , denote by (R, C) the restriction of the Hankel matrix to the respective sets R of rows and In the sequel, we will call (R, C) a table.
Assume that the target function can be computed by a P-finite automaton.Intuitively, our learning algorithm maintains a table from which it constructs a hypothesis automaton.Using the equivalence oracle, the algorithm checks whether the hypothesis automaton computes the function .In case of a negative answer, the witness of non-equivalence is used to augment the table (by augmenting the sets of rows and columns), and the process repeats.In order to build the hypothesis automaton we require the table to be closed in the following sense.Let R and C be two sequences of words from Σ * such that |C| = .We say that the table (R, C) is closed when for each ∈ Σ, there exists a matrix of polynomials ( ) ∈ Q[ ] × such that for all rows ∈ R, the equation row C ( ) = row C ( ) (| | + 1) holds.Given such a closed table (R, C), we can compute a hypothesis P-finite automaton ( , , ) of dimension as follows: The polynomials in the transition matrix of a hypothesis automaton need to be constructed by interpolation.To this end, we maintain a variable that represents a degree bound on the polynomials in ( ).Specifically, we will say that the table (R, C) is -closed when the maximal degree of the polynomials in the ( ) are bounded by .The -closedness condition allows to set up a linear system of equations where the unknowns , , are the coefficients of the polynomials of each entry of ( ), that is, we write the ( , )-th entry of ( ) as , , + , , −1 −1 +• • •+ , ,0 .More precisely, we search for the unknowns , , , ranging over Q. Focusing on the -th column of ( ), the -closedness condition row C ( ) = row C ( ) (| | + 1) entails, for all ∈ R, the following equation: where the are the column vectors 1, , . . .
and Δ the × diagonal matrix we obtain the following system of equations in 0 , . . ., : . . .
We recover the hypothesis automaton associated to the -closed table (R, C) from a solution to the above system of equations by setting the -th column of ( ) to be =0 .Henceforth we denote by (R, C) the matrix Δ 0 . . .Δ .
In the following proposition, we state a sufficient condition for the above linear system of equations to have a solution, meaning that the table (R, C) is -closed.P 5.6.Given two sequences of words R and C and ∈ N, the table (R, C) is -closed if (R, C) has full row rank.+1) , which, by hypothesis, has full row rank.Then for all vectors ∈ Q ×1 , the system = has a solution ∈ Q ( +1) ×1 .Indeed, the system = has a solution if and only if rank( ) = rank( ).Since ∈ Q × ( +1) and rank( ) = , we deduce that for all ∈ Q ×1 , the equality rank( ) = rank( ) holds and the system = has a solution.Write for the size of the sequence .We construct the matrices ∈ Q[ ] × , for ∈ Σ, as follows.By the above argument, for ∈ {1, • • • , }, the system of linear equations described in (5) has some solution, say * 0 , . . ., * .We define the -th column of ( ) to be =0 * , which in turn implies that the ( , )-th entry of ( ) is the polynomial *  It remains to argue that the matrix ( ) so defined satisfies the closedness condition, that is, for all rows ∈ R the condition row C ( ) = row C ( ) (| | +1) holds.But then, this is guaranteed by enforcing (4) for all columns ∈ C. We conclude by noting that constraints (4) constitute the system of linear equations described in (5).
Our algorithm for building the automaton associated to the -closed table (R, C) is given as function build_automata in Figure 8.This function is an implementation of the construction stated in (3), which is ensured by the -closedness assumption on the input table.We note again that the maximal degree of polynomials in constructed automaton is at most .In summary, we have: C 5.7.The function build_automata( , R, C), assuming that (R, C) is -closed, outputs an automaton H associated to the -closed table (R, C).
Concretely, in the function build_automata computing (R, C) and (R, C) can be evaluated by asking membership queries from the teacher, through membership_oracle, at most |R| × |C| times.Therefore, the execution of this function runs in time polynomial in + |R| + |C| + |Σ|.

5.2.1
Correctness of P-finite automata.Let R = [ 1 , . . ., ] and C = [ 1 , . . ., ] be two sequences of words from Σ * .Assume that (R, C) is closed and let H = ( , , ) be an associated P-finite automaton over Σ.We say that that H is correct on the word ∈ Σ * if ( , 1) = row C ( ).As previously mentioned, after building a hypothesis automaton H associated with a table, we will ask the teacher an equivalence query on H , through equivalence_oracle(H ), and receive a counterexample in case H is not equivalent to the target automaton.The automaton H is correct on by construction and is necessarily incorrect on , as indeed we initialize C with and ensure that the automaton is always correct on this word, and the fact that ( ) ≠ H ( • ) = ( , 1) 1 .We compute the longest prefix of such that H is correct on but incorrect on .Our learning algorithm extends its table by adding the row associated with .Computing such a prefix can be straightforwardly done as depicted in Figure 9.The function largest_correct_prefix(H, C, ) outputs , as well as the word ∈ C that renders H incorrect on .The execution of largest_correct_prefix(H, C, ) runs in time polynomial in its parameters, that is, in time polynomial in | | + |C|.
In Corollary 5.7, we assumed that the table (R, C) is closed in order to build the hypothesis automaton from it.However, when augmenting the table with the row associated with , the closedness condition might not hold anymore as the new row might be linearly dependent with the previous rows in R. We show in the next proposition that in such cases the closedness of the table can be restored by adding the column associated with to the table, where ( , , ) is the output of the function largest_correct_prefix(H, C, ).

P
. Write for the matrix (R ′ , C) col R ′ ( ) .Since (R, C) is a sub-matrix of and has |R| + 1 rows, we deduce that |R| + 1 ≥ rank( ) ≥ rank( (R, C)) = |R|.For a contradiction, assume that is not full row rank, implying that rank( ) = |R|.Then the last row of is a linear combination of all other rows of .In other words, writing R as the sequence [ 1 , . . ., ], there exists a row vector x such that for all words ∈ C, for all ∈ {0, . . ., }, x ( 1 ) . . .
Recall that the ( ) are matrices of univariate polynomials.Define ( ) to be the matrix whose ( , )-th entry is the coefficient of in the ( , )-th entry of ( ).We obtain that row Multiplying both sides of the equation by x, we obtain: which in turn implies that By hypothesis, the automaton H is correct on , meaning that ( , 1) = row C ( ).Subsequently, ) .This is in contradiction with the assumption ( , 1) ≠ ( ), concluding the proof.for ∈ {1, . . ., }. From (8) we obtain that: , where = (R, C) and Δ is the × diagonal matrix (| 1 | + 1, . . ., | | + 1).We deduce that the rank of (R, C) is equal to the rank of (R, C) col R ( ) .This is in contradiction with the assumption that (R, C) is not full row rank, but (R, C) col R ( ) is, concluding the proof.For all ∈ {1, . . ., }, define M := A ( )

Using
The ranks of the submodules M are at most .We aim at upper bounding by ( max , ); due to Corollary 5.3, the worst upper bound is reached when some module in the strictly increasing sequence of modules reaches full rank.Below we assume that that our increasing sequence reaches full rank, meaning that rank(M ) = .Define 1 , . . ., as the indices corresponding to when the rank of the submodules M has strictly increased.Formally, we have that 1 = 1 and 1 < . . .< .Furthermore, for all ∈ {2, . . ., }, By the above-mentioned bound on the degree of polynomials in the generators of M together with Corollary 5.3, we infer the following properties: By telescoping (9) from to 1 , we get ≤ + −1 =1 , where the right-hand side is at most . By a simple induction, for all ∈ {2, . . ., }, we obtain that ≤ ( + 1) −1 2( −1) , and so ≤ ( + 1) 2 = ( , ) holds .Now we are ready to analyse the maximum number of columns added to the sequence C during the successive recursive calls to the procedure partial_learner ℓ = ( , ′ ).Let R 1 , R 2 , . . .and C 1 , C 2 , . . .be the successive values passed to partial_learner, where R 1 , C 1 are initialized to [ ].

5.2.3
Bounding the number of rows.As previously mentioned, every row added by the learning algorithm is a prefix of a counterexample given by the teacher.In this section, we exhibit a bound on the total number of rows added during the learning procedure, which is polynomial in the maximum size of the counterexamples and the target automaton.For this, we define a bounded forward vector space that only considers words of bounded size.
Let A = ( , , ) be a P-finite automaton of dimension over Σ.Let ∈ N. The -bounded forward function associated with A is the function +1) given by: The -bounded forward space, denoted F A , is the vector space A ( ) +1) .Thus, the dimension of the vector space F A is at most ( + 1) .Intuitively, here represents the maximal size of the counterexamples given by the teacher.In the following proposition, we show that if 1 , . . ., are the prefixes of the counterexamples added as rows during the learning procedure, then the dimension of A ( 1 ), . . ., A ( ) Q is .P 5.12.Let R and C be sequences of words.Let , ∈ N be such that (R, C) has full row rank, and | | < for all words ∈ R. The dimension of the vector space A ( ) : ∈ R Q is |R|.

P
. Let R = [ 1 , . . ., ].It suffices to argue that the set { A ( ) : 1 ≤ ≤ } is linearly independent.Towards a contradiction, we assume without loss of generality that A ( ) is dependant on the other A ( ), meaning that that there exist 1 , . . ., −1 ∈ Q such that A ( ) = Finally, for all ∈ Σ * , denote by ( ) ∈ Q ( +1) ×1 the column vector defined as follows: A ( ) (1) . . .Therefore, if we denote by 1 , . . ., the rows of (R, C) then we have shown that which is in contradiction with rank( (R, C)) = |R|.P 5.13.Let , ℓ ∈ N. Let be the maximal length of counterexamples given by the teacher during the execution of partial_learner( , ℓ, [ ], [ ]).Then the number of recursive calls to partial_learner is at most ( + 1) , where is the number of states in the target automata.By Proposition 5.12, the dimension of the vector space A ( ) : ∈ R Q is |R|.But then, since ) , we have |R| ≤ ( + 1) .We note that every recursive call to partial_learner increases the size of R by one starting from [ ].Therefore, the number of recursive calls to partial_learner is at most ( + 1) .

5.2.4
The exact learner.The exact learner function is displayed in Figure 11.The core of the learning process comes from the procedure partial_learner.However, it still remains to correctly guess the values of , the maximal degree of polynomials in the target automaton A, and of , its number of states.Guessing is important in order to compute the limit value ℓ for the number of columns added during the execution of partial_learner.As we need to guess two positive integers, we use the standard diagonal progression of the Cantor pairing function.
The following theorem shows that P-finite automata can be exactly learned in time polynomial in the size of the target automaton and the maximal length of counterexamples given by the teacher.
T 5.14.Let A be the target P-finite automaton.The procedure exact_learner terminates and returns a P-finite automaton H such that A = H .Moreover, exact_learner runs in time polynomial in the length of the encoding of A and the maximal length of counterexamples given by the teacher during the execution of exact_learner.

P
. Let A be the number of states in the target automaton A and be the maximal degree of polynomials in A. Let be the maximal size of all counterexamples given by equivalence_oracle during the execution of exact_learner.
By Proposition 5.13, when executing partial_learner( , ℓ, [ ], [ ]), the number of recursive call to partial_learner is at most ( + 1) A , irrespective of the choice of , ℓ.Furthermore, by the construction of partial_learner, we also know that for every call to partial_learner with arguments , ℓ, R, C, the size of R is at most ( + 1) A and |C| ≤ |R|.
By the above, the computation of one recursive call of partial_learner is polynomial in + + A , which in turns implies that each execution of partial_learner( , ℓ, [ ], [ ]) runs in time polynomial in and in the length of the encoding of A, irrespective of the choice of , ℓ.
Intuitively, the variables , in exact_learner are the "guessed" values for the number of states and the degrees of polynomials in the target automaton, and the variable is to implement the standard diagonal progression for and .By Proposition 5.11, we know that when the variable in Line 10 in exact_learner reaches the value + A , in the inner for-loop, the output of the call to partial_learner with set to and ℓ set to ( + 1) A A is necessarily different from None, terminating the computation (the procedure might terminate before this point).Therefore, we will call partial_learner( , ℓ, [ ], [ ]) for different values of , ℓ at most ( + A ) 2 times, which concludes the proof.

Program 2 :
Scheme of linear tail-recursive programs main()= prog( ) Formally, a Q-weighted automaton A = ( , , ) of dimension over an alphabet Σ is defined by the initial weight vector ∈ Q 1× , a transition function : Σ → Q × and the final weight vector ∈ Q ×1 .The semantics of A, denoted by A : Σ * → Q, maps each word = 1 • • • to its weights computed as( 1 ) . . .( ) .The automaton of Program 1 is formally defined as :

Program 4 :
Scheme of P-recursive programs 1 def prog( , Let , ∈ N. Let be a PID and = 1 , . . ., be a -submodule of .Let be the × matrix whose -th column is .Let1 2

Figure 5 :
Figure 5: Computing generators of the forward module or a counterexample.1 def compute_Z_generators(A)= 2 ∈ } has description length polynomial in A. Each iteration of the while loop involves solving | | • |Σ| systems of linear equations over Z to determine membership in the module generated by { ( ) : ∈ }.Again, this requires time polynomial in A. Altogether, the algorithm runs in polynomial time.If the loop terminates by returning ⊆ Σ * then { ( ) : ∈ } contains and is closed by multiplication on the right by ( ) for all ∈ Σ.Thus this module is the forward module of A.

P 5. 1 .
The equivalence problem of P-finite automata is polynomial-time reducible to the zeroness problem.5.1.1Backward module.Below, we fix a P-finite automaton A = ( , , ( )) of dimension over Σ.The backward function associated to A, denoted by A , is the function A

Figure 7 :
Figure 7: Finding a generating set for the backward module of a P-finite automaton 1 def generators_backward_module(A)= // A a P-finite automaton over Σ 2 Let A = ( , , ( )) be a P-finite automaton of dimension .Let ⊆ Q[ ] be a finite generating set for the backward module B A .We have A ≡ 0 if and only if all ∈ satisfy
Moreover, given two words , ∈ Σ * such that appears in the sequence R and appears in the sequence C, we write row C ( ) for the associated row and col R ( ) for the associated column in (R, C), namely, row C ( ) := ( 1 ) . . .
. By the definition of A , as the vectors A ( ) have only non-zero entries, we can further without loss of generality assume that ≠ 0, with 1≤ ≤ − 1, implies | | = | |.Hence, for all ∈ N,