
Prolog Web Applications (2016) - ArneVogel
https://www.metalevel.at/prolog/web
======
triska
Thank you very much for your interest!

Prolog is ideally suited for web applications: HTML and XML documents are
readily represented as Prolog _terms_ and can be reasoned about very
conveniently and efficiently.

For instance, let's use Scryer Prolog to fetch all article titles from the HN
front page.

Put the following in hn.pl:

    
    
       :- use_module(library(http/http_open)).
       :- use_module(library(sgml)).
       :- use_module(library(xpath)).
       :- use_module(library(format)).
    

Then consult the file with:

    
    
       $ scryer-prolog hn.pl
    

and then post:

    
    
       ?- http_open("https://news.ycombinator.com", S, []),
          load_html(stream(S), DOM, []),
          xpath(DOM, //a(@class="storylink",text), E),
          portray_clause(E),
          false.
    

Yielding:

    
    
       "Apple announces it will switch to its own processors for future Macs".
       "macOS Big Sur Preview".
       "Most employees of NYT won\x2019\t be required back in physical offices until 2021".
       etc.
    

Scryer Prolog is a modern Rust-based Prolog system that conforms to the Prolog
ISO standard and provides several libraries for web applications:
[https://github.com/mthom/scryer-prolog](https://github.com/mthom/scryer-
prolog). A key attraction of Scryer Prolog is its compact internal
representation of strings as lists of characters, making the system ideally
suited for the use case Prolog was designed for: Convenient and efficient text
analysis, which is also required in many web applications.

Here is a short video about this topic:

[https://www.metalevel.at/prolog/videos/web_scraping](https://www.metalevel.at/prolog/videos/web_scraping)

Enjoy!

~~~
kqr
While this was instructive, tying together curl and xpath is not really the
sort of thing I'd be worried about when picking Prolog for something like
this. Any language looks good when all the heavy lifting is done by other
libraries.

How would I run this across multiple cores? Handle exponential backoff for
retries? Measure the response time for each submission? Store the result in a
database?

~~~
rscho
While Mr. Triska evangelizes for Scryer (which I really hope will succeed in
its endeavors), the more mature web framework still is in SWI prolog.

As for the database, this is where prolog shines: prolog _is_ the database.
You store data in the internal facts database.

Concurrency is another prolog strength. For example: [https://www.swi-
prolog.org/pack/list?p=spawn](https://www.swi-prolog.org/pack/list?p=spawn)

See also this very nice talk:
[https://www.google.com/url?sa=t&source=web&rct=j&url=https:/...](https://www.google.com/url?sa=t&source=web&rct=j&url=https://m.youtube.com/watch%3Fv%3DG_eYTctGZw8&ved=2ahUKEwi-z6LAqZbqAhVNC-
wKHQfjBuUQo7QBMAB6BAgAEAE&usg=AOvVaw1sYxqfAt6-T-BJ-aviCSgS)

As for retries, I have only a blurry idea of what that's about, but from my
limited understanding I suspect it's perfectly doable.

~~~
zelphirkalt
I do not know that much about Prolog. Only used it once or twice so far.

Would the internal facts database as storage be as efficient as a typical
database, say SQLite or Postgres? What I mean by "efficient" is: Would it
consume similar amounts of disk space or memory and would it be as fast in
responding to queries with results?

~~~
rscho
Potentially yes, but current prolog implementations are mostly academic
productions with few man-hours invested into them compared to more popular
languages. So no, you won't build huge industrial websites on current prolog,
but mid-size webapps are certainly within reach as modern prologs are much
faster than people usually suspect.

Prolog huge data munging is a thing though. Check the "semantic web" and
related for examples.

------
EamonnMR
Is there a decent prolog/SQL bridge? Being able to write applications and
queries using SQLite or Postgres database as the fact set for prolog would be
very nice.

~~~
cjallen88
Perhaps something like [https://www.swi-
prolog.org/pldoc/doc_for?object=section(%27p...](https://www.swi-
prolog.org/pldoc/doc_for?object=section\(%27packages/cql.html%27\))

~~~
EamonnMR
Thanks, I'll check that out.

