For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2004 > Define new Operators...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Define new Operators...
Tobias Mauderer

2004-10-01, 3:57 pm

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

leak_in_kitchen.

%**********************************


I don't really understand the syntax of the operator-command (op). As I read
in my book I can define new operators and the number is the predecence, the
fx, xfx... is for the tpye (infix, prefix...) and the last parameter is the
name. O.k. so far, so good.


But...


How does it really work? How knows the compiler that my defined operator
acts like an "and" and not than something else. How it is possible that I
can use "if hall_wet and bathroom_dry then leak_in_kitchen." and the "if"
works like an if and the "then" works like a then, only with these few
definition-parameters???


I hope you understand my question...


Thanks,


Tobias


Nick Wedd

2004-10-12, 8:56 am

In message <2s5cokF1gh7qhU1@uni-berlin.de>, Tobias Mauderer
<tobimau@gmx.de> writes
>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
>
>leak_in_kitchen.
>
>%**********************************
>
>
>I don't really understand the syntax of the operator-command (op). As I read
>in my book I can define new operators and the number is the predecence, the
>fx, xfx... is for the tpye (infix, prefix...) and the last parameter is the
>name. O.k. so far, so good.
>
>
>But...
>
>
>How does it really work? How knows the compiler that my defined operator
>acts like an "and" and not than something else. How it is possible that I
>can use "if hall_wet and bathroom_dry then leak_in_kitchen." and the "if"
>works like an if and the "then" works like a then, only with these few
>definition-parameters???


It doesn't. Prolog has no idea what you mean by "and" and "if". All
you have told it is the syntax, it knows nothing about the semantics.

The effect would be exactly the same if you wrote

:- op(800, fx, spade).
:- op(700, xfx, heart).
:- op(300, xfy, diamond).
:- op(200, xfy, club).

spade
hall_wet and bathroom_dry
heart
leak_in_kitchen.

That is, it would parse this as

spade( heart( club( hall_wet, bathroom_dry) leak_in_kitchen)).

Nick
--
Nick Wedd nick@maproom.co.uk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com