UNPKG

@ciao-lang/ts-ciao-interface

Version:

Simple Ciao interface for node.

107 lines (78 loc) 8.91 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>Constraint programming over rationals &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="ExtendLang.html">&#x2191;</a><a class="lpdoc-navbutton" href="actmod_rt.html">&#x2190;</a><a class="lpdoc-navbutton" href="clpr_doc.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=""><strong>Constraint programming over rationals</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="#Other information">Other information</a></li><ul><li><a href="#Some CLP(Q) examples">Some CLP(Q) examples</a></li><li><a href="#Meta-programming with CLP(Q)">Meta-programming with CLP(Q)</a></li></ul></ul></div><div class="lpdoc-main"><div id=""><h1>Constraint programming over rationals</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#clpq"></a> <strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Christian Holzbaur" href="ciaosearch.html#Christian Holzbaur">Christian Holzbaur</a>, <a class="lpdoc-idx-anchor" id="Daniel Cabeza" href="ciaosearch.html#Daniel Cabeza">Daniel Cabeza</a>, <a class="lpdoc-idx-anchor" id="Samir Genaim" href="ciaosearch.html#Samir Genaim">Samir Genaim (Meta-programming predicates)</a>.<p> <div class="lpdoc-alert"> <strong>Warning</strong>: This package is currently being adapted to the new characteristics of the Ciao module system. This new version works right now with limitations, but it is under further development at the moment. Use with (lots of) caution. </div> <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(clpq).</tt> or <tt>:- module(...,...,[clpq]).</tt><li><strong>New operators defined:</strong><br/><a class="lpdoc-idx-anchor" id="0" href="ciaosearch.html#.=./2"><tt>.=./2</tt></a> [700,xfx], <a class="lpdoc-idx-anchor" id="1" href="ciaosearch.html#.&lt;&gt;./2"><tt>.&lt;&gt;./2</tt></a> [700,xfx], <a class="lpdoc-idx-anchor" id="2" href="ciaosearch.html#.&lt;./2"><tt>.&lt;./2</tt></a> [700,xfx], <a class="lpdoc-idx-anchor" id="3" href="ciaosearch.html#.=&lt;./2"><tt>.=&lt;./2</tt></a> [700,xfx], <a class="lpdoc-idx-anchor" id="4" href="ciaosearch.html#.&gt;./2"><tt>.&gt;./2</tt></a> [700,xfx], <a class="lpdoc-idx-anchor" id="5" href="ciaosearch.html#.&gt;=./2"><tt>.&gt;=./2</tt></a> [700,xfx]. <li><strong>Implicit imports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="6" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="7" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="8" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="9" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="10" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>. </ul></ul></div></div><div id="Other information"><h2>Other information</h2> <p><div id="Some CLP(Q) examples"><h3>Some CLP(Q) examples</h3> <p>(Other examples can be found in the source and library directories.)<p><ul> <li>&apos;Reversible&apos; Fibonacci (clpq): </ul> <p><pre class="lpdoc-codeblock">:- module(fib_q, [fib/2], []). :- use_package(clpq). fib(X,Y):- X .=. 0, Y .=. 0. fib(X,Y):- X .=. 1, Y .=. 1. fib(N,F) :- N .&gt;. 1, N1 .=. N - 1, N2 .=. N - 2, fib(N1, F1), fib(N2, F2), F .=. F1+F2. </pre> <p> <ul> <li>Matrix multiplication (clpq): </ul> <p><pre class="lpdoc-codeblock">:- use_package(clpq). mmultiply([],_,[]). mmultiply([V0|Rest], V1, [Result|Others]):- mmultiply(Rest, V1, Others), multiply(V1,V0,Result). multiply([],_,[]). multiply([V0|Rest], V1, [Result|Others]):- multiply(Rest, V1, Others), vmul(V0,V1,Result). vmul([],[],0). vmul([H1|T1], [H2|T2], Result):- vmul(T1,T2, Newresult), Result .=. H1*H2+Newresult. matrix(1,[[1,2,3,4,5],[4,0,-1,5,6],[7,1,-2,8,9],[-1,0,1,3,2],[1,5,-3,2,4]]). matrix(2,[[3,2,1,0,-1],[-2,1,3,0,2],[1,2,0,-1,5],[1,3,2,4,5],[-5,1,4,2,2]]). %% Call with: ?- go(M). go(M):- matrix(1,M1), matrix(2,M2), mmultiply(M1, M, M2). </pre> <p> <ul> <li>Queens (clpq): </ul> <p><pre class="lpdoc-codeblock">:- use_package(clpq). :- use_module(library(lists), [member/2]). queens(N, Qs) :- constrain_values(N, N, Qs), place_queens(N, Qs). constrain_values(0, _N, []). constrain_values(N, Range, [X|Xs]) :- N .&gt;. 0, X .&gt;. 0, X .=&lt;. Range, N1 .=. N - 1, constrain_values(N1, Range, Xs), no_attack(Xs, X, 1). no_attack([], _Queen, _Nb). no_attack([Y|Ys], Queen, Nb) :- Queen .&lt;&gt;. Y+Nb, Queen .&lt;&gt;. Y-Nb, Nb1 .=. Nb + 1, no_attack(Ys, Queen, Nb1). place_queens(0, _). place_queens(N, Q) :- N &gt; 0, member(N, Q), N1 is N-1, place_queens(N1, Q). </pre> <p> </div><div id="Meta-programming with CLP(Q)"><h3>Meta-programming with CLP(Q)</h3> <p>The implementation of CLP(Q) in Ciao compiles the constraints in the program to a sequence of calls to the underlying constraints solver (at compile-time). This results in efficient implementation, since the structure of the constraints is processed only at compile-time, but requires the constraints to be known at static time which can be a limitation for metaprogramming-based applications such as static program analyzers. For example, the call:<p><pre class="lpdoc-codeblock"> ?- X=(A+B), Y=(C-D), X .&gt;. Y. no </pre> <p>fails because <span class="lpdoc-var">X</span> <a class="lpdoc-idx-anchor" id="11" href="ciaosearch.html#.&gt;."><tt>.&gt;.</tt></a> <span class="lpdoc-var">Y</span> is translated first to a sequence of calls that require (when they invoked) <span class="lpdoc-var">X</span> and <span class="lpdoc-var">Y</span> to be either numbers or free variables. To overcome this limitation, you can use <a class="lpdoc-idx-anchor" id="12" href="ciaosearch.html#clpq_meta/1"><tt>clpq_meta/1</tt></a> which delays the translation of the constraints from compile-time to run-time (i.e., when <a class="lpdoc-idx-anchor" id="13" href="ciaosearch.html#clpq_meta/1"><tt>clpq_meta/1</tt></a> is called), For example:<p><pre class="lpdoc-codeblock">?- X=(A+B),Y=(C-D), clpq_meta([X .&gt;. Y]). X = A+B, Y = C-D, C.&lt;.D+A+B ? </pre> <p>The argument of <a class="lpdoc-idx-anchor" id="14" href="ciaosearch.html#clpq_meta/1"><tt>clpq_meta/1</tt></a> accepts a goal or lists of goals, where each goal is limited to conjunctions, disjunctions, or CLP(Q) constraints. Other operations on constraints which are extensively used in meta-programming, in particular in static program analysis, are <em>projection</em> and <em>entailment check</em>. The projection operation restricts the constraints (that are available in the store) to a given set of variables and turns the answer into terms. You can use the multifile predicate <a class="lpdoc-idx-anchor" id="15" href="ciaosearch.html#dump_constraints/3"><tt>dump_constraints/3</tt></a> for that purpose:<p><pre class="lpdoc-codeblock">?- A .&gt;. C, C .&gt;. B, dump_constraints([A,B],[X,Y],Cs). Cs = [X.&gt;.Y], C.&gt;.B, C.&lt;.A ? ?- C=(B+D), clpq_meta([A .&gt;. C, D .&gt;. 0]), dump_constraints([A,B],[X,Y],Cs). C = B+D, Cs = [Y.&lt;.X], D.&lt;. -B+A, D.&gt;.0 ? </pre> <p>The <em>entailment check</em> is used to check if a list of constrains is entailed by the store. You can use the predicate <a class="lpdoc-idx-anchor" id="16" href="ciaosearch.html#clpq_entailed/1"><tt>clpq_entailed/1</tt></a> for that purpose:<p><pre class="lpdoc-codeblock">?- A .&gt;. C, C .&gt;. B, B .&gt;. D, clpq_entailed([ A .&gt;. B, A .&gt;. D]). B.&gt;.D, C.&gt;.B, C.&lt;.A ? yes ?- A .&gt;=. B, clpq_entailed([ A .&gt;. B ]). no </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>