Hacker News new | past | comments | ask | show | jobs | submit login
Trealla Prolog embedded in Go using WASM (github.com/guregu)
89 points by triska on Aug 7, 2022 | hide | past | favorite | 12 comments



Oh hey, this is mine. Thanks to triska for submitting it, and for his Power of Prolog lecture videos. I got started learning Prolog with them this year.

This package uses the novel (at least, novel to me) method of embedding a small C program (Trealla Prolog) through WASM/WASI and exposing it as a regular Go library. My hopes are that it can give us easier cross-platform compatibility going forward, and has some memory isolation/security benefits too. It's kind of like running a "serverless" worker architecture locally, which is kinda cool IMO.

This package is still in its early phases but I highly suggest that everyone checks out Prolog if it sounds interesting to them. It is a very powerful and very cool language that can change the way you approach solving problems. I'm currently working towards getting Prolog easier to embed and use in different languages, and I think Trealla is a great asset towards this.


>This package uses the novel (at least, novel to me) method of embedding a small C program (Trealla Prolog) through WASM/WASI and exposing it as a regular Go library

Could you please expand this with more details?


I plan on writing a blog post soon to explain it better. The quick summary is that it uses WASI to execute a C program not entirely unlike oldschool CGI, but now it's inside of a WebAssembly runtime instead of an OS process. It uses command line arguments to send the query and standard output to grab the results. This may become fancier in the future.

You can see the relevant code here: https://github.com/guregu/trealla-go/blob/333645147df083dd26... that builds up the command line arguments for tpl (the Trealla binary, shoved into the repo as tpl.wasm). The rest of the package is mostly shuffling data around.


Using a WASI runtime in Go you can run any code compiled to WASM. That also means you can wrap a C (or other) library compiled to WASM into a Go library.


Trealla Prolog is one of the most interesting new Prolog systems: In particular, it is the first Prolog system that uses mmap(2) to map files to memory for direct processing of file contents with Prolog's built-in grammar mechanism, Definite Clause Grammars (DCGs).

This is possible in Trealla due to its compact internal representation of lists of characters by using UTF-8 encoding internally, a representation pioneered by Scryer Prolog.

Thank you a lot gurego for all your work on this WASM embedding!


Prolog is still the best query language out there. If it had as much attention as other languages like python or javascript, we'd have some real space-age programming systems.


My only short exposure to Prolog was trying to implement custom submission rules in Gerrit. It made an utter fool out of me at every turn - it really seemed like an insane interface, compared to e.g. a Python or Javascript function that could be user-implemented.

I get the feeling there's more to Prolog than I was able to unlock - can you recommend any reference materials for beginners?


I guess Japanese scientists saw those benefits in 80's? https://en.wikipedia.org/wiki/Fifth_Generation_Computer_Syst...


I'm a bit confused, is Trealla prolog itself closed source? The README of this WASM embedding links to a private trealla-prolog/trealla repo.


It was temporarily private in a bit of a reorganization, should be public now.


Thanks Markus for this post, gurugu has done amazing work here, though outside my range of practical experience.


Anyone know if it compiles ok with the LLVM based version of Go?

In theory (!), the binaries produced should be a bunch smaller than the standard Go compiler ones... though I haven't yet tried myself. :)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: