# manipulating a compund term in Prolog

13 replies to this topic

### #1keb5

New Member

• Members
• 3 posts

Posted 09 June 2006 - 11:19 AM

Working in Prolog, I am trying to turn the compound term

into

Is there some way of manipulating the head of the compound term (perhaps by temporarily separating it from the body?) so that it can be changed?

This is quite urgent so any and all help would be gratefully received!
Thanks,
Kirsty

### #2Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 09 June 2006 - 04:10 PM

This sounds like a homework question, and I am afraid we don't help people with their homework on this site. Try looking in your textbook or asking your professor for help.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3keb5

New Member

• Members
• 3 posts

Posted 09 June 2006 - 06:45 PM

Actually I am a PhD student trying to develop a system so this is not a "homework" question! The system is an AI planning system being developed to look at how classical planning paradigms can be applied to the computerisation of medical guidelines. The urgency is because I don't want to delay the building of the rest of my system.

I am still in need of advice if anyone can help!!! :o)

### #4Nodlehs

Valued Member

• Members
• 152 posts

Posted 09 June 2006 - 09:47 PM

No offense, but it is homework.

You are a student, no matter your level, and you have a task involved in your schooling. Plus, I think if you are at a PhD level, that you would be able to find the information you are looking for in a prolog textbook or from a knowledgable Professor on campus.

### #5keb5

New Member

• Members
• 3 posts

Posted 10 June 2006 - 08:02 AM

Ok I appreciate your stance on what constitutes homework and realise I am unlikely to get a response but I wanted to point out that before posting to this site I did check every text book I could find (including reference manuals of various prolog systems) and searched the internet. There are no Prolog experts at my university either (in fact it was my professor who suggested poting to forums!).
I only use forum postings as a last resort when all other resources have been exhausted.

### #6Jare

Valued Member

• Members
• 247 posts

Posted 12 June 2006 - 09:25 PM

keb5 said:

There are no Prolog experts at my university either
Are there any Prolog experts left anywhere? ;) From what little I remember about that language, there is no answer to your question, but it's been 15 years and I'm probably not even understanding the question itself. Given the lack of expertise around, I'd suggest revisiting the decision to use Prolog if at all possible.

### #7SamuraiCrow

Senior Member

• Members
• 459 posts

Posted 13 June 2006 - 04:21 AM

I'm afraid Prolog is a one-of-a-kind language. There is no substitute. Even Lisp is a different type of processing than Prolog.

As for the question of how to do what the OP was proposing, I only had a two week orientation on Predicate Calculus so I don't know of a way to do what was proposed but I'll give it some thought.

-edit- It sounds to me like you could use a form of inheritance. Are you using an Object Prolog system or a traditional one?

-edit2- http://www.faqs.org/.../section-7.html has some Object-Oriented layers to add to Prolog that you might find useful.

### #8itsbrian

New Member

• Members
• 16 posts

Posted 19 July 2006 - 03:07 AM

I'm not yet sure how to seperate the head of the compound term perhaps someone else can help with that. If you are using SWI prolog one way of manipulating strings is to convert into a list of ascii code, do the manipulating then revert back to string. I'm only a beginner myself still learning but maybe this might help.

To asci:

L = [97, 100, 109, 105, 116, 80, 97, 116, 105, 101, 110, 116] ;

No

To string:
?- name(N, [97, 100, 109, 105, 116, 80, 97, 116, 105, 101, 110, 116]).

No

Of course you still need code to do the seperation/manipulation/modification part!

### #9Trap D

Member

• Members
• 55 posts

Posted 29 August 2006 - 11:16 PM

In SWI-Prolog, to do that, you would use assert, retract, dynamic, forall.

### #10Ooka

Member

• Members
• 98 posts

Posted 30 August 2006 - 07:55 AM

Never used prolog before, but google apparently still works :P

http://gnu-prolog.in.../manual067.html

That *seems* to define how you manipulate compound terms in prolog.

### #11Trap D

Member

• Members
• 55 posts

Posted 30 August 2006 - 08:51 AM

It *seems* to be "manipulating Prolog terms in C"

### #12Ooka

Member

• Members
• 98 posts

Posted 30 August 2006 - 08:24 PM

never used prolog before, just trying to help, maybe steer him onto a different track. Googling his topic title brings up a few hundred thousand topics... the answer's gotta be in there somewhere.

### #13itsbrian

New Member

• Members
• 16 posts

Posted 12 September 2006 - 08:12 AM

Here's one (crude) solution!

start(A, B, C, D) :-

tell('filename.pl'),

manipulate(A, B, C, D), !,

told.

start(_, _, _, _) :-

told.

manipulate(A, B, C, D) :-

write(A),

write(B),

write(C),

write(D),

write('.').

Note: both admitPatient and (P,oncology) would each be contained in some variable (ALL terms preferably). Since i haven't got terms as variables i'll put the query in manually as follows:

start('z', 'admitPatient', '1', '(P,oncology)').

This writes the modified term to filename.pl.
Which contains the new predicate:

zadmitPatient1(P,oncology).

Then to use the new predicate could use:

consult('filename.pl').

Is crude but is a start and it works!

To do it dynamically I guess you would need to add this line:

:- dynamic admitPatient/2.

The infix operator '=..' (pronounced "univ") can be used either to
decompose a term into a list containing its functor and arguments or
else to construct a term from such a list.

http://wwwcgi.rdg.ac...g/ploghelp/univ

i.e,

To extract the term 'admitPatient' perhaps a call similar to:

?- admitPatient(P, oncology) =.. [H|B] .

P = _G404

B = [_G404, oncology] ;

No

Variable H (inserted at paramater B in my 'static' code above for example) can then be manipulated; likewise Variable B (inserted at paramater D above).

:excl: Of course there's a better way as i'm just a novice. :unsure:

### #14Trap D

Member

• Members
• 55 posts

Posted 12 September 2006 - 12:32 PM

As I understand the problem, I think you want to change the facts of the database admitPatient(P,oncology) into zadmitPatient1(P, oncology).

I suppose that your facts are in a file named "admitPatient.pl", I modify the facts and I save the new database in "admitPatient1.pl".

The change are made with this :

% the predicates must be declared dynamic because they will be changed

my_test :-

% opening the new database for writing

% I work with all the patient of oncology

(  %  I remove the fact of the database

% I add the new fact in the database

% I save the fact in the file "admitPatient1.pl"

nl)),

% I save the rest of the database

nl)),

% it's done.

told.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users