Declarative Programming in Java using JSetL E. PanegaiG. Rossi Dipartimento di Matematica Università di Parma Roma, 21-22 Giugno 2005 Convegno Italiano.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Problems and Their Classes
Introduction to Programming in C++ John Galletly.
Introduction. IC-Parc2 ECLiPSe Components Constraint Logic Programming system, consisting of  A runtime core Data-driven computation, backtracking, garbage.
Giving a formal meaning to “Specialization” In these note we try to give a formal meaning to specifications, implementations, their comparisons. We define.
1 Symbolic Execution for Model Checking and Testing Corina Păsăreanu (Kestrel) Joint work with Sarfraz Khurshid (MIT) and Willem Visser (RIACS)
1/20 Generalized Symbolic Execution for Model Checking and Testing Charngki PSWLAB Generalized Symbolic Execution for Model Checking and Testing.
The Assembly Language Level
A Master-Slave Architecture to Integrate Sets and Finite Domains in Java F. Bergenti E. Panegai G. Rossi Università degli Studi di Parma Bari, Giugno.
Multi-agent Planning Amin Atrash. Papers Dynamic Planning for Multiple Mobile Robots –Barry L. Brummit, Anthony Stentz OBDD-based Universal Planning:
Complexity 15-1 Complexity Andrei Bulatov Hierarchy Theorem.
Introduction To System Analysis and Design
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Fall 2007CS 225 Introduction to Software Design Chapter 1.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Chapter 2: Algorithm Discovery and Design
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
The Fundamentals: Algorithms, the Integers & Matrices.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1.1 Chapter 1: Introduction What is the course all about? Problems, instances and algorithms Running time v.s. computational complexity General description.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Topic 3 The Stack ADT.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Invitation to Computer Science, Java Version, Second Edition.
Feasibility Study.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
Introduction to Software Design Chapter 1. Chapter Objectives  To become familiar with the software challenge and the software life cycle  To understand.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Week 10Complexity of Algorithms1 Hard Computational Problems Some computational problems are hard Despite a numerous attempts we do not know any efficient.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Roles and Coordination in powerJava M. Baldoni, G. Boella Dipartimento di Informatica, Università degli Studi di Torino (Italy) and L. van der Torre SEN3.
Efficient Synthesis of Feature Models Article Review By: Sigal Berkovitz & Yohai Vidergor.
Types in programming languages1 What are types, and why do we need them?
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Software Engineering Research Group, Graduate School of Engineering Science, Osaka University A Slicing Method for Object-Oriented Programs Using Lightweight.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Software Engineering1  Verification: The software should conform to its specification  Validation: The software should do what the user really requires.
Complexity & Computability. Limitations of computer science  Major reasons useful calculations cannot be done:  execution time of program is too long.
Recognising Languages We will tackle the problem of defining languages by considering how we could recognise them. Problem: Is there a method of recognising.
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
CSCI 2670 Introduction to Theory of Computing November 17, 2005.
LIMITATIONS OF ALGORITHM POWER
Concepts and Realization of a Diagram Editor Generator Based on Hypergraph Transformation Author: Mark Minas Presenter: Song Gu.
Chapter 11 Introduction to Computational Complexity Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Finding bugs with a constraint solver daniel jackson. mandana vaziri mit laboratory for computer science issta 2000.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
Testing It is much better to have a plan when testing your programs than it is to just randomly try values in a haphazard fashion. Testing Strategies:
Topic 4: Distributed Objects Dr. Ayman Srour Faculty of Applied Engineering and Urban Planning University of Palestine.
Adding Concurrency to a Programming Language Peter A. Buhr and Glen Ditchfield USENIX C++ Technical Conference, Portland, Oregon, U. S. A., August 1992.
Automatic Test Generation
The NP class. NP-completeness
Paul Ammann & Jeff Offutt
MSc/ICY Software Workshop , Semester 2
Control Flow Testing Handouts
Handouts Software Testing and Quality Assurance Theory and Practice Chapter 4 Control Flow Testing
Outline of the Chapter Basic Idea Outline of Control Flow Testing
Nondeterministic Evaluation
Microsoft Visual Basic 2005 BASICS
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Presentation transcript:

Declarative Programming in Java using JSetL E. PanegaiG. Rossi Dipartimento di Matematica Università di Parma Roma, Giugno 2005 Convegno Italiano di Logica Computazionale

2 Outline of the talk Aim: Supporting Declarative Programming (DP) in the context of conventional programming languages; Extending Java maintaining its features; A number of examples in DP style; Conclusions and future work. CILC 05

3 Introduction Real-world software development is still done using object-oriented programming languages, such as C++ and Java. Complex problems have always complex solutions? Can we declare a solution? CILC 05

