Reasoning about Arrays Aaron R. Bradley CU Boulder max := a[l]; - - PowerPoint PPT Presentation

reasoning about arrays
SMART_READER_LITE
LIVE PREVIEW

Reasoning about Arrays Aaron R. Bradley CU Boulder max := a[l]; - - PowerPoint PPT Presentation

Reasoning about Arrays Aaron R. Bradley CU Boulder max := a[l]; for(i := l+1; i <= u; i++) if (a[i] > max) max := a[i]; Establish postcondition: j. l j u a [ j ] max Loop invariant: j. l j < i a [ j ]


slide-1
SLIDE 1

Reasoning about Arrays

Aaron R. Bradley CU Boulder

slide-2
SLIDE 2

max := a[l]; for(i := l+1; i <= u; i++) if (a[i] > max) max := a[i];

Establish postcondition:

∀j. l ≤ j ≤ u → a[j] ≤ max

Loop invariant:

∀j. l ≤ j < i → a[j] ≤ max

Also establish postcondition:

∀j. a[j] = a0[j]

slide-3
SLIDE 3

for(i := 0; i < length(sets); i++) u := union(u, sets[i]);

Given postcondition of union(u, v):

rv = u ∪ v

Establish postcondition:

∀j. 0 ≤ j ≤ |sets| → sets[j] ⊆ u

Loop invariant:

∀j. 0 ≤ j < i → sets[j] ⊆ u

slide-4
SLIDE 4

Loop invariant:

∀j. 0 ≤ j < i → sets[j] ⊆ u

Translation:

∀j. 0 ≤ j < i → ∀e. sets[j][e] → u[e]

slide-5
SLIDE 5

Sets

  • e ∈ s :

s[e]

  • s ⊆ t

∀e. s[e] → t[e]

  • s ⊂ t

(∀e. s[e] → t[e]) ∧ (∃e1. ¬s[e1] ∧ t[e1])

  • s = t ∩ u

∀e. s[e] ↔ t[e] ∧ u[e]

  • s = t

∀e. s[e] ↔ ¬t[e]

Multisets (bags)

  • C(s, e)

s[e]

  • s = t ⊎ u

∀e. s[e] = t[e] + u[e]

  • . . .
slide-6
SLIDE 6

assert(v >= 0); ht := put(ht, k, v);

Precondition:

∀j ∈ keys(ht). get(ht, j) ≥ 0

Establish postcondition:

∀j ∈ keys(ht). get(ht, j) ≥ 0

Verification condition:

(∀j ∈ keys(ht). get(ht, j) ≥ 0) ∧ v ≥ 0 ∧ h′ = put(ht, k, v) → (∀j ∈ keys(h′). get(h′, j) ≥ 0)

slide-7
SLIDE 7

Flat data structures

  • Integer-indexed arrays
  • Collections: sets, multisets (bags)
  • Hashtables

Model and reason about them as arrays (uninterpreted functions).

slide-8
SLIDE 8

First-Order Theory T : (Σ, A)

  • Signature Σ: non-logical symbols (a, b, +, <, . . . )
  • Axioms A: formulae interpreting symbols

T -Interpretation I : (D, α)

  • Domain D: set of objects
  • Assignment α: assigns Σ-symbols to domain elements,

functions, predicates

  • for each F ∈ A, I |

= F Σ-formula F is T -valid iff for every T -interpretation I, I | = F .

slide-9
SLIDE 9

F is T -valid

iff

¬F is T -unsatisfiable

slide-10
SLIDE 10

Decision Problem for T

Decide if Σ-formula F is T -valid.

T is set of T -valid Σ-formulae.

slide-11
SLIDE 11

TA: First-Order Theory of Arrays

Signature:

ΣA : {a[i], ai ⊳ v, =}

Axioms:

  • Equality axioms
  • Infinite domain axiom schema: for all n > 0

∀i1, . . . , in. ∃j.

n

  • k=1

j = ik

  • Read-over-write

∀a, i, j, v. i = j → ai ⊳ v[j] = v ∀a, i, j, v. i = j → ai ⊳ v[j] = a[j]

