At the needs of my HW at uni I need to transform some Horn clauses to Prolog but I cannot figure out how to do it. I found out some guides but they describe how to do it with only one fact. So can you give me a brief example on how to do it?
Eg John is beautiful and rich
we can transform it at: not (Beautiful(John)) ^ not(Rich(John)) which is a Horn clause right? So how this can be translated it Prolog?
another example Everyone loves somebody. Horn clause: ∀X∃YLoves(X,Y) how can this be implemented in Prolog?
Thx in advance
For the first question you have
john :- beautiful, rich.
Or having something like:
beautiful(john).
rich(john).
with the query:
beautiful(X),rich(X).
Basically every rule in prolog is a horn clause.
By definition, a Logic Program is a program, comprising of Horn clauses.
In prolog when you have a rule that is declared as:
a1(X):-a2(X),a3(X),...,an(X)
is a horn clause because it is translated as:
a1 or not a2 or not a3 or ... or not an
So for your second question:
In prolog the universal quantifier is implied when you define a rule so it does
not affect the prolog clause.
So you need to negate your sentence so you can transform the existential quantifier
to a universal one.
So, you have:
∀X∃YLoves(X,Y)
then you negate:
∀X ∀Y false <- Loves(X,Y))
which translates into:
:- loves(X,Y).
Related
The tag logical purity mentions (=)/2 as pure. Is it
"intrinsically" pure or "operational" pure? To the
best of my knowledge it can be defined by this Horn clause:
∀x(=(x, x))
Which is this Prolog fact, if were not already a built-in:
X = X.
This means (=)/2 would be "intrinsically" pure, as SWI-Prolog already remarks. So what is
then the difference to first order equality (FOL=), if there
are any differences?
The "intrinsinc" definition of (=)/2 I guess does assure that the unify predicate is reflexive, symmetric and transitive. Requirements that are also satisfied by FOL=. But FOL= also requires congruence, which is this axiom schema:
/* Predicate Congruence in FOL= */
∀x1..∀xn∀yj(=(xj, yj) & p(x1, .., xn) -> p(x1, .., xj-1, yj, xj+1, .., xn))
Which is not represented by the only Horn clause for the unify predicate. And since the unify predicate is a built-in, the missing Horn clauses can also not be added. So what can go wrong? Can we make an example that would go wrong?
The classical example is this fact:
p(morning_star).
Obviously this query succeeds in Prolog:
?- p(morning_star).
true
But this query fails in Prolog, whereas in FOL= it would succeed:
?- morning_star = evening_star, p(evening_star).
false
The situation is different in so called unification modulo theories, where the unification and also the unify predicate might change their meaning.
I was watching some issues such as Logica fuzzy logic and Horn clauses and saw some simple applications examples of them , with Prolog.
The reason for this question is because these issues are also among the Herbrand theorem which I consider a little more complicated than others, at least for me , and I had difficulty finding an application example related to Prolog .
That's why I wanted to provide me some applicative examples using Prolog , not so basic (because that generates the Herbrand model , according to the definition , are basic rules and always find this application example when search about Herbrand ) , for exclusive use Herbrand. Thanks
This is a example applicative code in Prolog:
p(f(X)):- q(g(X)).
p(f(X)):- p(X).
p(a).
q(b).
A set of clauses has a model iff it has a Herbrand model.
To prove that clause C is a consequence of clauses Cs, simply show that Cs∪~C is unsatisfiable.
This is, in abstract terms, what Prolog does, via a special case of resolution: You can regard the execution of a (pure—what else) Prolog program as the Prolog engine trying to find a resolution refutation of the negated query.
The form of resolution that Prolog implements, SLD resolution with depth-first search, does not guarantee though that all unsatisfiable clauses are disproved, it is incomplete.
In Prolog, procedural properties may impact the derivation of consequences. For example, with your program:
?- p(X).
wating...
Whereas we simply reorder the clauses as:
q(b).
p(a).
p(f(X)):- q(g(X)).
p(f(X)):- p(X).
we get:
?- p(X).
X = a ;
X = f(a) ;
X = f(f(a)) .
Note though that many important declarative properties are indeed preserved in the pure and monotonic subset of Prolog. See logical-purity for more information.
I am trying to understand how to translate the prolog rule
brother(g(x), g(y)) :- brother(x,y).
brother(n,n).
to first order logic.
is ∀x,y(brother(x,y) -> brother(g(x), g(y)) a correct answer?
No, the answer is not correct.
First, decide whether x, y and n in the Prolog program are actually meant to be logical variables. In that case, you need to change the program: A Prolog variable begins with an uppercase letter or underscore. So, suppose you change the program to:
brother(g(X), g(Y)) :- brother(X, Y).
brother(N, N).
Then the translation you give is still not sufficient to capture the declarative meaning of this logic program.
For example, using just the implication you give, can you derive a single statement that actually holds?
Because Prolog uses chronological backtracking(from the Prolog Wikipedia page) even after an answer is found(in this example where there can only be one solution), would this justify Prolog as using eager evaluation?
mother_child(trude, sally).
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
With the following output:
?- sibling(sally, erica).
true ;
false.
To summarize the discussion with #WillNess below, yes, Prolog is strict. However, Prolog's execution model and semantics are substantially different from the languages that are usually labelled strict or non-strict. For more about this, see below.
I'm not sure the question really applies to Prolog, because it doesn't really have the kind of implicit evaluation ordering that other languages have. Where this really comes into play in a language like Haskell, you might have an expression like:
f (g x) (h y)
In a strict language like ML, there is a defined evaluation order: g x will be evaluated, then h y, and f (g x) (h y) last. In a language like Haskell, g x and h y will only be evaluated as required ("non-strict" is more accurate than "lazy"). But in Prolog,
f(g(X), h(Y))
does not have the same meaning, because it isn't using a function notation. The query would be broken down into three parts, g(X, A), h(Y, B), and f(A,B,C), and those constituents can be placed in any order. The evaluation strategy is strict in the sense that what comes earlier in a sequence will be evaluated before what comes next, but it is non-strict in the sense that there is no requirement that variables be instantiated to ground terms before evaluation can proceed. Unification is perfectly content to complete without having given you values for every variable. I am bringing this up because you have to break down a complex, nested expression in another language into several expressions in Prolog.
Backtracking has nothing to do with it, as far as I can tell. I don't think backtracking to the nearest choice point and resuming from there precludes a non-strict evaluation method, it just happens that Prolog's is strict.
That Prolog pauses after giving each of the several correct answers to a problem has nothing to do with laziness; it is a part of its user interaction protocol. Each answer is calculated eagerly.
Sometimes there will be only one answer but Prolog doesn't know that in advance, so it waits for us to press ; to continue search, in hopes of finding another solution. Sometimes it is able to deduce it in advance and will just stop right away, but only sometimes.
update:
Prolog does no evaluation on its own. All terms are unevaluated, as if "quoted" in Lisp.
Prolog will unfold your predicate definitions as written and is perfectly happy to keep your data structures full of unevaluated uninstantiated holes, if so entailed by your predicate definitions.
Haskell does not need any values, a user does, when requesting an output.
Similarly, Prolog produces solutions one-by-one, as per the user requests.
Prolog can even be seen to be lazier than Haskell where all arithmetic is strict, i.e. immediate, whereas in Prolog you have to explicitly request the arithmetic evaluation, with is/2.
So perhaps the question is ill-posed. Prolog's operations model is just too different. There are no "results" nor "functions", for one; but viewed from another angle, everything is a result, and predicates are "multi"-functions.
As it stands, the question is not correct in what it states. Chronological backtracking does not mean that Prolog will necessarily backtrack "in an example where there can be only one solution".
Consider this:
foo(a, 1).
foo(b, 2).
foo(c, 3).
?- foo(b, X).
X = 2.
?- foo(X, 2).
X = b.
So this is an example that does have only one solution and Prolog recognizes that, and does not attempt to backtrack. There are cases in which you can implement a solution to a problem in a way that Prolog will not recognize that there is only one logical solution, but this is due to the implementation and is not inherent to Prolog's execution model.
You should read up on Prolog's execution model. From the Wikipedia article which you seem to cite, "Operationally, Prolog's execution strategy can be thought of as a generalization of function calls in other languages, one difference being that multiple clause heads can match a given call. In that case, [emphasis mine] the system creates a choice-point, unifies the goal with the clause head of the first alternative, and continues with the goals of that first alternative." Read Sterling and Shapiro's "The Art of Prolog" for a far more complete discussion of the subject.
from Wikipedia I got
In eager evaluation, an expression is evaluated as soon as it is bound to a variable.
Then I think there are 2 levels - at user level (our predicates) Prolog is not eager.
But it is at 'system' level, because variables are implemented as efficiently as possible.
Indeed, attributed variables are implemented to be lazy, and are rather 'orthogonal' to 'logic' Prolog variables.
I am studying DCG grammars and parse trees in Prolog using Ivan Bratko's Programming for Artificial Intelligence. In a program that uses a DCG grammar to extrapolate the meaning of a sentence, I find these two predicates that, I think, represent a kind of semantic knowledge:
properName(john) --> [john].
properName(mary) --> [mary].
How should I read these predicates? I am thinking that they mean: it is true that an element of a list represented by the string "john" is a proper name and this proper name is John (same thing for Mary).
Is it my reading correct or are there some other implications?
properName(X) is just an unary rule (in the context of DCG; it is a ternary predicate in Prolog - check it out with ?- listing(properName) ). you could've called it "socks", or "jam", it's totally up to you. So the semantic knowledge about it representing proper name "john" or "mary" is nowhere to be found in the code (it uses naming as self-documenting feature, but documentation is not code).
The predicate allows for an atom john or mary to be present in the input stream, and nothing else; and demands that X unified with that atom.
You could've defined it thus:
name(X) --> [X], { member(X, [john, mary]) }.
then,
4 ?- phrase( name(X), [john,jack], Z).
X = john,
Z = [jack] ;
false.
5 ?- phrase( name(X), [jack,john], Z).
false.
8 ?- phrase( name(X), [john,mary], Z).
X = john,
Z = [mary] ;
false.
9 ?- phrase( name(X), [mary,john,jack], Z).
X = mary,
Z = [john, jack].
11 ?- phrase( name(jack), [jack,mary,john], Z).
false.
This is a trivial predicate that does not lend itself to interpretation outside of the context in which it is used.
In other words, it can only be used to demand that a proper name is used in a certain way, by a DCG rule that uses it on its right-hand side. The way you have shown it, in isolation, it means nothing more than:
'john' is a proper name, and so is 'mary'.
EDIT
I might be wrong here, but you are still abusing the English language to describe things that are best described using a formal language. Prolog is a formal language, with a defined syntax and semantics. It can be used to formally describe logical relationships, or computation. Trying to faithfully translate it into English is bound to be clumsy and unnecessary. Something as trivial as the predicate in your question turns into something that is silly, difficult to understand, and difficult to work with.
P.S. The correct spelling of the word you like so much is representation.