Declarative programming in java using jsetl
Download
1 / 27

Declarative Programming in Java using JSetL - PowerPoint PPT Presentation


  • 81 Views
  • Uploaded on

Convegno Italiano di Logica Computazionale. Declarative Programming in Java using JSetL. E. Panegai G. Rossi Dipartimento di Matematica Università di Parma. Roma, 21-22 Giugno 2005. Outline of the talk.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Declarative Programming in Java using JSetL' - coyne


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Declarative programming in java using jsetl

Convegno Italiano di Logica Computazionale

Declarative Programming in Java using JSetL

E. Panegai G. Rossi

Dipartimento di Matematica

Università di Parma

Roma, 21-22 Giugno 2005


Outline of the talk
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


Introduction
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


Introduction1
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


Introduction2
Introduction

  • The aim is to develop DP features. Two approaches:

    • Developing new (OO?) languages;

    • Extending existing languages.

CILC 05


Introduction extending java jsetl
IntroductionExtending 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


Logical variables
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


Logical variables1
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


Logical variables2
Logical Variables

  • weekTranslate() defines a binary relation.

  • With:

    Lvar lunedì = new Lvar(“”, “lunedì” );

    Lvar monday = new Lvar(“”, “Monday”);

    there arethree legal invocations of the method:

    • weekTranslate(lunedì, answer);

    • weekTranslate(answer, monday);

    • weekTranslate(lunedì, monday);

  • No assumption is made about which are input and which are output parameters.

  • CILC 05


    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


    Data structures and partially specified values
    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


    Data structures and partially specified values1
    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


    Data structures and partially specified values2
    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


    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


    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


    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


    Nondeterministic Operations

    • Traveling salesman problem: Showing the use of nondeterminism in connection with sets.

    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;

    }

    CILC 05


    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


    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


    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


    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


    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


    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


    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


    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


    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


    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: http://www.math.unipr.it/~gianfr/JSetL/

    CILC 05


    ad