Determinacy Inference for Logic Programs Lunjin Oakland University In collaboration with Andy Kent University, UK.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
Functional Design and Programming Lecture 11: Functional reasoning.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
1 Basic abstract interpretation theory. 2 The general idea §a semantics l any definition style, from a denotational definition to a detailed interpreter.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
1 Boolean Satisfiability in Electronic Design Automation (EDA ) By Kunal P. Ganeshpure.
19/06/20151 Forward versus Backward Verification of Logic Programs Andy King University of Kent, UK Lunjin Lu University of Oakland,
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Efficient Sharing Analysis Xuan Li, Andy King and Lunjin Lu Oakland University, U.S.A & University of Kent, U.K.
1 Systematic Domain Design Some Remarks. 2 Best (Conservative) interpretation abstract representation Set of states concretization Abstract semantics.
Modal logic and databases. Terms Object terms Concept terms ↓ t: object denoted by concept t in some context Type designations: o (object) and c (concept)
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Describing Syntax and Semantics
13/07/20151 A Goal-Independent Suspension Analysis for Logic Programs with Dynamic Scheduling Samir Genaim Universita’ degli Studi di Verona Italy Andy.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Propositional Calculus Math Foundations of Computer Science.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
By: Pashootan Vaezipoor Path Invariant Simon Fraser University – Spring 09.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
Cs7120 (Prasad)L9-RECUR-IND1 Recursion and Induction.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Checking Reachability using Matching Logic Grigore Rosu and Andrei Stefanescu University of Illinois, USA.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Slide 1 A Class of Prolog Programs with Non-linear Outputs Inferable from Positive Data M.R.K. Krishna Rao King Fahd University of Petroleum and Minerals.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
Logical Agents Chapter 7. Outline Knowledge-based agents Logic in general Propositional (Boolean) logic Equivalence, validity, satisfiability.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Condition Testing. Condition testing is a test case design method that exercises the logical conditions contained in a program module. A simple condition.
Discovering Musical Patterns through Perceptive Heuristics By Oliver Lartillot Presentation by Ananda Jacobs.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
EEL 5937 Content languages EEL 5937 Multi Agent Systems Lecture 10, Feb. 6, 2003 Lotzi Bölöni.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Recursion Higher Order Functions CSCE 314 Spring 2016.
Interpreting the Object Constraint Presented by: Ed Kausmeyer.
1 A framework for eager encoding Daniel Kroening ETH, Switzerland Ofer Strichman Technion, Israel (Executive summary) (submitted to: Formal Aspects of.
Abstraction and Abstract Interpretation. Abstraction (a simplified view) Abstraction is an effective tool in verification Given a transition system, we.
Adapted from Krste Asanovic
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Recursion and Induction
Computers & Programming Languages
CSE 311 Foundations of Computing I
Samir Genaim Universita’ degli Studi di Verona, Italy Andy King
Andy King Computing Laboratory University of Kent at Canterbury
Higher Order Functions
PROGRAMMING IN HASKELL
Formal Methods in software development
CSCE 314: Programming Languages Dr. Dylan Shell
Introduction to Computer Science
PROGRAMMING IN HASKELL
Predicate Abstraction
Presentation transcript:

Determinacy Inference for Logic Programs Lunjin Oakland University In collaboration with Andy Kent University, UK

Context  Project - “An Integrated Framework for Semantic Based Analysis of Logic Programs” Backward analyses Parametric analyses Context sensitive analyses  Project – “US-UK Collaborative Research on Backward analyses for Logic Programs”

Determinacy sort(Xs,Ys) :- perm(Xs,Ys), ordered(Ys). perm(Xs,[Z|Zs]) :- select(Z,Xs,Ys),perm(Ys,Zs). perm([],[]). ordered([]). ordered([X]) :- number(X). ordered([X,Y|Ys]) :- X =< Y, ordered([Y|Ys]). select(X,[X|Xs],Xs). select(X,[Y|Ys],[Y|Zs]) :- select(X,Ys,Zs).  A call is determinate if it has at most one computed answer and that answer is generated once. ?- sort([2,2],L). L=[2,2] ; No.

Goal  Infer sufficient conditions under which a call is determinate Generalizing determinacy checking

Motivation  Useful in language implementations  Program development Tune performance  Execute a determinate call under once; Detect possible bugs; Useful in program specialization  Unfold when certain determinacy condition is satisfied.  Determinacy is important in concurrent programming

Reversing a list (1) append(Xs, Ys, Zs) :- Xs = [], Ys = Zs. (2) append(Xs, Ys, Zs) :- Xs = [X|Xs1], Zs = [X|Zs1], append(Xs1, Ys, Zs1). (3) rev(Xs,Ys) :- Xs = [], Ys = []. (4) rev(Xs,Ys) :- Xs = [X|Xs1], Ys2 = [X], rev(Xs1, Ys1), append(Ys1, Ys2, Ys).

Overall structure of analysis Success Patterns wrt Mux Conditions wrt rigid’ Determinacy Conditions wrt rigid’ Success pattern wrt rigid’ Determinacy condition wrt rigid’

Computing success patterns wrt term abstraction Success Patterns wrt Mux Conditions wrt rigid’ Determinacy Conditions wrt rigid’ Success pattern wrt rigid’ Determinacy condition wrt rigid’

Abstracting terms  A concrete term is mapped into an abstract term via a map .  Example one: d k d 0 (t) = _ d k (X) = X d k (f(t 1,…,t n )) = f(d k-1 (t 1 ),…,d k-1 (t n ))  Example two: list length norm

Abstracting primitive constraints  Term abstraction  induces an abstraction map, also denoted , that takes concrete constraints into abstract constraints.  Examples (K=[X|L]) is (K=1+L) when  = || || (L=[X,Y,Z,W]) is (L=[X,Y|_]) when  = d 2.  Operations on abstract constraints \/ /\  x 

Abstracting program wrt a term abstraction (1) append(Xs, Ys, Zs) :- Xs = [], Ys = Zs. append(Xs, Ys, Zs) :- Xs≥0, Ys≥0, Zs≥0, Xs = 0, Ys = Zs. (2) append(Xs, Ys, Zs) :- Xs = [X|Xs1], Zs = [X|Zs1], append(Xs1, Ys, Zs1). append(Xs, Ys, Zs) :- Xs≥0, Ys≥0, Zs≥0, Xs1≥0, Zs1≥0, Xs = 1+Xs1, Zs = 1+Zs1, append(Xs1, Ys, Zs1).

Abstract program (1) append(Xs, Ys, Zs) :- Xs≥0, Ys≥0, Zs≥0, Xs = 0, Ys = Zs. (2) append(Xs, Ys, Zs) :- Xs≥0, Ys≥0, Zs≥0, Xs1≥0, Zs1≥0, Xs = 1+Xs1, Zs = 1+Zs1, append(Xs1, Ys, Zs1). (3) rev(Xs,Ys) :- Xs≥0, Ys≥0, Xs = 0, Ys = 0. (4) rev(Xs,Ys) :- Xs≥0, Ys≥0, Xs1≥0, Ys1≥0,Ys2≥0, Xs = 1+Xs1, Ys2 = 1, rev(Xs1, Ys1), append(Ys1, Ys2, Ys).

Computing success set of abstract program 1) append(Xs,Ys,Zs) :- Ys≥0,Zs≥0,Xs=0,Ys=Zs. 2) append(Xs, Ys, Zs) :- 1≥Xs,Xs≥0,Ys≥0,1≥Zs,Zs≥0,Zs=Xs+Ys 3) append(Xs,Ys,Zs) :- Xs≥0,Ys≥0,Zs=Xs+Ys 4) rev(Xs,Ys) :- Xs = 0, Ys = 0. 5) rev(Xs,Ys) :- 1≥Xs, Xs≥0, 1≥Ys, Ys≥0, Xs=Ys. 6) rev(Xs,Ys) :- Xs≥0, Xs=Ys.

