Backward chaining?

83 views
Skip to first unread message

François Royer

unread,
Jun 2, 2016, 1:34:25 PM6/2/16
to pyDatalog
Hello
Thank you for the great work! I am coming to pydatalog from core.logic in the clojure world and starting to like it.
One thing I was wondering was how to specify the resolution method, i.e. being datalog this should be forward chaining, right?
So, given the query, how can one discover the relations leading to that goal, i.e. run the rules backward?
Or maybe I missed something.
Cheers
François

François Royer

unread,
Jun 2, 2016, 2:00:42 PM6/2/16
to pyDatalog
Adding a few details...
This is the use case I am trying to recreate:

# Predicate that translates English to French by making an RPC. 

ToFrench(english) = french :- RPC.Call{ host: "... elided ...", service: "TranslationService", method: "Translate", 
input: {text: english, src_lang: ’ENGLISH, tgt_lang: ’FRENCH}, 
output: {translated_text: french, .._}}; 

EnglishCorpus("Shall I compare thee to a summer’s day?"); 
EnglishCorpus("Thou art more lovely and more temperate:"); 

FrenchCorpus(french) :- Corpus(english), french == ToFrench(english); 

? FrenchCorpus(french); 

# french: "Dois-je te comparer à un jour d’été?" 
# french: "Tu es plus belle et plus tempéré."

François Royer

unread,
Jun 2, 2016, 2:53:12 PM6/2/16
to pyDatalog
OK, auto-answering...
It turns out I misunderstood the resolution mechanism. 

A working example:

terms('weather,getweather, url, city, T,W, float')
In [20]:
def getweather(city):
    r=requests.get('https://www.wunderground.com/fr/'+city)
    tree = lxml.html.fromstring(r.text)
    sel = CSSSelector('#curTemp > span > span.wx-value')
    results = sel(tree)
    match = results[0]
    return match.text
In [39]:
(weather[X]==W) <= (T==getweather(X)) & (W=='Unknown')
(weather[X]==W) <= (T==getweather(X)) & ( float(T) > 20.0 ) & (W=='Warm')
Out[39]:
weather[1]==(*,X,W) <= ==(T,('<function getweather
In [40]:
print(weather['toulouse']==Y)
Y   
----
Warm
Reply all
Reply to author
Forward
0 new messages