Live data from Hacker News

Introduction to logic programming with Prolog

matchilling.com

81–90 of 93 posts

Re: Introduction to logic programming with Prolog

#81
post #78
post #77

Earlier quoted context omitted.

Are there any good materials (articles, books) for this kind of modern Prolog? I’m afraid my only experience with Prolog is those 80s features of Prolog and I’m not sure what you mean by constraints .

Please see my profile page: It contains links that I find relevant for learning modern Prolog features. Every predicate you impose on the set of solutions can be regarded as a constraint , because it can at most restrict the set of solutions, never increase it. So, in fact, every Prolog goal you invoke is a constraint. When reasoning over Herbrand terms, the only constraints are equality and disequality of terms, whi…

How does using constraints in Prolog relate to using a (finite domain) constraint solver?

Re: Introduction to logic programming with Prolog

#82

Earlier quoted context omitted.

> The point of Prolog as a practical logic programming language really is that it is portable accross Prolog implementations based on ISO Prolog (and maybe prolog-commons API compatibility). If you're using a Prologish tool (a Java framework, say) that is not quite Prolog, then you're loosing the compatibility for very little gain IMHO. I feel like I might be missing something obvious, but I'm not getting what the "c…

I'm not disagreeing that if you're developing a Web app, embedding logical or constraint programming techniques into a JavaScript API could make sense. OTOH, there's an obvious way for data exchange between JavaScript and Prolog, in that JSON can be be parsed by using Prolog's embedded `op/2` predicate and operator-precedence parser for defining custom DSLs.

What would be the workflow in Prolog if I want to import a set of facts from an external source, like a database/JSON/CSV?

Re: Introduction to logic programming with Prolog

#83
post #81
post #78

Earlier quoted context omitted.

Please see my profile page: It contains links that I find relevant for learning modern Prolog features. Every predicate you impose on the set of solutions can be regarded as a constraint , because it can at most restrict the set of solutions, never increase it. So, in fact, every Prolog goal you invoke is a constraint. When reasoning over Herbrand terms, the only constraints are equality and disequality of terms, whi…

How does using constraints in Prolog relate to using a (finite domain) constraint solver?

The beauty of this is that a constraint solver (over finite domains, Boolean values, sets etc.) blends in completely seamlessly into the way Prolog works.

From the perspective of Prolog and its users, a constraint solver is simply available just like any other predicate! All the internal reasoning a constraint solver performs is completely abstracted away. The only interface is typically a few Prolog predicates that let you access the features of the solver by stating what must hold about the involved variables.

