简体   繁体   中英

Prolog how to utilize list element as a variable in a predicate

Good day. I would like to ask how one is able to utilize a list element as a variable in a predicate.

This is the code that I have been able to make so far:

case(boeing, [f4, f6, f12, f14, f21, f1, f10]).

go(Case) :-
    case(Case, [H|T]),
    getf124(Case, H).

getf124(Case,factorsSoFar) :-
member(f3, factorsSoFar),
write(['the defendent is the owner of secret']),
nl, getf123(Case, [f124|factorsSoFar]).

getf124(Case,factorsSoFar):-
write(['Accepted that the defendent is not the owner of secret']),
nl, getf123(Case,factorsSoFar).

When I remove getf124(Case, H), I'm able to execute the query

?- go(boeing).
true

However this only means that I'm simply checking if my query contains the word specified in the rule base. (Which in this case is boeing)

My goal is to use the Head of the list [H|T] in the predicate

getf124(Case, factorsSoFar)

So if f4 is detected, then its supposed to output

Accepted that the defendent is not the owner of secret

and so on for each element in the list. (The tail)

** EDIT: Added the whole code for reference**

case(boeing, [f4, f6, f12, f14, f21, f1, f10]).

go(Case) :-
    case(Case, [H|T]),
    getf124(Case, H).

% determine acceptibilty of
% F124, Defendent Ownership Rights
getf124(Case,F) :-
member(f3, F),
write(['the defendent is the owner of secret']), nl, 
getf123(Case, [f124|F]).

getf124(Case,factorsSoFar):-
write(['Accepted that the defendent is not the owner of secret']),
nl, getf123(Case,factorsSoFar).

% determine acceptibilty of
% F123, Maintaining Secrecy via Outsiders
getf123(Case,factorsSoFar) :-
member(f12, factorsSoFar), 
member(f10,factorsSoFar),!,
write(['Efforts made vis a vis Outsiders']),
nl, getf122(Case, [f123|factorsSoFar]).

getf123(Case,factorsSoFar):-
write(['Accepted that Efforts not made vis a vis Outsiders']),
nl, getf122(Case,factorsSoFar).

% determine acceptibilty of
% F122, Maintaining Secrecy via Defendent
getf122(Case,factorsSoFar) :-
member(f4, factorsSoFar), 
member(f1,factorsSoFar),!,
write(['Efforts made a vis a vis Defendent']),
nl, getf121(Case, [f122|factorsSoFar]).

getf122(Case,factorsSoFar):-
write(['Accepted that Efforts not made vis a vis Defendent']),
nl, getf121(Case,factorsSoFar).


% determine acceptibilty of
% F121, Confidentiality Agreement
getf121(Case,factorsSoFar):-
member(f23,factorsSoFar), 
member(f4,factorsSoFar),!,
write(['There was no Confidentiality Agreement']),
nl, getf115(Case,factorsSoFar).

getf121(Case,factorsSoFar):-
write(['Accepted that there was a Confidentiality Agreement']),
nl, getf115(Case,[f121|factorsSoFar]).

% determine acceptibilty of
% F115, Notice of Confidentiality
getf115(Case,factorsSoFar):-
member(f23,factorsSoFar),
write(['defendent was not on notice of Confidentiality']),
nl, getf114(Case,factorsSoFar).

getf115(Case,factorsSoFar):-
member(f21,factorsSoFar),
write(['defendent was on notice of Confidentiality']),
nl, getf114(Case,[f115|factorsSoFar]).

getf115(Case,factorsSoFar):-
member(f14,factorsSoFar),
write(['defendent was on notice of Confidentiality']),
nl, getf114(Case,[f115|factorsSoFar]).

getf115(Case,factorsSoFar):-
member(f5,factorsSoFar),
write(['defendent was not on notice of Confidentiality']),
nl, getf114(Case,factorsSoFar).

