[racket] Datalog: summing
Some errata below..
On Mon, Sep 19, 2011 at 14:55, Wojciech Kaczmarek <wojtekk at kofeina.net>wrote:
> Just for the record:
>
> 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:
>
I see I stated the above in a rush.
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.
So the code below is not needed; instead, you can look at it as a simple
example of Racklog pattern matching of predicate terms.
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.
> (define %sum
> (%rel (x y xs acc)
> [('[] acc) (%= 0 acc)]
> [((cons x xs) acc) (%sum xs y) (%is acc (+ x y))]))
>
> You can expect it will be slower than post-processing the resulting list in
> plain Racket.
>
> 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.
>
>
> On Mon, Sep 19, 2011 at 14:40, Wojciech Kaczmarek <wojtekk at kofeina.net>wrote:
>
>> In typical Prolog the simplest way would be to use aggregate:
>>
>>
>> http://www.swi-prolog.org/pldoc/doc_for?object=section(2,'A.1',swi('/doc/Manual/aggregate.html'))
>>
>>
>> <http://www.swi-prolog.org/pldoc/doc_for?object=section(2,'A.1',swi('/doc/Manual/aggregate.html'))>
>>
>> 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.
>>
>> Similar for Datalog - you can generate list by Datalog querying, then sum
>> via regular Racket functions.
>>
>>
>>
>> On Mon, Sep 19, 2011 at 11:25, Mark Carter <mcturra2000 at yahoo.co.uk>wrote:
>>
>>> How do I create summing rules?
>>>
>>> Suppose I have the following facts:
>>>
>>> post(cash, 26) .
>>> post(cash, -16) .
>>> post(cash, 50) .
>>> post(beer, 20) .
>>>
>>>
>>> How do I create a rule "bal"
>>> bal(X, ???)
>>> such that the ??? gives me the sum of the second arguments in post
>>> satisfying the first argument X?
>>>
>>> Also, is there a way of writing floats, or is that out of the question?
>>>
>>>
>>> _________________________________________________
>>> For list-related administrative tasks:
>>> http://lists.racket-lang.org/listinfo/users
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110919/daaf7c6c/attachment.html>