Code Comments
Programming Forum and web based access to our favorite programming groups.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...
Post Follow-up to this message"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]
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.