@ciao-lang/ts-ciao-interface
Version:
Simple Ciao interface for node.
85 lines (68 loc) • 5.18 kB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta name="theme-color" content="#273f79"/><link rel="stylesheet" href="lpdoc.css" type="text/css"/><script type="text/javascript" src="lpdoc.js"></script><title>Using the persdb library — The Ciao System v1.22</title></head><body><div class="lpdoc-page fixleftbar"><a href="#" id="sidebar-toggle-button" class="lpdoc-navbutton"><span id="sidebar-button-arrow">☰</span></a><div id="sidebar" class="lpdoc-sidebar"><div style="height: 40px; margin-left: auto; margin-right: auto"><img src="ciao-logo_autofig.png" width=auto height=100%></div><div class="lpdoc-nav"><span class="lpdoc-on-right"><a class="lpdoc-navbutton" href="persdb_rt.html">↑</a><a class="lpdoc-navbutton" href="persdb_rt.html">←</a><a class="lpdoc-navbutton" href="datadir.html">→</a><a class="lpdoc-navbutton" href="ciaosearch.html">🔍</a></span><span><a href="ciaofulltoc.html">TOC</a></span></div><hr></hr><ul class="lpdoc-itemize-sectpath"><li><a href="ciao.html">The Ciao System</a> »<br/> </li><li><a href="ExtendLang.html">PART IV - Language extensions</a> »<br/> </li><li><a href="persdb_rt.html">Persistent predicate database</a> »<br/> </li><li><a href=""><strong>Using the persdb library</strong></a></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#An example of persistent predicates (static version)">An example of persistent predicates (static version)</a></li><li><a href="#An example of persistent predicates (dynamic version)">An example of persistent predicates (dynamic version)</a></li><li><a href="#A simple application / a persistent queue">A simple application / a persistent queue</a></li></ul></div><div class="lpdoc-main"><div id=""><h1>Using the persdb library</h1>
<strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="The Ciao Development Team" href="ciaosearch.html#The Ciao Development Team">The Ciao Development Team</a>.<p>
Through the following examples we will try to illustrate the two mains ways of declaring and using persistent predicates: statically (the preferred method) and dynamically (necessary when the new persistent predicates have to be defined at run-time). The final example is a small application implementing a simple persistent queue.<p><p><div id="An example of persistent predicates (static version)"><h2>An example of persistent predicates (static version)</h2> <p><pre class="lpdoc-codeblock">:- module(example_static,[main/0],[persdb]).
:- use_module(library(aggregates)).
:- use_module(library(write)).
:- use_module(library(read)).
%% Declare the directory associated to the key "db" where the
%% persistence sets of the persistent predicates are stored:
persistent_dir(db,'./').
%% Declare a persistent predicate:
:- persistent(bar/1, db).
%% Read a term, storing it in a new fact of the persistent predicate
%% and list all the current facts of that predicate
main :-
read(X),
assertz_fact(bar(X)),
findall(Y,bar(Y),L),
write(L).
erase_one :-
retract_fact(bar(_)).
erase_all :-
retractall_fact(bar(_)).
</pre> <p></div><div id="An example of persistent predicates (dynamic version)"><h2>An example of persistent predicates (dynamic version)</h2> <p><pre class="lpdoc-codeblock">:- module(example_dynamic, [main/1], [persdb, dynamic]).
:- use_module(library(aggregates)).
:- use_module(library(write)).
:- impl_defined(bar/1).
main([X]):-
% Declare the directory associated to the key "db"
asserta_fact(persistent_dir(db,'./')),
% Declare the predicate bar/1 as dynamic (and data) at run-time
data(bar/1),
% Declare the predicate bar/1 as persistent at run-time
make_persistent(bar/1, db),
assertz_fact(bar(X)),
findall(Y, bar(Y), L),
write(L).
</pre> <p></div><div id="A simple application / a persistent queue"><h2>A simple application / a persistent queue</h2> <pre class="lpdoc-codeblock">:- module(queue, [main/0],[persdb]).
:- use_module(library(streams)).
:- use_module(library(read)).
:- use_module(library(write)).
:- use_module(library(aggregates)).
persistent_dir(queue_dir,'./pers').
:- persistent(queue/1, queue_dir).
queue(first).
queue(second).
main:-
write('Action ( in(Term). | slip(Term) | out. | list. | halt. ): '),
read(A),
( handle_action(A)
-> main
; write('Unknown command.'), nl, main ).
handle_action(end_of_file) :-
halt.
handle_action(halt) :-
halt.
handle_action(in(Term)) :-
assertz_fact(queue(Term)).
handle_action(slip(Term)) :-
asserta_fact(queue(Term)).
handle_action(out) :-
( retract_fact(queue(Term))
-> write('Out '), write(Term)
; write('FIFO empty.') ),
nl.
handle_action(list) :-
findall(Term,queue(Term),Terms),
write('Contents: '), write(Terms), nl.
</pre> <p></div><br/></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>