1 A simple abstract interpreter to compute Sign s.

Slides:



Advertisements
Similar presentations
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS4026 Formal Models of Computation Running Haskell Programs – power.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Modern Programming Languages, 2nd ed.
O X Click on Number next to person for a question.
Quadratic Inequalities
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
1 OCAML §nucleo funzionale puro l funzioni (ricorsive) l tipi e pattern matching l primitive utili: liste l trascriviamo pezzi della semantica denotazionale.
25 seconds left…...
Week 1.
Complexity Analysis (Part II)
O X Click on Number next to person for a question.
Introduction to Recursion and Recursive Algorithms
Claus Brabrand, UFPE, Brazil Aug 11, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
1 Some abstract interpreters for type inference. 2 Types as abstract interpretations §inspired by a paper of Cousot (POPL ‘97), which l derives several.
Closures & Environments CS153: Compilers Greg Morrisett.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Copyright © Cengage Learning. All rights reserved.
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Inferring Disjunctive Postconditions Corneliu Popeea and Wei-Ngan Chin School of Computing National University of Singapore - ASIAN
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Tutorial on Widening (and Narrowing) Hongseok Yang Seoul National University.
1 Basic abstract interpretation theory. 2 The general idea §a semantics l any definition style, from a denotational definition to a detailed interpreter.
Programming Language Semantics Denotational Semantics Chapter 5 Based on a lecture by Martin Abadi.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Abstraction and Approximation via Abstract Interpretation: a systematic approach to program analysis and verification Giorgio Levi Dipartimento di Informatica,
Prof. Aiken CS 294 Lecture 21 Abstract Interpretation Part 2.
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
1 How to transform an abstract analyzer into an abstract verifier.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Lecture 10 Abstract Interpretation using Fixpoints.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Abstraction and Abstract Interpretation. Abstraction (a simplified view) Abstraction is an effective tool in verification Given a transition system, we.
1 Abstract interpretation Giorgio Levi Dipartimento di Informatica, Università di Pisa
ML: a quasi-functional language with strong typing
The interpreter.
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Abstract Interpretation
The Metacircular Evaluator
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
3.6 Interpreter: Recursion
Logics for Data and Knowledge Representation
Programming Languages and Compilers (CS 421)
Abstract Interpretation
Assignments and Procs w/Params
Recursive Procedures and Scopes
Presentation transcript:

1 A simple abstract interpreter to compute Sign s

2 The Sign Abstract Domain   concrete domain ( P ( Z ), , Z, ,  ) sets of integers,   abstract domain ( Sign,  bot, top, lub, glb )

3 The example of Sign  Sign  (x) = , if x= bot l {y|y>0}, if x= + l {y|y  0}, if x= 0+ l {0}, if x= 0 {y|y  0}, if x= 0- l {y|y<0}, if x= - Z, if x= top  Sign  y) = glb of bot, if y=  -, if y  {y|y<0} 0-, if y  {y|y  0} 0, if y  {0} 0+, if y  {y|y  0} +, if y  {y|y  0} top, if y  Z

4 A simple abstract interpreter computing Signs §concrete semantics executable specification (in ML) of the denotational semantics of untyped -calculus without recursion §abstract semantics l abstract interpreter computing on the domain Sign

5 The language: syntax §type ide = Id of string §type exp = | Eint of int | Var of ide | Times of exp * exp | Ifthenelse of exp * exp * exp | Fun of ide * exp | Appl of exp * exp

6 A program Fun(Id "x", Ifthenelse(Var (Id "x"), Times (Var (Id "x"), Var (Id "x")), Times (Var (Id "x"), Eint (-1)))) §the ML expression function x -> if x=0 then x * x else x * (-1)

7 Concrete semantics §denotational interpreter §eager semantics §separation from the main semantic evaluation function of the primitive operations l which will then be replaced by their abstract versions §abstraction of concrete values l identity function in the concrete semantics §symbolic “non-deterministic” semantics of the conditional

8 Semantic domains § type eval = | Funval of (eval -> eval) | Int of int | Wrong § let alfa x = x § type env = ide -> eval let emptyenv (x: ide) = alfa(Wrong) let applyenv ((x: env), (y: ide)) = x y let bind ((r:env), (l:ide), (e:eval)) (lu:ide) = if lu = l then e else r(lu)

9 Semantic evaluation function § let rec sem (e:exp) (r:env) = match e with | Eint(n) -> alfa(Int(n)) | Var(i) -> applyenv(r,i) | Times(a,b) -> times ( (sem a r), (sem b r)) | Ifthenelse(a,b,c) -> let a1 = sem a r in (if valid(a1) then sem b r else (if unsatisfiable(a1) then sem c r else merge(a1,sem b r,sem c r))) | Fun(ii,aa) -> makefun(ii,aa,r) | Appl(a,b) -> applyfun(sem a r, sem b r)

10 Primitive operations let times (x,y) = match (x,y) with |(Int nx, Int ny) -> Int (nx * ny) | _ -> alfa(Wrong) let valid x = match x with |Int n -> n=0 let unsatisfiable x = match x with |Int n -> not n=0 let merge (a,b,c) = match a with |Int n -> if b=c then b else alfa(Wrong) | _ -> alfa(Wrong) let applyfun ((x:eval),(y:eval)) = match x with |Funval f -> f y | _ -> alfa(Wrong) let rec makefun(ii,aa,r) = Funval(function d -> if d = alfa(Wrong) then alfa(Wrong) else sem aa (bind(r,ii,d)))