getf115(Case,factorsSoFar):-
member(f4,factorsSoFar),
write(['defendent was on notice of Confidentiality']),
nl, getf114(Case,[f115|factorsSoFar]).

getf115(Case,factorsSoFar):-
member(f13,factorsSoFar),
write(['defendent was on notice of Confidentiality']),
nl, getf114(Case,[f115|factorsSoFar]).

getf115(Case,factorsSoFar):-
write(['Accepted that defendent was not on notice of Confidentiality']),
nl, getf114(Case,factorsSoFar).

% determine acceptibilty of
% F114, Confidentiality relationship

getf114(Case,factorsSoFar):-
member(f115,factorsSoFar);
member(f121, factorsSoFar),
write(['There was a Confidential Relationship']),
nl, getf112(Case, [f114|factorsSoFar]).

getf114(Case,factorsSoFar):-
write(['Accepted that there was no Confidentiality Relationship']),
nl, getf112(Case,factorsSoFar).

% determine acceptability of
% F112, Information used 
getf112(Case,factorsSoFar):- 
member(f18,factorsSoFar), 
write(['The information was used']),
nl, getf111(Case,[f112|factorsSoFar]).

getf112(Case,factorsSoFar):- 
member(f8,factorsSoFar), 
write(['the information was used']),
nl, getf111(Case,[f112|factorsSoFar]).

getf112(Case,factorsSoFar):- 
member(f7,factorsSoFar), 
write(['the information was used']),
nl, getf111(Case,[f112|factorsSoFar]).

getf112(Case,factorsSoFar):- 
member(f17,factorsSoFar), 
write(['the information was not used']), 
nl, getf111(Case,factorsSoFar).

getf112(Case,factorsSoFar):- 
write(['accepted that the information was used']),
nl, getf111(Case,[f112|factorsSoFar]).

% determine acceptibilty of
% F111, Questionable means
getf111(Case,factorsSoFar):-
member(f25,factorsSoFar);
write(['Questionable means not were used']),
nl, getf108(Case,factorsSoFar).

getf111(Case,factorsSoFar):-
member(f17,factorsSoFar);
write(['Questionable means not were used']),
nl, getf108(Case,factorsSoFar).

getf111(Case,factorsSoFar):-
member(f22,factorsSoFar);
write(['Questionable means were used']),
nl, getf108(Case,[f111|factorsSoFar]).

getf111(Case,factorsSoFar):-
member(f26,factorsSoFar);
write(['Questionable means were used']),
nl, getf108(Case,[f111|factorsSoFar]).

getf111(Case,factorsSoFar):-
member(f14,factorsSoFar);
write(['Questionable means were used']),
nl, getf108(Case,[f111|factorsSoFar]).

getf111(Case,factorsSoFar):-
member(f2,factorsSoFar);
write(['Questionable means were used']),
nl, getf108(Case,[f111|factorsSoFar]).

getf111(Case,factorsSoFar):-
member(f1,factorsSoFar);
write(['Questionable means not were used']),
nl, getf108(Case,factorsSoFar).

getf111(Case,factorsSoFar):-
write(['Accepted that questionable means not were used']),
nl, getf108(Case,factorsSoFar).

% determine acceptibilty of
% F108, Information available elsewhere
getf108(Case,factorsSoFar):-
member(f16,factorsSoFar);
member(f24,factorsSoFar),
write(['the Information was available elsewhere']),
nl, getf106(Case, [f108|factorsSoFar]).

getf108(Case,factorsSoFar):-
write(['Accepted that the Information was not available elsewhere']),
nl, getf106(Case,factorsSoFar).

% determine acceptibilty of
% F106, Information Known
getf106(Case,factorsSoFar):-
member(f20,factorsSoFar),
write(['The information is known']),
nl, getf105(Case, [f106|factorsSoFar]).

