Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How does the operator-command works?
Hello,

I have the following code-example:

%*************************
:- op(800, fx, if).
:- op(700, xfx, then).
:- op(300, xfy, or).
:- op(200, xfy, and).

fact(hall_wet).
fact(bathroom_dry).

if
hall_wet and bathroom_dry
then
problem_in_kitchen.
%*************************

I don't really understand the functionality of the operator-expression ("op"
at the
beginning of this example). I read in my book that the first parameter is
the precedence,
the sceond one the type (prefix, postfix...) and the last one the name. So
far, so good.

But...

How does it work? How does the compiler knows that "and" acts like an "and"
instead of
something else. How does he knows that "if hall_wet and bathroom_dry then
problem_in_kitchen." accomplish the desired action, only with these three
parameters in the
operator-definition???

I hope you understand what I mean...

Tobias

P.S.: I tried to post my question one time before, but it didn't work - I
don't hope that it appears twice...



Report this thread to moderator Post Follow-up to this message
Old Post
Tobias Mauderer
10-01-04 08:57 PM


Re: How does the operator-command works?
"Tobias Mauderer" <tobimau@gmx.de> writes:
> How does it work? How does the compiler knows that "and" acts like an "and
"
> instead of
> something else. How does he knows that "if hall_wet and bathroom_dry then
> problem_in_kitchen." accomplish the desired action, only with these three
> parameters in the
> operator-definition???

Simple.  It doesn't know.

op/3 defines a *syntactic* construct.  Only I/O predicates like read/1 and
write/1 use the operator definitions.  After your example has been read
by the system, it will be treated like (in fact, it is *identical* to)
the term

if(then(and(hall_wet, bathroom_dry), problem_in_kitchen)).

If you want to use these operators to define logical rules that can be
executed, you have to do one of two things:

- Write a meta-interpreter which understands the new operators; or
- define term expansion rules which convert the new operators to the
standard form for Prolog rules.

An example (untested) for the second approach would be

term_expansion(if(then(Precondition, Consequence)),
(Consequence :- TranslatedPrecondition)) :-
translate_precondition(Precondition, TranslatedPrecondition,true).

translate_precondition(and(A, B), T0,T) :-
!,
translate_precondition(A, T0,T1),
translate_precondition(B, T1,T).
translate_precondition(A, (A, T),T).

HTH,
Jens.
--
mailto:jjk@acm.org                 As the air to a bird, or the sea to a fis
h,
http://www.bawue.de/~jjk/        so is contempt to the contemptible. [Blake]

Report this thread to moderator Post Follow-up to this message
Old Post
Jens Kilian
10-01-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.