r/prolog

▲ 19 r/prolog

Writing a game in Prolog - how to avoid redundant choice points to ensure tail-call optimisation?

I have started writing a little roguelike game in Prolog (for fun as a first project). I think there are lots of ways in which Prolog is a nice fit for this, and several ways that it isn't. I'm happy to be pragmatic but wanted to ask more experienced folks about the idiomatic way to write Prolog.

My approach is to have a (tail)-recursive predicate which threads state as an argument (rather than using assert/retract) and updates the game based on user input, something like:

game_loop(State) :-
  render(State),
  handle_input(State, NewState),
  game_loop(NewState).

This works well and is tail-call optimised as long as render/1 and handle_input/2 don't leave choice-points that Prolog might want to backtrack into. For a game that might run for many iterations, I want to avoid stack overflow so TCO is important.

To guarantee this, I find that I am writing a lot of predicates using a single clause with (->)/2 so that I don't leave redundant choice points. Pragmatically this is fine, the approach works, the intention is clear, and I still gain many benefits from using Prolog even if it's a bit "extra-logical". But (and I'm perhaps overthinking this) I wonder if this is a unidiomatic? It means my predicates are often one-way and deterministic, which is nice procedurally but does that take away from some of the advantage of using Prolog?

The other thing I'm often doing is making sure that (first) argument indexing will enable Prolog to rule out redundant choice points, but sometimes that's not enough (if for example I need an else-like clause such as functor(_, ...) which could unify with earlier cases).

I've seen some mention of if_/3 but it looks like it's not built-in in SWI-Prolog (or at least not for WASM which I'm targeting?). Welcome any opinions on this approach!

reddit.com
u/drvog — 4 days ago
▲ 17 r/prolog+1 crossposts

Easy-ISLisp Ver5.63 has been released.

This release mainly focuses on compiler improvements, stability enhancements, and bug fixes.

Highlights:

  • Improved compiler stability
  • Better handling of labels mutual recursion
  • Improved nested lambda/free variable handling
  • Refined optimization and type inference behavior
  • Added (eisl-version) for distributed parallel child-node version checking
  • Re-tested distributed parallel functionality on a Raspberry Pi cluster

The distributed parallel features were verified again after recent compiler modifications to ensure that no regressions were introduced.

The compiler now appears to have reached a more stable operational level for ordinary user programs.

GitHub:
https://github.com/sasagawa888/eisl

Feedback and bug reports are welcome.

u/sym_num — 5 days ago
▲ 20 r/prolog

I think I’m done with my Prolog database engine for now

Okay, I think I’m done with my database engine for now.

I started AsaDB mostly because I was curious about Prolog and logic programming. Somehow that curiosity turned into me spending about two months building a database engine almost entirely by myself.

I’m still just a university student. At my campus, I’ve only had programming-related courses for about three semesters, so most of what went into this project was something I had to learn while building it.

And honestly, I’m exhausted.

The engine does work. I managed to get persistent storage, SQL parsing, transactions, PRIMARY KEY / UNIQUE constraints, joins, views, a web panel, server mode, imports, and several other things running.

Some recent 100,000-row results:

  • Plain import: 42.5 s
  • PRIMARY KEY: 72.0 s
  • UNIQUE: 69.5 s
  • PRIMARY KEY + UNIQUE: 81.5 s
  • Simple metadata COUNT: ~30 ms
  • Lookups: roughly 8–11 s
  • 100k × 100k JOIN: roughly 35 s
  • CREATE VIEW: ~27 ms
  • Simple expressions with LIMIT can be below 100 ms

But there are still problems I haven’t been able to solve properly.

PRIMARY KEY and UNIQUE lookups are sometimes no faster than a plain scan. JOIN with a small LIMIT is almost as slow as processing the full join. Subqueries can run for more than 120 seconds and fail. EXISTS and JOIN-based views have caused crashes. I tried fixing several of these problems, but every fix seems to uncover another layer involving indexing, execution planning, memory management, concurrency, or storage.

The screenshots are basically where I’m leaving it: swipl asadb starts the server workspace, a RIGHT JOIN over the 100k-row benchmark tables returns the correct 100,000 rows, and views can be created successfully.

So this isn’t really “the database never worked.”

It worked far enough that I finally discovered how difficult database engines actually are.

I think I’ve reached the point where I simply don’t have enough experience or energy to keep fighting the architecture right now.

Thank you, Prolog. I started this because I wanted to satisfy my curiosity about a logic programming language, and I ended up learning far more than I expected.

Maybe this is the end of AsaDB, maybe it’s only a very long break.

Either way, I think I need to step away from Prolog for a while.

this is AsaDB repository :D

u/Aires_id — 6 days ago
▲ 26 r/prolog+1 crossposts

I've created TrackLog: a collection of Prolog libraries, examples, and guidelines for building a personal knowledge base in pure logic

  1. TrackLog blurs the boundaries between database administration and programming
  2. TrackLog is based on Formal Grammar (DCG): you will develop your own grammar to accomodate your language needs
  3. TrackLog will help you to create extremely precise and relevant data fuel to feed it to LLMs
  4. TrackLog stands for "Tracking in Logic"
  5. TrackLog libraries are 100% compatible with SWI Prolog dialect and 99% ISO-compatible, so you can port them to another dialect if you need it
  6. My original goal was to create a medical tracker to help people to manage complex illnesses and chronic disorders
  7. Logical Programming is a generalization of functional programming and also a generalization of relational databases. You can see logical programming as a missing glue layer between a database and a program
  8. You can see TrackLog as a second brain to help you capture and analyze data and aid in pure logical decision-making which is free of 100+ cognitive distortions and biases, completely traceable and based on formal logic

Please, use examples.pl as a main guide. I've provided several practical use-cases: Learning Tracker, Item Tracker, Exercise Tracker and Programming Tracker.

Repo: https://github.com/kciray8/tracklog

I'm glad to hear any feedback!

u/Iaroslav-Baranov — 8 days ago
▲ 19 r/prolog

How to develop your own implementation of Prolog from scratch in 2026 in one sprint (Guidelines)

Use any language of your choice. I used Java. You can use my Java/Spring implementation as a reference

  1. Week one: dive deep into the standard to create a working prototype
    1. Use Section 6.4 "Tokens" for lexer.
    2. Use Section 6.3 "Terms" for parser. Use Pratt Parser to parse Prolog: it works surprisingly well with it.
    3. If your goal is to understand Prolog, use Section 7.7 "Executing a Prolog goal" as the description of stack-based computation model on which you will base your Prolog engine. It is simple but slow. If your goal is to create a high-performance implementation, you can implement WAM instead from the beginning, but it will take you significantly more time
    4. Keep in mind that the ISO Prolog standard has a lot of minor typos, but every typo is fixable if you put it into context and think about it longer. You don't even need to look up 3 corrigendums (corrections). In fact, they don't cover many important typos so forget about them and just focus on the original 1995 document.
  2. Week two: make your implementation stronger and cover the first 28 problems from the Prolog 99 problems list. They are a perfect benchmark. https://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/

This should be enough! Only 2 weeks (a sprint) and you will have a SUBSTANTIAL boost in understanding Prolog on the deepest level possible, so later you can switch into existing implementations (like SWI Prolog) and see them differently

u/Iaroslav-Baranov — 8 days ago