Predicate level success patterns append(x1,x2,x3) :- (x1≥0)  (x2≥0)  (x1+x2=x3) rev(x1,x2) :- (x1≥0)  (x1=x2)

Clause level success patterns (1) append(x1,x2,x3) :- (x1=0)  (x2≥0)  (x2=x3). (2) append(x1,x2,x3) :- (x1≥1)  (x2≥0)  (x1+x2=x3). (3) rev(x1,x2) :- (x1=0)  (x2=0). (4) rev(x1,x2) :- (x1≥1)  (x1=x2)

Synthesizing mutual exclusion conditions Success Patterns wrt Mux Conditions wrt rigid’ Determinacy Conditions wrt rigid’ Success pattern wrt rigid’ Determinacy condition wrt rigid’

Tracking rigidity  Induced rigidity: term abstraction  induces a rigidity predicate: rigid  (t)  (t)=((t)) for any e.g. rigid  ([1,X]) = true e.g. rigid  (X) = false  Tracked rigidity: it may be simpler to track a rigidity predicate which implies the induced one. rigid’  (t)  rigid  (t)  Domain of rigidity: Pos Rigidity dependence is expressed as positive Boolean functions such as (x1x2).

Galois connection  rigid’  (f) = {Sub|Sub.assign()|=f}  rigid’  () = {fPos|  rigid  (f)} assign()= {x rigid’  ( (x))|x dom()}

Mutual exclusion conditions  A mutual exclusion condition for a predicate is a rigidity constraint under which at most one clause of the predicate may commence a successful derivation.

Mutual exclusion of two clauses Let C 1 and C 2 have success patterns p(x) :- c 1 p(x) :- c 2. If Y and X P (Y,p(x),C 1,C 2 ) then C 1 and C 2 are mutually exclusive where X P (Y,p(x),C 1,C 2 ) = ( -Y (c 1 ) -Y (c 2 ))

