Search code examples
prologswi-prologerlog

Skip/pass non-standard prolog code


I'm developing under SWI-Prolog, but my target is Erlog (https://github.com/rvirding/erlog). I need a way to use non-standard Prolog syntax. Is there a way to write prolog code that will be disregarded by the SWI-compiler i.e. make it invisible.

Here is example how does it look like :

do_stuff(G,Amt) :- ecall(erlog_demo:efunc('Elixir.Blah':stuff({G,Amt})).

I was thinking if there is a way for SWI to skip that and I have another declaration that does nothing.

do_stuff(_,_).

One option probably is to comment it and then use parser to remove the comment before running in Erlog, but this seem cumbersome.

Any other ideas.

======

is_dialect(swi) :- catch(current_prolog_flag(dialect, swi), _, fail).
:- if(is_dialect(swi)).
    do_stuff(_,_).
:- else.
   do_stuff(G,Amt) :- ecall(erlog_demo:efunc('Elixir.Blah':stuff({G,Amt})).
:- endif.

Syntax error: Operator expected


Solution

  • I use this idiom to keep code running in different implementations

    :- if(swi).
    
     gen_hash_lin_probe(Key, HashTable, Value) :-
        arg(_, HashTable, E),
        nonvar(E),
        E = Key-Value.
    
    :- elif(yap).
    
     gen_hash_lin_probe(Key, HashTable, Value) :-
        HashTable =.. [htlp|Args],
        nth1(_, Args, E),
        nonvar(E),
        E = Key-Value.
    
    :- endif.
    

    where predicates swi/0 or yap/0 are imported from this module(prolog_impl)

    :- module(prolog_impl, [swi/0, yap/0, prolog_impl/1]).
    
    swi :- prolog_impl(swi).
    yap :- prolog_impl(yap).
    
    prolog_impl(K) :-
        F =.. [K,_,_,_,_],
        current_prolog_flag(version_data, F).