4 ms·
Indeed, that's the definition I am familiar with. To give an example, consider Prolog: parent(john,mark). parent(steve,john). ancestor(A,D) :- paren
by danieldk 12y ago
Indeed, that's the definition I am familiar with. To give an example, consider Prolog:
parent(john,mark).
parent(steve,john).
ancestor(A,D) :-
parent(A,D).
ancestor(A,D) :-
parent(P,D),
ancestor(A,P).
This is a declarative program - it describes being an ancestor means (the parent of a person or an ancestor of the parent of a person). The program does not specify the steps to find a ancestor given a descendant or a descendant given an ancestor. Yet we can do such queries thanks to Prolog's resolution procedure:
?- ancestor(steve,mark).
true
findall(A,ancestor(A,mark),L).
L = [john, steve].
?- findall(D,ancestor(steve,D),L).
L = [john, mark].
Of course, as the sibling poster points out, it's a spectrum. Even Prolog has procedural aspects: the ordering of clauses has an effect on execution, as well as the use of cuts, etc. But the definition cited is more a definition for pure functional programming.
- arethuza 12y agoIsn't it fairer to say that any non-trivial Prolog program has significant procedural aspects? Yes, you can construct small examples that are purely declarative, and therefore map nicely to predicate calculus, but I found that eventually as programs grew in complexity you ended up using Prolog as yet another procedural language with a few isolated bits of interesting behaviour. I suspect that's why there was a brief phase where people were keen on embedding Prolog engines into more conventional platforms as specialized "inference engines" rather than trying to do everything in Prolog.
- keenerd 12y agoOnly if you get lazy. It is certainly possible to write in a mostly declarative fashion, though loading of initial conditions and saving results don't map well. I write non-trivial declarative stuff (I hesitate to use the word programs) in CNF‡, and that pseudo-language simply lacks every sort of traditional control structure. (I've done Conway's GoL so I guess turing-level computation could be implemented.) Prolog does make it very easy to fall off the declarative wagon, but the imperative parts should only be used sparingly as glue code. ‡Okay, I cheat and render to CNF.
- arethuza 12y agoMy recollections (they are a bit vague as it was ~20 years ago) was that you could write the "core" of your solution (I was looking at simulations of things like power stations) in nice declarative Prolog - but then, inevitably, your glue/interface code grows in complexity faster than your "core" so that you end up with that dominating and I didn't think Prolog was great for that at all. I think I eventually gave up on it entirely and ended up writing something in Lisp that took systems of ODEs and wrote standalone C++ models.
- kriro 12y agoIt isn't just about being lazy. I'd argue that the foreach that exists in a couple of Prologs is not declarative (despite being called logical loops etc.) but I still use it because I prefer that over repeating myself. Repeated clauses as OR gets pretty cumbersome once you work with bigger codebases because it sort of violates DRY. If you change/refactor something...suddenly you have to wade through a bunch of clauses. I've rewritten quite a few of these by falling back on imperative code.