views:

121

answers:

2

In Prolog you can write a ground fact as:

lost(jen).

You can also write a non-ground fact as:

lost(X).

Does this makes any sense? Could you show me a practical/real example where non ground facts are used?

Thanks,

+2  A: 

Well, you can have other things in facts besides atoms, for example you can have lists. Once you've done that, you may want to know about a one-element list, and you can have

oneelement([X]).

Likewise, say you want to compare what is the last element in a list

lastelement([X],X).
lastelement([_|Remainder],X) :- lastelement(Remainder,X).

The very useful member predicate is defined as

member([X|_],X).
member([_|Remainder],X) :- member(Remainder,X).

Each of these uses a non-ground fact as its base case, by matching a special form that's more specific than just lost(X)

Ken Bloom
I just want to make a remark, that if you will use a variable in a clause only 1 time (not 2 or more usages), prolog (in particular SWI) will give you varning, like "Warning: /path/to/file.pro:123: Singleton variables: [Var]"You should use _ in such places.
Xonix
@Xonix: Or just prefix the existing name with an underscore like this: `_Foobar`. This avoids the singleton warning while still letting you use an explanatory name.
bcat
+2  A: 

Another case, avoiding lists, is where most cases are "true" and you just want to exclude some few cases that are false. So you deliberately fail thoses cases, then let everything else pass through.

Then you can do, say...

inhabited(antarctica) :- !, fail.

% all the other continents are inhabited
inhabited(_).
pfctdayelise