Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Similar documents
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Model checking pushdown systems

6. Hoare Logic and Weakest Preconditions

Programming Languages Third Edition

Denotational Semantics. Domain Theory

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

Lecture 5 - Axiomatic semantics

Lectures 20, 21: Axiomatic Semantics

CS2104 Prog. Lang. Concepts

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

ProPed. Tool for Symbolic Verification of Probablistic Recursive Programs. University of Missouri Columbia, Missouri, USA

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

Introduction to Structural Operational Semantics

Application: Programming Language Semantics

Chapter 3 (part 3) Describing Syntax and Semantics

Induction and Semantics in Dafny

Program Analysis: Lecture 02 Page 1 of 32

PROGRAM ANALYSIS & SYNTHESIS

Introduction to Axiomatic Semantics

A Partial Correctness Proof for Programs with Decided Specifications

An Annotated Language

3.7 Denotational Semantics

Abstract Interpretation

A CRASH COURSE IN SEMANTICS

Chapter 3. Describing Syntax and Semantics ISBN

Formal Semantics of Programming Languages

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

Semantics with Applications 3. More on Operational Semantics

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Foundations: Syntax, Semantics, and Graphs

Formal Semantics of Programming Languages

Denotational Semantics

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

CSE 307: Principles of Programming Languages

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

Formal Syntax and Semantics of Programming Languages

Defining Languages GMU

Discrete Mathematics Lecture 4. Harper Langston New York University

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Chapter 3. Describing Syntax and Semantics ISBN

Context Free Languages and Pushdown Automata

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics

Introduction to Axiomatic Semantics (1/2)

Program Syntax; Operational Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Hoare Logic: Proving Programs Correct

Formal Methods. CITS5501 Software Testing and Quality Assurance

Lecture 6. Abstract Interpretation

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Hoare Logic and Model Checking

Formal Syntax and Semantics of Programming Languages

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

CS402 - Theory of Automata Glossary By

Reasoning About Imperative Programs. COS 441 Slides 10

Pierce Ch. 3, 8, 11, 15. Type Systems

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Introduction to Axiomatic Semantics (1/2)

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

No model may be available. Software Abstractions. Recap on Model Checking. Model Checking for SW Verif. More on the big picture. Abst -> MC -> Refine

Shared Variables and Interference

Variables. Substitution

Formal Specification and Verification

3.4 Denotational Semantics

Handout 9: Imperative Programs and State

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18

Verifying Safety Property of Lustre Programs: Temporal Induction

Software Engineering using Formal Methods

Programming Languages 3. Definition and Proof by Induction

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Programming Languages Fall 2013

Shared Variables and Interference

An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C

Graph Contraction. Graph Contraction CSE341T/CSE549T 10/20/2014. Lecture 14

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

CORRECTNESS ISSUES AND LOOP INVARIANTS

Compilers and computer architecture From strings to ASTs (2): context free grammars

COMP 382: Reasoning about algorithms

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Distributed Systems Programming (F21DS1) Formal Verification

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

1 Introduction. 3 Syntax

Introduction to Denotational Semantics (1/2)

CMSC 330: Organization of Programming Languages

Programming Embedded Systems

CSC Discrete Math I, Spring Sets

Compiler Construction I

Dynamic Logic David Harel, The Weizmann Institute Dexter Kozen, Cornell University Jerzy Tiuryn, University of Warsaw The MIT Press, Cambridge, Massac

Transcription:

Overview Lecture #4: Probabilistic GCL 1 Joost-Pieter Katoen 2 3 Recursion RWTH Lecture Series on 2018 Joost-Pieter Katoen 1/31 Joost-Pieter Katoen 2/31 Dijkstra s guarded command language: Syntax Elementary pgcl ingredients Program variables x Vars whose values are fractional numbers Arithmetic expressions E over the program variables skip diverge x := E prog1 ; prog2 if (G) prog1 else prog2 prog1 [] prog2 while (G) prog empty statement divergence assignment sequential composition choice non-deterministic choice iteration Boolean expressions G (guarding a choice or loop) over the program variables A distribution expression µ Σ Dist(Q) A probability expression p Σ [0, 1] Q Joost-Pieter Katoen 3/31 Joost-Pieter Katoen 4/31

