@ciao-lang/ts-ciao-interface
Version:
Simple Ciao interface for node.
118 lines (99 loc) • 14.8 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>Terms with named arguments -records/feature terms — 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="ExtendLang.html">↑</a><a class="lpdoc-navbutton" href="traits_doc.html">←</a><a class="lpdoc-navbutton" href="fsyntax_doc.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=""><strong>Terms with named arguments -records/feature terms</strong></a></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#Usage and interface">Usage and interface</a></li><li><a href="#Documentation on new declarations">Documentation on new declarations</a></li><li><a href="#Documentation on exports">Documentation on exports</a></li><li><a href="#Other information">Other information</a></li><ul><li><a href="#Using argument names in a toy database">Using argument names in a toy database</a></li><li><a href="#Complete code for the zebra example">Complete code for the zebra example</a></li></ul></ul></div><div class="lpdoc-main"><div id=""><h1>Terms with named arguments -records/feature terms</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#argnames"></a>
<strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Daniel Cabeza" href="ciaosearch.html#Daniel Cabeza">Daniel Cabeza</a>, <a class="lpdoc-idx-anchor" id="Manuel Hermenegildo" href="ciaosearch.html#Manuel Hermenegildo">Manuel Hermenegildo</a>, <a class="lpdoc-idx-anchor" id="Jose F. Morales" href="ciaosearch.html#Jose F. Morales">Jose F. Morales</a>.<p>
<a class="lpdoc-idx-anchor" id="0" href="ciaosearch.html#Naming term arguments"></a> This library package provides syntax which allows accessing term arguments by name (these terms are sometimes also referred to as <a class="lpdoc-idx-anchor" id="1" href="ciaosearch.html#records"><em>records</em></a>, and are also similar to <a class="lpdoc-idx-anchor" id="2" href="ciaosearch.html#feature terms"><em>feature terms</em></a> [<a class="lpdoc-idx-anchor" id="3" href="ciaorefs.html#Ait-KaciPodelskiSmolka92">AKPS92</a>]).<br/><div id="Usage and interface"><h2>Usage and interface</h2><div class="lpdoc-cartouche"><ul><li><strong>Library usage:</strong><br/><tt>:- use_package(argnames).</tt>
or
<tt>:- module(...,...,[argnames]).</tt><li><strong>Exports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Predicates:</em><br/><a class="lpdoc-idx-anchor" id="4" href="#$~/3"><tt>$~/3</tt></a>.
</ul><li><strong>New operators defined:</strong><br/><a class="lpdoc-idx-anchor" id="5" href="ciaosearch.html#$/2"><tt>$/2</tt></a> [150,xfx], <a class="lpdoc-idx-anchor" id="6" href="ciaosearch.html#=>/2"><tt>=>/2</tt></a> [950,xfx], <a class="lpdoc-idx-anchor" id="7" href="#argnames/1"><tt>argnames/1</tt></a> [1150,fx].
<li><strong>New declarations defined:</strong><br/><a class="lpdoc-idx-anchor" id="8" href="#argnames/1"><tt>argnames/1</tt></a>.
<li><strong>Implicit imports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="9" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="10" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="11" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="12" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="13" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>.
</ul></ul></div></div><div id="Documentation on new declarations"><h2>Documentation on new declarations</h2><div><div class="lpdoc-defname"><span class="lpdoc-predtag">DECLARATION</span><a class="lpdoc-idx-anchor" id="argnames/1" href="ciaosearch.html#argnames/1">argnames/1</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl">:- <tt>argnames(ArgNamedPredSpec)</tt>.
</span><p>An <a class="lpdoc-idx-anchor" id="14" href="#argnames/1"><tt>argnames/1</tt></a> declaration assigns names to the argument positions of terms (or literal/goals) which use a certain functor/arity. This allows referring to these arguments by their name rather than by their argument position. Sometimes, argument names may be clearer and easier to remember than argument positions, specially for predicates with many arguments. Also, in some cases this may allow adding arguments to certain predicates without having to change the code that uses them. These terms with named arguments are sometimes also referred to as <a class="lpdoc-idx-anchor" id="15" href="ciaosearch.html#records">records</a>, and are also similar to <a class="lpdoc-idx-anchor" id="16" href="ciaosearch.html#feature terms">feature terms</a> [<a class="lpdoc-idx-anchor" id="17" href="ciaorefs.html#Ait-KaciPodelskiSmolka92">AKPS92</a>]. For example, in order to write a program for the <em>zebra</em> puzzle we might declare:<p><pre class="lpdoc-codeblock">:- use_package([argnames]).
:- argnames house(color, nation, pet, drink, car).
</pre> <p>which first includes the package and then assigns a name to each of the arguments of any term (or literal/goal) with <tt>house/5</tt> as the main functor.<p>For convenience the package extends the built-in <a class="lpdoc-idx-anchor" id="18" href="datafacts_rt.html#data/1"><tt>data/1</tt></a> declaration so that names to arguments can be asigned as with the <a class="lpdoc-idx-anchor" id="19" href="#argnames/1"><tt>argnames/1</tt></a> declaration, as for example:<p><pre class="lpdoc-codeblock">:- data product(id, description, brand, quantity).
</pre> <p>Once an <a class="lpdoc-idx-anchor" id="20" href="#argnames/1"><tt>argnames/1</tt></a> is given, it is possible to use the names to refer to the arguments of any term (or literal/goal) which has the same main functor as that of the term which appears in the <a class="lpdoc-idx-anchor" id="21" href="#argnames/1"><tt>argnames/1</tt></a> declaration. This is done by first writing the functor name, then the infix operator <tt>$</tt>, and then, between curly brackets, zero, one, or more pairs <em>argument-name</em><tt>=></tt><em>argument-value</em>, separated by commas (i.e., the infix operator <tt>=></tt> is used between the name and the value). Again, argument names must be atomic. Argument values can be any term. Arguments which are not specified are assumed to have a value of ``<tt>_</tt>'' (i.e., they are left unconstrained).<p>Thus, after the declaration for <tt>house/5</tt> in the example above, any occurrence in that code of, for example, <tt>house${nation=>Owns_zebra,pet=>zebra}</tt> is exactly equivalent to <tt>house(_,Owns_zebra,zebra,_,_)</tt>. Also, <tt>house${}</tt> is equivalent to <tt>house(_,_,_,_,_)</tt>. The actual zebra puzzle specification might include a clause such as:<p><pre class="lpdoc-codeblock">zebra(Owns_zebra, Drinks_water, Street) :-
Street = [house${},house${},house${},house${},house${}],
member(house${nation=>Owns_zebra,pet=>zebra}, Street),
member(house${nation=>Drinks_water,drink=>water}, Street),
member(house${drink=>coffee,color=>green}, Street),
left_right(house${color=>ivory}, house${color=>green}, Street),
member(house${car=>porsche,pet=>snails}, Street),
...
</pre> <p>Another syntax supported, useful mainly in declarations to avoid specifying the arity, is <tt>house${/}</tt>, which is equivalent in our example to <tt>house/5</tt> (but for data declarations there is a special syntax as we have seen).<p>Any number of <a class="lpdoc-idx-anchor" id="22" href="#argnames/1"><tt>argnames/1</tt></a> declarations can appear in a file, one for each functor whose arguments are to be accessed by name. As with other packages, argument name declarations are <em>local to the file</em> in which they appear. The <a class="lpdoc-idx-anchor" id="23" href="#argnames/1"><tt>argnames/1</tt></a> declarations affect only program text which appears after the declaration. It is easy to make a set of declarations affect several files for example by putting such declarations in a sepatate file which is included by all such files.<p>An <a class="lpdoc-idx-anchor" id="24" href="#argnames/1"><tt>argnames/1</tt></a> declaration does not change in any way the internal representation of the associated terms and does not affect run-time efficiency. It is simply syntactic sugar.<p><p> <strong>Runtime support</strong> <p>It is possible to write pairs with unbound argument names. In that case, runtime information is emitted to resolve the argument name at execution time. </p><ul class="lpdoc-itemize-minus"></ul></div></div><p>
</div><div id="Documentation on exports"><h2>Documentation on exports</h2><div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="$~/3" href="ciaosearch.html#$~/3">$~/3</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>$~(Term,Replacement,NewTerm)</tt>
</span><p><span class="lpdoc-var">NewTerm</span> is as <span class="lpdoc-var">Term</span> but with the arguments specified in <span class="lpdoc-var">Replacement</span> changed (they need to be in argnames syntax). The predicate is in fact virtual, since it is translated by the package to a pair of unifications. For example, given the declaration <tt>:- argnames house(color, nation, pet, drink, car)</tt>, the goal <pre class="lpdoc-codeblock"> $~(House, house${car => seat, pet => mouse}, NewHouse)
</pre> would be compiled to the unifications <pre class="lpdoc-codeblock"> House = house(C,N,_,D,_), NewHouse = house(C,N,mouse,D,seat).
</pre></p><ul class="lpdoc-itemize-minus"></ul></div></div><p>
</div><div id="Other information"><h2>Other information</h2>
<p>Two simple examples of the use of the argnames library package follow.<p><div id="Using argument names in a toy database"><h3>Using argument names in a toy database</h3> <p><pre class="lpdoc-codeblock">:- module(simple_db,_,[argnames,assertions,regtypes]).
:- use_module(library(aggregates)).
:- doc(title,"A simple database application using argument names").
:- data
product( id, description, brand, quantity ).
% ----------------------------------------------------------
product( 1, "Keyboard", "Logitech", 6 ).
product( 2, "Mouse", "Logitech", 5 ).
product( 3, "Monitor", "Philips", 3 ).
product( 4, "Laptop", "Dell", 4 ).
% (${/} must go after argnames)
:- pred product${/}
:: int * string * string * int.
% Compute the stock of products from a given brand.
% Note call to findall is equivalent to: findall(Q,product(_,_,Brand,Q),L).
brand_stock(Brand,Stock) :-
findall(Q,product${brand=>Brand,quantity=>Q},L),
sumlist(L,Stock).
sumlist([],0).
sumlist([X|T],S) :-
sumlist(T,S1),
S is X + S1.
</pre> <p></div><div id="Complete code for the zebra example"><h3>Complete code for the zebra example</h3> <p><pre class="lpdoc-codeblock">:- module(_,zebra/3,[argnames]).
/* There are five consecutive houses, each of a different
color and inhabited by men of different nationalities. They each
own a different pet, have a different favorite drink, and drive a
different car.
1. The Englishman lives in the red house.
2. The Spaniard owns the dog.
3. Coffee is drunk in the green house.
4. The Ukrainian drinks tea.
5. The green house is immediately to the right of the ivory
house.
6. The Porsche driver owns snails.
7. The Masserati is driven by the man who lives in the yellow
house.
8. Milk is drunk in the middle house.
9. The Norwegian lives in the first house on the left.
10. The man who drives a Saab lives in the house next to the man
with the fox.
11. The Masserati is driven by the man in the house next to the
house where the horse is kept.
12. The Honda driver drinks orange juice.
13. The Japanese drives a Jaguar.
14. The Norwegian lives next to the blue house.
The problem is: Who owns the Zebra? Who drinks water?
*/
:- argnames house(color, nation, pet, drink, car).
zebra(Owns_zebra, Drinks_water, Street) :-
Street = [house${},house${},house${},house${},house${}],
member(house${nation => Owns_zebra, pet => zebra}, Street),
member(house${nation => Drinks_water, drink => water}, Street),
member(house${nation => englishman, color => red}, Street),
member(house${nation => spaniard, pet => dog}, Street),
member(house${drink => coffee, color => green}, Street),
member(house${nation => ukrainian, drink => tea}, Street),
left_right(house${color => ivory}, house${color => green}, Street),
member(house${car => porsche, pet => snails}, Street),
member(house${car => masserati, color => yellow}, Street),
Street = [_, _, house${drink => milk}, _, _],
Street = [house${nation => norwegian}|_],
next_to(house${car => saab}, house${pet => fox}, Street),
next_to(house${car => masserati}, house${pet => horse}, Street),
member(house${car => honda, drink => orange_juice}, Street),
member(house${nation => japanese, car => jaguar}, Street),
next_to(house${nation => norwegian}, house${color => blue}, Street).
member(X,[X|_]).
member(X,[_|Y]) :- member(X,Y).
left_right(L,R,[L,R|_]).
left_right(L,R,[_|T]) :- left_right(L,R,T).
next_to(X,Y,L) :- left_right(X,Y,L).
next_to(X,Y,L) :- left_right(Y,X,L).
</pre> <p></div><br/></div></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>