Formalising Luck Improved Probabilistic Semantics for - - PowerPoint PPT Presentation

formalising luck
SMART_READER_LITE
LIVE PREVIEW

Formalising Luck Improved Probabilistic Semantics for - - PowerPoint PPT Presentation

Context Choice-recording semantics Integrating backtracking strategies Conclusion Formalising Luck Improved Probabilistic Semantics for Property-Based Generators Diane Gallois-Wong supervised by C at alin Hrit cu, INRIA Paris


slide-1
SLIDE 1

1/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Formalising Luck

Improved Probabilistic Semantics for Property-Based Generators

Diane Gallois-Wong supervised by C˘ at˘ alin Hrit ¸cu, INRIA Paris September 5th 2016

slide-2
SLIDE 2

2/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Property-Based Testing

Property-Based Testing: running a program on many random inputs to test a property about it (QuickCheck) Testing insertion function for binary search trees insertBST: ∀(tr : int tree)(x : int). isBST(tr) ⇒ isBST(insertBST tr x)

slide-3
SLIDE 3

3/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Property-Based Generators

∀(tr : int tree)(x : int). isBST(tr) ⇒ isBST(insertBST tr x) Problem: isBST is sparse, most of the tests are irrelevant Solution: Property-Based Generators (PBGs) Problem: hard to implement, easily unbalanced/incomplete (e.g. generate almost only small trees) Luck: programming language dedicated to writing PBGs

slide-4
SLIDE 4

4/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Luck: principle

Property-Based Generator in Luck: Form of the program: predicate (boolean function) representing the property Running it: give unknowns as arguments, generate valuation V for them so that predicate evaluates to True (Can also be interpreted as a usual predicate by giving concrete values as arguments) Mixing constraint solving and instantiation / backtracking: constraint solving by default, user-controlled instantiation

slide-5
SLIDE 5

5/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Luck: standard basis

Standard lambda calculus with pairs binary sums: LT1+T2 e RT1+T2 e case e of (L x → e1) (R y → e2) recursive types Plus unknowns and special constructs to control instantiation

slide-6
SLIDE 6

6/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Binary search trees generator in Luck

fun (bst : int -> int -> int -> int tree -> bool) size low high tree = if size == 0 then tree == Empty else case tree of | 1 % Empty -> True | size % Node x l r -> ((low < x && x < high) !x) && bst (size / 2) low x l && bst (size / 2) x high r

Apply bst to given integers size, low, high to obtain a BSTs generator (predicate int tree → bool)

slide-7
SLIDE 7

7/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Choice-recording semantics

Big-step operational probabilistic semantics Derivations for a generator gen produce a possible output but also the choices made to reach it, recorded in trace t gen ⇑t output Doesn’t handle backtracking: output is either an actual valuation V, or need to backtrack ∅ (error monad)

slide-8
SLIDE 8

8/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Traces of choices

Choice: (m, n, q) n: number of possibilities m: index of the one actually taken (0 ≤ m < n) q: probability of making this choice (q ∈ Q, 0 < q ≤ 1) Trace: sequence of choices Probability P(t) of a trace t: product of the probabilities of its choices

slide-9
SLIDE 9

9/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

(Sub)probability distribution

( P(t): product of the probabilities of the choices in t ) πgen = [ output →

  • t | gen ⇑toutput

P(t) ] gen ⇑[(0,2, 1

2 ); (0,2, 1 3 ); (0,2, 2 5 )]

∅ gen ⇑[(0,2, 1

2 ); (0,2, 1 3 ); (1,2, 3 5 )]

V1 gen ⇑[(0,2, 1

2 ); (1,2, 2 3 )]

∅ gen ⇑[(1,2, 1

2 )]

V2 [V1 →

1 10; V2 → 1 2; ∅ → 1 15 + 1 3]

slide-10
SLIDE 10

10/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Advantages of this new semantics

Former semantics: directly derives whole probability distribution in a collecting style gen ⇑ πgen Our new choice-recording semantics is: simpler: proofs with Coq more expressive:

still able to produce πgen better handles not always terminating programs provides more detailed information, used in next part

slide-11
SLIDE 11

11/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Integrating backtracking strategies: motivation

πgen ranges over valuations V but also need to backtrack ∅ Running a generator in practice: apply a backtracking strategy to always output a valuation Objective: determine final distribution ρbstrat

gen

  • ver valuations
  • f generator gen when using backtracking strategy bstrat
slide-12
SLIDE 12

12/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Choice tree of a generator

(Assume all executions terminate) gen ⇑[(0,2, 1

2 ); (0,2, 1 3 ); (0,2, 2 5 )]

∅ gen ⇑[(0,2, 1

2 ); (0,2, 1 3 ); (1,2, 3 5 )]

