3 ms·
First of all, I fully agree that it is hard to start with Prolog initially. However, let us take a look at these particular examples: As to opening a file and
by zmonx 9y ago
First of all, I fully agree that it is hard to start with Prolog initially. However, let us take a look at these particular examples:
As to opening a file and reading its contents as a string:
I find it best to use Ulrich Neumerkel's library(pio) to accomplish this task. Importantly, this lets you apply a DCG to a file in a pure way. I start with a DCG that simply describes a list of characters:
content([]) --> [].
content([C|Cs]) --> [C], content(Cs).
I save this in content.pl, just to have a file to try. I can now apply this DCG to the file contents with phrase_from_file/2:
?- phrase_from_file(content(Cs), 'content.pl').
Cs = [c, o, n, t, e, n, t, '(', '['|...] .
Thus, I have read the file contents as a list of characters, which I can easily convert to anything I want with other predicates.
As to accessing databases: That's quite straight-forward too, in particular if we take into account the following: If you are really using Prolog professionally, then typically Prolog is the database. You simply assert facts, and retrieve them by querying the built-in Prolog database.
Personally, I find Prolog queries much more convenient and also more expressive than SQL, and great fun too.
- BjoernKW 9y ago> If you are really using Prolog professionally, then typically Prolog is the database. You simply assert facts, and retrieve them by querying the built-in Prolog database. That was my personal aha moment with Prolog when I realised that Prolog statements are quite similar to SQL queries in that you declaratively define the results you expect instead of the exact directions describing how to arrive at those results. It's a very powerful and elegant concept.
- Arcsech 9y agoThe problem is "Prolog is the database" is that there isn't a great solution for persisting that database, which is usually what people mean when they talk about databases. Sure, for SWI-Prolog there's Persistency but that a) doesn't help you if your data doesn't fit in memory, and b) doesn't give a lot of the same guarantees as something like Postgres about durability through sudden failures (as far as I can tell, the docs sure don't mention it). It's closer to SQLite than a database you'd use for a production web app.
- zmonx 9y agoYes, I agree with this. In SWI-Prolog, in addition to library(persistency), there are already some published results on transaction support for the internal database, which will give you some features that facilitate such use cases. This is already available and in fact also running for production web apps, but currently only for the RDF database. It is true that if you need more advanced functionality that dedicated database systems readily give you, then you have to wait until such features become available in SWI-Prolog, pay for their implementation, or resort to using an external database for which there often are bindings.