getf106(Case,factorsSoFar):-
member(f27,factorsSoFar),
member(f15,factorsSoFar), !,
write(['The information is known']),
nl, getf105(Case, [f106|factorsSoFar]).

getf106(Case,factorsSoFar):-
member(f27,factorsSoFar),
member(f123,factorsSoFar),!,
write(['The information is known']),
nl, getf105(Case, [f106|factorsSoFar]).

getf106(Case,factorsSoFar):-
write(['Accepted that the information is not known']),
nl, getf105(Case, [f106|factorsSoFar]).

% determine acceptibilty of 
% F105, Information Known Or available
getf105(Case,factorsSoFar):-
member(f106, factorsSoFar);
member(f108, factorsSoFar),
write(['The information was known or available']),
nl, getf104(Case, [f105|factorsSoFar]).

getf105(Case,factorsSoFar):-
write(['Accepted that the information was neither known or available']),
nl, getf104(Case, factorsSoFar).

% determine acceptibilty of
% F104, Information Valuable
getf104(Case,factorsSoFar):-
member(f8,factorsSoFar),
write(['the information was Valuable']),
nl, getf120(Case, [f104|factorsSoFar]).

getf104(Case,factorsSoFar):-
member(f15,factorsSoFar),
write(['the information was Valuable']),
nl, getf120(Case, [f104|factorsSoFar]).

getf104(Case,factorsSoFar):-
member(f105,factorsSoFar),
write(['the information was not Valuable']),
nl, getf120(Case,factorsSoFar).

getf104(Case,factorsSoFar):-
write(['Accepted that the information was Valuable']),
nl, getf120(Case, [f104|factorsSoFar]).


% determine acceptibilty of
% F120, Information legitimately obtained
getf120(Case,factorsSoFar):-
member(f111,factorsSoFar), 
member(f105,factorsSoFar),!,
write(['the information was not legitimately obtained']),
nl, getf110(Case,factorsSoFar).

getf120(Case,factorsSoFar):-
write(['Accepted that the information was legitimately obtained']),
nl, getf110(Case,[f120|factorsSoFar]).

% determine acceptibilty of
% F110, Improper Means 
getf110(Case,factorsSoFar):-
member(f120, factorsSoFar), 
member(f111,factorsSoFar),!,
write(['Improper means were not used']),
nl, getf102(Case,factorsSoFar).

getf110(Case,factorsSoFar):-
write(['Accepted that Improper means were used']),
nl, getf102(Case, [f110|factorsSoFar]).

% determine acceptibilty of
% F102, Efforts to maintain secrecy
getf102(Case,factorsSoFar):-
member(f19,factorsSoFar),
write(['Efforts were not taken to maintain secrecy']),
nl, getf203(Case,factorsSoFar).

getf102(Case,factorsSoFar):-
member(f23,factorsSoFar),
write(['Efforts were not taken to maintain secrecy']),
nl, getf203(Case,factorsSoFar).

getf102(Case,factorsSoFar):-
member(f27,factorsSoFar),
member(f123,factorsSoFar),!,
write(['Efforts were not taken to maintain secrecy']),
nl, getf203(Case,factorsSoFar).

getf102(Case,factorsSoFar):-
member(f6,factorsSoFar),
write(['Efforts were taken to maintain secrecy']),
nl, getf203(Case, [f102|factorsSoFar]).

getf102(Case,factorsSoFar):-
member(f122,factorsSoFar),
write(['Efforts were taken to maintain secrecy']),
nl, getf203(Case, [f102|factorsSoFar]).

getf102(Case,factorsSoFar):-
member(f123,factorsSoFar),
write(['Efforts were taken to maintain secrecy']),
nl, getf203(Case, [f102|factorsSoFar]).

getf102(Case,factorsSoFar):-
write(['Accepted that efforts were not taken to maintain secrecy']),
nl, getf203(Case,factorsSoFar).

% determine acceptibilty of 
% F203, Information Trade secret