V1 gen ⇑[(0,2, 1

2 ); (1,2, 2 3 )]

∅ gen ⇑[(1,2, 1

2 )]

V2 V2 ∅ V1 ∅

(0, 2, 2

5 )

(1, 2, 3

5 )

(0, 2, 1

3 )

(1, 2, 2

3 )

(0, 2, 1

2 )

(1, 2, 1

2 )

slide-13
SLIDE 13

13/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Markov chains to model application of a strategy

Time-homogeneous Markov chains with discrete time and finite state space Init. V2 ∅ V1 ∅

2 5 3 5 1 3 2 3 1 2 1 2

1 1 1 1 Restart-from-scratch strategy Absorbing Markov chain [V1 → 1 6; V2 → 5 6]

slide-14
SLIDE 14

14/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Example of non absorbing Markov chain

Backtrack-from-leaf-to-parent strategy Init. V ∅ ∅

1 3 2 3 1 2 1 2

1 1 1 Markov chain is not absorbing: no probability distribution

slide-15
SLIDE 15

15/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

More complex strategy I

Remember hopeless nodes that lead only to ∅ leaves Backtrack to parent from ∅ leaf or node with only hopeless children Restart execution when more than a set number Bmax

  • f ∅ leaves have been encountered

Set of states: Nodes × ( P(Nodes) × {0, 1, ..., Bmax} ) ( node, ( hopeless nodes, number of ∅ leaves seen ) )

slide-16
SLIDE 16

16/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

More complex strategy II

A V2 G B ∅ F C V1 E ∅ D

2 5 3 5 1 3 2 3 1 2 1 2

(D, S, n)

1

− → (C, S ∪ {D}, n + 1) if n < Bmax (D, S, Bmax)

1

− → (A, S ∪ {D}, 0) (C, S, n)

2/5

− − → (D, S, n) (C, S, n)

3/5

− − → (E, S, n)

  • if D, E /

∈ S (C, S, n)

1

− → (E, S, n) if D ∈ S, E / ∈ S (C, S, n)

1

− → (B, S ∪ {C}, n) if D, E ∈ S (impossible, yet transition exists) (G, S, n)

1

− → (G, S, n)

slide-17
SLIDE 17

17/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Definition of a strategy

Backtracking strategy: computable function associating to a choice tree a Markov chain verifying: it is time-homogeneous with discrete time it has a finite set of states of form Nodes × M it has a single initial state of form (root, M0) absorbing states are exactly those with a non-∅ leaf it is absorbing

slide-18
SLIDE 18

18/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Computability of the final probability distribution

Theorem Final distribution ρbstrat

gen

is computable from gen and bstrat. gen → choice tree: computable (finite) choice tree → Markov chain: bstrat computable (def) Markov chain → ρbstrat

gen :

Transition matrix of absorbing Markov chain Q R I

  • Probability to be absorbed in j from non-absorbing i:
  • s≥0 QsR = (I − Q)−1R
slide-19
SLIDE 19

19/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

“Activated only upon backtracking” strategies

A strategy is “activated only upon backtracking” if for any Markov chain in its image: for any state that is reachable from the initial state without visiting a ∅ leaf, the transitions follow the choice tree A C B

2 3 1 3

Consequence: ∀V. ρbstrat

gen (V) ≥ πgen(V)

slide-20
SLIDE 20

20/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Restriction about computation of MC from CT

A strategy is a computable function from choice trees to Markov chains: very permissive Possible restriction: transitions from a state (node, M) computable only from node, M, children of node and edges to them in choice tree Examples need minor adaptation: restart-from-scratch: add memorised information which always contains root more complex strategy: add path of ancestors of the current node to memorised information

slide-21
SLIDE 21

21/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Evaluation of backtracking strategies

πgen usually more intuitive for the user than ρbstrat

gen

so ideally bstrat should keep ρbstrat

gen

close to πgen Extremality result: restart-from-scratch most conservative, according to two functions evaluating closeness of distributions

  • Bhattacharyya distance

DB(P, Q) = − log

i

  • P(i)Q(i)

Kullback-Leibler divergence DKL(P||Q) =

i P(i) log P(i) Q(i)

  • Future work: sum with other terms to minimise, such as time

complexity term (e.g. expected number of steps in the Markov chain before being absorbed)

slide-22
SLIDE 22

22/ 22 Context Choice-recording semantics Integrating backtracking strategies Conclusion

Conclusion

Choice-recording semantics style: simple (Coq proofs) yet more expressive than the previous one. Formalisation of backtracking strategies. Integration of them into the semantics to obtain the final probability distribution of a generator. Computability result. Future work:

identify and study narrower subclasses of strategies further explore evaluation of strategies

to characterise viable strategies.