190 likes | 205 Views
Program Optimizations in Specware/Designware. Douglas R. Smith. Kestrel Institute www.kestrel.edu. WG2.1 Meeting, 14-19 June 99, Potsdam, Germany. Software Development by Refinement. CAT. semantics : SPEC op. category of models for Spec 0. denotes. Spec 0. reduct functor. Spec 1.
E N D
Program Optimizations in Specware/Designware Douglas R. Smith Kestrel Institute www.kestrel.edu WG2.1 Meeting, 14-19 June 99, Potsdam, Germany
Software Development by Refinement CAT semantics: SPECop category of models for Spec0 denotes Spec0 reduct functor Spec1 models for Spec1 refinement in SPEC models for Spec2 Spec2 … … Specn models for Specn code generation a model (for Specn) Code Code generation via a logic morphism from SPEC to the logic of a programming language
Morphisms Partial-OrderInteger E integer le axioms theorems Interpretation ( Between Theories) Partial-OrderInteger E integer le (x, y) x mod y = 0 axioms theorems Specifications and Morphisms Spec Partial-Order sort E op _le_: E, EBoolean axiom reflexive x le x axiom transitive x le y y le z x le axiom antisymmetric x le y y le x x = y Specifications Represent Domain models Transportation, Resource, Task Software Requirements Crew Scheduling Algorithm Theories Global-Search Abstract Data Types Set(Time) Software Architecture Scheduling-System Code Modules Network-Flow Morphisms Represent Spec Structuring TaskSchedulingResource Parameter Binding Time Integer Spec Refinement Partial-OrderInteger Algorithm Design Global-SearchScheduling Knowledge Refinement Constraint SatisfactionInteger Programming Datatype Refinement Set(Integer)Bit Vector
spec REFLEXIVE-RELATION is sort E op _rr_ : E, E Boolean axiom reflexivity is a rr a end-spec spec BINARY-RELATION is sort E op _br_ : E, E Boolean end-spec spec TRANSITIVE -RELATION is sort E op _tr_ : E, E Boolean axiom transitivity is a trb btrc atrc end-spec spec PREORDER-RELATION is sort E op : E, E Boolean axiom reflexivity is a a axiom transitivity is a b b c a c end-spec Calculating a Colimit in SPEC
REFLEXIVE-RELATION BINARY-RELATION E br E rr x rr x po {E,E,E}rename E {br, rr, tr}rename axiom x x axiom x y y z x z E tr axiom x tr y y tr z x tr z TRANSITIVE-RELATION PREORDER-RELATION Calculating a Colimit in SPEC Collect equivalence classes of sorts and ops from all specs in the diagram.
Instantiating a Parameter Spec INTEGER sort Integer … integer structure and axioms spec 1-SORT is sort E end-spec po spec SET-OF-INTEGER is sorts Integer, Set-of-Integer const empty-set : Set-of-Integer op insert: Integer, Set-of-Integer Set-of-Integer axiom condensation is insert(e, insert (e, S)) = insert(e, S) … set structure and axioms … integer structure and axioms end-spec spec SET is sorts E, Set const empty-set : Set op insert: E, SetSet axiom idempotence is insert(e, insert (e, S)) = insert(e, S) … set structure and axioms end-spec
Library of Refinements A E A C A po B Spec1 C F B D H po D E Spec2 E H B J po A F Spec3 G I D K po DP theory Expression + Context Set H Spec4 Dynamic Programming Algorithm Bit Vector Context-dependent Simplification Applying Abstract Refinements Spec0
Time, Quantity Resource Task A, B, C Reservation = ResourceTaskTime Set(ABC) Schedule = set(Reservation) Time, Quantity Resource A, B, C Task Reservation = ResourceTaskTime Map(A, Set(ABC)) Map Schedule = Map(Resource, Set(Reservation)) Map Refining the Schedule datatype
Refinements (green arrows) are organized into a taxonomy A B S0+ B classifying the structure of S0 C S0++ C E D S0+++ D applying the refinement D E po S1 E Design by Classification Refinements are accessed and applied incrementally via a ladder construction A S0
Taxonomy of Algorithm Theories Problem Theory (D|I R|O) generate-and-test Constraint Satisfaction (R = set of maps) Global Structure (R = set + recursive partition) global search binary Search backtrack branch-and-bound Local Structure (R = set + relation) genetic algorithms Problem Reduction Structure Linear Programming simplex method interior point primal dual Local Structure (R = set + relation) local search hill climbing simulated annealing tabu search Divide-and-Conquer divide-and-conquer Complement Reduction sieves Integer Linear Programming 0-1 methods GS-CSP (R = recursively partitioned set of maps) Problem Reduction Generators dynamic programming branch-and-bound game tree search Network Flow specialized simplex Ford-Fulkerson Local Poset Structure (R = set + partial order) GS-Horn-CSP (Horn-like Constraints) constraint propagation Monotone Deflationary Function fixed point iteration Local Semilattice Structure (R = semilattice) Transportation NW algorithm Assignment Problem Hungarian method
context analysis determines that a=0 so we can safely replace a*e by 0 Program Optimization Techniques Context-Dependent Simplification Idea: simplify an expression with respect to its context while a0 do ….. if a>0 then ….. else F( a*e, …..) ….. end • Equational Rewriting • Context-Dependent Simplification • Finite Differencing • Partial Evaluation • Common Subexpression Elimination • Dead Code Removal • Loop Fusion • Deforestation • Tabulation • Tupling
How to optimize in Designware? Goal: express optimization rules in terms of specs and morphisms; find basic mechanisms for applying them. Fact: Cannot replace one expression by another using colimit of spec morphisms; we need a morphism-constructing substitution operation: metatheorem LEIBNITZ if e1 expressions(S) & C = context(e1, S) & |-S C e1=e2 & m = make-morphism-by-subst(e1, e2, S) then isomorphism?(m) make-morphism-by-subst spec S’ is . . . [e2] thm C e1=e2 end-spec spec S is . . . [e1] thm C e1=e2 end-spec
How to optimize in Designware? metatheorem LEIBNITZ if e1 expressions(S) & C = context(e1, S) & |-S C e1=e2 & m = make-morphism-by-subst(e1, e2, S) then isomorphism?(m) How to use Leibnitz: 1. Apply a (meta)theorem to create an equational theorem in spec S 2. Apply make-morphism-by-subst to create an isomorphism according to Leibnitz or 1. Use pushout on an optimization refinement to create a theorem in spec S 2. Apply make-morphism-by-subst to create an isomorphism according to Leibnitz
Metatheorems to use with Leibnitz To apply a given equational law: metatheorem APPLY-LAW if |-S d1=d2 & e1 expressions(S) & = match(d1, e1) & C = context(e1, S) then |-S d1 =d2 Context-Dependent Simplification metatheorem CD-SIMPLIFY if e1 expressions(S) & C = context(e1, S) & X = vars(e1) & Z = vars(C) & e2= find-witness(`X y Z/X C e1= y’, S) then |-S C e1= e2
Transformation Rules in Designware source pattern target pattern + conditions what is the source expression? expression theory requirement spec S0 match the source pattern and check conditions source theory S1 po target theory instantiate the target pattern S2 S2 is an extension of S0 with new function definitions and theorems (to be used by the simplifier)
A Fusion Law if f(x y) = x f(y) and and are associative then f(foldr(, xs, unit)) = foldr(, xs, f(unit)) spec FOLDR-FUSION is import Seq-of-A sort E op f: A E op : A A A axiom associative?( ) op unit: A op foldr : (A A A) Seq-of-A A A def foldr(g,as,u) = ... op : A E E axiom associative?( ) op foldr : (A E E) Seq-of-A A E def foldr(g,as,u) = ... theorem foldr-fusion-law is xf(y) = f (x y) f(foldr(, xs, unit)) = foldr( , xs, f(unit)) end-spec
A Fusion Law if f(x y) = x f(y) and and are associative then f(foldr(, xs, unit)) = foldr(, xs, f(unit)) Spec EXPR is sort E op expr : E spec foldr-fusion is import EXPR, Seq-of-A op f: A E op : A A A op foldr : (A A A) Seq-of-A A A op : A E E axiom associativity of , axiom expr = f(foldr(, xs, unit)) axiom f (x y) = xf(y) end-spec spec fold-fusion-law is import fold-fusion op foldr : (A E E) Seq-of-A A E theoremexpr = foldr( , xs, f(unit)) end-spec
| | | | | | | | | | Applying the Fusion Law size(queens(k)) = … size(reduce(, SS, {})) Nat size(reduce(, SS, {})) E expr E expr f foldr unit Nat size(reduce(, SS, {})) size reduce (disjoint union) {} (x,sz) size(x) + sz f(foldr(, xs, unit)) = foldr( , xs, f(unit)) size(reduce(, SS, e)) = foldr((x,sz) size(x) + sz , SS, size({}))
Observations on generating an equational theoremvia pushout with library refinement • optimization refinements expressed in the same format • as algorithm theory and datatype refinements • morphism construction generalizes matching • can’t designate a particular expression in an object-level spec • can’t exploit local context • cumbersome mechanism