Some errata below..<br><br><div class="gmail_quote">On Mon, Sep 19, 2011 at 14:55, Wojciech Kaczmarek <span dir="ltr"><<a href="mailto:wojtekk@kofeina.net">wojtekk@kofeina.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Just for the record:<div><br></div><div>If you'd need to stay in the "Prolog" world with your summing, for example in order to use summing result in another predicate, here's the Racklog version:</div></blockquote>
<div><br></div><div>I see I stated the above in a rush.</div><div><br></div><div>There's no need to craft own predicate which behaves like typical deterministic function only in order to use it in another predicate. Other predicate which would use %sum, could also be using Racket's foldl. You need own predicates when there can be some varying in which terms are free variables and which are bound in the call; if a predicate imitates a function which just binds the result to its last term, you can use Racket function instead.</div>
<div><br></div><div>So the code below is not needed; instead, you can look at it as a simple example of Racklog pattern matching of predicate terms.</div><div><br></div><div>I'm sorry for the confusion. I guess it takes a little time to get used to thinking when we're on the "prolog" (backtracing) side and where we're back using the normal evaluation model.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div></div><div><div>(define %sum</div><div> (%rel (x y xs acc)</div><div> [('[] acc) (%= 0 acc)]</div>
<div> [((cons x xs) acc) (%sum xs y) (%is acc (+ x y))]))</div></div><div><br></div><div>You can expect it will be slower than post-processing the resulting list in plain Racket.</div>
<div><br></div><div>One mode of operation which generally seems reasonable is to perform any complicated queries in Racklog, then call bag-of/set-of to turn all "backtractable" results into plain list, then process the list in Racket.</div>
<div><div></div><div class="h5">
<div><br><br><div class="gmail_quote">On Mon, Sep 19, 2011 at 14:40, Wojciech Kaczmarek <span dir="ltr"><<a href="mailto:wojtekk@kofeina.net" target="_blank">wojtekk@kofeina.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In typical Prolog the simplest way would be to use aggregate: <div><br></div><div><a href="http://www.swi-prolog.org/pldoc/doc_for?object=section(2,'A.1',swi('/doc/Manual/aggregate.html'))" target="_blank">http://www.swi-prolog.org/pldoc/doc_for?object=section(2,'A.1',swi('/doc/Manual/aggregate.html'))</a></div>
<div><br><a href="http://www.swi-prolog.org/pldoc/doc_for?object=section(2,'A.1',swi('/doc/Manual/aggregate.html'))" target="_blank"></a></div><div><br></div><div>In Racklog you can simply use bag-of to generate a list of your facts satisfying some predicate, then sum it on the Racket side eg. using foldl.</div>
<div><br></div><div>Similar for Datalog - you can generate list by Datalog querying, then sum via regular Racket functions.</div><div><div></div><div><div><br></div><div><br></div><div><br></div><div><div class="gmail_quote">
On Mon, Sep 19, 2011 at 11:25, Mark Carter <span dir="ltr"><<a href="mailto:mcturra2000@yahoo.co.uk" target="_blank">mcturra2000@yahoo.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">How do I create summing rules?<br>
<br>
Suppose I have the following facts:<br>
<br>
post(cash, 26) .<br>
post(cash, -16) .<br>
post(cash, 50) .<br>
post(beer, 20) .<br>
<br>
<br>
How do I create a rule "bal"<br>
bal(X, ???)<br>
such that the ??? gives me the sum of the second arguments in post satisfying the first argument X?<br>
<br>
Also, is there a way of writing floats, or is that out of the question?<br>
<br>
<br>
_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://lists.racket-lang.org/listinfo/users" target="_blank">http://lists.racket-lang.org/listinfo/users</a><br>
<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br>