Motivating examples (1) Program Analysis and Transformation oddEven � even(X), even(s(X)). main(X) � based on Tree Automata zeroList(X), ....., even(0). member(1,X). even(s(X)) � odd(X). John Gallagher odd(s(X)) � even(X). zeroList([]). University of Roskilde, Denmark zeroList([0|X]) � zeroList(X). Can the query oddEven succeed? Supported by Framework 5 IST Project ASAP member(X,[X|_]). member(X,[_|Y]) � member(X,Y). Can the query main(X) succeed? Computer Science, building 42.1 Roskilde University Universitetsvej 1 P.O. Box 260 DK-4000 Roskilde Denmark Phone: +45 4674 2000 Fax: +45 4674 3072 www.dat.ruc.dk PAT 2005 Summer School, DIKU, Copenhagen 2 Motivating examples (2) Motivating Examples (3) Operations on a token ring (with any /* transpose a matrix */ gen([0,1]). row --> []; [any | row] number of processes) gen([0,0,1) . (example from Podelski & Charatonik). gen([0,0,0,...,1]). transpose(Xs,[]) :- matrix --> []; [row | matrix] .... nullrows(Xs). gen([0,1]). transpose(Xs,[Y|Ys]) :- Show "type correctness" gen([0 | X]) � gen(X). makerow(Xs,Y,Zs), of transpose(X,Y) . I.e. trans(X,Y) � trans1(X,Y). transpose(Zs,Ys). X and Y are both of type "matrix" trans([1 |X],[0|Y]) � trans2(X,Y). in all possible solutions. Intended reachable trans1([0,1|T],[1,0 |T]). makerow([],[],[]). states trans1([H|T],[H|T1]) � trans1(T,T1). makerow([[X|Xs]|Ys],[X|Xs1],[Xs|Zs]):- reachable([0,0,...,1,...0,0]) trans2([0],[1]). makerow(Ys,Xs1,Zs). Show "mode correctness" (lists with exactly one 1) trans2([H|T],[H|T1]) � trans2(T,T1). of transpose(X,Y) . I.e. reachable(X) � gen(X). X is a ground term iff reachable(X) � reachable(Y), trans(Y,X). nullrows([]). Y is a ground term. nullrows([[]|Ns]) :- What are the possible answers for nullrows(Ns). reachable(X)? Can X be a list containing more than one '1'? PAT 2005 Summer School, DIKU, Copenhagen 3 PAT 2005 Summer School, DIKU, Copenhagen 4
Motivating Examples (4) Motivating Examples (5) zero --> 0. Operations on a token ring (with any /* transpose a matrix */ row --> []; [any | row] one --> 1. number of processes) zerolist --> []; [zero|zerolist] transpose(Xs,[]) :- (example from Podelski & Charatonik). matrix --> []; [row | matrix] goodlist --> [one|zerolist]; nullrows(Xs). [zero|goodlist] . gen([0,1]). transpose(Xs,[Y|Ys]) :- Suppose we are partially gen([0 | X]) � gen(X). makerow(Xs,Y,Zs), evaluating transpose(X,Y) w.r.t trans(X,Y) � trans1(X,Y). transpose(Zs,Ys). a partially known matrix, Show that all solutions of trans([1 |X],[0|Y]) � trans2(X,Y). where X is a list of unknown reachable(X) are such that trans1([0,1|T],[1,0 |T]). makerow([],[],[]). values, e.g. X = [U1,U2,U3]. X is a goodlist. trans1([H|T],[H|T1]) � trans1(T,T1). makerow([[X|Xs]|Ys],[X|Xs1],[Xs|Zs]):- I.e. specialise for 3 X m matrices. trans2([0],[1]). makerow(Ys,Xs1,Zs). trans2([H|T],[H|T1]) � trans2(T,T1). Show that every call to transpose reachable(X) � gen(X). during partial evaluation has reachable(X) � reachable(Y), trans(Y,X). nullrows([]). its first argument instantiated to nullrows([[]|Ns]) :- a list. nullrows(Ns). PAT 2005 Summer School, DIKU, Copenhagen 5 PAT 2005 Summer School, DIKU, Copenhagen 6 Approximating sets of terms Regular/Recognizable Tree Languages • Let � be a signature - a set of function • Suppose � = {[], [.|.], 0, s(.)} symbols, each having a rank (arity) • We can specify the set of all lists, i.e. • Term( � ) is the set of all terms (trees) {[],[0],[s(0)],[s(s([])), 0], [[]], [[0],[0,0]],...} constructible from � • i.e. terms of form f(t 1 ,...,t n ) where f � � , f has [] --> list arity n and t 1 � Term( � ),...,t n � Term( � ) [any|list] --> list • when arity is 0, we write f() as f. 0 --> any • Term n ( � ) denotes the set of n-ary relations [] --> any over Term( � ). [any|any] --> any s(any) --> any PAT 2005 Summer School, DIKU, Copenhagen 7 PAT 2005 Summer School, DIKU, Copenhagen 8
Cartesian Approximation NFTA - Nondeterministic finite tree automata Tree automata provide a means of specifying infinite sets of • Our aim is to approximate the relations computed trees (terms) over some signature � . by logic programs. • Let R be some relation over Term( � ) A (nondeterministic) finite tree automaton (N)FTA is a tuple <Q, Q f , � , � > where • The Cartesian approximation of a relation R is the Q is a finite set of states product of the sets of values in each position of the Q f � Q are the accepting states � is a finite set of transitions (rules) of the form relation. f(q1,…,qn) � q0, • E.g. let R = reverse = {<[],[]>, <[a],[a]>, where q0, q1,…,qn � Q, and f is an n-ary <[a,b],[b,a]>, <[a,a,b],[b,a,a]>,...}, function in � . • or written as {reverse([],[]), reverse([a],[a]), reverse([a,b],[b,a]), reverse([a,a,b],[b,a,a]), ...} An FTA A defines a set of terms L(A) (we will see how shortly) • Cartesian approximation is R 1 X R 2 where R 1 = {[], Example: <{list, any}, {list}, {[], [.|.], 0, s(.)}, � > [a], [a,b], [a,a,b],....} and R 2 = {[], [a], [b,a], where � = {[] � list,[any|list] � list, 0 � any,[] � any, [b,a,a],....} [any|any] � any, s(any) � any} PAT 2005 Summer School, DIKU, Copenhagen 9 PAT 2005 Summer School, DIKU, Copenhagen 10 Approximation Using FTAs Two Approaches to Analysis using FTAs 1. Given a program and an FTA, compute an • The set of values in each argument will be approximated using an FTA. approximation of the program in terms of • So we could approximate reverse as reverse = the states in the given FTA. {<x,y> | x � L(A), y � L(A)} where A is the FTA • e.g. given the matrix transpose program and <{list,a,b},{list}, � , � > the FTA defining matrices, derive the relation • � = {[],[.|.],a,b}, � = {[] � list, [a|list] � list, [b|list] � transpose(matrix,matrix) as an approximation. list, a � a, b � b} 2. Given a program, derive an FTA that is a safe approximation of the relations • So reverse has lists of a and b as arguments. defined by the programs • we write reverse(list, list) as the approximation. • e.g. given the reverse program, derive the list- • in general, we write a Cartesian approximation of relation R using FTAs as R(q1,...,qn) where q1,...,qn FTA and the relation approximation are the states in an FTA. reverse(list,list). PAT 2005 Summer School, DIKU, Copenhagen 11 PAT 2005 Summer School, DIKU, Copenhagen 12
FTA Properties and Operations Running an FTA • FTAs form a reasonably expressive • Top-down • Bottom-up language for describing sets of terms. 1. Initialise current term = 1. Initialise current term • Languages defined by FTAs are closed an accepting state = a term in Term( � ) 2. Pick a state q at a leaf in under operations (intersection, union, 2. Pick a subterm the current term, and complement). f(q1,...,qn) from the find a rule f(q1,...,qn) � current term, and find a • Emptiness of an FTA and membership of a q rule f(q1,...,qn) � q term in L(A) are decidable. 3. Replace q by f(q1,...,qn) 3. Replace f(q1,...,qn) by q 4. Terminate (successfully) 4. Terminate (successfully) when a term in Term( � ) • We will see later that expressiveness can when the current term is is generated an accepting state. be increased more, while retaining desirable computational properties. PAT 2005 Summer School, DIKU, Copenhagen 13 PAT 2005 Summer School, DIKU, Copenhagen 14 Running the list-FTA Language accepted by an FTA • Top-down and bottom-up are equivalent • • Bottom-up Top-down • Given an FTA <Q,Q f , � , � > • • [s(s(0)), 0] list replace list by [any|list] replace [] by list • there exists a top-down run (derivation) from • [any|list] • [s(s(0)), 0|list] accepting state q � Q f to t � Term( � ) if and only • [s(any)|list] • [s(s(0)), any|list] if there exists a bottom-up run (derivation) from t to q. • [s(s(any))|list] • [s(s(0))|list] • In either case we say that t is accepted by replace any by 0 replace 0 by any (state q of) the FTA. • [s(s(0))|list] • [s(s(any))|list] • [s(s(0)), any|list] • [s(any)|list] • The set of all terms accepted by some final • [s(s(0)), 0|list] • [any|list] replace list by [] replace [any|list] by list state of an FTA A is called the language of • [s(s(0)), 0] • list A, L(A). PAT 2005 Summer School, DIKU, Copenhagen 15 PAT 2005 Summer School, DIKU, Copenhagen 16
Recommend
More recommend