Search code examples
prologswi-prologdeclarativeprolog-toplevel

How to use variables in Prolog query shell?


I know that I can use variables in Prolog shell (something like using '$' character, I think...but I don't remember...)

If I execute the following query it seems to work fine:

?- leggiFile('dataggare.txt', ListaTesto), tokenizzaLista(ListaTesto, TokenizedList, 1).
ListaTesto = [68, 117, 114, 97, 110, 116, 101, 32, 105|...],
TokenizedList = [t(1, [68, 117, 114, 97, 110, 116, 101]), t(-1, [32]), t(2, [105, 108]), t(-1, [32]), t(3, [77, 101, 100|...]), t(-1, [44]), t(-1, [32]), t(4, [...|...]), t(..., ...)|...] 

But if I try to execute the two query leggiFile/2 and tokenizzaLista/2 separately, in this way go into error:

?- leggiFile('dataggare.txt', ListaTesto).
ListaTesto = [68, 117, 114, 97, 110, 116, 101, 32, 105|...].

?- tokenizzaLista($ListaTesto, TokenizedList, 1).
ERROR: variable `ListaTesto' does not exist

Why? it seems to me very strange. What am I missing?


Solution

  • ?- open('uty.pl',read,S).
    S = <stream>(0x236d4d0).
    
    ?- read($S,K).
    K = (:-module(uty, [atoi//2, cache_file/2, cache_path/4, call_nth/2, cat/2, count_solutions/2, ... / ...|...])).
    
    ?- read($S,K).
    K = (:-reexport(nb_uty, [ (<<)/2, (>>)/2, ++ / 2, (**)/2])).
    ...
    

    but I'm not sure if garbage collection could disturb...

    Documentation states

    Bindings resulting from the successful execution of a top-level goal are asserted in a database if they are not too large.