Live data from Hacker News

An embeddable Prolog scripting language for Go

github.com

11–20 of 36 posts

Re: An embeddable Prolog scripting language for Go

#11

What are the trade offs of embedding a Prolog interpreter versus having a library that implements prolog logic without having to have another language. Are there any high quality prolog as a library implementations in languages such as Rust, C++, Java, Go?

I've never used it but for Java there's https://gitlab.com/pika-lab/tuprolog/2p-in-kotlin ( documentation seems to be lagging but here's the 3.3 manual https://gitlab.com/pika-lab/tuprolog/2p/-/wikis/uploads/d67e... )

And specifically for Javascript there's http://tau-prolog.org/

Also pretty much every lisp that's gotten past toy stage has a Prolog implementation

Although both Sicstus and SWI have strong Java and C(++) interop https://sicstus.sics.se/sicstus/docs/latest4/html/sicstus.ht... https://www.swi-prolog.org/pldoc/doc_for?object=packages

Re: An embeddable Prolog scripting language for Go

#13

Every language should come with a Prolog interpreter. :) Well done, ichiban team!

I never used prolog. In my mind it is some old unused language. What is good about it and why should it be embedded into other languages?

rule-based programming is a slightly different take than procedural programming, and can be quite a bit more expressive and efficient to evaluate than procedural for the right kind of problems.

'make' is pretty good example, but 'package manager' might be more relevant to people. instead of having explicit control flow you have rules that say 'this is the case if these other things are the case', and they get triggered to evaluate implicitly (and recursively).

variables and values in declarative languages are implicitly quantified to be sets of objects, so we say 'for all X where' .. instead of '_the_ X where', which makes it similar to SQL.

actually there you go - pretend this is an embedded database - that's clearly useful, except with a better query language.

Re: An embeddable Prolog scripting language for Go

#14

Every language should come with a Prolog interpreter. :) Well done, ichiban team!

I never used prolog. In my mind it is some old unused language. What is good about it and why should it be embedded into other languages?

My elevator pitch for Prolog is this:

It's a constraint solving language, linear problem solving language, a database query language, a parsing language, and an expert systems language. All unified (sorry that's a pun) transparently in a single language suitable for general purpose programming instead of half a dozen partially interoperable DSLs.

Re: An embeddable Prolog scripting language for Go

#15
post #6

What are the trade offs of embedding a Prolog interpreter versus having a library that implements prolog logic without having to have another language. Are there any high quality prolog as a library implementations in languages such as Rust, C++, Java, Go?

> What are the trade offs of embedding a Prolog interpreter versus having a library that implements prolog logic without having to have another language. A library that implements Prolog logic is an interpreter. The difference here is that Prolog source code is passed in as a string as opposed to having an embedded domain specific language where you construct the abstract syntax tree. Go is a very simple language and…

There are advantages to embedding. You can retain the host language type system and and object model. If you have a great query language and model but have to write a ton of code to marshal back and forth, it might not be adding that much value (classic impedance mismatch).

While go’s compile time metaprogamming is virtually non-existent, it’s runtime metaprogramming with reflection is more or less complete. There’s a runtime cost to using it, but that can be mitigated.

See https://github.com/cockroachdb/cockroach/tree/master/pkg/sql... for a reflection-driven, embedded logic query language in go that achieves pragmatic goals of writing logic queries over data structure graphs at reasonable performance and pretty good expressibility.

Re: An embeddable Prolog scripting language for Go

#16

What are the trade offs of embedding a Prolog interpreter versus having a library that implements prolog logic without having to have another language. Are there any high quality prolog as a library implementations in languages such as Rust, C++, Java, Go?

