Вопрос

I have a question regarding declarative programming, to see how well I understand the concept. I have an assignment stating I should make a sudoku puzzle solver in Java in a "declarative way". Looking up what that means I found a lot of ambiguous definitions stating the program should be written in such a fashion that is describes what has to be done instead of specifying how. Looking around, I found an interesting example of someone explaining it with a list, specifically filtering the odd numbers of a list.

Imperative style (called function):

List resultList = new List()
foreach element in startlist
    do if element % 2 == 1
        do add element to resultList
        od
    od
return returnList

Declarative style (calling context):

newList = startList.filter(num -> num%2 ==1)

So in the declarative style, it is specified what elements we want to keep, but the way the list is made is not specified. This is abstracted into the API of the list.

My simple question then is: doesn't this approach mean the library of the lists is NOT declarative, because the algorithms are moved there? In that case, the term "declarative" applies to the part of the program making calls to this library. Hence, isn't it impossible for a program to be 100% declarative, as (of course) somewhere will need to be an algorithm specifying how things are done? Please correct me if I'm wrong here because this would be at the heart of my understanding of the declarative concept.

In any case, I was thinking of writing the sudoku solver as a combination of my own libraries (containing all the "imperative" logic, wrapped in "declarative" function programming style) and a specification of the steps that should be taken to solve the sudoku, as calls to these libraries. I was also thinking I could use the Command pattern as a wrapper to a way to pass functions as arguments in Java, as to further make the solver classes more "declarative". I've already experimented with this and tried something along the lines of newList = startList.filter(new OddNumberCommand()), with OddNumberCommand a command that has the execute function, taking an element from the list as argument and returning true or false if the element is to be kept or discarded from the result list.

Anyway, this is all just the way my mind spins and I wanted to turn to the help of others that understand this concept better in order to make sure if I'm heading down the right or wrong path. Please let me know where I'm wrong and what's right in my reasoning so I can learn to use declarative programming properly.

Thanks

Это было полезно?

Решение

As a commenter said, your example is more functional programming than declarative programming, although the two are similar in some respects. In real declarative programming, you'd be more results oriented -- you'd be specifying what to do with the interesting elements in the list, rather than transforming the list for its own sake. A rule engine like Jess is an example of a declarative programming system (disclaimer, I'm the author of Jess.)

To answer your one simple question: yes. All functional or declarative programming systems involve a runtime system that has to be implemented on the real computer the program is running on. Since the real computer is invariably an imperative von Neumann machine, that runtime system has to be written imperatively. It is therefore an additional layer of abstraction between your program and the hardware.

Другие советы

From the definition of the Declarative Programming:

Characteristics of declarative languages:

  1. Model of computation based on a system where relationships are specified directly in terms of the constituents of the input data.
  2. Made up of sets of definitions or equations describing relations which specify what is to be computed, not how it is to be computed.
  3. Non-destructive assignment of variables.
  4. Explicit representations for data structures used.
  5. Order of execution does not matter (no side effects).
  6. Expressions/definitions can be used as values.
  7. Programmer no longer responsible for control.

Declarative programs define the logic (the desired goal or result) and not the control (how we achieve the desired goal). We say that declarative languages are goal directed or goal driven.

I think that in Sudoku case the point is to use the interfaces as the contracts among the classes. This way you can change the implementations without breaking the model.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top