Search code examples
prologswi-prolog

Prolog - How to set timestamp as an argument in a predicate?


Is there any way to use get_time(T) [which is built-in function] as an argument when I assert new facts to DB? (I just want to compare between facts assertion time).

Code:

:- dynamic start/2.
    
start_interval(A) :- start(A, _), !, false.
start_interval(A) :- assert(start(A, get_time(T))).

Run Example:

Warning: c:/users/*****/desktop/prolog/4.pl:6:
Warning:    Singleton variables: [T]
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.1)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. For legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- start_interval(1).
true.

?- start_interval(2).
true.

?- listing(start).
:- dynamic start/2.

start(1, get_time(_)).
start(2, get_time(_)).

true.

?- get_time(T).
T = 1598718310.038124.

Instead of "start(#, get_time(_))", I would like to get to timestamp, which was made when I called start_interval(Num) at first. (You can see also the output of get_time(T) when I call it)

Is it possible? Maybe there is another way to compare between facts assertion time?


Solution

  • You just have to actually call (whatever is behind) the get_time(T) expression (nominally a predicate, but not really, as its behaviour depends on the exact moment at which it is called. Very non-logical: we are in the real of I/O).

    As you write it, it remains an un-called syntactic element, standing literally for itself.

    (Also, use assertz/1 in preference to the oldish assert/1):

    So:

    :- dynamic start/2.
    
    start_interval(A) :- start(A, _), !, false.
    start_interval(A) :- get_time(T), assertz(start(A, T)).
    

    Then:

    ?- start_interval(1).
    true.
    
    ?- start(A,T).
    A = 1,
    T = 1598726506.9420764.