Program Synthesis in Software: Understanding Code Generation Techniques
200 likes | 285 Vues
Learn about program synthesis, sorting algorithms, linear arithmetic, sets reasoning, and code generation in software development. Explore practical exercises and automated reasoning tools for efficient code creation.
Program Synthesis in Software: Understanding Code Generation Techniques
E N D
Presentation Transcript
Software Synthesis RuzicaPiskac Max Planck Institute for Software Systems, Germany
What is a Program? • Program = a sequence of commands describing what needs to be done valbigSet = .... val (setA, setB) = choose((a: Set, b: Set) ) => ( a.size == b.size && a union b == bigSet && a intersect b == empty)) Code val n = bigSet.size/2 valsetA = take(n, bigSet) valsetB = bigSet −− setA
What is a Program? • Program = a sequence of commands describing what needs to be done val bigSet = .... val (setA, setB) = choose((a: Set, b: Set) ) => ( a.size == b.size && a union b == bigSet && a intersect b == empty)) Code assert (bigSet.sizeis an even number) val n = bigSet.size/2 valsetA = take(n, bigSet) valsetB = bigSet −− setA
Sorting algorithm • Why should we sort data? • Practical exercise: • Form a line, where you are sorted by your first name, from Aaron to Zoe
Arrays • Data structure: • A[0] = 23, A[1]=4, A[2]=6, A[3]=15, A[4]=5, A[5]=7
Specification • Array a2 is a sorted version of array a1 • Define the counting function and
Automated Reasoning • decision procedure answers whether the input formula is satisfiable or not • formula is satisfiable for x=0, y=1 • formula is unsatisfiable satisfiable(model) theorem prover based on DECISION PROCEDURES formula in some logic unsatisfiable (proof)
Synthesis for Linear Integer Arithmetic choose((h: Int, m: Int, s: Int) ⇒ ( h * 3600 + m * 60 + s == totalSeconds && h ≥ 0 && m ≥ 0 && m < 60 && s ≥ 0 && s < 60 )) Returned code: assert (totalSeconds ≥ 0) val h = totalSecondsdiv 3600 valtemp = totalSeconds + (-3600) * h valm = min(temp div60, 59) vals = totalSeconds + (-3600) * h + (-60) * m
Linear Integer Arithmetic - Equalities h * 3600 + m * 60 + s == totalSeconds Code: <further code will come here> valh = lambda valm = mu valvals = totalSeconds + (-3600) * lambda + (-60) * mu
Linear Integer Arithmetic - Equalities h * 3600 + m * 60 + s == totalSeconds Resulting formula (new specifications): 0 ≤ λ, 0 ≤ μ, μ ≤ 59, 0 ≤ totalSeconds – 3600λ - 60μ, totalSeconds – 3600λ - 60μ ≤ 59 10
Linear Integer Arithmetic - Inequalities 0 ≤ λ, 0 ≤ μ, μ ≤ 59, 0 ≤ totalSeconds – 3600λ - 60μ, totalSeconds – 3600λ - 60μ ≤ 59 expressing constraints as bounds on μ 0 ≤ λ, 0 ≤ μ, μ ≤ 59, μ ≤ ⌊(totalSeconds – 3600λ)/60⌋ , ⌈(totalSeconds – 3600λ – 59)/60⌉ ≤ μ Code: valmu = min(59, (totalSeconds -3600* lambda) div 60)
Linear Integer Arithmetic - Inequalities 0 ≤ λ, 0 ≤ μ, μ ≤ 59, μ ≤ ⌊(totalSeconds – 3600λ)/60⌋ , ⌈(totalSeconds – 3600λ – 59)/60⌉ ≤ μ combine each lower and upper bound 0 ≤ λ, 0 ≤ 59, 0 ≤ ⌊(totalSeconds – 3600λ)/60⌋ , ⌈(totalSeconds – 3600λ – 59)/60⌉ ≤ ⌊(totalSeconds – 3600λ)/60⌋ , ⌈(totalSeconds – 3600λ – 59)/60⌉ ≤ 59 basic simplifications Code: vallambda = totalSecondsdiv 3600 Preconditions: 0 ≤ totalSeconds 0 ≤ λ, 60λ ≤ ⌊totalSeconds/60⌋, ⌈(totalSeconds –59)/60⌉ – 59 ≤ 60λ
Synthesis for Sets • Observation: • Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigSet && a intersect b == empty a b bigSet
Synthesis for Sets • Observation: • Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigSet && a intersect b == empty a b bigSet
Synthesis for Sets • Observation: • Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigSet && a intersect b == empty a b bigSet
Synthesis for Sets • Observation: • Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size&& a union b == bigSet && a intersect b == empty New specification: kA = kB a b bigSet
Synthesis for Sets • Observation: • Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigSet && a intersect b == empty New specification: kA = kB && kA +kB = |bigSet| a b bigSet
Synthesis: Applications • Automatic code completion • Flash Fill feature in the new Microsoft Excell (video!) • Available at: http://research.microsoft.com/en-us/um/people/sumitg/flashfillextensions.wmv
InSynth • InSynth – a tool for synthesis of code fragments (snippets) • interactive • getting results in a short amount of time • multiple solutions – a user needs to select • component based • assemble program from given components (local values, API) • partial specification • hard constraints – type constraints • soft constraints - use of components “most likely” to be useful