Some Prolog systems (like Ciao Prolog https://github.com/ciao-lang/ciao/blob/master/core/lib/forei...) implement bidirectional foreign interfaces. Once you have C bindings it is easy to write bindings from Rust, C++, or any other language (that can interoperate with C). I give here some details about Ciao because this is the system I know better but it should be similar for other popular Prolog implementations.

The tradeoffs depend on the complexity of the Prolog code and your needs for performance and features: pure LP, Prolog (search+unification+cut), garbage collection, dynamic database updates, constraint domains, etc. The Ciao Prolog engine is around 300-400KB. Adding a few libraries, compiler, etc. it goes to 2MB. Naive Prolog systems can be one order of magnitude smaller at the cost of sacrificing ISO compatibility, performance, etc. Note that "performance" can be very misleading. Some Prolog programs may run particularly fast in some Prolog system and very badly in others.

Re: An embeddable Prolog scripting language for Go

#17

Every language should come with a Prolog interpreter. :) Well done, ichiban team!

I never used prolog. In my mind it is some old unused language. What is good about it and why should it be embedded into other languages?

For the same reasons that LINQ is used in C# programs.

Re: An embeddable Prolog scripting language for Go

#18

Every language should come with a Prolog interpreter. :) Well done, ichiban team!

I never used prolog. In my mind it is some old unused language. What is good about it and why should it be embedded into other languages?

A practical (but not fielded) use that I found with Prolog: programs are multi-use. That is, if you mentally model (and I think this is natural for new Prolog programmers) a program as running in one direction, you will be pleasantly surprised to find that you can use the same program to accomplish multiple effects. Using the built-in append/3 as an example:

  % find the list resulting from appending two lists
  append([1],[2],C).   %% => C = [1,2]
  % find the prefix given a suffix
  append(A,[2],[1,2]). %% => A = [1]
  % find the suffix given a prefix
  append([1],B,[1,2]). %% => B = [2]
  % find all prefixes and suffixes
  append(A,B,[1,2]).
    A = [], B = [1,2];
    A = [1], B = [2];
    A = [1,2], B = [];
    false ;; no more results
The first line is what most programmers new to Prolog will be used to, a function performs a computation in a forward fashion (here: given two knowns, calculate something). But all the others come along "for free" (if you write it correctly, which isn't too hard once you've learned a bit of the language and style).

My practical use for this was with a (prototyped, not fielded) scenario generator. Using the rules (specification for the protocol) I could generate arbitrary scenarios. I could constrain them by filling in some variables, but leaving others blank. And I could take an existing scenario and determine if it was even valid. Using append again:

  append(A,[3],[1,2]). %% => false
Now, in my situation instead of a hardcoded [1,2] I had a recording of an exchange between nodes, and I could use this to get an answer to why it didn't conform to the spec. One program that could be used in many different ways.

The other reason to use Prolog, this was a much higher level of abstraction to work with than what we ended up using. It was much shorter, but less familiar to others. So we ended up with a C# + SQL solution where we had to write each variation of the program (generate, test for validity, identify problems).

Re: An embeddable Prolog scripting language for Go

#19

Every language should come with a Prolog interpreter. :) Well done, ichiban team!

I never used prolog. In my mind it is some old unused language. What is good about it and why should it be embedded into other languages?

Prolog, like Lisp, excels in problems where the best solution is to create a language in which to describe your problem, and then solve it by using some kind of inference engine. Unlike Lisp, it comes with an inference engine built-in, one that is as good as possible (formally speaking, it's sound and complete). So if that's the kind of use case you have your options are either: use Prolog; embed it; or implement an informally-specified, bug-ridden, slow implementation of half of Prolog.

Re: An embeddable Prolog scripting language for Go

#20
post #9

I've been keeping an eye on this to use for the rules engine in a card game I'm writing[0]. Very excited to get back into using Prolog; I think it's fallen by the wayside a bit in the last decade or two but there's some sectors that still have strong arguments for using it if not as the main language then at least an extension language. [0] Inspired by a HN comment a while back about Gleemin, the MTG expert engine in…

I would love to hear more about this! Do you blog?
Post reply on HN