slide-12
SLIDE 12

T Z

A : First-Order Theory of Integer-Indexed Arrays Signature:

ΣZ

A : ΣA ∪ ΣZ = {a[i], ai ⊳ v, =, 0, 1, +, ≥}

Axioms:

  • Axioms of integer arithmetic
  • Equality axioms
  • Read-over-write

∀a, i, j, v. i = j → ai ⊳ v[j] = v ∀a, i, j, v. i = j → ai ⊳ v[j] = a[j]

slide-13
SLIDE 13

Fragment of T

Subset of T given by syntactic restriction. Example: “quantifier-free” fragment (QFF) of TA Is

a[i] = e1 ∧ e1 = e2 → ai ⊳ e2[i] = a[i] TA-valid?

Alternately, is

a[i] = e1 ∧ e1 = e2 ∧ ai ⊳ e2[i] = a[i] TA-unsatisfiable?

slide-14
SLIDE 14

Nelson-Oppen Combination Method

Given:

  • Theories T1, . . . , Tk that share only = (and are stably infinite)
  • Decision procedures P1, . . . , Pk
  • Quantifier-free (Σ1 ∪ · · · ∪ Σk)-formula F

Decide if F is (T1 ∪ · · · ∪ Tk)-satisfiable using P1, . . . , Pk. Think about arrays in context of Nelson-Oppen.

slide-15
SLIDE 15

History

  • 1962: John McCarthy formalizes arrays as first-order theory TA.
  • 1969: James King describes and implements DP for QFF of TA.
  • 1979: Nelson & Oppen describe combination method for QF

theories sharing =.

  • 1980s: Suzuki, Jefferson; Jaffar; Mateti describe DPs for QFF of

theories of arrays with predicates for sorted, partitioned, etc.

  • 1997: Levitt describes DP for QFF of extensional theory of

arrays in thesis.

  • 2001: Stump, Barrett, Dill, Levitt describe DP for QFF of

extensional theory of arrays.

slide-16
SLIDE 16
  • 2006: Bradley, Manna, Sipma describe DP for array property

fragment of TA, T Z

A .

  • Other recent references:

– Sofronie-Stokkermans et al.: local theory extensions – Ghilardi, Nicolini, Ranise, Zucchelli – Iosef, Habermehl, Vojnar: use flat counter automata – Fontaine: Combinations with Bernays-Schonfinkel-Ramsey class

slide-17
SLIDE 17

Array Property Fragment of TA

Array property:

∀i. F[i] → G[a[i]]

  • F : index guard

iguard := iguard ∧ iguard | iguard ∨ iguard | atom atom := var = var | evar = var | var = evar | ⊤ var := evar | uvar

  • G: value constraint

i only appears in a[i] (possibly within nested array properties)

Array property fragment: Boolean combination of array properties and QF formulae.

slide-18
SLIDE 18

Array Property Fragment of TA ∪ T

Same definition when T is a Nelson-Oppen theory.

slide-19
SLIDE 19

Decision Procedure

Given: Array property formula F

  • 1. F1: push negations to atoms
  • 2. F2: Eliminate writes

G[ai ⊳ v] G[a′] ∧ a′[i] = v ∧ (∀j. j = i → a[j] = a′[j])

  • 3. Construct index set

I : {t : t is symbolic index} ∪ {κ}

slide-20
SLIDE 20
  • 4. F4: κ is unique

F2 ∧

  • t∈I\κ

κ = t

  • 5. F5: Instantiate quantifiers

H[∀i. G[i]] = ⇒ H

  • t∈I

G[t]

  • 6. F5 is QF

. Decide satisfiability using Nelson-Oppen DP .

slide-21
SLIDE 21

Example: Extensional theory (Stump et al., 2001) a = bi ⊳ v ∧ a[i] = v

In array property fragment:

(∀j. a[j] = bi ⊳ v[j]) ∧ a[i] = v

Eliminate write:

(∀j. a[j] = b′[j]) ∧ a[i] = v ∧ b′[i] = v ∧ (∀j. j = i → b′[j] = b[j])

Index set:

I : {i, κ}

slide-22
SLIDE 22

QF formula:

a[i] = b′[i] ∧ a[κ] = b′[κ] ∧ a[i] = v ∧ b′[i] = v ∧ (i = i → b′[i] = b[i]) ∧ (κ = i → b′[κ] = b[κ]) ∧ κ = i

Simplified:

a[i] = b′[i] ∧ a[κ] = b′[κ] ∧ a[i] = v ∧ b′[i] = v ∧ b′[κ] = b[κ] ∧ κ = i

slide-23
SLIDE 23

Why κ? (∀i. a[i] > 0) ∧ (∀i. a[i] < 0)

But requires infinite domain for indices. Recall axiom schema: For all n > 0

∀i1, . . . , in. ∃j.

n

  • k=1

j = ik

slide-24
SLIDE 24

Correctness

  • Sound? It’s just quantifier elimination (except for κ).
  • Complete?

Assume I |

= F5. Construct J such that J | = F . proj(t) =    i

if αI[t] = vi for some i ∈ I

κ

  • therwise
slide-25
SLIDE 25

F[proj(i)] G[a[proj(i)]] K | = F[i] G[a[i]] (1) (2)

?

slide-26
SLIDE 26

Array Property Fragment of T Z

A Array property:

∀i. F[i] → G[a[i]]

  • F : index guard

iguard := iguard ∧ iguard | iguard ∨ iguard | atom atom := expr ≤ expr | expr = expr expr := uvar | pexpr pexpr := pexpr′ pexpr′ := Z | Z · evar | pexpr′ + pexpr′

  • G: value constraint

i only appears in a[i] (possibly within nested array properties)

slide-27
SLIDE 27

Array property fragment: Boolean combination of array properties and QF formulae.

slide-28
SLIDE 28

Decision Procedure

Given: Array property formula F

  • 1. F1: push negations to atoms
  • 2. F2: Eliminate writes

G[ai ⊳ v] G[a′] ∧ a′[i] = v ∧ (∀j. j ≤ i − 1 ∨ i + 1 ≤ j → a[j] = a′[j])

  • 3. Construct index set

I : {t : t is symbolic index} ({0} if empty)

slide-29
SLIDE 29
  • 4. F4: Instantiate quantifiers

H[∀i. G[i]] = ⇒ H

  • t∈I

G[t]

  • 5. F4 is QF

. Decide satisfiability using Nelson-Oppen DP .

slide-30
SLIDE 30

Example sorted(a, ℓ, u) : ∀i, j. ℓ ≤ i ≤ j ≤ u → a[i] ≤ a[j]

Is

sorted(a0 ⊳ 05 ⊳ 1, 0, 5) ∧ sorted(a0 ⊳ 105 ⊳ 11, 0, 5) (T Z

A ∪ TZ)-satisfiable?

w x y z 1 10 w x y z 11

slide-31
SLIDE 31

Example sorted(a0 ⊳ 05 ⊳ 1, 0, 5) ∧ sorted(a0 ⊳ 105 ⊳ 11, 0, 5)

Index set: {−1, 0, 1, 4, 5, 6}

  • {0, 5} from 0 ≤ i ≤ j ≤ 5
  • {−1, 1} from ·0 ⊳ ·
  • {4, 6} from ·5 ⊳ ·

Contradiction: 0 ≤ a[1] ≤ 1 ∧ 10 ≤ a[1] ≤ 11 Need 1 or 4 in index set.

slide-32
SLIDE 32

Complexity

Quantifier elimination is in NEXP for Nelson-Oppen theories:

  • 1. |I| is linear in size of F , so linear-time quantifier instantiation.
  • 2. NP DPs applied to QF formula at most exponentially larger than

F .

  • 3. Exponential in largest stack of universal quantifiers.

Fixing stack height (“extensional”, “sorting” fragment) gives NP procedure.

slide-33
SLIDE 33

