0

I have a rule that matches bc. When I encounter that in a string, I don't want to parse that string, otherwise parse anything else.

% Prolog

bc(B, C) --> [B, C], {
  B = "b",
  C = "c"
}.

not_bc(O) --> [O], % ?! bc(O, C).

% ?- phrase(not_bc(O), "bcdefg").
% false.
% ?- phrase(not_bc(O), "abcdefg").
% O = "a".
% ?- phrase(not_bc(O), "wxcybgz")
% O = "w".
% ?- phrase(not_bc(O), "wxybgz") 
% O = "w".

Simplified version of my problem, hopefully solutions are isomorphic.

Similar to this question: Translation to DCG Semicontext not working - follow on

cokeman19
  • 2,405
  • 1
  • 25
  • 40
eguneys
  • 6,028
  • 7
  • 31
  • 63
  • 1
    The requirement is unclear. Looks like you only care about having the first character, if the string does not *start* with "bc"? – brebs Jul 04 '22 at 16:18
  • I always want the first character, except if the first two characters is "bc". – eguneys Jul 04 '22 at 18:09

2 Answers2

0

In swi-prolog:

process_text(C1) --> [C1, C2], { dif([C1, C2], `bc`) }.

Results:

?- time(phrase(process_text(C), `bca`, _)).
% 11 inferences, 0.000 CPU in 0.000 seconds (79% CPU, 376790 Lips)
false.

?- time(phrase(process_text(C), `bd`, _)).
% 10 inferences, 0.000 CPU in 0.000 seconds (80% CPU, 353819 Lips)
C = 98.

?- time(phrase(process_text(C), `zbcagri4gj40w9tu4tu34ty3ty3478t348t`, _)).
% 10 inferences, 0.000 CPU in 0.000 seconds (80% CPU, 372717 Lips)
C = 122.

A single character, or no characters, are both presumably meant to be failures.

This is nicely efficient, only having to check the first 2 characters.

brebs
  • 3,462
  • 2
  • 3
  • 12
0

An alternative:

process_bc(_) --> "bc", !, { fail }.
process_bc(C) --> [C].

This differs from my other solution in accepting:

?- time(phrase(process_bc(C), `b`, _)).
% 8 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 387053 Lips)
C = 98.
brebs
  • 3,462
  • 2
  • 3
  • 12
  • Can you give some reference about using this technique. I think this is what I need, because it defines the negative condition as a separate rule. – eguneys Jul 04 '22 at 22:29
  • (General) Prolog references: https://swi-prolog.discourse.group/t/useful-prolog-references/1089 – brebs Jul 05 '22 at 07:05