# Yfy-pattern

**URL:** https://swi-prolog.discourse.group/t/yfy-pattern/2923
**Category:** Help!
**Tags:** discussion
**Created:** [September 12, 2020, 11:08am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923 "2020-09-12T11:08:51Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Yessir](https://avatars.discourse-cdn.com/v4/letter/y/e36b37/32.png) [@Yessir](https://swi-prolog.discourse.group/u/Yessir)
#### Post date: [September 12, 2020, 11:08am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/1 "2020-09-12T11:08:51Z")

</div>

I’m using: SWI-Prolog version (threaded, 64 bits, version 8.0.3).

Can someone explain to me why yfy is not a meaningful pattern for precedence and position of operators?

Need help fast! Thanks.

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [September 12, 2020, 11:12am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/2 "2020-09-12T11:12:36Z")

</div>

While I am looking for an authoritative answer think about what it does? Try to explain what it does to someone and where you would use it? See the point?

---

<div class="post-metadata">

### Author: ![Yessir](https://avatars.discourse-cdn.com/v4/letter/y/e36b37/32.png) [@Yessir](https://swi-prolog.discourse.group/u/Yessir)
#### Post date: [September 12, 2020, 11:22am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/3 "2020-09-12T11:22:38Z")

</div>

Im a full on rookie in Prolog so I don’t know much about the patterns. Isn’t it true that x must be strictly lower than the precedence of the operator and y must be the same or lower than the precedence of the operator? If true, that’s the most I know about it. I still can’t explain to myself why yfy wouldn’t work and I can’t come up with an example. I was looking for someone wo explain it to me in layman’s terms by maybe using an example.

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [September 12, 2020, 11:26am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/4 "2020-09-12T11:26:44Z")

</div>

I could explain it but not to the point that it would make perfect sense. In other words I know it well enough to satisfy my needs but you would ask more questions about my answer that I would not be able to feel comfortable in responding, thus the reason I am searching for an authoritative answer.

> [@Yessir](#):
>
> Isn’t it true that x must be strictly lower than the precedence of the operator and y must be the same or lower than the precedence of the operator?

That looks correct.

From the SWI-Prolog documentation for op/3

> Type is one of: `xf` , `yf` , `xfx` , `xfy` , `yfx` , `fy` or `fx` . The‘ `f` ’indicates the position of the functor, while `x` and `y` indicate the position of the arguments.‘ `y` ’should be interpreted as “on this position a term with precedence lower or equal to the precedence of the functor should occur’'. For‘ `x` ’the precedence of the argument must be strictly lower. The precedence of a term is 0, unless its principal functor is an operator, in which case the precedence is the precedence of this operator. A term enclosed in parentheses `( ... )` has precedence 0.

---

<div class="post-metadata">

### Author: ![Yessir](https://avatars.discourse-cdn.com/v4/letter/y/e36b37/32.png) [@Yessir](https://swi-prolog.discourse.group/u/Yessir)
#### Post date: [September 12, 2020, 11:31am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/5 "2020-09-12T11:31:29Z")

</div>

Thank you for your answer! I will stick around and wait for an authoritative answer from whoever it may come.

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [September 12, 2020, 11:32am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/6 "2020-09-12T11:32:11Z")

</div>

> [@Yessir](#):
>
> Thank you for your answer!

That was not meant to be an answer, just letting you know you are on the right track.

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [September 12, 2020, 11:44am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/7 "2020-09-12T11:44:15Z")

</div>

Here is an authoritative answer ([ref](http://www.cs.ubbcluj.ro/~csatol/log_funk/prolog/slides/4-operators.pdf) - see page 8), but it will leave you with questions if you don’t work through some exercises to understand why. Still looking for a better answer.

Here is another reference with a bit more of an explanation[. Jacc’s LR-Parsing with Dynamic Operators](http://www.hassan-ait-kaci.net/hlt/doc/hlt/jaccdoc/dynamicLR.html)

> Note that yfy is not allowed as an operator specifier because that would mean an ambiguous way of parsing the operator by associating either to the left or to the right.

That is the meaning I use but it only makes sense once you work through examples. As working through the examples to make yourself satisfied you do understand it can take hours and I don’t have the necessary examples on hand I will be stopping to seek a better answer at this point.

However one thing that will be of value in working the examples is using write\_canonical/1 as noted by @CapelliC in his StackOverflow [answer](https://stackoverflow.com/a/9341638/1243762).

---

<div class="post-metadata">

### Author: ![Yessir](https://avatars.discourse-cdn.com/v4/letter/y/e36b37/32.png) [@Yessir](https://swi-prolog.discourse.group/u/Yessir)
#### Post date: [September 12, 2020, 11:49am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/8 "2020-09-12T11:49:26Z")

</div>

Yeah it was more like thank you for answering to my topic :).

---

<div class="post-metadata">

### Author: ![Yessir](https://avatars.discourse-cdn.com/v4/letter/y/e36b37/32.png) [@Yessir](https://swi-prolog.discourse.group/u/Yessir)
#### Post date: [September 12, 2020, 11:50am UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/9 "2020-09-12T11:50:40Z")

</div>

I understand that it is impossible to use but I do have more questions now yes 😅.

---

<div class="post-metadata">

### Author: ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)
#### Post date: [September 12, 2020, 3:22pm UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/10 "2020-09-12T15:22:56Z")

</div>

Jacc looks interesting, as well the ‘trick’ Hassan explained.  
Thanks

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [September 12, 2020, 3:28pm UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/11 "2020-09-12T15:28:14Z")

</div>

> [@CapelliC](#):
>
> ‘trick’ Hassan explained.

I can’t find what you are referring to. 🤔

---

<div class="post-metadata">

### Author: ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)
#### Post date: [September 12, 2020, 3:38pm UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/12 "2020-09-12T15:38:48Z")

</div>

You’re right, I was referring to shalllow backtracking, but it seems to be an extension of LALR parsing carefully implemented in Jacc, not a ‘trick’ that could be implemented in YACC. Sorry…

---

<div class="post-metadata">

### Author: ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)
#### Post date: [September 12, 2020, 3:52pm UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/13 "2020-09-12T15:52:08Z")

</div>

If `yfy` were allowed, and you could define `op(500, yfy, myop)`, then `a myop b mypo c` could be parsed as either `myop(a, myop(b,c))` or `myop(myop(a, b), c)`. To avoid this ambiguity, `yfy` is not allowed.  
However, `fy` and `yf` are allowed. Exercise for the reader: what should this do:

```prolog
?- op(500, fy, myop).
?- op(500, yf, myop2).
?- X = myop a myop2, write_canonical(X).

```

---

<div class="post-metadata">

### Author: ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)
#### Post date: [November 13, 2024, 4:40pm UTC](https://swi-prolog.discourse.group/t/yfy-pattern/2923/15 "2024-11-13T16:40:53Z")

</div>

> [@anon37422618](#):
>
> > [@peter.ludemann](#):
> >
> > what should this do:
> > 
> > ?- op(500, fy, myop).  
> > ?- op(500, yf, myop2).  
> > ?- X = myop a myop2, write\_canonical(X).
> 
> No ambiguity? Whats the solution?

ISO core standard says (p18).

> 2 The lterm non-terminal assigns an unambiguous reading to terms such as fy tl yf where the operators have the same priority.

Now I fail to understand that, but they give an example that says `fy 2 yf` should be read as `fy(2 yf)`.

SWI-Prolog does this wrong ☹ The intend is that SWI-Prolog reads the valid ISO cases correctly and accepts more, ideally at some point in time in a way shared by as many as possible other systems. But, I’m not going to make incremental changes (except for fixing ISO compliance) to improve compatibility. That would lead to too much inconvenience. A one shot change to make parsing consistent among would be great.
