Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 698
sten Avatar asked Jan 08 '23 09:01

sten


2 Answers

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).
like image 140
CapelliC Avatar answered Jan 16 '23 20:01

CapelliC


One closing parenthesis is missing in the "else" branch.

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

Simply count the number of opening parentheses. When . (period, full-step) is reached the difference count of opening vs closing must amount to exactly zero.

Hope that helps!

like image 45
repeat Avatar answered Jan 16 '23 18:01

repeat