4 Introduction Main features of DP: –Relations: defining what, and not how, should be computed. –Assignment: not destructive, single value assignment. –Data Structures: explict representation of data structures, such as lists and sets. –Execution Order and Control: do not matter! It’s part of the language. CILC 05

5 Introduction The aim is to develop DP features. Two approaches: –Developing new (OO?) languages; –Extending existing languages. CILC 05

6 Introduction Extending Java: JSetL Collection of facilities to support DP that allow the language to be used as a highly declarative modelling tool: –Logical variables; –Unification; –Constraint solving; –Partially specified data structures; –Non determinism. CILC 05

7 Logical Variables Heart of declarative computational model: –Single assignment; –Values can be of any type; –In JSetL logical variables are Lvar Lvar x = new Lvar(); –Defining and handling dynamic data structures. CILC 05

8 Logical Variables Example: Translating the name of the days of the week from Italian to English and vice- versa. public static void weekTranslate (Lvar g, Lvar d) { if (Solver.boolSolve(g.eq("lunedì").and(d.eq("monday")))) return; if (Solver.boolSolve(g.eq("martedì").and(d.eq("tuesday")))) return; … } The use of logical variables and unification allows the same methods to be used for different purposes. CILC 05

9 Logical Variables weekTranslate() defines a binary relation. With : Lvar lunedì = new Lvar(“”, “lunedì” ); Lvar monday = new Lvar(“”, “Monday”); there are three legal invocations of the method: 1.weekTranslate(lunedì, answer); 2.weekTranslate(answer, monday); 3.weekTranslate(lunedì, monday); No assumption is made about which are input and which are output parameters. CILC 05

10 Data Structures and Partially Specified Values Two distinct kinds of data structures: lists and sets (collections without order and repetitions). Data structures that contain uninitialized logical variables in place of single elements or as a part of the data structure itself represent partially specified values. CILC 05

