tags:

views:

873

answers:

5

What kind of problems is better solved in Prolog than in Haskell? What are the main differences between these two languages?

ADDED: Is there a Haskell library (kind of a logical solver) that can mimic Prolog functionality?

+11  A: 

Prolog is mainly a language targeted at logical problems, especially from the AI and linguistic fields. Haskell is more of a general-purpose language.

Prolog is declarative (logical) language, what makes it easier to state logical problems in it. Haskell is a functional language and hence much better suited to computational problems.

Wikipedia on declarative programming:

In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. It attempts to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing it. This is in contrast from imperative programming, which requires a detailed description of the algorithm to be run.

Declarative programming consider programs as theories of a formal logic, and computations as deductions in that logic space. Declarative programming has become of particular interest recently, as it may greatly simplify writing parallel programs.

Wikipedia on functional programming:

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as embellishments to the lambda calculus.

In short a declarative language declares a set of rules about what outputs should result from which inputs and uses those rules to deduce an output from an input, while a functional language declares a set of mathematical or logical functions which define how input is translated to output.

As for the ADDED question : none that I know of but you can either translate Prolog to Haskell, or implement Prolog in Haskell :)

Kornel Kisielewicz
Thank you for your valuable answers.
danatel
This answer is slightly misleading. Logic programming (Prolog) and functional programming (Haskell) are *both* declarative programming languages, but they represent different approaches to the declarative paradigm.
humble coffee
+3  A: 

Prolog is a logic programming language, whereas Haskell is a functional language. Functional languages are based on the concept of a function which takes a number of arguments and computes a value.

Prolog, on the other hand, does not have functions. Instead, predicates are used to prove a "theorem". Prolog predicates do not compute a value, they can answer "yes" or "no" and optionally bind input variables to values:

The usefulness of functional and logic programming often overlap. Functional programming has gained quite a bit of traction lately, while Prolog is still much a niche language, much due to the fact that it is much more different from the common concepts of functions and methods of mainstream OOP than functional programming is, and often considered (very) difficult to learn.

Certain problems become almost trivial to implement in Prolog, especially in combination with constraint solvers.

You can read more about logic programming on Wikipedia.

JesperE
Thank you for your valuable answer. It is a pity than only one best answer can be selected.
danatel
+12  A: 

Regarding the logic library question: If it doesn't exist, it should be possible to build one a variety of ways. The Reasoned Schemer builds logical reasoning capabilities into Scheme. Chapters 33-34 of PLAI discuss Prolog and implementing Prolog. These authors are building bridges between Scheme and Prolog. The creators of PLT Scheme have built as one of their languages a Lazy Scheme after the lazy evaluation feature of Haskell. Oleg Kiselyov's LogicT paper is brilliant as usual--he pushes the boundary for what is possible in many languages. There is also a logic programming example on the Haskell Wiki.

gknauth
Thank you for your valuable answer. It is a pity than only one best answer can be selected.
danatel
A: 

In reality there are only 2 languages:

  1. Machine language
  2. Human language.

All other languages in between are merely translators and nothing more. When we use the machine language we must think like the machine and when using human languages we think like humans.

The true job of a programmer is to think both ways. Some programming tools like the assembler force the programmer to spend a lot more time thinking like the machine. Other tools like Prolog allows us to spend more time thinking like a human.

There is a penalty to be paid at each extreme either in performance or in cost.

If the business logic of your application can be reduced to a set of rules and its output to a set of goals (for example writing a game of Chess) then Prolog is ideal. On the other hand if you need to take the input and tell the computer how to compute the output then a functional language would be more appropriate.

Square Rig Master
If you have used Prolog much, you know that you spend most of your time thinking like a backtracking constraint-resolution machine rather than a human. :) Humans don't think like Prolog at all, although perhaps more than like a Turing machine.
Nathan Sanders
Perhaps I should backtrack given the constraints of the thread :)You are absoloutely right about the Turing machine. I have used and continue to use Prolog. In fact I still have my copy of Turbo Prolog on 5.1/4 inch double sided double density floppy disks in pristine condition. Perhaps I should take it to the comic book guy.
Square Rig Master
+3  A: 

You might find the paper Escape from Zurg: An Exercise in Logic Programming an interesting read. It shows a side-by-side comparison of the implementation of a simple search problem in Prolog and Haskell, along with a little typeclass framework for representing search problems more generally. The conclusion that the authors come to is that expressing at least some of these types of problems in Haskell is easier than in Prolog, primarily because the Haskell type system makes it easier to come up with nice representations of search states and moves from state to state.

Ian Ross