Title: Optimisation of Declarative Programs Lecture 1: Introduction
1Optimisation of Declarative Programs Lecture 1
Introduction
- Michael Leuschel
- Declarative Systems Software Engineering
- Dept. Electronics Computer Science
- University of Southampton
- http//
www.ecs.soton.ac.uk/mal
Morten Heine Sørensen DIKU, Copenhagen
2Lecture 1
1. Overview 2. PE 1st steps 3. Why PE 4. Issues
in PE
- Explain what these lectures are about
- Program Optimisation in general
- Partial Evaluation in particular
- Program Transformation, Analysis, and
Specialisation - Explain why you should attend the lectures
- Why are the topics interesting ?
- Why are they useful ?
- Overview of the whole course
3Part 1Program Optimisation and Partial
Evaluation A first overview
1. Overview 2. PE 1st steps 3. Why PE 4. Issues
in PE
4(Automatic) Program Optimisation
- When
- Source-to-source
- during compilation
- at link-time
- at run-time
- Why
- Make existing programs faster (10 ? 500 ? ?
...) - Enable safer, high-level programming style (??)
- (Program verification,)
5Program Optimisation II
- What
- constant propagation, inlining , dead-code
elimination, eliminate redundant computations,
change of algorithm or data-structures, - Similar to highly optimising compilers
- much more aggressive,
- (much) greater speedups and
- much more difficult to control
- How ?
6Program Transformation Unfold/fold
Final Program Pn
Initial Program P0
Program P1
...
Under some conditions Same semantics Same
termination properties Better performance
7Program Specialisation
Drawing Program P
- What
- Specialise a program for aparticular application
domain - How
- Partial evaluation
- Program transformation
- Type specialisation
- Slicing
P
8Overview
Partial Evaluation
Program Transformation
Program Specialisation
Program Optimisation
9Program Analysis
- What
- Find out interesting properties of your programs
- Values produced, dependencies, usage,
termination, ... - Why
- Verification, debugging
- Type checking
- Check assertions
- Optimisation
- Guide compiler (compile time gc, )
- Guide source-to-source optimiser
10Program Analysis II
- How
- Rices theorem all non-trivial properties are
undecidable ! - ? approximations or sufficient conditions
- Techniques
- Ad hoc
- Abstract Interpretation Cousot77
- Type Inference
11Abstract Interpretation
Concrete domain
-2 -1 0 1 2 3 ...
- Principle
- Abstract domain
- Abstract operations
- safe approximation of concrete operations
- Result
- Correct
- Termination can be guaranteed
- ? Decidable approximation of undecidable
properties
N N N N 0 N ...
12Part 2A closer look at partial evaluation
First Steps
1. Overview 2. PE 1st steps 3. Why PE 4. Issues
in PE
13A closer look at PE
- Partial Evaluation versus Full Evaluation
- Why PE
- Issues in PE Correctness, Precision, Termination
- Self-Application and Compilation
14Full Evaluation
- Full input
- Computes full output
function power(b,e) is if e 0 then 1
else bpower(b,e-1)
15Partial Evaluation
- Only part of the input
- ? produce part of the output ?
- power(?,2)
- ? evaluate as much as you can
- ? produce a specialized program
- power_2(?)
16PE A first attempt
function power(b,e) is if e 0 then 1
else bpower(b,e-1)
- Small (functional) language
- constants (N), variables (a,b,c,)
- arithmetic expressions ,/,,-,
- if-then-else, function definitions
- Basic Operations
- Evaluate arithmetic operation if all arguments
are known - 23 ? 5 54 ? false x(23) ? x5
- Replace if-then-else by then-part (resp.
else-part) if test-part is known to be true
(resp. false)
17Example power
function power(b,2) is if e 0 then 1
else bpower(b,e-1)
function power(b,2) is if 2 0 then 1
else bpower(b,e-1)
function power(b,2) is if false then 1
else bpower(b,2-1)
Residual code
function power_2(b) is bpower_1(b)
function power(b,1) is if e 0 then 1
else bpower(b,e-1)
function power(b,1) is if false then 1
else bpower(b,1-1)
function power_1(b) is bpower_0(b)
18Example power (contd)
function power(b,0) is if e 0 then 1
else bpower(b,e-1)
function power(b,0) is if 0 0 then 1
else bpower(b,e-1)
function power(b,0) is if 0 0 then 1
else bpower(b,e-1)
Residual code
function power_0(b) is 1
19Example power (contd)
function power_1(b) is bpower_0(b)
function power_2(b) is bpower_1(b)
function power_0(b) is 1
- What we really, really want
function power_2(b) is bb
20Extra Operation Unfolding
evaluating the function call operation
- Replace call by definition
function power(b,2) is if 2 0 then 1
else bpower(b,1)
function power(b,2) is if 2 0 then 1
else b (if 1 0 then 1 else
bpower(b,0))
function power(b,2) is if 2 0 then 1
else b (if 1 0 then 1 else b (if 0
0 then 1 else bpower(b,-1)))
Residual code
function power_2(b) is bb1
SQR function
21PE Not always beneficial
function power(3,e) is if e 0 then 1
else 3power(3,e-1)
function power(3,e) is if e 0 then 1
else bpower(b,e-1)
function power_3(e) is if e 0 then 1
else 3power_3(e-1)
22Part 3Why Partial Evaluation (and Program
Optimisation)?
1. Overview 2. PE 1st steps 3. Why PE 4. Issues
in PE
23Constant Propagation
- Static values in programs
function my_program(x) is y 2
power(x,3)
captures inlining and more
function my_program(x) is y 2 x x x
24Abstract Datatypes / Modules
- Get rid of inherent overhead by PE
function my_program(x) is if not
empty_tree(x) then y get_value(x)
get rid of redundant run-time tests
function my_program(x) is if x ! null then
if x ! null then y x-gtval
25Higher-Order/Reusing Generic Code
- Get rid of overhead ? incite usage
function my_program(x,y,z) is r
reduce(,1,map(inc,x,y,z))
function my_program(x,y,z) is r
(x1)(y1)(z1)
26Higher-Order II
- Can generate new functions/procedures
function my_program(x) is r
reduce(,1,map(inc,x))
function my_program(x) is r
red_map_1(x) function red_map_1(x) is if
xnil then return 1 else return x-gthead
red_map_1(x-gttail)
27Staged Input
- Input does not arrive all at once
5
2
2.1
my_program (x , y , z)
42
28Examples of staged input
- Ray tracing calculate_view(Scene,Lights,Viewpoint
)Interpretation interpreter(ObjectProgram,Call)
prove_theorem(FOL-Theory,Theorem) check_integrit
y(Db_rules,Update) schedule_crews(Rules,Facts) - Speedups
- 2 you get your money back
- 10 quite typical for interpretation overhead
- 100-500 (and even 8) possible
29Ray Tracing
Static
30Why go beyond partial evaluation ?
- Improve algorithms (not necessarily PS)
- Tupling
- Deforestation
- Superlinear speedups
- Exponential ? linear algorithm
- Non-executable ? executable programs
- Software Verification, Inversion, Model Checking
31Tupling
Corresponds to loop-fusion
32Deforestation
33Part 4Issues in Partial Evaluation and Program
Optimisation
1. Overview 2. PE 1st steps 3. Why PE 4. Issues
in PE
Efficiency/Precision
Correctness
Termination
Self-application
34Correctness
- Language
- Power/Elegance unfolding LP easy, FP ok, C
aargh - Modularity global variables, pointers
- Semantics
- Purely logical
- Somewhat operational (termination,)
- Purely operational
- Informal/compiler
admissive
restrictive
35Programming Language for Course
- Lectures use mainly LP and FP
- Dont distract from essential issues
- Nice programming paradigm
- A lot of room for speedups
- Techniques can be useful for other languages, but
- Correctness will be more difficult to establish
- Extra analyses might be required (aliasing, flow
analysis, )
36Efficiency/Precision
x 2yp(x) y5 ? p(7)
- Unfold enough but not too much
- Enough to propagate partial information
- But still ensure termination
- Binding-time Analysis (for offline systems)
- Which expressions will be definitely known
- Which statements can be executed
- Allow enough polyvariance but not too much
- Code explosion problem
- Characteristic trees
p(7) p(9) p(11) p(13) ...
37Termination
- Who cares
- PE should terminate when the program does
- PE should always terminate
- " within reasonable time bounds
State of the art
38Self-Application
- Principle
- Write a partial evaluator (program specialiser,)
which can specialise itself - Why
- Ensures non-trivial partial evaluator
- Ensures non-trivial language
- Optimise specialisation process
- Automatic compiler generation !
39Compiling by PE
Call2
Call1
Call3
Result1
Interpreter
Object Program P
Result2
Result3
Call2
Call1
Call3
PE
Result1
P-Interpreter
Result2
Result3
40Compiler Generation by PE
Object Program R
static
Object Program Q
Object Program R
Object Program P
Object Program Q
Interpreter I
Object Program P
PE
PE
I-PE
Compiler !
Useful for Lge Extensions, Debugging, DSL,...
P-Interpreter
Q-Interpreter
R-Interpreter
41Cogen Generation by PE
Interpreter R
static
Interpreter Q
Interpreter R
Interpreter P
Interpreter Q
PE
Interpreter P
PE
PE
PE-PE
Compiler Generator !
3rd Futamura Projection
P-Compiler
Q-Compiler
R-Compiler
42Part 5Overview of the course (remainder)
43Overview of the Course Lecture 2
- Partial Evaluation of Logic ProgramsFirst Steps
- Topics
- A short (re-)introduction to Logic Programming
- How to do partial evaluation of logic
programs(called partial deduction) first steps - Outcome
- Enough knowledge of LP to follow the rest
- Concrete idea of PE for one particular language
44Overview of the Course Lecture 3
- (Online) Partial Deduction Foundations,
Algorithms and Experiments - Topics
- Correctness criterion and results
- Control Issues Local vs. Global Online vs.
Offline - Local control solutions Termination
- Global control Ch. trees, WQO's at the global
level - Relevance for other languages and applications
- Full demo of Ecce system
- Outcome
- How to control program specialisers (analysers,
...) - How to prove them correct
45Overview of the Course Lecture 4(?)
- Extending Partial Deduction
- Topics
- Unfold/Fold vs PD
- Going from PD to Conjunctive PD
- Control issues, Demo of Ecce
- Abstract Interpretation vs Conj. PD (and
Unfold/Fold) - Integrating Abstract Interpretation
- Applications ( Demo) Infinite Model Checking
- Outcome
- How to do very advanced/fancy optimisations
46Overview of the CourseOther Lectures
- By Morten Heine Sørensen
- Context
- Functional Programming Languages
- Topics
- Supercompilation
- Unfold/fold
- ...
47Summary of Lecture 1What to know for the exam
- Terminology
- Program Optimisation, Transformation, Analysis,
Specialisation, PE - Basics of PE and Optimisation in general
- Uses of PE and Program Optimisation
- Common compiler optimisations
- Enabling high-level programming
- Staged input, optimising existing code
- Self-application and compiler generation
48Optimisation of Declarative Programs Lecture 2
- Michael Leuschel
- Declarative Systems Software Engineering
- Dept. Electronics Computer Science
- University of Southampton
- http//
www.ecs.soton.ac.uk/mal
49Overview of Lecture 2
1. LP Refresher 2. PE of LP 1st steps
- Recap of LP
- First steps in PE of LP
- Why is PE non-trivial ?
- Generating Code Resultants
- Correctness
- Independence, Non-triviality, Closedness
- Results
- Filtering
- Small demo of the ecce system
50Part 1Brief Refresher onLogic Programming and
Prolog
1. LP Refresher 2. PE of LP 1st steps
51Brief History of Logic Programming
- Invented in 1972
- Colmerauer (implementation, Prolog
PROgrammation en LOGique) - Kowalski (theory)
- Insight subset of First-Order Logic
- efficient, procedural interpretation based on
resolution (Robinson 1965 Herbrand 1931) - Program theory
- Computation logical inference
52Features of Logic Programming
- Declarative Language
- State what is to be computed, not how
- Uniform Language to express and reason about
- Programs, specifications
- Databases, queries,
- Simple, clear semantics
- Reasoning about correctness is possible
- Enable powerful optimisations
53Logical Foundation SLD-Resolution
- Selection-rule driven Linear Resolution for
Definite Clauses - Selection-rule
- In a denial selects a literal to be resolved
- Linear Resolution
- Derive new denial, forget old denial
- Definite Clauses
- Limited to (Definite) Program Clauses
- (Normal clause negation allowed in body)
542. Higher-Order Logic
? R. R(tom) ? human(tom)
variables over objects and relations
1. First-Order Logic - constants, functions,
variables (tom mother(tom) X) -
relations ( human(.) married(. , .) ) -
quantifiers (? ? )
human(sokrates)
? X. human(X) ? mortal(X)
variables over objects
0. Propositional Logic - propositions (basic
units either true or false) - logical
connectives (? ? ? ?)
rains
rains ? carry_umbrella
p ?? p
rains ?? rains
no variables
55FO Resolution Principle Example
1. select literals
1.) ?X umbrella(X) ? ?rains(X) 2.)
rains(london) 1.) umbrella(london) ?
?rains(london) 2.) rains(london) 12)
umbrella(london)
2. no renaming required
3. mgu ? X / london
4. apply ?
5. resolution
56Terminology
- SLD-Derivation for P ? ? G
- Sequence of goals and mgus, at every step
- Apply selection rule on current goal (very first
goal G) - Unify selected literal with the head of a program
clause - Derive the next goal by resolution
- SLD-Refutation for P ? ? G
- SLD-Derivation ending in ?
- Computed answer for P ? ? G
- Composition of mgus of a SLD-refutation,
restricted to variables in G
57SLDDerivation
G
- ? knows_logic(Z)
- ? good_student(X) ? teacher(Y,X) ?
logician(Y) - ? teacher(Y,tom) ? logician(Y)
- ? logician(peter)
- ?
- 1) knows_logic(X) ? good_student(X) ?
teacher(Y,X) ? logician(Y) - 2) good_student(tom)
- 3) logician(peter)
- 4) teacher(peter,tom)
Z/X
X/tom
Y/peter
Computed answer Z/tom ? P J knows_logic(tom)
??
58Backtracking
- ? knows_logic(Z)
- ? good_student(X) ? teacher(Y,X) ?
logician(Y) - ? teacher(Y,jane) ? logician(Y)
- ? teacher(Y,tom) ? logician(Y)
- ? logician(peter)
- ?
- 1) knows_logic(X) ? good_student(X) ?
teacher(Y,X) ? logician(Y) - 2) good_student(jane)
- 3) good_student(tom)
- 4) logician(peter)
- 5) teacher(peter,tom)
?
Backtracking required !
Try to resolve with another clause
59SLD-Trees
- ? knows_logic(Z)
- ? good_student(X) ? teacher(Y,X) ?
logician(Y) - ? teacher(Y,tom) ? logician(Y)
- ? logician(peter)
- ?
- ? teacher(Y,jane) ? logician(Y)
?
Prolog explores this tree Depth-First, always
selects leftmost literal
60SLD-Trees II
- Branches of SLD-tree SLD-derivations
- Types of branches
- refutation (successful)
- finitely failed
- infinite
61Some info about Prolog
- Depth-first traversal of SLD-tree
- LD-resolution
- Always select leftmost literal in a denial
62Prologs Syntax
- Facts
- human(socrates).
- Rules (clauses)
- carry_umbrella - rains,no_hat.
- Variables
- mortal(X) - human(X).
- exists_human - human(Any).
- Queries
- ?- mortal(Z).
? (implication)
? (conjunction)
existential variable (?)
universal variable (?)
63Prologs Datastructures
lower-case
- Constants
- human(socrates).
- Integers, Reals
- fib(0,1).
- pi(3.141).
- (Compound) Terms
- head_of_list(cons(X,T),X).
- tail_of_list(cons(X,T),T).
- ?- head_of_list(cons(1,cons(2,nil)),R).
functor
Prolog actually uses . (dot) for lists
64Lists in Prolog
- Empty list
- (syntactic sugar for nil )
- Non-Empty list with first element H and tail T
- HT (syntactic sugar for .(H,T) )
- Fixed-length list
- a (syntactic sugar for a
.(a,nil) ) - a,b,c (syntactic sugar for a b c
)
65Part 2Partial Evaluation of Logic
ProgramsFirst Steps
1. LP Refresher 2. PE of LP 1st steps
66What is Full Evaluation in LP
- In our context full evaluation constructing
complete SLD-tree for a goal - ? every branch either
- successful, failed or infinite
- SLD can handle variables !!
- ? can handle partial data-structures
- (not the case for imperative or functional
programming) - ? Unfolding ordinary SLD-resolution !
- ? Partial evaluation full evaluation ??
- ? Partial evaluation trivial ??
67Example
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app (a,B,C)
C/aC
app (,B,C)
C/B
?
app(a,B,aB). app_a(B,aB).
68ExampleRuntimeQuery
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app (a,b,C)
C/aC
app (,b,C)
app (a,b,C)
app_a (b,C)
C/b
C/a,b
C/a,b
?
?
?
app(a,B,aB). app_a(B,aB).
c.a.s. C/a,b
69Example II
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app (A,B, a)
A/aA
A/,B/a
app (A,B,)
?
A/
?
app(,a,a). app(a,,a). app_a(,a).
app_a(a,).
70Example III
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
- BUT complete SLD-tree usually infinite !!
app (A,a,B)
A/HA,B/HB
A/,B/a
app (A,a,B)
?
A/HA,B/HB
A/,B/a
app (A,a,B)
?
...
71Partial Deduction
- Basic Principle
- Instead of building one complete SLD-treeBuild
a finite number of finite SLD- trees ! - SLD-trees can be incomplete
- 4 types of derivations in SLD-trees
- Successful, failed, infinite
- Incomplete no literal selected
72Example (revisited)
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app_a(,a). app_a(HA,HB) -
app_a(A,B).
73Example (revisited)
app (A,a,B)
app (A,a,B)
?
Infinite complete tree is covered
app (A,a,B)
?
app (A,a,B)
?
...
74Main issues in PD ( PE in general)
- How to construct the specialised code ?
- When to stop unfolding (building SLD-tree) ?
- Construct SLD-trees for which goals ?
- When is the result correct ?
- ? Will be addressed in this Lecture !
75Generating CodeResultants
G0
- ? A1,,Ai,,An
- ? G1
- ? G2
- ...
- ? Gk
- Resultant ofSLD-derivation
- is the formula
- G0?1...?k ? Gk
- if n1 Horn clause !
?0
?1
?k
76Resultants Example
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app(,a,a). app(HA,a,HB) -
app(A,a,B).
77Formalisation of Partial Deduction
- Given a set S A1,,An of atoms
- Build finite, incomplete SLD-trees for each ? Ai
- For every non-failing branch
- generate 1 specialised clause bycomputing the
resultants - When is this correct ?
78Correctness 1 Non-trivial trees
- Trivial tree
- Resultant app (A,a,B) ? app (A,a,B)
- loops!
- Correctness conditionPerform at least one
derivation step
? app (A,a,B)
79Correctness 2 Closedness
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
? app (aA,B,C)
C/aC
? app (A,B,C)
app(aA,B,aC) - app(A,B,C).
Not an instance of app (aA,B,C)
Not C/a,b !!
80Closedness Condition
- To avoid uncovered calls at runtime
- All predicate calls
- in the bodies of specialised clauses( atoms in
leaves of SLD-trees !) - in the calls to the specialised program
- must be instances of at least one of the atoms in
S A1,,An
81Correctness 3 Independence
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
? app (aA,B,C)
C/aC
? app (A,B,C)
app(aA,B,aC) - app(A,B,C). app(,C,C).
app(HA,B,HC) - app(A,B,C).
Closedness Ok
82Extra answers
app(aA,B,aC) - app(A,B,C). app(,C,C).
app(HA,B,HC) - app(A,B,C).
? app (X,Y,Z)
X/a,Z/aZ
Z/XZ
? app (,Y,Z)
? app (,Y,Z)
Extra answer ! more instantiated !!
?
?
X a, ZaY
ZXY
83Independence Condition
- To avoid extra and/or more instantiated answers
- require that no two atoms in S A1,,An have a
common instance - S app (aA,B,C), app(A,B,C)
- common instances app(a,X,Y), app(a,b,,X),
- How to ensure independence ?
- Just Renaming !
84Renaming No Extra answers
app_a(aA,B,aC) - app(A,B,C). app(,C,C)
. app(HA,B,HC) - app(A,B,C).
app(aA,B,aC) - app(A,B,C). app(,C,C).
app(HA,B,HC) - app(A,B,C).
? app (X,Y,Z)
Z/XZ
? app (,Y,Z)
No extra answer !
?
ZXY
85Soundness Completeness
- P obtained by partial deduction from P
- If non-triviality, closedness, independence
conditions are satisfied - P ? ?G has an SLD-refutation with c.a.s. ? iff
P ? ?Ghas - P ? ?G has a finitely failed SLD-tree iffP ?
?Ghas - Lloyd, Shepherdson J. Logic Progr. 1991
86A more detailed example
? map (inv,L,R)
map(P,,). map(P,HT,PHPT) -
C..P,H,PH, call(C),map(P,T,PT). inv(0,1).
inv(1,0).
map(P,,). map(P,HT,PHPT) -
C..P,H,PH, call(C),map(P,T,PT). inv(0,1).
inv(1,0).
Overhead removed 2? faster
map(inv,,). map(inv,0L,1R) -
map(inv,L,R). map(inv,1L,0R) -
map(inv,L,R).
87Filtering
? map (inv,L,R)
map(P,,). map(P,HT,PHPT) -
C..P,H,PH, call(C),map(P,T,PT). inv(0,1).
inv(1,0).
map(inv,,). map(inv,0L,1R) -
map(inv,L,R). map(inv,1L,0R) -
map(inv,L,R).
map_1(,). map_1(0L,1R) -
map_1(L,R). map_1(1L,0R) -
map_1(L,R).
88RenamingFiltering Correctness
- Correctness results carry over
- Benkerimi,Hill J. Logic Comp 3(5), 1993
- No worry about Independence
- Non-triviality also easy to ensure
- ? only worry that remains (for correctness)
Closedness
89Small Demo (ecce system)
- Append
- Higher-Order
- Match
- Interpreter
90Summary of part 2
- Partial evaluation in LP
- Generating Code
- Resultants, Renaming, Filtering
- Correctness conditions
- non-triviality
- closedness
- independence
- Soundness Completeness
91Optimisation of Declarative Programs Lecture 3
- Michael Leuschel
- Declarative Systems Software Engineering
- Dept. Electronics Computer Science
- University of Southampton
- http//
www.ecs.soton.ac.uk/mal
92Part 1Controlling Partial DeductionLocal
Control Termination
1. Local Control Termination 2. Global
Control Abstraction
93Issues in Control
A
A1 A2 A3 A4 ...
- Correctness
- ensure closedness? add uncovered leaves to set
A - Termination
- build finite SLD-trees
- build only a finite number of them !
- Precision
- unfold sufficiently to propagate information
- have a precise enough set A
global
local
CONFLICTS!
94ControlLocal vs Global
A
A1 A2 A3 A4 ...
- Local Control
- Decide upon the individual SLD-trees
- Influences the code generated for each Ai
- ? unfolding rule
- given a program P and goal G returns finite,
possibly incomplete SLD-tree - Global control
- decide which atoms are in A
- ? abstraction operator
95Generic Algorithm
Input program P and goal G Output specialised
program P Initialise i0, A0 atoms in G
repeat Ai1 Ai for each a ? Ai do
Ai1 Ai1 ? leaves(unfold(a)) end for
Ai1 abstract(Ai1) until Ai1
Ai compute P via resultantsrenaming
96Overview Lecture 3
- Local Control
- Determinacy
- Ensuring Termination
- Wfos, Wqos
- Global Control
- abstraction
- most specific generalisation
- characteristic trees
97(Local) Termination
- Who cares
- PE should terminate when the program does
- PE should always terminate
- " within reasonable time bounds
State of the art
Well-founded orders Well-quasi orders
98Determinate Unfolding
- Unfold atoms which match only 1 clause(only
exception first unfolding step) - avoids code explosion
- no duplication of work
- good heuristic for pre-computing
- too conservative on its own
- termination
- always ? ? No
- when program terminates ??
99Duplication
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
?app(A,B,C),app (_I,C,a,a)
a2(A,B,a,a) - app(A,B,a,a). a2(A,B,a) -
app(A,B,a). a2(A,B,) - app(A,B,).
100Duplication 2
a2(A,B,a,a) - app(A,B,a,a). a2(A,B,a) -
app(A,B,a). a2(A,B,) - app(A,B,).
?a2(a,,C)
- Solutions
- Determinacy!
- Follow runtime selection rule
101Order of Solutions
t(A,B) - p(A,C),p(C,B). p(a,b). p(b,a).
?t(A,C)
- Same solutions
- Determinacy!
- Follow runtime selection rule
?p(A,B),p(B,C)
t(b,b). t(a,a).
?
?
102Lookahead
- Also unfold if
- atom matches more than 1 clause
- only 1 clause remains after further unfolding
- less conservative
- still ensures no duplication, explosion
fail
103Lookahead example
? s4(5,R)
? sel (4,5,R)
sel(P,,). sel(P,HT,HPT) - PltH,
sel(P,T,PT). sel(P,HT,PT) - PgtH,
sel(P,T,PT). s4(L,PL) - sel(4,L,PL).
R/5R
? 4gt5, sel(4,,R)
? 4lt5, sel(4,,R)
s4(5,5).
104Example
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
?app (A,a,A)
At runtime app(,a,) terminates !!!
?
105Well-founded orders
- lt (strict) partial order (transitive,
anti-reflexive, anti-symmetric) with no ?
descending chainss1 gt s2 gt s3 gt - To ensure termination
- define lt on expressions/goals
- unfold only if sk1 lt sk
- Example termsize norm (number of function and
constant symbols)
106Example
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
?app (a,bC,A,B)
. 5
Ok
. 3
Ok
. 1
Stop
. 1
. 0
?
107Other wfos
- linear norms, semi-linear norms
- f(t1,,tn) cf cf,1 t1 cf,n tn
- X cV
- lexicographic ordering
- .1, , .k first use .1, if equal use
.2... - multiset ordering
- t1,,tn lt s1,,sk, t2,,tn if all si lt t1
- recursive path ordering
- ...
108More general-than/Instance-of
- AgtB if A strictly more general than B
- p(X)
- p(s(X))
- p(s(s(X)))
-
- not a wfo
- AgtB if A strict instance of B
- p(s(s(X)))
- p(s(X))
- p(X)
- X
- is a wfo (Huet80)
109Refining wfos
rev(,L,L). rev(HX,A,R) - rev(X,HA,R).
. 4
?rev (a,bC,,R)
. 6
? measure just arg 1
. 2
. 6
Ok
. 0
Stop
. 0
?
110Well-quasi orders
- ? quasi order (reflexive and transitive)
withevery infinite sequence s1,s2,s3, we can
findiltj such that si ? sj - To ensure termination
- define ? on expressions/goals
- unfold only if for no iltk1 si ? sk1
?
111Alternative Definitions of wqos
- All quasi orders ? which contain ? (a?b ? a?b)
are wfos - can be used to generate wfos
- Every set has a finite generating set (a?b ? a
generates b) - For every infinite sequence s1,s2, we can find
an infinite subsequence r1,r2, such that ri ?
ri1 - Wfo with finitely many incomparable elements
- ...
112Examples of wqos
- A?B if A and B have same top-level functor
- p(s(X))
- q(s(s(X)))
- r(s(s(s(X))))
- p(s(s(s(s(X)))))
- ...
- Is a wqo for a finite alphabet
- A?B if not A lt B, where gt is a wfo
- p(s(s(X)))
- r(s((X))
- q(X))
- p(s(s(s(s(X)))))
- Is a wqo (always) with same power as gt
?
Termsize not decreasing
113Instance-of Relation
- AgtB if A strict instance of B is a wfo
- A?B if A instance of B wqo ??
- p(s(X))
- p(s(s(X)))
- ...
- p(s(X))
- p(s(X))
-
?
p(0) p(s(0)) p(s(s(0))) p(s(s(s(0)))) p(s(s(s(s(0)
)))) ...
?
? Every wqo is a wfo but not the other way around
114Comparison WFOs and WQOs
- No ? descending chains s1 gt s2 gt s3 gt
- Ensure that sk1 lt sk
- More efficient(only previous element)
- Can be used statically
- (admissible sequences can be composed)
- Every ? sequencesi ? sj with iltj
- Ensure that not si ? sk1
- uncomparable elements
- More powerful (SAS98)
- ? more powerful than all monotonic and
simplification orderings
115Homeomorphic Embedding ?
- Diving s ? f(t1,,tn) if ?i s ? ti
- Coupling f(s1,,sn) ? f(t1,,tn) if ?i si ?
ti
n0
f(a)
g(f(f(a)),a)
g
f
a
f
a
f
a
116Admissible transitions
- rev(a,bT,,R)
- solve(rev(a,bT,,R),0)
- t(solve(rev(a,bT,,R),0),)
- path(a,b,)
- path(b,a,)
- t(solve(path(a,b,),0),)
- rev(aT,a,R)
- solve(rev(aT,a,R),s(0))
- t(solve(rev(aT,a,R),s(0)),rev))
- path(b,a,a)
- path(a,b,b)
- t(solve(path(b,a,a),s(0)),path))
117Higman-Kruskal Theorem (1952/60)
- ? is a WQO (over a finite alphabet)
- Infinite alphabets associative operators
f(s1,,sn) ? g(t1,,tm) if f ? g and ?i
si ? tji with 1?j1ltj2ltltjn?m and(q,p(b)) ?
and(r,q,p(f(b)),s) - Variables X ? Y more refined solutions
possible (DSSE-TR-98-11)
118An Example for ?
- rev(a,bX,)
- rev(bX,b)
- rev(X,b,a)
- rev(Y,H,b,a)
- eval(rev(a,bX,),0)
- eval(rev(bX,b),s(0))
- eval(rev(X,b,a),s(s(0)))
- eval(rev(Y,H,b,a),s(s(s(0))))
? Data consumption ? Termination ? Stops at the
right time ? Deals with encodings
119Summary of part 1
- Local vs Global Control
- Generic Algorithm
- Local control
- Determinacy
- lookahead
- code duplication
- Wfos
- Wqos
- -homeomorphic embedding
120Part 2 Controlling Partial DeductionGlobal
control and abstraction
1. Local Control Termination 2. Global
Control Abstraction
121Most Specific Generalisation (msg)
- A is more general than B iff ?? BA?
- for every B,C there exists a most specific
generalisation (msg) M - M more general than B and C
- if A more general then B and C then A is also
more general than M - there exists an algorithm for computing the msg
- (anti-unification, least general generalisation)
122MSG examples
- a a
- a b
- X Y
- p(a,b) p(a,c)
- p(a,a) p(b,b)
- q(X,a,a) q(a,a,X)
- a
- X
- X
- p(a,X)
- p(X,X)
- q(X,a,Y)
123First abstraction operator
- Benkerimi,Lloyd90 abstract two atoms in Ai1
by their msg if they have a common instance - will also ensureindependence(no renaming
required) - Termination ?
Input program P and goal G Output specialised
program P Initialise i0, A0 atoms in G
repeat Ai1 Ai for each a ? Ai do
Ai1 Ai1 ? leaves(unfold(a)) end for
Ai1 abstract(Ai1) until Ai1
Ai compute P via resultantsrenaming
124Rev-acc
rev(,L,L). rev(HX,A,R) - rev(X,HA,R).
?rev (L,,R)
unfold
unfold
?
rev(L,,R)
unfold
rev(L,H,R)
rev(L,H,H,R)
rev(L,H,H,H,R)
125First terminating abstraction
- If more than one atom with same predicate in
Ai1 replace them by their msg - rev (L,,R), rev (L,H,R) ? rev(L,A,R)
- Ensures termination
- strict instance-of relation wfo
- either fixpoint or strictly smaller atom
- But loss of precision !
- rev (a,bL,,R), rev (L,b,a,R) ?
rev(L,A,R) ? No specialisation !!
126Global trees
- Also use wfos MartensGallagher95 or wqos for
the global control SørensenGlück95,
LeuschelMartens96 - Arrange atoms in Ai1 as a tree
- register which atoms descend from which(more
precise spotting of non-termination) - Only if termination is endangered apply msg on
the offending atoms - use wfo or wqo on each branch
127Rev-acc
rev(,L,L). rev(HX,A,R) - rev(X,HA,R).
?rev (L,,R)
unfold
unfold
rev(L,A,R)
rev(L,,R)
DANGER
rev(L,H,R)
128More precise spotting
app(,L,L). app(HX,Y,HZ) -
app(X,Y,Z). t(X,L) - app(X,2,L). t(X,L) -
app(X,2,3,5,L).
Global Tree
Set
DANGER ABSTRACT
t(X,L)
t(X,L)
No Danger
app(X,2,L)
app(X,2,3,5,L)
129Examining Syntactic Structure
- p(a,B,C) ? p(A,a,C)
- abstract yes or no ???
130pappend
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
app_a(,a). app_a(HA,HC) -
app_a(A,C).
app_a(B,aB).
131pcompos
compos(HTX,HTY,H). compos(XTX,YTY,E)
- compos(TX,TY,E).
compos_a(a,a).
compos_a(a,a).
132Characteristic Trees
- Register structure of the SLD-trees
- position of selected literals
- clauses that have been resolved with
- GallagherBruynooghe91 Only abstract atoms if
same characteristic tree
133Characteristic Trees II
134Rev-acc revisited (Blackboard)
rev(,L,L). rev(HX,A,R) - rev(X,HA,R).
?rev (a,bC,,R)
135Path Example (Blackboard)
path(A,B,L) - arc(A,B). path(A,B,L)
- not(member(A,L)), arc(A,C), path(C,B,aL).
member(X,X_). member(X,_L) -
member(X,L). arc(a,a).
?path (a,b,)
?path (a,b,a)
136Characteristic Trees Problems
- Leuschel95
- characteristic trees not preserved upon
generalisation - Solution ecological PD or constrained PD
- LeuschelMartens96
- non-termination can occur for natural examples
- Solution apply homeomorphic embedding o n
characteristic trees - ? ECCE partial deduction system (Full DEMO)
137Summary of part 2
- Most Specific Generalisation
- Global trees
- reusing wfos, wqos at the global level
- Characteristic trees
- advantage over purely syntactic structure
138Summary of Lectures 2-3
- Foundations of partial deduction
- resultants, correctness criteria results
- Controlling partial deduction
- generic algorithm
- local control
- determinacy, wfos, wqos
- global control
- msg, global trees, characteristic trees
139Optimisation of Declarative ProgramsLecture 4
- Michael Leuschel
- Declarative Systems Software Engineering
- Dept. Electronics Computer Science
- University of Southampton
- http//
www.ecs.soton.ac.uk/mal
140Part 1A Limitation of Partial Deduction
141SLD-Trees
- ? knows_logic(Z)
- ? good_student(X) ? teacher(Y,X) ?
logician(Y) - ? teacher(Y,tom) ? logician(Y)
- ? logician(peter)
- ?
- ? teacher(Y,jane) ? logician(Y)
?
Prolog explores this tree Depth-First, always
selects leftmost literal
142Partial Deduction
- Basic Principle
- Instead of building one complete SLD-treeBuild
a finite number of finite SLD- trees ! - SLD-trees can be incomplete
- 4 types of derivations in SLD-trees
- Successful, failed, infinite
- Incomplete no literal selected
143Formalisation of Partial Deduction
- Given a set S A1,,An of atoms
- Build finite, incomplete SLD-trees for each ? Ai
- For every non-failing branch
- generate 1 specialised clause bycomputing the
resultants - Why only atoms ?
- Resultants Horn clauses
- Is this a limitation ??
144ControlLocal vs Global
A
A1 A2 A3 A4 ...
- Local Control
- Decide upon the individual SLD-trees
- Influences the code generated for each Ai
- ? unfolding rule
- given a program P and goal G returns finite,
possibly incomplete SLD-tree - Global control
- decide which atoms are in A
- ? abstraction operator
145Generic Algorithm
Input program P and goal G Output specialised
program P Initialise i0, A0 atoms in G
repeat Ai1 Ai for each a ? Ai do
Ai1 Ai1 ? leaves(unfold(a)) end for
Ai1 abstract(Ai1) until Ai1
Ai compute P via resultantsrenaming
146Limitation 1 Side-ways Information
- t(X) - p(X), q(X).
- p(a).
- q(b).
Original program !
147Limitation 1 Solution ?
- t(X) - p(X), q(X).
- p(a).
- q(b).
- Just unfold deeper
t(X) - fail.
? q(a)
fail
148Limitation 1 Solution ?
- t(X) - p(X), q(X).
- p(a).
- p(X) - p(X).
- q(b).
- We cannot fullyunfold !!
? q(a)
? p(Y),q(Y)
fail
149Limitation 1
- Side-ways information passing
- solve(Call,Res),manipulate(Res)
- Full unfolding not possible
- termination (e.g. ? of Res)
- code explosion
- efficiency (work duplication)
- Ramifications
- difficult to automatically specialise
interpreters(aggressive unfolding required) - no tupling, no deforestation
150Limitation 1 Solutions
- Pass least upper bound around
- For a query ?A,B
- Take msg of all (partial) solutions of A
- Apply msg to B
- Better
- Specialise conjunctions instead of atoms
- ? Conjunctive Partial Deduction
151Part 2Conjunctive Partial Deduction
152Conjunctive Partial Deduction
- Given a set S C1,,Cn of atoms
- Build finite, incomplete SLD-trees for each ? Ci
- For every non-failing branch
- generate 1 specialised formula Ci ?L
bycomputing the resultants - To get Horn clauses
- Rename conjunctions into atoms !
- ? Assign every Ci an atom with the same
variables and each with a different predicate name
153Renaming Function
p(Y),q(Y)
pq(Y)
- p(f(X)),q(b) ? p(X),q(X)
- p(f(X)),q(b) ? p(X),r(X),q(X)
pq(f(X),b) ? pq(X) pq(f(X),b) ? pq(X), r(X)
or pq(f(X),b) ? r(X), pq(X)
? Resolve ambiguities contiguous if no
reordering allowed
154Soundness Completeness
- P obtained by conjunctive partial deduction from
P - If non-triviality, closedness wrt renaming
function - P ? ?G has an SLD-refutation with c.a.s. ? iff
P ? ?Ghas - If in addition fairness (or weak fairness)
- P ? ?G has a finitely failed SLD-tree iffP ?
?Ghas - ? see Leuschel et al JICSLP96
155Maxlength Example (Blackboard)
max_length(X,M,L) - max(X,M),len(X,L). max(X,M)
- max(X,0,M). max(,M,M). max(HT,N,M) -
HltN,max(T,N,M). max(HT,N,M) -
HgtN,max(T,H,M). len(,0). len(HT,L) -
len(T,TL),L is TL1.
?maxlen (X,M,L)
?max(X,N,M),len(X,L)
ml(X,N,M,L)
156Double-Append Example (blackboard)
app(,L,L). app(HX,Y,HZ) - app(X,Y,Z).
? app (A,B,I), app(I,C,Res)
da(,B,B,C,Res) - app(B,C,Res). da(HA,B,HI
,C,HR) - da(A,B,I,C,R).
da(,B,C,Res) - app(B,C,Res). da(HA,B,C,HR
) - da(A,B,C,R).
157Controlling CPD
- Local Control
- becomes easier
- we have to be less aggressive
- we can concentrate on efficiency of the code
- Global Control
- gets more complicated
- handle conjunctions
- structure abstraction (msg) no longer sufficient !
158Global Control of CPD
- p(0,0).
- p(f(X,Y),R) - p(X,R),p(Y,R).
- Specialise p(Z,R)
- We need splitting
- How to detect infinite branches
- homeomorphic embedding on conjunctions
- How to split
- most specific embedded conjunction
159Homeomorphic Embedding on Conj.
- Define and/m ? and/n
- f(s1,,sn) ? g(t1,,tm) if f ? g
and ?i si ? tji with 1?j1ltj2ltltjn?m
and(q,p(b)) ? and(r,q,p(f(b)),s)
160Most Specific Embedded Subconj.
p(X) ? q(f(X))
t(Z) ? p(s(Z)) ? q(a) ? q(f(f(Z)))
t(Z), q(a), p(s(Z)) ?
q(f(f(Z)))
161Generic Algorithm
Input program P and goal G Output specialised
program P Initialise i0, A0 G repeat
Ai1 Ai for each c ? Ai do Ai1
Ai1 ? leaves(unfold(c)) end for Ai1
abstract(Ai1) until Ai1 Ai compute P
via resultantsrenaming
Changesover PD
162Ecce demo of conjunctive partial deduction
- Max-length
- Double append
- depth, det. Unf. with PD and CPD
- rotate-prune
163Comparing with FP
- Conjunctions
- can represent nested function calls
(supercompilation Turchin, deforestation
Wadler)g(X,RG),f(RG,Res) f(g(X)) - can represent tuples (tupling Chin)g(X,ResG),f(
X,ResF) ?f(X),g(X)? - We can do both deforestation and tupling !
- No such method in FP
- Semantics for infinite derivations (WFS) in LP
- safe removal of infinite loops, etc
- applications interpreters,model checking,...
164Comparing with FP II
- Functionality has to be inferred in LP
- fib(N,R1),fib(N,R2) vs fib(N)fib(N)
- Dataflow has to be analysed
- rot(Tree,IntTree),prune(IntTree,Result)
rot(L,Tl),rot(R,Tr),prune(Tl,Rl),prune(Tr,Rr)
vs - prune(rot(Tree)) tree( prune(rot(L)), I,
prune(rot(R)) ) - Failure possible
- reordering problematic ! fail,loop ? loop,fail
165Part 3Another Limitation of Partial Deduction
(and CPD)
166Limitation 2 Global Success Information
? p(Y)
- p(a).
- p(X) - p(X).
- We get sameprogram back
- The fact that in all successful derivations Xa
is not explicit !
?
? p(Y)
167Limitation 2 Failure example
- t(X) - p(X), q(X).
- p(a).
- p(X) - p(X).
- q(b).
- Specialised program
- t(X) - pq(X).
- pq(X) - pq(X).
- But even better
- t(X) -fail.
? q(a)
? p(Y),q(Y)
fail
168Append-last Example (blackboard)
app(,L,L). app(HX,Y,HZ) -
app(X,Y,Z). last(X,X). last(HT,X) -
last(T,X).
? app (A,a,I), last(I,X)
al(,a,a). al(HA,HI,X) - al(A,I,X).
al(,a,a). al(HA,HI,a) - al(A,I,a).
169Limitation 2 Ramifications
Fully known value
?
Known binding
Binding Lost !
Partially knownDatastructure
Environment for interpreter
170Part 4Combining Abstract Interpretation with
(Conjunctive) Partial Deduction
171More Specific Version (Msv)
- Marriot,Naish,Lassez88
- Abstract interpretation technique
- abstraction of TP
- sem(P) lfp(TP)
- abstract domain concrete domain
- ?(A) A? ? substitution
- same domain as PD, CPD !!
172TP operator
G
B
A
F
C
H
D
E
- Head - B1, B2, , Bn.
- Head ?1?n
S
TP(S)
Head?1?n
173TP operator
- Theorem VanEmden,Kowalski
- least Herbrand Model
- all ground logical consequences of P
- success set of SLD (ground queries)
- lfp(TP)
- TP??(?)
174TP operator Example
- q.
- r - q.
- p - p.
- nat(0).
- nat(s(X)) - nat(X).
- TP?1(?) q, nat(0)
- TP?2(?) q, r, nat(0), nat(s(0))
- TP??(?) q, r, nat(0), nat(s(0)),
175Abstraction of TP
- Compose TP with predicate-wise msg
- Ensures termination
- q.
- r - q.
- p - p.
- nat(0).
- nat(s(X)) - nat(X).
- TP?1(?) q, nat(0)
- TP?2(?) q, r, nat(0), nat(s(0))
- TP?2(?) TP??(?) q, r, nat(X)
Msg nat(X)
176More Specific Version Principles
- Compute S TP??(?)
- For every body atom of the program
- unify with an element of S
- if none exists clause can be removed !
- The resulting program is called a more specific
version of P - Correctness
- computed answers preserved
- finite failure preserved
- infinite failure can be replaced by finite failure
177MSV applied to append-last
al(,a,a). al(HA,HI,X) - al(A,I,X).
- TP?1(?) al(,a,a)
- TP?2(?) al(,a,a), al(H,H,a,a)
- TP?2(?) al(L,XY,a)
- TP (TP?2(?)) al(,a,a),
al(HL,H,XY,a) - TP?3(?) TP??(?) al(L,XY,a)
Note msv on original program gives no result !!
al(,a,a). al(HA,H,HI,a) -
al(A,HI,a).
178Naïve Combination
- repeat
- Conjunctive Partial Deduction
- More Specific Version
- until fixpoint (or we are satisfied)
- Can be done with ecce
- Power/Applications
- cf. Demo (later in the talk)
- interpreters for the ground representation
179Even-Odd example (blackboard)
- even(0).
- even(s(X)) - odd(X).
- odd(s(X)) - even(X).
- eo(X) - even(X),odd(X).
- Msv alone not sufficient !
- Specialise eo(X) msv
- eo(X) - fail.
180Refined Algorithm
- Leuschel,De Schreye Plilp96
- Specialisation continued before fixpoint of msv
is reached ! - Required when several predicates interact
- E.g. for proving functionality of multiplication
181ITP example (demo)
- plus(0,X,X).
- plus(s(X),Y,s(Z)) - plus(X,Y,Z).
- pzero(X,XPlusZero) - plus(X,0,XPlusZero).
- passoc(X,Y,Z,R1,R2) - plus(X,Y,XY),plus(XY,Z,R1),
- plus(Y,Z,YZ),plus(X,YZ,R2).
X !
R1 !
182Model Checking Example
sema
? state !
restart
exit_cs
enter_cs
? number of systems !
- trace(,State,State).
- trace(ActionAs,InState,OutState) -
- trans(Action,InState,S1), trace(As,S1,OutState).
- trans(enter_cs,s(X),s(Sema),CritSec,Y,C,
- X,Sema,s(CritSec),Y,C).
- trans(exit_cs, X,Sema,s(CritSec),Y,C,
- X,s(Sema),CritSec,s(Y),C).
- trans(restart, X,Sema,CritSec,s(Y),ResetCtr,
- s(X),Sema,CritSec,Y,s(ResetCtr)).
183Model Checking continued
- unsafe(X) -
- trace(Tr,X,s(0),0,0,0,_,_,s(s(_)),_,_).
- Specialise unsafe(s(0))
- unsafe(s(0)) - fail.
- Specialise unsafe(X)
- unsafe(X) - fail.
184Ecce Msv Demo
- Append last
- Even odd (itp)
- pzero (itp)
- passoc (itp)
- Infinite Model Checking (petri2)
185Full Reconicilation
- Why restrict to simple abstract domain
- (? all instances)
- Why not use
- groundness dependencies
- regular types
- type graphs
p(0)
Msg p(X)
?
p(s(0))
Better p(?) ? 0s(?)
186Full Reconicilation II
- Partial Deduction has to be extended
- handle any (downwards closed) domain
- Abstract Interpretation has to be extended
- generate totally correct code (not just safe
approximation) - Framework Leuschel JICSLP98
- no implementation yet !
187Summary of Lecture 4
- Conjunctive partial deduction
- better precision
- tupling
- deforestation
- More Specific Versions
- simple abstract interpretation technique
- Combination of both
- powerful specialiser/analyser
- infinite model checking