11 Data Structures and Partially Specified Values Check whether a list L contains at least two elements. public static void at_least_two(Lst L) throws Failure { Lvar x = new Lvar(); Lvar y = new Lvar(); Lst R = new Lst(); Solver.solve(L.eq(R.ins1(y).ins1(x))); // L = [x, y | R] return; } CILC 05

12 Data Structures and Partially Specified Values Deterministic concatenation: Given two lists L1 and L2 computes the new list L3 as the concatenation of L1 and L2. public static void concat(Lst L1, Lst L2, Lst L3) throws Failure { if (Solver.boolSolve(L1.eq(Lst.empty))) { Solver.solve(L2.eq(L3)); } else { Lvar x = new Lvar(); Lst R = new Lst(); Lst L3new = new Lst(); Solver.solve(L1.eq(R.ins1(x)).and(L3.eq(L3new.ins1(x)))); // L1 = [x | R]  L3 = [x | L3new] concat(R, L2, L3new)); } return; } CILC 05

13 Data Structures and Partially Specified Values Check whether all elements of a set S are pairs, i.e., they have the form [x1, x2], for any x1 and x2. public static void all_pairs(Set s) throws Failure { Lvar x1 = new Lvar(); Lvar x2 = new Lvar(); Lvar[] Y = {x1, x2}; Lvar x = new Lvar(); Solver.solve(forall(x, s, Y, x.eq(Lst.empty.ins1(x1).insn(x2)))); return; } CILC 05

14 Nondeterministic Operations The ability to state solutions in a nondeterministic way provides a very high- level control abstraction. Set unification and many other set operations are inherently and naturally nondeterministic. CILC 05

15 Nondeterministic Operations Compute the maximum of a set of integers s. public static Lvar max(Set s) throws Failure { Lvar x = new Lvar(); Lvar y = new Lvar(); Solver.solve(x.in(s)); Solver.solve(forall(y, s, y.leq(x))); return x; } Declarative reading: an element x of s is the maximum of s, if for each element y of s it holds that y ≤ x. CILC 05

16 Nondeterministic Operations Compute all permutations (one at a time) of the set {1, 2, 3, 4}. Set I = new Set("I", I_elems); // I = {1, 2, 3, 4} Set S = Set.mkset(4); // S = {Lvar_1, Lvar_2, Lvar_3, Lvar_4} Solver.solve(S.eq(I)); // {Lvar_1, Lvar_2, Lvar_3, Lvar_4}.eq({1,2,3,4}) System.out.print("Do you want another solution(y/n)?"); … while( input.readLine().equals("y") ) { if( Solver.nextSolution() ){ S.output(); System.out.print("Do you want another solution(y/n)?"); } else { … } } CILC 05

17 Nondeterministic Operations public static void tsp(Set edges, Lvar source, Lvar startNode) throws Failure { Lvar nextNode = new Lvar(); Lst newArc = new Lst(Lst.empty.ins1(startNode).insn(nextNode)); Solver.solve(newArc.in(edges).and(nextNode.nin(visited))); visited = visited.ins(nextNode); path = path.insn(nextNode); if (path.size() < n) tsp(edges, source, nextNode); else tsplast(edges, source, nextNode); return; } Traveling salesman problem: Showing the use of nondeterminism in connection with sets. CILC 05

18 Constraints DP is often associated with constraint programming. The ability to deal with constraints allows to compute with partially specified values. In JSetL basic set-theoretical operations, as well as equalities, inequalities and integer comparison expressions, are dealt with as constraints. A solver nondeterministically searches for a solution that satisfies all constraints introduced. Constraint solving in JSetL is basically the same developed for CLP( SET ). CILC 05

19 Constraints The ability to deal with constraints allows the programmer to compute with partially specified values and ignore the order of execution of statements. x.read(); Solver.add(x.neq(0)); Solver.solve(); is the same as executing: Solver.add(x.neq(0)); x.read(); Solver.solve(); CILC 05

20 Constraints Backtracking is confined to constraint solving: Solver.solve(x.in(s)); // x uninitialized Lvar, s = {0,1} x.output(); Solver.solve(x.neq(0)); It’s possible to write parts of a program to be sensible to backtracking defining them as new constraints: public static void printVar(Lvar x) { x.output(); return; } Solver.solve(NewConstraints.printVar(x)); CILC 05

21 Intensional Sets With a DP style is possible to represent a set by its intensional definition. Compute the set of all pairs [x,y] such that x and y belong to a given set s and x  y. public static void allPairs(Set S) throws Failure { Lvar X = new Lvar(); Lvar y = new Lvar(); Lst pair = new Lst(Lst.empty.ins1(x).insn(y)); Set Pairs = Solver.setof(pair, x.in(s).and(y.in(s)).and(x.neq(y))); // collect all [x,y] satisfying x  s  y  s  x  y Pairs.output(); return; } CILC 05

22 Intensional Sets Compute the set of all prime numbers smaller or equal to a given integer n. public static Set primes(Lvar n) throws Failure { Lvar x = new Lvar(); Solver.add(x.in(Set.interv(2,n))); // x in 2..n // check whether x is prime Lvar y = new Lvar(); Lvar m = new Lvar(); Solver.add(m.eq(x.sub(1))); // m = x - 1 Solver.forall(y, Set.interv(2,m), (x.mod(y)).neq(0)); // compute the set of all prime numbers in 2..n return Solver.setof(x); } CILC 05

23 Nondeterminism Between Statements JSetL supplies facilities to build nondeterministic procedures (using backtracking) as new constraints. This allows the user to define completely “invertible” methods (no distinction between input and output parameters). CILC 05

24 Nondeterminism Between Statements A fully nondeterministic version of the list concatenation, that can be used also to compute all possible lists l1 and l2 from l3. public static void concat(Lst l1, Lst l2, Lst l3) { eihter { Solver.add(l1.eq(Lst.empty)); Solver.add(l2.eq(l3)); } orelse { Lvar x = new Lvar(); Lst l1new = new Lst(); Lst l3new = new Lst(); Solver.add(l1.eq(l1new.ins(x))); // l1 = [x | l1new] Solver.add(l3.eq(l3new.ins(x))); Solver.add(concat(l1new, l2, l3new)); } return; } CILC 05

25 Nondeterminism Between Statements Nondeterministic method to find a path between two nodes in a directed labeled graph G. public static void path(Set G, Lvar source, Lvar dest) throws Failure { either { Lst finalEdge = Lst.empty.ins1(source).insn(dest)); Solver.add(finalEdge.in(G)); } orelse { Lvar x = new Lvar(); Lst intermediateEdge = Lst.empty.ins1(source).insn(x)); Solver.add(intermediateEdge.in(G)); Solver.add(path(G, x, dest); } return; } CILC 05

26 Conclusions and Future Work We have shown how facilities provided by the JSetL library can be used to write programs that exhibit a quite good declarative reading. Computation efficiency has not been a primary requirement in the development of JSetL, however: –various improvements to the current implementation are feasible. –Possible to integrate an efficient FD constraint solver without changing the library interface. CILC 05

27 Conclusions and Future Work Interesting new possibilities: –more solvers can coexist and cooperate; –solvers can be different threads; –constructors of the solver class can be parametric w.r.t. constraint solving policies. Download at: CILC 05