UNPKG

@ciao-lang/ts-ciao-interface

Version:

Simple Ciao interface for node.

85 lines (68 loc) 5.18 kB
<!DOCTYPE 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 &mdash; 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">&#9776;</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">&#x2191;</a><a class="lpdoc-navbutton" href="persdb_rt.html">&#x2190;</a><a class="lpdoc-navbutton" href="datadir.html">&#x2192;</a><a class="lpdoc-navbutton" href="ciaosearch.html">&#x1F50D;</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> &raquo;<br/> </li><li><a href="ExtendLang.html">PART IV - Language extensions</a> &raquo;<br/> </li><li><a href="persdb_rt.html">Persistent predicate database</a> &raquo;<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 &quot;db&quot; where the %% persistence sets of the persistent predicates are stored: persistent_dir(db,&apos;./&apos;). %% 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 &quot;db&quot; asserta_fact(persistent_dir(db,&apos;./&apos;)), % 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,&apos;./pers&apos;). :- persistent(queue/1, queue_dir). queue(first). queue(second). main:- write(&apos;Action ( in(Term). | slip(Term) | out. | list. | halt. ): &apos;), read(A), ( handle_action(A) -&gt; main ; write(&apos;Unknown command.&apos;), 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)) -&gt; write(&apos;Out &apos;), write(Term) ; write(&apos;FIFO empty.&apos;) ), nl. handle_action(list) :- findall(Term,queue(Term),Terms), write(&apos;Contents: &apos;), 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>