Mutual exclusion of two clauses (1) append(x1,x2,x3) :- (x1=0)  (x2≥0)  (x2=x3). c1 (2) append(x1,x2,x3) :- (x1≥1)  (x2≥0)  (x1+x2=x3). c2  -{x1} (c1) = (x1=0)  -{x1} (c2) = (x1≥1) X P ({x1},p(x),C 1,C 2 ) = true  -{x2,x3} (c1) = (x2≥0)  (x2=x3)  -{x2,x3} (c2) = (x2≥0)  (x2<x3) X P ({x2,x3},p(x),C 1,C 2 ) = true

Synthesizing mutual exclusions X P (p(x)) = {Y|C 1,C 2 S. (C 1 C 2 X P (Y,p(x),C 1,C 2 ))} with S = the set of clauses defining p. X P (append(x1,x2,x3)) = x1  (x2x3) X P (rev(x1,x2)) = x1  x2

Synthesizing Determinacy Conditions Success Patterns wrt Mux Conditions wrt rigid’ Determinacy Conditions wrt rigid’ Success pattern wrt rigid’ Determinacy condition wrt rigid’

Synthesizing determinacy conditions  Determinacy inference takes two steps: The first is a lfp that computes rigidity success patterns The second is a gfp that calculates rigidity call patterns that ensures determinacy. (objective) Both lfp and gfp works on rigidity abstraction of the original program.

Abstracting program wrt rigidity (1) append(Xs, Ys, Zs) :- Xs = [], Ys = Zs. append(Xs, Ys, Zs) :- Xs  (Ys  Zs). (2) append(Xs, Ys, Zs) :- Xs=[X|Xs1], Zs=[X|Zs1], append(Xs1,Ys,Zs1). append(Xs, Ys, Zs) :- (XsXs1)(ZsZs1), append(Xs1,Ys,Zs1).

Rigidity program (1) append(Xs,Ys,Zs) :- Xs  (Ys  Zs). (2) append(Xs,Ys,Zs) :- (Xs  Xs1)  (Zs  Zs1), append(Xs1,Ys,Zs1). (3) rev(Xs,Ys) :- Xs  Ys. (4) rev(Xs,Ys) :- (Xs  Xs1)  Ys2, rev(Xs1,Ys1), append(Ys1,Ys2,Ys).

Computing rigidity success patterns  Rigidity success patterns of the original program is obtained by calculating success set of the rigidity program. append(x1,x2,x3) = x1  (x2  x3) rev(x1,x2) = x1  x2

Synthesizing determinacy conditions via backward analysis Success Patterns wrt Mux Conditions wrt rigid’ Determinacy Conditions wrt rigid’ Success pattern wrt rigid’ Determinacy condition wrt rigid’

Determinacy Conditions  Determinacy conditions describes those queries that are determinate.  There is one determinacy condition for each predicate.  p(x) :- g states that (p(x)) has at most one computed answer whenever   rigid’  (g).

Lower approximation of determinacy conditions  If p(x) :- g is a determinacy condition and g’ |= g then p(x):-g’ is a determinacy condition.  Determinacy conditions can thus be approximated from below without compromising correctness (but not necessarily from above)

Greatest fixpoint computation  Iteration commences with I 0 ={ p(x):-true | p }.  I k+1 is computed from I k by considering each clause in turn and calculating a (more) correct determinacy condition. Intially I k+1 = I k Strengthen I k+1

Propagating determinacy conditions backwards p(x)  f 0, p 1 (x 1 ),…,p n (x n ). A correct condition g’ is obtained by Propagating the condition on each call e i =  -x ((f 0   j<i f j )  g i )) where p i (x i ):-g i  I k+1 and f j is the rigidity success pattern for p j (x j ). Conjoining e 1, e 2, …, e n and mutual exclusion condition for p(x).

Updating I k+1  I k+1 contains a determinacy condition p(x) :- g” and this is updated to p(x) :- g”/\g’ if g” | g’.  Determinacy conditions becomes progressively stronger on each iteration.  This process will converge onto the gfp.

Gfp computation for reverse  I0: append(x1,x2,x3) :- true rev(x1,x2) :- true  I1: append(x1,x2,x3) = x1  (x2  x3) rev(x1,x2) = x1  x2  I3=I2: append(x1,x2,x3) = x1  (x2  x3) rev(x1,x2) = x1

Small Benchmarks

Performance

Other work on backward analysis for logic programs  Termination Inference (Codish & Genaim 2005)  Backward analysis via program transformation (Gallagher 2003)  Suspension (Genaim and King 2003)  Groundness (King & Lu 2002)  Type (Lu & King 2002)  Pair sharing (Lu & King 2004)  Set sharing (Li & Lu 2005)  Equivalence of Forward and backward analysis (King & Lu 2003)

Conclusion  Backwards analysis can infer sufficient conditions that ensures some properties are satisfied.  Determinacy inference analysis is composed of off-the-self success pattern analysis, mutual exclusion synthesis and a backwards analysis.  Initial experiments shows that it is practical and infers useful results.

Future work  Independence of computation rule For improved precision  Mutual exclusion condition More term abstractions