getf203(Case,factorsSoFar):-
member(f102,factorsSoFar),
member(f104,factorsSoFar),
write(['Information was a trade secret']),
nl, getf201(Case,[f203|factorsSoFar]).

getf203(Case,factorsSoFar):-
write(['Accepted that Information was not a trade secret']),
nl, getf201(Case,factorsSoFar).

% determine acceptibilty of
% F201, Information misappropriation

getf201(Case,factorsSoFar):-
member(f114,factorsSoFar),
member(f112,factorsSoFar),
write(['Information was misappropriated']),
nl, getf200(Case,[f201|factorsSoFar]).

getf201(Case,factorsSoFar):-
member(f110,factorsSoFar),
write(['Information was misappropriated']),
nl, getf200(Case,[f201|factorsSoFar]).

getf201(Case,factorsSoFar):-
write(['Accepted that Information was not misappropriated']),
nl, getf200(Case,factorsSoFar).


% determine acceptibilty of
% F200, Trade secret misappropriation
getf200(Case,factorsSoFar):-
member([f203,factorsSoFar]),
member(f201,factorsSoFar),
member(f124,factorsSoFar),!,
write(['the trade secret was misappropriated']),
nl, write(['find for plaintiff']).

getf200(Case,factorsSoFar):-
write(['the trade secret was not misappropriated']),
nl, write(['find for defendent']).

EDIT #2: OMG I applied the said edits (idk why I didn't use a proper variable, variables are supposed to start with capital letters, what a bummer facepalm )

?- go(spaceAero).
[Accepted that the defendent is not the owner of secret]
[Accepted that Efforts not made vis a vis Outsiders]
[Accepted that Efforts not made vis a vis Defendent]
[Accepted that there was a Confidentiality Agreement]
[Accepted that defendent was not on notice of Confidentiality]
[There was a Confidential Relationship]
[The information was used]
[Questionable means not were used]
[Accepted that the Information was not available elsewhere]
[Accepted that the information is not known]
true ;
[Accepted that the information was neither known or available]
[the information was Valuable]
[Accepted that the information was legitimately obtained]
[Accepted that Improper means were used]
[Efforts were not taken to maintain secrecy]
[Accepted that Information was not a trade secret]
[Information was misappropriated]

Perfect, Everything outputs. except I did encounter an error on the last line. Find for plaintiff which was supposed to output, Though upon looking at the debug, it seems that all of the list elements were properly collected though

boeing[f200, f201, f203, f102,f110,f104,f111,
f112,f114,f115,f121,f122,f123,
f4,f6,f12,f14,f21,f1,f10]

There is much confusion in the code.

go(Case) :-
    case(Case, [H|T]),   % Singleton variable! Use _T instead
    getf124(Case, H).

but what you probably want to do is "pass" the whole list. H will be f4 only here.

So:

go(Case) :-
    case(Case, L), 
    getf124(Case, L).

In here:

getf124(Case,factorsSoFar) :-
   member(f3, factorsSoFar),
   write(['the defendent is the owner of secret']),nl,
   getf123(Case, [f124|factorsSoFar]).

factorsSoFar is a constant (starts with lowercase). You probably want:

getf124(Case,F) :-
   member(f3, F),
   write(['the defendent is the owner of secret']),nl,
   getf123(Case, [f124|F]).

Now I'm not sure what the last line above should do, but here it creates a new list from F , which has a new constant item f124 at first position.

Same problem with a constant "as second argument here". It's not really an "argument" as it means _for all value of Case , Case is related to constant factorsSoFar via relation getf124 if ...._

getf124(Case,factorsSoFar):-
   write(['Accepted that the defendent is not the owner of secret']),
   nl, getf123(Case,factorsSoFar).

Hint: Use Unit testing for better progress

:-begin_tests(boeing).

test(one) :- go(boeing).

:-end_tests(boeing).

rt :- run_tests(boeing).

Now you can run the test with rt.

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