[HN Gopher] Prolog Web Applications (2016) ___________________________________________________________________ Prolog Web Applications (2016) Author : ArneVogel Score : 47 points Date : 2020-06-22 06:15 UTC (16 hours ago) (HTM) web link (www.metalevel.at) (TXT) w3m dump (www.metalevel.at) | triska wrote: | 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. 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 | | Enjoy! | kqr wrote: | 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? | triska wrote: | As I see it, one noteworthy feature of these libraries is how | readily they can be implemented _in Prolog itself_. | | For instance, library(format), library(http/http_open) and | library(xpath) that are used in the example I posted are all | written in Prolog, using at most a few very basic and general | building blocks that are implemented in Rust: | | https://github.com/mthom/scryer- | prolog/blob/master/src/lib/f... | | https://github.com/mthom/scryer- | prolog/blob/master/src/lib/h... | | https://github.com/mthom/scryer- | prolog/blob/master/src/lib/x... | | To run the example across multiple cores, we need more | support from the underlying Prolog engine, for instance to | run multiple _threads_. In my view, this is a key aspect of | designing a Prolog system: deciding what must be provided by | the engine, and what is built on top of it. Scryer Prolog is | still in its early stages of development. I expect it to | provide support for multiple threads and interfaces to | external databases in a few years at the earliest. This also | depends on how many contributors the project will be able to | attract. | rscho wrote: | 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 | | See also this very nice talk: https://www.google.com/url?sa=t | &source=web&rct=j&url=https:/... | | 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. ___________________________________________________________________ (page generated 2020-06-22 23:00 UTC)