Search code examples
prologsicstus-prolog

How to simulate atom_to_term(+Atom, -Term, -Bindings) of SWI-Prolog in SICStus Prolog?


I am using SICStus Prolog to write a Dali agent and I need to convert an atom to a term but I can't use atom_to_term which is built in in SWI-Prolog


Solution

  • Use library(codesio):

    | ?- use_module(library(codesio)).
    yes
    ?- set_prolog_flag(double_quotes,codes).
    true.
    | ?- read_from_codes("a(X,Y).",T).     
    
    T = a(_A,_B) ? yes
    | ?- read_term_from_codes("a(X,Y).",T,[variable_names(VN_list)]).
    T = a(_A,_B),
    VN_list = ['X'=_A,'Y'=_B] ?
    

    In addition to that, you need atom_codes/2 which is ISO.

    For more complex operations, you can open a stream with open_codes_stream/2. Which needs to be closed with close/1.