11 From the concrete to the collecting semantics §the concrete semantic evaluation function l sem: exp -> env -> eval §the collecting semantic evaluation function semc: exp -> env ->  (eval) l semc e r = {sem e r} all the concrete primitive operations have to be lifted to  (eval) in the design of the abstract operations

12 Example of concrete evaluation # let esempio = sem( Fun (Id "x", Ifthenelse (Var (Id "x"), Times (Var (Id "x"), Var (Id "x")), Times (Var (Id "x"), Eint (-1)))) ) emptyenv;; val esempio : eval = Funval # applyfun(esempio,Int 0);; - : eval = Int 0 # applyfun(esempio,Int 1);; - : eval = Int -1 # applyfun(esempio,Int(-1));; - : eval = Int 1 §in the “virtual” collecting version applyfunc(esempio,{Int 0,Int 1}) = {Int 0, Int -1} applyfunc(esempio,{Int 0,Int -1}) = {Int 0, Int 1} applyfunc(esempio,{Int -1,Int 1}) = {Int 1, Int -1}

13 From the collecting to the abstract semantics  concrete domain: (  (ceval),   ) §concrete (non-collecting) environment: l cenv = ide -> ceval  abstract domain: (eval,  ) §abstract environment: env = ide -> eval §the collecting semantic evaluation function semc: exp -> env ->  (ceval) §the abstract semantic evaluation function l sem: exp -> env -> eval

14 The Sign Abstract Domain  concrete domain ( P ( Z ), , Z, ,  ) sets of integers,   abstract domain ( Sign,  bot, top, lub, glb )

15 Redefining eval for Sign type ceval = Funval of (ceval -> ceval) | Int of int | Wrong type eval = Afunval of (eval -> eval) | Top | Bottom | Zero | Zerop | Zerom | P | M let alfa x = match x with Wrong -> Top | Int n -> if n = 0 then Zero else if n > 0 then P else M  the partial order relation  the relation shown in the Sign lattice, extended with its lifting to functions there exist no infinite increasing chains we might add a recursive function construct and find a way to compute the abstract least fixpoint in a finite number of steps §lub and glb of eval are the obvious ones  concrete domain: ( P (ceval), , , ceval, ,  )  abstract domain: (eval, , Bottom, Top, lub, glb)

16 Concretization function  concrete domain: ( P (ceval), , , ceval, ,  )  abstract domain: (eval, , Bottom, Top, lub, glb)   s ( x ) = {}, if x = Bottom {Int(y) |y>0}, if x = P {Int(y) |y  0}, if x = Zerop {Int(0)}, if x = Zero {Int(y)|y  0}, if x = Zerom {Int(y)|y<0}, if x = M ceval, if x = Top {Funval(g) |  y  eval  x   s (y , g(x)   s (f(y))}, if x = Afunval(f)

17 Abstraction function  concrete domain: ( P (ceval), , , ceval, ,  )  abstract domain: (eval, , Bottom, Top, lub, glb)   s ( y ) = glb{ Bottom, if y = {} M, if y  {Int(z)| z<0} Zerom, if y  {Int(z)| z  0} Zero, if y  {Int(0)} Zerop, if y  {Int(z)| z  0} P, if y  {Int(z)| z>0} Top, if y  ceval lub{Afunval(f)| Funval(g)   s (Afunval(f))}, if y  {Funval(g)} & Funval(g)  y} }

18 Galois connection   s and  s l are monotonic l define a Galois connection

19 Times Sign §optimal (hence correct) and complete (no approximation)

20 Abstract operations l in addition to times and lub let valid x = match x with | Zero -> true | _ -> false let unsatisfiable x = match x with | M -> true | P -> true | _ -> false let merge (a,b,c) = match a with | Afunval(_) -> Top | _ -> lub(b,c) let applyfun ((x:eval),(y:eval)) = match x with |Afunval f -> f y | _ -> alfa(Wrong) let rec makefun(ii,aa,r) = Afunval(function d -> if d = alfa(Wrong) then d else sem aa (bind(r,ii,d))) l sem is left unchanged

21 An example of abstract evaluation # let esempio = sem( Fun (Id "x", Ifthenelse (Var (Id "x"), Times (Var (Id "x"), Var (Id "x")), Times (Var (Id "x"), Eint (-1)))) ) emptyenv;; val esempio : eval = Afunval # applyfun(esempio,P);; - : eval = M # applyfun(esempio,Zero);; - : eval = Zero # applyfun(esempio,M);; - : eval = P # applyfun(esempio,Zerop);; - : eval = Top # applyfun(esempio,Zerom);; - : eval = Zerop # applyfun(esempio,Top);; - : eval = Top applyfunc(esempio,{Int 0,Int 1}) = {Int 0, Int -1} applyfunc(esempio,{Int 0,Int -1}) = {Int 0, Int 1} applyfunc(esempio,{Int -1,Int 1}) = {Int 1, Int -1} §wrt the abstraction of the concrete (collecting) semantics, approximation for Zerop §no abstract operations which “invent” the values Zerop and Zerom l which are the only ones on which the conditional takes both ways and can introduce approximation

22 Recursion l the language has no recursion fixpoint computations are not needed l if (sets of) functions on the concrete domain are abstracted to functions on the abstract domain, we must be careful in the case of recursive definitions a naïve solution might cause the application of a recursive abstract function to diverge, even if the domain is finite we might never get rid of recursion because the guard in the conditional is not valid or satisfiable we cannot explicitely compute the fixpoint, because equivalence on functions cannot be expressed termination can only be obtained by a loop checking mechanism (finitely many different recursive calls) l we will see a different solution in a case where (sets of) functions are abstracted to non functional values the explicit fixpoint computation will then be possible