Complexity NEXP-hard even for uninterpreted domain and range.

  • Bernays-Schonfinkel-Ramsey (BSR) class: ∃∗∀∗, only

predicates

  • Deciding satisfiability is NEXP-complete
  • Reduction:

∃x. F[x] = ⇒ ∃x. d(x) ∧ F[x] ∀x. F[x] = ⇒ ∀x. d(x) → F[x]

Why d? Only infinite TA-interpretations, but possible finite BSR-interpretations. Thanks to De Moura, Bjorner, Kuncak for mentioning BSR.

slide-34
SLIDE 34

Undecidable Extensions

  • Extra quantifier alternation
  • Nested reads under ∀i: a[a[i]]
  • No separation: ∀i. F[a[i], i]
  • Arithmetic: a[i + 1] when i is universal
  • Strict comparison: i < j when i, j are universal
  • Permutation predicate
slide-35
SLIDE 35

Reduce from undecidability of Diophantine equations:

p(x1, . . . , xn) = 0

(over nonnegative x) “Walk”:

  • Begin at origin.
  • At each step, increment one xi.
  • End at solution.

A walk exists iff p(x) = 0 has a solution.

slide-36
SLIDE 36
  • 1. q(x) = p(x)2

q(x) > 0 on walk, except at solution

  • 2. Difference tree:

a : x2 − 2xy + y2 b : 2x − 2y + 1 c : −2x + 2y + 1 2 −2 −2 2

slide-37
SLIDE 37
  • 3. Walk arithmetic is linear arithmetic:

Initially

(a, b, c) = (0, 1, 1) x := x + 1 (a, b, c) := (a + b, b + 2, c − 2) y := y + 1 (a, b, c) := (a + c, b − 2, c + 2)

slide-38
SLIDE 38

Initial condition:

θ(i) :

  • p

ap[i] = p(0)

Transition relation for variable x:

ρx(i, j) :

  • p

ap[j] = ap[i] + a∆xp[i]

Idle transition:

ρ0(i, j) : a[i] = a[j]

slide-39
SLIDE 39

Easy: extra quantifier alternation

∃a, s. ∀i. ∃j.          θ(0) ∧

  • ρ0(i, j) ∨
  • x

ρx(i, j)

  • ∧ s[j] = s[i] − aq[i]

∧ s[i] > 0         

slide-40
SLIDE 40

Tricky: permutation predicate perm(a, b) Define identifiers:

perm(a, b) ∧ ∀i. b[i] = a[i] + 1

Bounded case for integer-indexed arrays: n identifiers

a[0] = n ∧ b[0] = 0 ∧ perm(a, b, 0, n) ∧ ∀i. 0 < i ≤ n → 0 ≤ a[i], b[i] ≤ n ∧ b[i] = a[i] + 1

slide-41
SLIDE 41

Unbounded case:

∃a, d, e, z, n. ∀i, j.               θ(z) ∧         (d[j] > 0 ∧ d[j] = d[i] + 1) ∨ (d[j] < 0 ∧ d[j] = d[i] − 1)   →

  • x

ρx(i, j)       ∧ aq[n] = 0 ∧ d[z] = 0 ∧ perm(d, e) ∧ ∀i. e[i] = d[i] + 1              

slide-42
SLIDE 42

Open: ∀i, j. i = j → a[i] = a[j]

  • For integer-indexed arrays: undecidable
  • Otherwise: ?
slide-43
SLIDE 43

Incremental Instantiation

  • 1. Instantiate F to F ′
  • 2. Find I |

= F ′ (otherwise, F is unsatisfiable)

  • 3. Construct G : ¬F[I]
  • 4. Find J |

= G (otherwise, F is satisfiable)

  • 5. Enlarge instantiation set (according to I and J) and repeat

May avoid full instantiation (whether satisfiable or unsatisfiable)

slide-44
SLIDE 44

Summary

  • Array property fragments allow encoding

– properties of arrays and array segments – operations on sets, multisets, and hashtables

  • Simple decision procedure: quantifier instantiation
  • Larger natural fragments are undecidable