Worked example: query evaluation over probabilistic databases #
This file is a tutorial, read top to bottom like
DescriptiveComplexity.Examples.ConjunctiveQueries, and the first one about a
counting problem. The domain is probabilistic databases: a database whose
facts are present independently, each with its own probability, and the
problem of computing the probability that a fixed Boolean query holds
(probabilistic query evaluation). Its data complexity is #P-hard already
for the conjunctive query
h₀ = ∃ x y, R(x) ∧ S(x, y) ∧ T(y)
(Dalvi and Suciu 2012, Proposition 5.2), which is the first result formalized here; the second is that on the same instances the query
∃ x y, R(x) ∧ S(x, y)
is easy: the numerator of its probability is in FP, the polynomial-time functions. The two queries are the smallest pair on the two sides of the dichotomy of that paper, the second being hierarchical and the first not; the dichotomy itself is not formalized, and nothing here is generic in the query.
The model #
A fact is certain, or it is uncertain and present with some probability, independently of the others. The file works in two stages.
- Uniform probability. Steps 1 to 6 take every uncertain fact to be
present with probability
1/2. A probability is then a count: withkuncertain facts there are2 ^ kequally likely possible worlds (DescriptiveComplexity.card_isWorld), and the probability of a query is the number of worlds in which it holds, divided by2 ^ k. This is all the hardness ofh₀needs. - Probabilities in the instance. Step 7 gives each uncertain fact its own
probability
a / (a + c), as two natural weights written in binary (DescriptiveComplexity.Counting.WeightedWorlds). The probability of a query is then a ratio of two numbers,WeightedWorlds φ / WeightedWorlds ⊤(DescriptiveComplexity.funcProb_holdsEvent_eq_ratio), both of them in#Pfor every first-order query (DescriptiveComplexity.weightedWorlds_mem_sharpP). The uniform case is the case where all weights are1, so the hardness ofh₀carries over to the numerator.
The steps #
- Vocabulary. The schema
FirstOrder.Language.rstof the query; an instance is a structure over two copies of it, certain facts and uncertain ones. This step, and the next two, are generic in the schema and are library helpers, inDescriptiveComplexity.Counting.PossibleWorlds. - Semantics. A possible world keeps the certain facts and some of the
uncertain ones (
DescriptiveComplexity.IsWorld), andDescriptiveComplexity.PossibleWorlds φcounts the worlds satisfyingφ. - Membership. A world is one relation per symbol of the schema, so the
problem counts the witnesses of a first-order kernel: it is in
#Pfor every first-order query (DescriptiveComplexity.possibleWorlds_mem_sharpP). Nothing is specific toh₀up to here. - The query,
DescriptiveComplexity.h0, and what it says of a world (DescriptiveComplexity.realize_h0). - Hardness. From #PP2DNF (
DescriptiveComplexity.SharpPP2DNF), the models of⋁ (x ∧ y)over the edges of a bipartite graph. The instance has the edges as certainS-facts, the left vertices as uncertainR-facts and the right vertices as uncertainT-facts (DescriptiveComplexity.h0Interp); a world is then a set of vertices, andh₀holds in it exactly when some edge has both ends chosen (DescriptiveComplexity.possibleWorlds_h0_eq). The reduction is parsimonious, one-dimensional and quantifier-free. - The theorem: counting the worlds of
h₀is one-call#P-complete (DescriptiveComplexity.possibleWorlds_h0_sharpP_oneCallComplete). - Probabilities in the instance: the numerator of the probability of
h₀is one-call#P-complete (DescriptiveComplexity.weightedWorlds_h0_sharpP_oneCallComplete), by the ordered parsimonious reduction from the uniform case (DescriptiveComplexity.possibleWorlds_ordered_parsimonious_weightedWorlds). - A concrete database.
DescriptiveComplexity.ProbDbis a probabilistic database in plain terms:nconstants and, for each possible fact, whether it is absent, certain, or uncertain with two weights. Its weighted countDescriptiveComplexity.ProbDb.countand its total weightDescriptiveComplexity.ProbDb.totalare computed (the#guards at the end run them).DescriptiveComplexity.probDbEncodingencodes a database as a weighted instance, with the size bounds ofDescriptiveComplexity.Encodingdischarged, and it is faithful (DescriptiveComplexity.probDbEncoding_countFaithful,DescriptiveComplexity.probDbEncoding_countFaithful_total): the abstract counting problems return, on the encoded instance, the two numbers computed from the database. So the probability ofh₀over a database iscount / total(DescriptiveComplexity.probDb_worldProb_eq).
What kind of hardness #
#PP2DNF is itself complete under one-call reductions only, so the statement
is one-call completeness, not plain completeness: every problem of #P is
answered by one question about the probability of h₀, followed by
arithmetic. A formula of this kind always has a world in which it holds as
soon as the graph has an edge, so no parsimonious reduction from #SAT can
exist unless P = NP. Whether the problem is complete under subtractive
reductions is not known here.
- The decoder.
DescriptiveComplexity.probDbDecodereads a database back from any presented weighted instance whose position order is linear, by a computation (it runs, too), and the database has the weighted count of the instance (DescriptiveComplexity.probDbDecoding, aDescriptiveComplexity.CountDecoding). So the hardness of the abstract problem is hardness on instances that are databases.
The easy query #
Steps 10 to 15 are the second result, on the same weighted instances.
- The query,
DescriptiveComplexity.rs. - The weights. Every fact is a Boolean variable with a weight of
presence and a weight of absence
(
DescriptiveComplexity.Counting.WeightedFacts); the facts of the schema are theR-facts, theS-facts and theT-facts (DescriptiveComplexity.factEquiv). - Failing is a product. The query fails in a world when no
xhas bothR(x)and someS(x, y). Given whichR-facts are present, this constrains eachS-fact separately; summing over theR-facts then factorizes too. The weight of the worlds in which the query fails is∏x. F(x), times the total weight of theT-facts (DescriptiveComplexity.SafeQ.weightedCount_fail),F(x)being the weight of failing atx. - Succeeding, without a subtraction. The weight of the worlds in which
the query holds is the total weight minus that product. A quantitative
term has no subtraction; the first success identity
(
DescriptiveComplexity.prod_add_eq_prod_add_sum) removes it, by sorting the worlds by the firstxat which the query succeeds – and, inside the weight of succeeding atx, by the firsty(DescriptiveComplexity.SafeQ.weightedWorlds_rs_eq). - The term. The closed form is a term of quantitative first-order logic
over the instance, its leaves being the weights, read in binary
(
DescriptiveComplexity.SafeQ.rsT). The order it sorts by is the order of the definition, on which the value does not depend. - The theorem:
DescriptiveComplexity.weightedWorlds_rs_mem_FP.
Steps 12 and 13 use the library's lemmas on independent Boolean variables
(DescriptiveComplexity.Counting.Independence), and step 14 its helpers for
writing terms (DescriptiveComplexity.Counting.QuantitativeBinders,
DescriptiveComplexity.Counting.WeightedTerms).
The concrete step, and what it relies on #
The two other tutorials open with the concrete instances; here they come last
(steps 8 and 9), the hardness result needing none of it. The encoder and the
decoder are two corollaries of one theorem: an instance that matches a
database (DescriptiveComplexity.ProbDb.Matches) has its counts
(DescriptiveComplexity.ProbDb.Matches.count_eq). The step is short because
its two generic ingredients are library lemmas: the binary digits written by
an encoder decode to the number they came from
(DescriptiveComplexity.binNum_fin_of_testBit, in
DescriptiveComplexity.Numbers.BinEnum), and faithfulness for a counting
problem is DescriptiveComplexity.Encoding.CountFaithful. What remains is
specific to the database format: reading a status off each fact
(DescriptiveComplexity.factWeight_of_status) and matching the abstract
worlds and facts with the three tables of a concrete world
(DescriptiveComplexity.worldEquiv, DescriptiveComplexity.factEquiv).
One restriction of the format is deliberate: the weights are written on n
bits, n being the number of constants, since the constants double as bit
positions. A database with longer weights is padded with unused constants.
The relation symbols of the language.
Instances For
Dependency graph
Equations
- FirstOrder.Language.instDecidableEqRstRel.decEq FirstOrder.Language.rstRel.r FirstOrder.Language.rstRel.r = isTrue FirstOrder.Language.instDecidableEqRstRel.decEq._proof_1
- FirstOrder.Language.instDecidableEqRstRel.decEq FirstOrder.Language.rstRel.r FirstOrder.Language.rstRel.t = isFalse FirstOrder.Language.instDecidableEqRstRel.decEq._proof_3
- FirstOrder.Language.instDecidableEqRstRel.decEq FirstOrder.Language.rstRel.s FirstOrder.Language.rstRel.s = isTrue FirstOrder.Language.instDecidableEqRstRel.decEq._proof_2
- FirstOrder.Language.instDecidableEqRstRel.decEq FirstOrder.Language.rstRel.t FirstOrder.Language.rstRel.r = isFalse FirstOrder.Language.instDecidableEqRstRel.decEq._proof_4
- FirstOrder.Language.instDecidableEqRstRel.decEq FirstOrder.Language.rstRel.t FirstOrder.Language.rstRel.t = isTrue FirstOrder.Language.instDecidableEqRstRel.decEq._proof_5
Instances For
Dependency graph
Dependency graph
Dependency graph
Dependency graph
The schema of the example: two unary relations and a binary one.
Equations
- FirstOrder.Language.rst = { Functions := fun (x : ℕ) => Empty, Relations := FirstOrder.Language.rstRel }
Instances For
Dependency graph
Dependency graph
Dependency graph
Steps 1 and 4: the schema, and the query #
Dependency graph
The query h₀ = ∃ x y, R(x) ∧ S(x, y) ∧ T(y).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Dependency graph
Step 5: hardness, from #PP2DNF #
The worlds of a bipartite graph. Let the instance B hold a bipartite
graph A this way: S is certain and is the set of edges from the left side
to the right side, R is uncertain and is the left side, T is uncertain and
is the right side. Then the worlds satisfying h₀ are as many as the models
of the partitioned positive 2-DNF formula of the graph: a world is a set of
left vertices and a set of right vertices, and h₀ says that some edge has
both its ends chosen.
Dependency graph
The instance of a bipartite graph: the edges from left to right are the
certain S-facts, the left vertices the uncertain R-facts, and the right
vertices the uncertain T-facts. One-dimensional, single-tagged and
quantifier-free.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
#PP2DNF reduces parsimoniously to counting the worlds of h₀
(Dalvi and Suciu 2012, Proposition 5.2).
Equations
- DescriptiveComplexity.sharpPP2DNF_parsimonious_possibleWorlds_h0 = { Tag := Unit, tagFinite := ⋯, tagNonempty := ⋯, dim := 1, toInterpretation := DescriptiveComplexity.h0Interp, correct := ⋯ }
Instances For
Dependency graph
Step 6: the theorems #
Counting the worlds of h₀ is one-call #P-hard.
Dependency graph
Counting the worlds of h₀ is one-call #P-complete: it is in #P,
like the count of the worlds of any first-order query, and every problem of
#P reduces to it with one call.
Registered in the Lax archive as
Lax794877.H0Complete.possibleWorlds_h0_sharpP_oneCallComplete.
Dependency graph
Step 7: probabilities in the instance #
The weighted worlds of h₀ are one-call #P-hard to count, already
when every weight is 1, i.e., at uniform probability 1/2.
Dependency graph
The numerator of the probability of h₀ is one-call #P-complete:
counting the weighted worlds of h₀ is in #P, like those of any
first-order query, and every problem of #P reduces to it with one call.
Registered in the Lax archive as
Lax794877.H0Complete.weightedWorlds_h0_sharpP_oneCallComplete.
Dependency graph
Step 8: a concrete database, and its encoding #
What a database says of a fact: it is absent, it is certain, or it is
uncertain with weights a and c, i.e., present with probability
a / (a + c).
- absent : FactStatus
The fact is not in the database.
- certain : FactStatus
The fact is in the database for sure.
- uncertain
(a c : ℕ)
: FactStatus
The fact is present with weight
aand absent with weightc.
Instances For
Dependency graph
Dependency graph
Equations
- One or more equations did not get rendered due to their size.
- DescriptiveComplexity.instDecidableEqFactStatus.decEq DescriptiveComplexity.FactStatus.absent DescriptiveComplexity.FactStatus.absent = isTrue ⋯
- DescriptiveComplexity.instDecidableEqFactStatus.decEq DescriptiveComplexity.FactStatus.absent (DescriptiveComplexity.FactStatus.uncertain a c) = isFalse ⋯
- DescriptiveComplexity.instDecidableEqFactStatus.decEq DescriptiveComplexity.FactStatus.certain DescriptiveComplexity.FactStatus.certain = isTrue ⋯
- DescriptiveComplexity.instDecidableEqFactStatus.decEq DescriptiveComplexity.FactStatus.certain (DescriptiveComplexity.FactStatus.uncertain a c) = isFalse ⋯
- DescriptiveComplexity.instDecidableEqFactStatus.decEq (DescriptiveComplexity.FactStatus.uncertain a c) DescriptiveComplexity.FactStatus.absent = isFalse ⋯
- DescriptiveComplexity.instDecidableEqFactStatus.decEq (DescriptiveComplexity.FactStatus.uncertain a c) DescriptiveComplexity.FactStatus.certain = isFalse ⋯
Instances For
Dependency graph
The fact is certain.
Instances For
Dependency graph
The fact is uncertain.
Instances For
Dependency graph
The weight of presence of an uncertain fact (and 0 otherwise).
Equations
- (DescriptiveComplexity.FactStatus.uncertain a c).presW = a
- x✝.presW = 0
Instances For
Dependency graph
The weight of absence of an uncertain fact (and 0 otherwise).
Equations
- (DescriptiveComplexity.FactStatus.uncertain a c).absW = c
- x✝.absW = 0
Instances For
Dependency graph
A probabilistic database over the schema R, S, T, with n
constants: the status of every possible fact. The weights are written in
binary on n bits, so they are below 2 ^ n.
- n : ℕ
The number of constants.
- r : Fin self.n → FactStatus
The status of the fact
R(x). - s : Fin self.n → Fin self.n → FactStatus
The status of the fact
S(x, y). - t : Fin self.n → FactStatus
The status of the fact
T(y). The weights of the
R-facts fit innbits.The weights of the
S-facts fit innbits.The weights of the
T-facts fit innbits.
Instances For
Dependency graph
The encoding of a probabilistic database as a weighted instance: the universe is the set of constants, which serve as bit positions too, in their own order, and the bits of a weight are its binary digits.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The concrete semantics #
Everything here is computed: a world is three Boolean tables, and the weighted count of the query can be evaluated on a small database.
A world may give the fact the truth value b: an absent fact is false, a
certain fact is true, an uncertain fact is either.
Equations
Instances For
Dependency graph
The weight the fact contributes to a world giving it the truth value b:
its weight of presence or of absence if it is uncertain, and 1 otherwise.
Equations
Instances For
Dependency graph
Dependency graph
The world is a possible world of the database.
Equations
Instances For
Dependency graph
Equations
Dependency graph
The weight of a world: the product of the weights of the facts.
Equations
Instances For
Dependency graph
The query h₀ holds in a world.
Equations
Instances For
Dependency graph
Dependency graph
The concrete weighted count of h₀: the sum of the weights of the
possible worlds of the database in which the query holds.
Equations
Instances For
Dependency graph
The weighted count of all the possible worlds: the denominator of the probability.
Instances For
Dependency graph
Faithfulness #
Dependency graph
The concrete world of a family of relations.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The family of relations of a concrete world.
Equations
- DescriptiveComplexity.tableRels W ⟨1, FirstOrder.Language.rstRel.r⟩ = fun (x : Fin 1 → Fin n) => W.1 (x 0) = true
- DescriptiveComplexity.tableRels W ⟨2, FirstOrder.Language.rstRel.s⟩ = fun (x : Fin 2 → Fin n) => W.2.1 (x 0) (x 1) = true
- DescriptiveComplexity.tableRels W ⟨1, FirstOrder.Language.rstRel.t⟩ = fun (x : Fin 1 → Fin n) => W.2.2 (x 0) = true
Instances For
Dependency graph
Families of relations over the schema are concrete worlds.
Equations
- DescriptiveComplexity.worldEquiv n = { toFun := DescriptiveComplexity.worldTables, invFun := DescriptiveComplexity.tableRels, left_inv := ⋯, right_inv := ⋯ }
Instances For
Dependency graph
The facts over the schema: an R-fact, an S-fact or a T-fact.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
What a world may do with a fact, in terms of its status.
Dependency graph
The status of a fact, by cases: an uncertain fact is not certain.
Dependency graph
A weighted instance says of a fact what a status says: the fact is certain exactly when the status is, it is open exactly when the status is uncertain, and then its two weights, read in binary on the order of the instance, are those of the status.
The fact is certain exactly when its status is.
The fact is open exactly when its status is uncertain.
- pres : st.isUnc = true → binNum (WLe FirstOrder.Language.rst (Fin n)) (fun (x : Fin n) => True) (bitsOf (WeightedRel.pres q.fst.snd) q.snd) = st.presW
The weight of presence of an uncertain fact.
- abs : st.isUnc = true → binNum (WLe FirstOrder.Language.rst (Fin n)) (fun (x : Fin n) => True) (bitsOf (WeightedRel.abs q.fst.snd) q.snd) = st.absW
The weight of absence of an uncertain fact.
Instances For
Dependency graph
A weighted instance over the constants of a database matches it: its position order is linear, and it says of every fact what the database says.
- lin : IsLinOrd (WLe FirstOrder.Language.rst (Fin i.n))
The positions are linearly ordered.
The
R-facts.The
S-facts.The
T-facts.
Instances For
Dependency graph
The status the database gives a fact.
Equations
Instances For
Dependency graph
Dependency graph
The product of the weights of the facts, in a family of relations, is the weight of its concrete world.
Dependency graph
The possible worlds of the instance are the possible worlds of the database.
Dependency graph
On an instance matching a database, the abstract count of the weighted
worlds of h₀ is the weighted count computed from the database. The
encoder's faithfulness and the decoder's soundness are both this.
Dependency graph
On an instance matching a database, the abstract count of all the weighted worlds is the total weight computed from the database.
Dependency graph
The encoding is faithful #
The encoded structure of a database, on its constants.
Instances For
Dependency graph
The encoding is faithful: the abstract count of the weighted worlds of
h₀, on the encoded instance, is the weighted count computed from the
database.
Dependency graph
The encoding is faithful for the denominator too: the abstract count of all the weighted worlds is the total weight computed from the database.
Dependency graph
The probability of h₀ over a database is the ratio of the two numbers
computed from it. The uncertain facts of the database being present
independently, each with probability its weight of presence over the sum of
its weights, the probability that the query holds is count / total.
Registered in the Lax archive as
Lax794877.ExampleDatabaseProbability.probDb_worldProb_eq.
Dependency graph
Step 9: the decoder #
The converse of the encoding: from a concretely presented weighted instance back to a database, by a computation. The presented order has to be linear – otherwise the instance carries no number – and that is the only well-formedness condition. A fact that is both certain and uncertain is read as certain, which is what the semantics does with it.
The presented order of the positions, as a computation.
Equations
Instances For
Dependency graph
Dependency graph
Is the presented order linear?
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Dependency graph
A binary number of the presentation, as a computation: the sum of the place values of its bits, the place of a position being the number of positions strictly below it.
Equations
Instances For
Dependency graph
The status of a fact, from what the four relations say of it.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Dependency graph
Dependency graph
The bits the presentation attaches to a fact, through a symbol of arity one more than the fact's.
Equations
- DescriptiveComplexity.bitsB S R x p = S.relBool R (Fin.snoc x p)
Instances For
Dependency graph
The database a presentation with a linear order presents.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The presented structure matches the database decoded from it.
Dependency graph
The decoder: on a presentation whose order is linear, the database it
presents; none otherwise.
Equations
- DescriptiveComplexity.probDbDecode S = if h : DescriptiveComplexity.linB S = true then some (DescriptiveComplexity.decodeDb S ⋯) else none
Instances For
Dependency graph
Well-formedness of a weighted instance: its positions are linearly ordered. A first-order sentence.
Equations
Instances For
Dependency graph
The decoding: every well-formed presented instance decodes to a
database with the same weighted count of h₀. So the hardness of the abstract
problem is not hardness on structures no database produces.
Equations
- DescriptiveComplexity.probDbDecoding = { dec := DescriptiveComplexity.probDbDecode, sound := ⋯, total := ⋯ }
Instances For
Dependency graph
A database, computed #
Two constants. R(0) is uncertain with probability 1/2, S(0, 1) is
certain, T(1) is uncertain with probability 1/4. The query holds in the
one world keeping both uncertain facts, of weight 1 · 1, out of a total
weight (1 + 1) · (1 + 3) = 8: its probability is 1/8.
A small probabilistic database.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The encoded instance of the database, as a presentation: what a decoder reads.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The decoder runs too, and gets the two numbers back from the encoded instance.
Step 10: the easy query #
The query ∃ x y, R(x) ∧ S(x, y).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Step 11: the weights #
The weight of presence of the fact R(x).
Equations
Instances For
Dependency graph
The weight of absence of the fact R(x).
Equations
Instances For
Dependency graph
The weight of presence of the fact S(x, y).
Equations
Instances For
Dependency graph
The weight of absence of the fact S(x, y).
Equations
Instances For
Dependency graph
The total weight of the fact T(y), which the query does not read.
Equations
Instances For
Dependency graph
The weight of failing at x: R(x) is present and no S(x, y) is,
or R(x) is absent.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The weight of succeeding at x: R(x) is present, and the S(x, y)
are sorted by the first y that is present.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Step 12: failing is a product #
The facts, as variables: the R-facts, the S-facts and the T-facts.
Equations
- DescriptiveComplexity.SafeQ.Var A = (A ⊕ A × A ⊕ A)
Instances For
Dependency graph
“The query holds”, on a valuation of the facts.
Equations
Instances For
Dependency graph
Failing is a product: the weight of the worlds in which the query
fails is the product, over x, of the weight of failing at x, times the
total weight of the T-facts.
Dependency graph
Step 13: succeeding, without a subtraction #
The count of the weighted worlds of the query, in closed form: the
worlds in which the query holds, sorted by the first x at which it
succeeds.
Dependency graph
Step 14: the term #
The weight of presence of R(x), as a term.
Equations
Instances For
Dependency graph
The weight of absence of R(x), as a term.
Equations
Instances For
Dependency graph
The weight of presence of S(x, y), as a term.
Equations
Instances For
Dependency graph
The weight of absence of S(x, y), as a term.
Equations
Instances For
Dependency graph
The total weight of T(y), as a term.
Equations
Instances For
Dependency graph
The weight of failing at x, as a term.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The weight of succeeding at x, as a term: a sum over the first y.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
The term computing the weighted count of the query: the total weight
of the T-facts, times the sum, over the first x at which the query
succeeds, of the weight of failing before, succeeding at x, and doing
anything after; all of it 0 unless the positions are linearly ordered.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Dependency graph
Step 15: the theorem #
The weighted count of ∃ x y, R(x) ∧ S(x, y) is in FP: the numerator
of the probability of this query over a probabilistic database is computed in
polynomial time, where that of ∃ x y, R(x) ∧ S(x, y) ∧ T(y) is #P-hard
(DescriptiveComplexity.weightedWorlds_h0_sharpP_oneCallComplete).
Registered in the Lax archive as
Lax794877.HierarchicalQueryInFP.weightedWorlds_rs_mem_FP.