Probabilistic GCL: Syntax Let s start simple Kozen McIver Morgan skip empty statement diverge divergence x := E assignment x :r= mu random assignment (x µ) prog1 ; prog2 sequential composition if (G) prog1 else prog2 choice prog1 [p] prog2 probabilistic choice while (G) prog iteration x := 0 [0.5] x := 1; y := -1 [0.5] y := 0 This program admits four runs and yields the outcome: Pr[x =0, y =0] = Pr[x =0, y = 1] = Pr[x =1, y =0] = Pr[x =1, y = 1] = 1 /4 Conditioning in the form of observe-statements omitted for now. Joost-Pieter Katoen 5/31 Joost-Pieter Katoen 6/31 A loopy program On termination For 0 < p < 1 an arbitrary probability: bool c := true; int i := 0; i++; (c := false [p] c := true) bool c := true; int i := 0; i++; (c := false [p] c := true) The loopy program models a geometric distribution with parameter p. Pr[i = N] = (1 p) N 1 p for N > 0 This program does not always terminate. It almost surely terminates. Joost-Pieter Katoen 7/31 Joost-Pieter Katoen 8/31

The good, the bad, and the ugly Duelling cowboys int cowboyduel(float a, b) { int t := A [0.5] t := B; bool c := true; if (t = A) { (c := false [a] t := B); else { (c := false [b] t := A); return t; Joost-Pieter Katoen 9/31 Joost-Pieter Katoen 10/31 Random assignments The random assignment x µ works as follows: 1. evaluate distribution expression µ in the current program state s 2. sample from the resulting probability distribution µ(s) yielding value v with probability µ(s)(v) 3. assign the value v to the variable x. Overview 1 For denoting distribution expressions, we use the bra ket notation. 1 2 [a + 1 3 [b + 1 6 [c denotes the distribution µ with µ(a) = 1 /2, µ(b) = 1 /3, and µ(c) = 1 /6. The support set of µ equals { a, b, c 2 3 Recursion Examples on the black board. Joost-Pieter Katoen 11/31 Joost-Pieter Katoen 12/31

Why formal semantics matters The inventors of semantics Unambiguous meaning to all programs Basis for proving correctness of programs of program transformations of program equivalence of static analysis of compilers...... Tony Hoare Robert W. Floyd Gordon Plotkin Christopher Strachey Dana Scott Joost-Pieter Katoen 13/31 Joost-Pieter Katoen 14/31 Approaches to semantics : (developed by Plotkin) The meaning of a program in terms of how it executes on an abstract machine. Useful for modelling the execution behaviour of a program. Axiomatic semantics: (developed by Floyd and Hoare) Provides correctness assertions for each program construct. Useful for verifying that a program s computed results are correct with respect to the specification. Denotational semantics: (developed by Strachey and Scott) Provides a mapping of language constructs onto mathematical objects. Useful for obtaining an abstract insight into the working of a program. Today: operational semantics of pgcl in terms of Markov chains. Later: denotational semantics of pgcl in terms of weakest preconditions. Joost-Pieter Katoen 15/31 Structural operational semantics: ingredients Variable valuation s Vars Q maps each program variable onto a value (here: rational numbers) Expression valuation 1, let [[ E ]] denote the valuation of expression E Configuration (aka: state) P, s denotes that program P is about to be executed (aka: program counter) and the current variable valuation equals s. Transition rules for the execution of commands: P, s P, s premise transition rules are written as conclusion where the premise is omitted if it is vacuously true. 1 Here, we will not go into the details of this (simple) part. Joost-Pieter Katoen 16/31

Recall: Markov chains A Markov chain (MC) is a triple (Σ, σ I, P) with: Σ being a countable set of states σ I Σ the initial state, and P Σ Dist(Σ) the transition probability function where Dist(Σ) is a discrete probability measure on Σ. Aim: Model the behaviour of a pgcl program P by the MC [[ P ]]. Approach: Take states of the form Q, s with program Q or, and variable valuation s Vars Q sink models program termination (successful or violated observation) Take initial state σ I = P, s where s fulfils the initial conditions Transition relation is the smallest relation satisfying the SOS rules on the next slides Where transition probabilities equal to one are omitted Joost-Pieter Katoen 17/31 Joost-Pieter Katoen 18/31 Transition rules (1) Transition rules (2) skip, s, s diverge, s diverge, s, s sink sink sink P, s µ P; Q, s ν with ν( P ; Q, s ) = µ( P, s ) where ; Q = Q x = E, s, s[x = s([[ E ]])] s G if (G){P else {Q, s P, s s / G if (G){P else {Q, s Q, s µ(s)(v) = a > 0 x µ, s a, s[x = v] P[ p] Q, s µ with µ( P, s ) = p and µ( Q, s ) = 1 p s G while(g){p, s P; while (G){P, s s / G while(g){p, s, s Joost-Pieter Katoen 19/31 Joost-Pieter Katoen 20/31

Example Duelling cowboys int cowboyduel(float a, b) { int t := A [0.5] t := B; bool c := true; if (t = A) { (c := false [a] t := B); else { (c := false [b] t := A); return t; 11 A 0 4 A 0 6 A 1 a! 2 H--+13 B 3 A * I 8 V.>--'-r-L...I B I 1- b.,...,~./ 4 A 1 8 B I 5A l \ 581 \ 6 A l 4 B I 1 - a 6 /\ I ~ b 8 B 1 4 B 0 I I B 0 This (parametric) MC is finite. Once we count the number of shots before one of the cowboys dies, the MC becomes countably infinite. Joost-Pieter Katoen 21/31 Joost-Pieter Katoen 22/31 Playing with geometric distributions An alternative program X is a random variable, geometrically distributed with parameter p Y is a random variable, geometrically distributed with parameter q Q: generate a sample x, say, according to the random variable X Y int XminY1(float p, q){ // 0 <= p, q <= 1 int x := 0; bool flip := false; while (not flip) { // take a sample of X to increase x (x +:= 1 [p] flip := true); flip := false; while (not flip) { // take a sample of Y to decrease x (x -:= 1 [q] flip := true); return x; // a sample of X-Y int XminY2(float p, q){ int x := 0; bool flip := false; (flip := false [0.5] flip := true); // flip a fair coin if (not flip) { while (not flip) { // sample X to increase x (x +:= 1 [p] flip := true); else { flip := false; // reset flip while (not flip) { // sample Y to decrease x x -:= 1; (skip [q] flip := true); return x; // a sample of X-Y Joost-Pieter Katoen 23/31 Joost-Pieter Katoen 24/31

Program equivalence: X Y int XminY1(float p, q){ int x, c := 0, 1; (x +:= 1 [p] c := 0); c := 1; (x -:= 1 [q] c := 0); return x; int XminY2(float p, q){ int x := 0; (c := 0 [0.5] c := 1); if (c) { (x +:= 1 [p] c := 0); else { c := 1; x -:= 1; (skip [q] c := 0); return x; The outcome of a pgcl program Unlike a deterministic program, a pgcl program P has not an output for a given input. Instead, it yields a unique probability distribution over its final states. In fact, this is a sub-distribution (probability mass at most one), as with a certain probability P may diverge. Let P be a pgcl program and s an input state. Then the distribution over final states obtained by running P starting in s is given by Pr(s, ). The probability that x = k for some k Z coincides for both programs if and only if q = 1 2 p. Joost-Pieter Katoen 25/31 Joost-Pieter Katoen 26/31 Recursion Reachability probabilities Overview If the MC [[ P ]] of pgcl program P has finitely many states, reachability probabilities can be obtained in an automated manner. This applies to the cowboy example for given probabilities a and b. The same holds for expected rewards, e.g., the expected number of steps until termination of a finite-state program P. 1 2 3 Recursion Joost-Pieter Katoen 27/31 Joost-Pieter Katoen 28/31

Recursion Recursion Probabilistic GCL with recursion: Syntax skip empty statement diverge divergence x := E assignment x :r= mu random assignment (x µ) prog1 ; prog2 sequential composition if (G) prog1 else prog2 choice prog1 [p] prog2 probabilistic choice while (G) prog iteration P = prog process definition call P process invocation Pushdown Markov chains Pushdown Markov chain A pushdown Markov chain D is a tuple (Σ, σ I, Γ, γ 0, ) where: Σ is a countable set of (control) states σ I Σ is the initial (control) state Γ is a finite stack alphabet γ 0 Γ is the bottom-of-the-stack symbol Σ Γ Dist(Σ) (Γ \ { γ 0 is the probability transition relation Recursion does not increase the expressive power, but is often convenient. Joost-Pieter Katoen 29/31 Joost-Pieter Katoen 30/31 Recursion Recursion: pushdown Markov chains Joost-Pieter Katoen 31/31