5 ms·
Both languages are relational, which is fairly unique among programming languages. For example you can define a graph in Prolog like this: edge(1, 2). edge
by ngruhn 3y ago
Both languages are relational, which is fairly unique among programming languages. For example you can define a graph in Prolog like this:
edge(1, 2).
edge(2, 3).
edge(3, 4).
These edges are naturally undirected. Similarly, in SQL you can have a table "Edges":
+——-+——-+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
+——-+——-+
which also imposes no strict direction. In non-relational languages you have to define maps for both directions.
However, in Prolog you can easily define something like reachability using recursion:
reachable(V, V).
reachable(V, W) :-
edge(V, U),
reachable(U, W).
In SQL you would have to join the "Edges" table with itself an unknown number of times. I suspect this is possible somehow but definitely less natural.
- Epa095 3y agoThe reachable predicate on your 'naturally undirected edges' treat them as directed;-) I would rather say that your edges are naturally directed, and then you can add a clause to make them undirected.
- j-pb 3y agoI get what you're trying to say, that in Prolog you are able to lookup structures by their shape, and not by some pre-defined path, as in other languages, so you can do both `edge(1, A)` and `edge(A, 1)`. But the graph in your example is still directed, simply by the virtue of predicate arguments being ordered. So your example misses two rules to be correct. reachable(V, W) :- edge(V, W). reachable(V, W) :- edge(W, V). reachable(V, W) :- reachable(V, U), reachable(U, W). Alternatively you could introduce edge(V, W) :- edge(W, V).
- cmrdporcupine 3y ago> In SQL you would have to join the "Edges" table with itself an unknown number of times. In fact fore recent SQL standards have added extensions for these kinds of recursive joins. (And some databases like Oracle, DB2 have had recurse or tclose operators for a long time.) I don't think they're particularly elegant -- they're awkward like most things in SQL because SQL's syntax sucks -- but the ability is in fact there these days.