text stringlengths 128 2.05k |
|---|
A recursive algorithm to solve this problem for [MATH] consists in [MATH] assuming that the first bit is ”0”, make a recursive call with [MATH] to concatenate with words of length [MATH] and [MATH] occurrences of ”1”, then [MATH] assuming that the first bit is ”1”, make a recursive call with [MATH] to concatenate with ... |
Algorithm 5 example of recursive generator 1: generator genWords [MATH] 2: if [MATH] then 3: if [MATH] then 4: yield [MATH] 5: end |
if 6: else 7: for [MATH] do 8: yield [MATH] 9: end for 10: for [MATH] do 11: yield [MATH] 12: end for 13: end if 14: end generator |
15: 16: function main ( ) 17: for [MATH] do 18: display [MATH] 19: end for 20: end function Invoking the main subroutine displays the list of 6 words given above, in the same order. The execution of a recursive generator is very different from the execution of a usual recursive subroutine. In the present case, at each ... |
Appendix B Hints on implementation The Statues algorithm is expressed in a highly abstract way, in order to be short and language-neutral. We aim here to provide some suggestions for implementing this algorithm in an actual program. We assume that the target programming language natively supports recursive generators a... |
1. If the programming language supports object orientation, it is advisable to define the different types of pex in dedicated classes. These classes should inherit from one abstract class representing any pex. The DAG structure can be captured using the composite design pattern (Gamma et al. ,, 1995 . Then, following t... |
2. The referential consistency, a key aspect of the algorithm, requires that pex instances are assembled in a DAG without duplication of instances (see section 3.1 ). In any pex, each occurrence of a name shall refer to the same object. This may be achieved easily by using references or pointers. |
3. There are several ways to implement the binding store [MATH] , which is a mutable object that must be shared by all pexes. As suggested by the algorithm, [MATH] could be implemented by using an associative array accessible as a global variable (the keys could be, for example, variable names or references to pex inst... |
4. In some cases, functional pex can cause errors (e.g. division by zero), which prevents resuming the calculation. In many language, this may raise an exception that halts the processing. In such situation, depending on the implementation of the binding store, it may be important to unbind all pexes that are currently... |
5. If operator overloading is supported by the programming language, the expressiveness of functional pexes may be greatly improved by redefining infix operators for arithmetic ( [MATH] ), comparison ( [MATH] ) and logic (not, and, or, …). |
6. The pmf for elementary pexes do not need a direct access through dictionary. A compact data structure allowing sequential access is sufficient for the algorithm. |
7. A lot of functional pexes cover 2-ary functions. The performance of the algorithm can be slightly improved by having a dedicated functional pex, which does not rely on tuple RVs to make the combinatorics. Such case shall be handled by a dedicated case in the genAtomsByType generator, with two embedded for loops. |
8. The binding/unbinding performed by genAtoms is unconditional. In several cases however, such handling is superfluous; actually, this treatment could be skipped in two cases: for singleton elementary pexes and for any pex which is referred only once in the evaluated pex. An optimized implementation could then detect ... |
For the readers eager to see concrete application of these hints in Python, let us mention that the [MATH] Lea implementation (section 6.2 ) follows the points 1, 2, 3, 4 (binding value attribute), 5 and 6 presented above. The Lea implementation (section 6.1 ) follows all the eight hints but, as already explained, is h... |
Appendix C Proof of algorithm correctness We provide in the present appendix a proof of the correctness of the Statues algorithm. This proof uses propositional logic and basic probability theory. |
C.1 Conventions and definitions In the following, an uppercase latin letter, like [MATH] , represents a random variable (as defined in ), a lowercase latin letter, like [MATH] , represents a value or a pex (as defined in 3.1 ) and a lower case greek letter, like [MATH] represents a logical proposition. By convention, t... |
[EQUATION] is defined as a disjunction of a subset of [MATH] propositions [MATH] , each of which is present only if some condition cond [MATH] is true. If [MATH] or if cond [MATH] is false for any [MATH] , then this expression is defined to be equivalent to [MATH] |
We shall use the following definitions. Definition C.1 The set of propositions [MATH] are mutually exclusive iff [EQUATION] Definition C.2 |
binding assertion [MATH] is a logical proposition of the form [EQUATION] for any set of [MATH] random variables [MATH] and any set of [MATH] values [MATH] such that this conjunction is not contradictory, i.e. [MATH] . A state expressing no binding (in particular, at algorithm start-up) is indicated by the binding asser... |
Definition C.3 Be a binding assertion [MATH] and a pex [MATH] for some random variable [MATH] . We say that [MATH] binds [MATH] (or [MATH] |
binds [MATH] ) iff [MATH] appears in some equality present in [MATH] . We have then [EQUATION] for some value [MATH] With such definitions, the state of the binding store, i.e. the binding of any subset of random variables at any given state of the execution of the algorithm, can be expressed as a binding assertion. |
C.2 Adding invariants to the algorithm The Statues algorithm involves one entry-point subroutine ( marg ) and two generators genAtoms and genAtomsByType . To establish a formal proof of the correctness of marg , we need first to specify formally what is meant to be correct for marg and, incidentally, the same for genAt... |
Algorithm 6 Statues algorithm – part 2 (rewritten): genAtoms generator 1: generator genAtoms [MATH] [MATH] assuming [MATH] 2: if |
[MATH] then [MATH] [MATH] is bound 3: yield [MATH] [MATH] yield unique atom to caller 4: else [MATH] [MATH] is unbound 5: for [MATH] |
do 6: yield [MATH] [MATH] yield atom to caller 7: end for 8: end if 9: end generator Here are some explanations on the specific example of algorithm to help understanding of these invariant notations. The given binding condition [MATH] is true on the whole execution of genAtoms (from line 2 to 8), even if the control i... |
Algorithm 7 Statues algorithm – part 3 (rewritten): genAtomsByType generator 1: generator genAtomsByType [MATH] 2: 3: switch [MATH] |
do 4: 5: case [MATH] [MATH] [MATH] is an elementary pex 6: for [MATH] do 7: yield [MATH] 8: end for 9: 10: case [MATH] [MATH] [MATH] is a functional pex |
[MATH] do 33: for [MATH] do 34: yield [MATH] 35: end for 36: end for 37: 38: end generator To be consistent, the marg subroutine (algorithm ) shall be slightly updated also: the binding condition [MATH] shall be added as genAtoms argument; this is meant to declare that there is no binding at start-up; this replaces the... |
Algorithm 8 Statues algorithm – part 1 (rewritten): marg subroutine 1: function marg [MATH] 2: [MATH] [MATH] init unnormalized pmf |
3: for [MATH] do [MATH] collect atoms 4: if [MATH] then 5: [MATH] 6: end if 7: [MATH] [MATH] condense pmf 8: end for 9: if [MATH] |
then [MATH] pmf is empty: error 10: halt with error 11: end if 12: [MATH] [MATH] normalize pmf 13: return [MATH] 14: end function |
C.3 Formal specifications In order to establish the proof of correctness, we have to provide a specification formalizing the pre- and post-conditions of the marg subroutine, the entry-point of the algorithm. |
Definition C.4 Be a pex [MATH] for some random variable [MATH] . The subroutine marg [MATH] is correct iff it terminates – either by reporting an error if dom [MATH] is empty |
– or by returning a pmf [MATH] such that the following two conditions hold: {addmargin} [4em]0em [EQUATION] Since the marg subroutine calls genAtoms , which itself calls genAtomsByType , these two generators shall also be formally specified. Note that, contrarily to the specification given above, the following specific... |
Definition C.5 Be a pex [MATH] for some random variable [MATH] and be a binding assertion [MATH] . The generator genAtoms [MATH] is correct iff it yields [MATH] atoms [MATH] such that the following six conditions hold: |
{addmargin} [4em]0em [EQUATION] Definition C.6 Be a pex [MATH] for some random variable [MATH] and be a binding assertion [MATH] not binding [MATH] . The generator genAtomsByType [MATH] is correct iff it yields [MATH] atoms [MATH] such that the following five conditions hold: |
{addmargin} [4em]0em [EQUATION] In the previous definitions, the index [MATH] is meant to cover the range [MATH] or the empty range if [MATH] ; in GA5 and GABT5, [MATH] is meant for any value, whether belonging to dom [MATH] or not. These definitions cover the case [MATH] , where no atom is yielded. In such case, the c... |
C.4 Proof Outline of the proof . Proving the correctness of the Statues algorithm consists in proving that the entry-point subroutine marg is correct (proposition C.11 ). This in turn requires proving that genAtoms generator is correct (proposition C.10 ) and that genAtomsByType generator is correct (proposition C.9 );... |
We first state hereafter two general propositions: the first one is a generalization of the conditional probability formula, the second one establishes the conservation of mutual exclusiveness when adding conjunctions. |
Proposition C.1 For any propositions [MATH] such that [MATH] is not contradictory, [EQUATION] Proof. Be the propositions [MATH] such that [MATH] is not contradictory. Using the formula of conditional probability, we can derive |
[EQUATION] Proposition C.2 For any set of [MATH] mutually exclusive propositions [MATH] and for any set of [MATH] propositions [MATH] , the set of conjunctions [MATH] are mutually exclusive. |
Proof. Be a set of [MATH] mutually exclusive propositions [MATH] and be a set of [MATH] propositions [MATH] . Be the indexes [MATH] and [MATH] . Using definition C.1 , we can derive |
[EQUATION] Hence, [MATH] are mutually exclusive. We are now equipped to establish several propositions that eventually prove that genAtomsByType and genAtoms are correct with regard to their specifications given in C.6 and C.5 |
Proposition C.3 For any pex [MATH] and for any binding assertion [MATH] , if [MATH] binds [MATH] or if genAtomsByType [MATH] is correct, then genAtoms [MATH] is correct. |
Proof. Be a pex [MATH] for some random variable [MATH] and be a binding assertion [MATH] . According to algorithm , there are two cases to examine, depending whether [MATH] binds [MATH] or not. |
Case 1 If [MATH] binds [MATH] , that is [MATH] for some value [MATH] , then genAtoms [MATH] yields the sole atom [MATH] (see line 3 of algorithm ). As required by the specification C.5 , there are six statements to verify: GA1, GA2, GA3, GA4, GA5 and GA6. |
GA1 genAtoms [MATH] terminates since no loop is executed. GA2 [MATH] is a binding assertion, by assumption. GA3 [MATH] is trivially mutually exclusive since it is a singleton. |
GA4 [MATH] is trivially verified. GA5 : Be a value [MATH] The implication to prove is [EQUATION] There are two cases to verify: {addmargin} [1em]0em - if [MATH] , then ( ) becomes [MATH] |
- if [MATH] , then ( ) becomes [MATH] In both cases, the statement ( ) is verified due to the condition [MATH] stated in the present case. |
GA6 : The implication [MATH] is trivially verified since it is the condition stated in the present case. Case 2 If [MATH] does not bind [MATH] , then, according to lines 5-7 of algorithm genAtomsByType [MATH] yields [MATH] . Since we assume here that this generator is correct, this set of atoms verify the statements GA... |
GA1 genAtoms [MATH] terminates since GABT1 ensures that the executed loop terminates. GA2 GA3 GA4 : these three statements are trivially verified since, in the present case, these are equivalent respectively to GABT2, GABT3, GABT4, which are verified by assumption. |
GA5 : Be a value [MATH] . Using GABT5, we can derive [EQUATION] GA6 : Be an index [MATH] . The implication [MATH] is trivially verified. |
The following five propositions establish the correctness of genAtomsByType (algorithm ) for each of the five types of pex. For the four derived pex types, the correctness of genAtoms is assumed. These propositions shall then be used in the proof by induction stating the unconditional correctness of genAtomsByType (pro... |
Proposition C.4 For any elementary pex [MATH] and for any binding assertion [MATH] not binding [MATH] [MATH] is correct. Proof. Be an elementary pex [MATH] for some elementary random variable [MATH] . According to lines 6-8 of algorithm , the [MATH] generator yields [MATH] for the [MATH] elements of the pmf, with [MATH... |
GABT1 : The [MATH] generator terminates after having yielded the [MATH] elements of its pmf, which is finite by definition. GABT2 : Be an index [MATH] [MATH] is a binding assertion since, by assumption, [MATH] is a binding assertion not binding [MATH] and since [MATH] by definition of a pmf. |
GABT3 [MATH] by application of proposition C.2 , given that the [MATH] values are distinct by definition of a pmf. GABT4 : Be an index [MATH] . Since [MATH] is not binding [MATH] , we have |
[EQUATION] By definition of a pmf, [MATH] and [MATH] . So the expected equality [EQUATION] is verified. GABT5 : Be a value [MATH] . The implication to verify is |
[EQUATION] There are two cases to distinguish, whether [MATH] belongs to [MATH] or not. {addmargin} [1em]0em - if [MATH] , then ( ) becomes [MATH] , which is trivially verified; |
- if [MATH] , then the disjonction has no member and is [MATH] by definition; then ( ) becomes [MATH] , which is verified since [MATH] |
Because the generators are mutually recursive, their correctness is proved by induction. This proof technique requires associating a natural number to each pex, in order to be able to express the base case and the inductive step. For this purpose, we introduce the concept of level of a pex: |
Definition C.7 The level of a given pex [MATH] is defined as follows: [EQUATION] The level of a given pex can be interpreted as the length of the longest path in its associated DAG. For instance, the pex represented on figure has a level 4 and the pex represented on figure has a level 7. This concept of level is needed... |
Proposition C.5 For any functional pex [MATH] and for any binding assertion [MATH] not binding [MATH] , if genAtoms [MATH] is correct for any pex [MATH] such that [MATH] , then [MATH] is correct. |
Proof. Be a functional pex [MATH] for some function [MATH] and some random variable [MATH] . Be a binding assertion [MATH] not binding [MATH] . According to lines 11-13 of the algorithm [MATH] yields [MATH] at iteration [MATH] . Since [MATH] , we have |
[MATH] , hence we can assume here that this genAtoms generator is correct and that it verifies the statements GA1, GA2, GA3, GA4, GA5 and GA6. Then, [MATH] yields [MATH] . As required by the specification C.6 , there are five statements to verify: GABT1, GABT2, GABT3, GABT4 and GABT5. |
GABT1 genAtomsByType [MATH] terminates since GA1 ensures that the executed loop terminates and since [MATH] terminates in any case ( [MATH] is a true function by definition of functional pex). |
GABT2 : Be an index [MATH] . Using GA6, we can derive [EQUATION] Since [MATH] is a binding assertion by GA2, [MATH] is also a binding assertion, given that ( 10 ) ensures that there is no contradiction. |
GABT3 [MATH] are mutually exclusive by application of proposition C.2 , given that [MATH] are mutually exclusive by GA3. GABT4 : Be an index [MATH] . Since GA2 ensures that [MATH] is non contradictory, we can use proposition C.1 |
[EQUATION] The first factor can be replaced by a fraction using GA4 equality; according to ( 10 ), the second factor is equal to 1. Hence, we get the expected equality: |
[EQUATION] GABT5 : Be a value [MATH] . Using the definition of [MATH] and GA5 relations, we can derive [EQUATION] Proposition C.6 |
For any tuple pex [MATH] and for any binding assertion [MATH] not binding [MATH] , if genAtoms [MATH] is correct for any pex [MATH] such that [MATH] , then [MATH] is correct. |
Proof. Be a tuple random variable [MATH] for some random variables [MATH] and [MATH] , where [MATH] is either a tuple random variable or the empty tuple [MATH] . Be the tuple pex [MATH] and be a binding assertion [MATH] not binding [MATH] . According to lines 16-20 of algorithm [MATH] yields [MATH] at iteration [MATH] ... |
[MATH] and [MATH] , hence we can assume here that these genAtoms generators are correct and that they verify the statements GA1, GA2, GA3, GA4, GA5 and GA6. Then, the [MATH] yields [MATH] at iteration [MATH] . As required by the specification C.6 , there are five statements to verify: GABT1, GABT2, GABT3, GABT4 and GAB... |
GABT1 genAtomsByType [MATH] terminates since the GA1 conditions ensure that the executed loops terminate. GABT2 : Be the indexes [MATH] . Using GA6, we can derive |
[EQUATION] Since [MATH] is a binding assertion by GA2, [MATH] is also a binding assertion, given that ( 14 ) ensures that there is no contradiction. |
GABT3 [MATH] are mutually exclusive by application of proposition C.2 , given that [MATH] are mutually exclusive by GA3. GABT4 : Be the indexes [MATH] . Since GA2 ensures that [MATH] is non contradictory, we can use proposition C.1 in chain: |
[EQUATION] The first two factors can be replaced by fractions using GA4 equalities. According to ( 14 ), the third factor is equal to 1. Hence, we get the expected equality: |
[EQUATION] GABT5 : Be a value [MATH] . There are two cases to distinguish, depending whether [MATH] is a tuple or not. Case 1 If [MATH] is not a tuple, then the expected implication |
[EQUATION] is verified since the disjonction has no member, hence both sides of the implication are false. Case 2 If [MATH] is a tuple, then [MATH] for some value [MATH] and some tuple [MATH] . Using GA5 relations, we can derive |
[EQUATION] Proposition C.7 For any conditional pex [MATH] and for any binding assertion [MATH] not binding [MATH] , if genAtoms [MATH] is correct for any pex [MATH] such that [MATH] , then genAtomsByType [MATH] is correct. |
Proof. Be a conditional random variable [MATH] for some random variables [MATH] and [MATH] , where [MATH] is boolean. Be the conditional pex [MATH] and be a binding assertion [MATH] not binding [MATH] . According to the lines 23-29 of algorithm C.6 genAtoms [MATH] yields [MATH] at iteration [MATH] and, if [MATH] is [MA... |
[MATH] and [MATH] , hence we can assume here that these genAtoms generators are correct and that they verify the statements GA1, GA2, GA3, GA4, GA5 and GA6. Then, [MATH] yields [MATH] at iteration [MATH] for the index [MATH] such that [MATH] is [MATH] . As required by the specification C.6 , there are five statements t... |
GABT1 genAtomsByType [MATH] terminates since the GA1 conditions ensure that the executed loops terminate. GABT2 : Be the indexes [MATH] such that [MATH] is [MATH] . Using GA6, we can derive |
[EQUATION] Since [MATH] is a binding assertion by GA2, [MATH] is also a binding assertion, given that ( 19 ) ensures that there is no contradiction. |
GABT3 [MATH] where [MATH] is [MATH] are mutually exclusive by application of proposition C.2 , given that [MATH] are mutually exclusive by GA3. |
GABT4 : Be the indexes [MATH] such that [MATH] is true. Since GA2 ensures that [MATH] is non contradictory, we can use proposition C.1 in chain: |
[EQUATION] The first two factors can be replaced by fractions using GA4 relations; according to ( 19 ), the third factor is equal to 1. Hence, we get the expected equality |
[EQUATION] GABT5 : Be a value [MATH] . Using the definition of [MATH] and GA5 relations, we can derive [EQUATION] Proposition C.8 |
For any table pex [MATH] and any binding assertion [MATH] not binding [MATH] , if genAtoms [MATH] is correct for any pex [MATH] such that [MATH] , then genAtomsByType [MATH] is correct. |
Proof. Be a table random variable [MATH] for some random variable [MATH] and for table [MATH] associating each value [MATH] of dom [MATH] to some random variable [MATH] . According to the definition of a table RV, we can write also [MATH] , which is more convenient for the rest of the proof. Be the table pex [MATH] , w... |
[MATH] and [MATH] , hence we can assume here that these genAtoms generators are correct and that they verify the statements GA1, GA2, GA3, GA4, GA5 and GA6. Then, [MATH] yields [MATH] at iteration [MATH] for the index [MATH] . As required by the specification C.6 , there are five statements to verify: GABT1, GABT2, GAB... |
GABT1 genAtomsByType [MATH] terminates since the GA1 conditions ensure that the executed loops terminate. GABT2 : Be the indexes [MATH] . Be the indexes [MATH] . Using GA6, we can derive |
[EQUATION] Since [MATH] is a binding assertion by GA2, [MATH] is also a binding assertion, given that ( 23 ) ensures that there is no contradiction. |
[EQUATION] The first two factors can be replaced by fractions using GA4 relations. According to ( 23 ), the third factor is equal to 1. Hence, we get the expected equality |
[EQUATION] GABT5 : Be a value [MATH] . Using the definition of [MATH] and GA5 relations, we can derive [EQUATION] The previous propositions can now be used to prove the unconditional correctness of the genAtomsByType and genAtoms generators. |
Proposition C.9 For any pex [MATH] and any binding assertion [MATH] not binding [MATH] [MATH] is correct. Proof. Be a pex [MATH] and a binding assertion [MATH] . The proof goes by induction, using level [MATH] . If level( [MATH] ) = 0, then [MATH] is an elementary pex and genAtomsByType [MATH] is correct by proposition... |
Proposition C.10 For any pex [MATH] and any binding [MATH] genAtoms [MATH] is correct. Proof. This is a direct application of propositions C.9 and C.3 |
Now that the correctness of the generator genAtoms has been established according to its specification C.5 , we are able to conclude the proof of the Statues algorithm, by proving the correctness of the marg subroutine, as specified in C.4 |
Proposition C.11 For any pex [MATH] marg [MATH] is correct. Proof. Be a pex [MATH] for some random variable [MATH] . According to line 3 of algorithm [MATH] yields [MATH] at iteration [MATH] . By proposition C.10 , since [MATH] is a binding assertion, genAtoms [MATH] is correct: it yields [MATH] atoms [MATH] that verif... |
Case 1 If no atom is yielded ( [MATH] ), then the condition GA5 becomes [EQUATION] which expresses that dom [MATH] is empty. Since [MATH] , the [MATH] pmf remains empty: according to algorithm (lines 9-11), the marg function reports an error. This is the correct behavior when dom [MATH] is empty, according to definitio... |
Case 2 If at least one atom is yielded ( [MATH] ), then the genAtoms function returns a pmf [MATH] from the atoms [MATH] yielded by genAtoms . Be a value [MATH] . We examine two cases, depending whether [MATH] belongs or not to dom [MATH] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.