How can I simulate a soft cut in Prolog?

How can I simulate soft cut i * β†’ T; E in ISO Prolog? I have side effects, so I can’t name him several times.

Except for the last requirement, I think the following definition works:

if_(I, T, E) :-
    not(not(I)) ->
    call((I, T));
    call((not(I), E)).

(I actually use the XSB prologue; an XSB solution will also be useful to me.)

+4
source share
3 answers

, ISO Prolog XSB, . , " ". , XSB ISO , .

:- dynamic(if_counter/1).

if_counter(0).

:- dynamic(no_if_answer/1).
if(If_0, Then_0, Else_0) :-
   once(if_counter(Id)),
   Idx is Id+1,
   (  Idx > Id -> true
   ;  throw(error(representation_error(max_integer),
               'XSB misses ISO conforming integers'))
   ),
   retractall(if_counter(_)),
   asserta(if_counter(Idx)),
   asserta(no_if_answer(Id)),
   (  If_0,
      retractall(no_if_answer(Id)),
      Then_0
   ;  retract(no_if_answer(Id)) ->
      Else_0
   ).

, If_0 . , , retract(no_if_answer(Id)) , retractall(no_if_answer(Id)), , . EDIT: , , , , , , .

, , . :

| ?- if(X = a, T = equal, T = not_equal).

X = a
T = equal;

no

! , , X = b:

| ?- X = b, if(X = a, T = equal, T = not_equal).

X = b
T = not_equal;

no
| ?- if(X = a, T = equal, T = not_equal), X = b.

no % bad!!

( , , ).

, , , if_/3. . library(reif) SICStus, :

| ?- if_(X = a, T = equal, T = not_equal).
X = a,
T = equal ? ;
T = not_equal,
prolog:dif(X,a) ? ;
no
+2

, ... , , ( ), If . -, ? , ISO-Prolog , stream-term, (ab) :

if(If, Then, Else) :-
    open(., read, S),
    (
        If,
        close(S, [force(true)]),
        Then
    ;
        catch(close(S), error(existence_error(stream,_),_), fail),   % fail if already closed
        Else
    ).

, , If , else. , ECLiPSe. ( XSB) ( ISO), .

, position, , ! , XSB:

if(If, Then, Else) :-
    % open('ReadableAndNonemptyFile', read, S),      % general ISO
    open(atom(a), read, S),                          % XSB (needs no file)
    stream_property(S, position(Zero)),
    get_char(S, _),
    (
        catch(If, Ball, (close(S),throw(Ball))),
        set_stream_position(S, Zero),
        Then

    ; stream_property(S, position(Zero)) ->
        close(S),
        fail
    ;
        close(S),
        Else
    ).

, open(atom(...),...) - XSB-, ISO-Prolog ...

+2

: , . ( , , coinduction Logtalk), , , Prolog , , ISO Prolog. , Prolog . , , SWI-Prolog, YAP, SICStus Prolog, GNU Prolog, CxProlog, ECLiPSe, Jekejeke Prolog Ciao. , , , *->/2, (SICStus Prolog Ciao) if/3(YAP has both). In addition, semantics vary in angular cases (the Logtalk distribution includes the Prolog matching package, which also checks the variation *->/2).

+1
source

Source: https://habr.com/ru/post/1660863/


All Articles