简体   繁体   中英

How to access list from prolog Knowledge base

this is the element in my knowledge base, (exactly like this):

  DataBase = [book('A',[author('B','C')],154,12,6,[publishedYear(1990)]),
  book('Z',[author('Y','X')],155,1,62,[publishedYear(2009)])].

It is in the same pl-file with our other predicates. How am I able to acces it? for example if I want to do:

  ?- write(DataBase).

It gives an error, because apparantly it can't access the variable.

edit: the error I get is that it is a singleton variable:

   Singleton variables: [DB] 

and:

   No permission to modify static_procedure `(=)/2'

(First of all, what you call "function" should be called "predicate")

If I understand correctly what you think you did, you wrote

Var = Something.

And then tried to "access" that variable in your predicate. You should note that a "variable" in logic programming is not the same as a "variable" in other programming paradigms.

In C, for example, a variable is a place where you can put information that you can later change. In logic programming, a variable is there to be unified , it is a variable in the mathematical sense.

You should also note that variables are implicitly universally quantified , so when you write

predicate(X,Y).

You are actually writing

forall X, forall Y, predicate(X,Y).

This means that outside of that predicate, if you use the string "X" again to name a variable, it is no longer the same one. Meaning, in the following

pred1(X).
pred2(X).

These are not the same variable.

What you can do to achieve what you seem to aim at is, for example:

database([book('A',[author('B','C')],154,12,6,[publishedYear(1990)]),
          book('Z',[author('Y','X')],155,1,62,[publishedYear(2009)])]).

and, if you want to use it in a predicate pred :

pred :- database(DB), do_this_with_it(DB).

But I might be wrong in understanding what you meant, so please do say if it doesn't answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM