4 ms·
Can SQL do resolution or backtracking similar to Prolog, I wonder.
by gtycomb 10y ago
Can SQL do resolution or backtracking similar to Prolog, I wonder.
- benjiweber 10y agoSQL gives you all the solutions to query by default unless you limit them right? A relation (table) in SQL is conceptually a predicate that holds for all tuples (rows) therein. You can AND the predicates with "NATURAL JOIN". You can OR them with "UNION" e.g. this example https://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/search.html https://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpa... becomes something like this in SQL. example=# select * from red; item --------- apple_1 block_1 car_27 (3 rows) example=# select * from car; item ----------- desoto_48 edsel_57 (2 rows) example=# select * from blue; item ---------- flower_3 glass_9 honda_81 (3 rows) example=# select * from bike; item ---------- iris_8 my_bike honda_81 (3 rows) example=# CREATE view fun as (select item from red NATURAL JOIN car) UNION (select item from blue NATURAL JOIN bike); CREATE VIEW example=# SELECT * FROM fun; item ---------- honda_81 (1 row)
- tom_mellior 10y agoThis is all true, but only for the small subset of Prolog that happens to be introduced on that web page. This analogy breaks down as soon as you have more complex data, not just atoms, or as soon as you introduce recursive rules. Prolog tutorials really do Prolog a disservice by introducing it as a database query language, which is then misunderstood (or misremembered) by many as "it's only a database query language".
- benjiweber 10y agoSQL can do recursive rules too. SQL tutorials often do SQL a disservice by introducing it as a database query language rather than relational algebra :) example=# select * from move; place | method | newplace ---------+--------+---------- home | taxi | halifax halifax | train | gatwick gatwick | plane | rome (3 rows) example=# create view on_route as with recursive on_route(place, newplace, length, path) as (select place, newplace, 1 as length, place as path FROM move UNION select on_route.place, move.newplace, on_route.length + 1, path || '->' || move.place as path FROM move JOIN on_route ON on_route.newplace = move.place) SELECT place, newplace, length, path || '->' || newplace as path FROM on_route; CREATE VIEW example=# select distinct true from on_route WHERE place = 'home' and newplace = 'rome'; bool ------ t (1 row) example=# select path from on_route WHERE place = 'home' and newplace = 'rome'; path ------------------------------ home->halifax->gatwick->rome
- tom_mellior 10y agoHa, excellent :-) I learned something, thanks. (The greater point still stands, terms and unification and all that.)