So, to use a constraint solver in Prolog, you simply use the predicates it provides, just as you would use any other Prolog predicate. In the example above, I am using the constraint (#=)/2, which is a predicate that is true iff its arguments evaluate to the same integer.

From the perspective of implementors, Prolog is a great implementation language for constraint solvers due to its built-in search and backtracking mechanisms. It also allows you to use the standardized Prolog syntax that many users are already familiar with, instead of having to devise yet another modeling language on top of your solver.

Thus, I would describe the relation as a natural symbiosis: It is natural to use constraint solvers in Prolog, and natural to implement them in Prolog.

Re: Introduction to logic programming with Prolog

#84
post #38

Earlier quoted context omitted.

That is the best way to introduce computers to a kid: "let it do the hard work for you". Logo used to be very popular for kids here in the '80s. I learnt Logo when i was 7, then i "progressed" over the years to Basic, C, Pascal, C++, Java, C#, Python and now, after about 28 years, Lisp. And now I realize Logo is close to Lisp in many ways and a far more powerful language than some of the ones I "progressed" to.

It's always been interesting to me how Logo, a Lisp dialect, was so successful as a language for teaching children to program, but so many people still go around with the idea that Lisp is really hard to wrap your head around.

> People still go around with the idea that Lisp is really hard to wrap your head around.

Usually, the people who genuinely had a "hard to wrap head around experience" weren't actually using Lisp but a different language called Scheme which is hard to do for anything practical, if you stick just to the standard-defined dialect.

Chances are that when they used Scheme, it was a school setting, and they were complete or almost complete programming newbies, and were asked to do everything with recursion, and also given demanding homework, like interpreting Scheme in Scheme and whatnot.

Re: Introduction to logic programming with Prolog

#85
post #58

Another excellent resource for learning Prolog is the book "The Art of Prolog." One of the finest language texts you will ever read.

I read it recently and I can't recommend it enough. Even if you aren't really interested in Prolog, it is a must read.

Re: Introduction to logic programming with Prolog

#86
post #74

Earlier quoted context omitted.

> The point of Prolog as a practical logic programming language really is that it is portable accross Prolog implementations based on ISO Prolog (and maybe prolog-commons API compatibility). If you're using a Prologish tool (a Java framework, say) that is not quite Prolog, then you're loosing the compatibility for very little gain IMHO. I feel like I might be missing something obvious, but I'm not getting what the "c…

If you replace Prolog with SQL - does it make more sense?

Yes, and I missed it because I was thinking in terms of "hmm, I wonder if there are any points in my client-side code-base that could be cleaned up by using logic programming?"

Re: Introduction to logic programming with Prolog

#87
post #82

Earlier quoted context omitted.

I'm not disagreeing that if you're developing a Web app, embedding logical or constraint programming techniques into a JavaScript API could make sense. OTOH, there's an obvious way for data exchange between JavaScript and Prolog, in that JSON can be be parsed by using Prolog's embedded `op/2` predicate and operator-precedence parser for defining custom DSLs.

What would be the workflow in Prolog if I want to import a set of facts from an external source, like a database/JSON/CSV?

JSON could be parsed as a term by Prolog, with suitable priority/associativity definitions for the comma, colon, and curly brace operators.

For CSV, you could write a DCG grammar for the particular file format at hand.

Integration with a relational DB, itself being based on logic, would be very different. Most Prologs have APIs to map tables to predicates.

Re: Introduction to logic programming with Prolog

#88
post #68

Earlier quoted context omitted.

Eh, they are quite different under the hood. To put it real simple (and probably quite sloppy): Prolog determines the output by applying logic. ML uses mathematics to analyze the input and "guesses" the output, based on the training set. As a corollary: Prolog will negate anything that can't be proven true from the rules you give it. A ML algorithm will always give you some answer, though the quality depends on the l…

I think he meant ML as the family of meta programming languages? He did say systems though so you are probably right

Also, the part about "Input some learning data" makes me think to Machine Learning rather than Meta Language.

Btw, Prolog and ML might look like distant cousins: e.g. you use recursion and pattern matching in both. But under the hood there are big differences that arise from Prolog's logic programming paradigm: for instance, you can use append(L1, L2, L3) both to concatenate and split a list, depending on the variables you set:

  append([1,2], [3,4], X)       % ==> X=[1,2,3,4]
  append([1,2], Y, [1,2,3,4])   % ==> Y=[3,4]

Re: Introduction to logic programming with Prolog

#89
post #73

Earlier quoted context omitted.

are modern prolog just a layer on top of the old semantics or somehow an incompatible paradigm ?

In fact, the "old semantics" actually included language constructs that are only now becoming available (again) in Prolog systems. An example of such a construct is dif/2, which is a constraint that was provided in even the very first Prolog system, sometimes called Prolog 0, several decades ago. However, it was not widely available in Prolog systems until much more recently, even though professional Prolog systems s…

> The new features are not meant to be used "on top" of old features, but instead!

That is very powerful indeed. From incompetently playing around with stuff like that, my impression was that "constraints everywhere" is also extremely slow. That's not surprising, there's a lot of machinery working below the surface to deliver all that power. To recover performance whenever the full generality of the paradigm is not needed, I expect we'd need to work hard, say very smart static analysis, or JIT specialization, or...

But certainly my incompetence made me miss a lot. If you know how "constraints everywhere" can be made "fast everywhere", I'd love to hear about it:-)

Re: Introduction to logic programming with Prolog

#90
post #67

Earlier quoted context omitted.

I haven't encountered anything across answer set programming in the large as you put it. It is still pretty academic and mainly used for solving puzzle and optimization problems.

And yet declaring all business logic was once the way that all decisions would be encoded!

Looking at this from a more general perspective of the Symbolic AI approach I would say it had its days. Data driven systems are proving to be a lot more powerful and easy to build. I remember I used to spend ages trying to encode my problems in AnswerSet or AgentSpeak and come up with all the rules. It never worked!
Post reply on HN