Event Calculus in SWI-Prolog?

I’ve started reading Mueller’s Commonsense Reasoning An Event Calculus Based Approach (2014). The Ciao version of sCASP includes a basic event calculus (BEC) file and folders of examples from this book. When I include sCASP in SWI-Prolog and load bec_theory.pl, I see errors regarding the syntax T1 .<. T, T .<. T2:

stoppedIn(T1, Fluent, T2) :-
    T1 .<. T, T .<. T2,
    terminates(Event, Fluent, T),
    happens(Event, T).

@jan , I apologize for needing so much help right now. I’m anxious to move the “thinking” part of my agents in an existing application to Prolog / sCASP / something logical. Is .<. syntactic sugar that was omitted from the SWI-Prolog implementation of sCASP? What is the best way to work through examples in the book?

The .<. and similar operators where synonyms for #<, etc. I have removed this duplication. If you find them, please submit a PR.

PR means I correct the code in a fork of my own? I look forward to the time when I can contribute in that way! I’ve been programming in Prolog for just a few months and learned about ASP a week ago. I pray not to see red ERROR messages in my interpreter every time I submit a query and my prayers usually go unanswered.

I’m not sure what #< means. < is the numerical comparison operator I see in the documentation and #< triggers “predicate not defined”. I used <, etc and added lines like :- dynamic light_on/1. to the top of the file, but I still see a lot of warnings and all queries return false. So…that wasn’t correct.

Is anyone on the list working with event calculus, situational calculus, or TAL right now?

(#<)/2 is part of s(CASP), not Prolog (it is also part of clp(fd) as the manual link shows, but these are unrelated). You should distinguish Prolog from s(CASP). The latter is a small subset of Prolog that is evaluated under different semantics.

(#<)/2 is a constraint. I’ve added some examples to SWISH -- SWI-Prolog for SHaring to show what happens. Plain Prolog (<)/2 is just arithmetic and demands both sides to be known before it can be called.

@jan , I’ve created a SWISH notebook that documents my adjustments to the basic event calculus code for SWI-Prolog’s sCASP. I have not checked whether the code runs in the Caio version. If you do not see a blatant error, perhaps that is the next step?

%% changed ?- to ?

? holdsAt(light_on, 2).                            % success 

Syntax error: Operator expected
Syntax error: Operator expected
Syntax error: Operator expected
No permission to call sandboxed `set_prolog_flag(_3056,_3058)'
Syntax error: Operator expected
Invalid mode declaration in PlDoc comment: 'Actions'
Cannot run query due to a syntax error (check query window)

I have made some updates. Now runs fine.

  • set_prolog_flag/2 error was a SWISH issue. Fixed.
  • Avoid %% comments. A double % is considered structured comment in SWI-Prolog and used for documentation that must satisfy some syntax rules. Those are most warnings.
  • Fixed one real syntax error #<# instead of #<
  • Repeated the :- use_module(library(scasp)). in the other programs. This should not be needed, but syntax analysis doesn’t span notebook cells at the moment and without the scasp module the #<, etc operators are not known.

It now works. Whether the answer is correct, I don’t know :slight_smile:


@jan , thank you so much! Yes, I’ve checked it with several queries. It is working great!

Would you be kind enough to elaborate on what “Ciao” and “sCASP” refer to? Google didn’t return satisfactory results.
I believe I could learn a lot from that book, but I’m yet to purchase it and currently can’t.
Please also mention if that book uses Prolog as the language for exposition.
Thank you.

Ciao - The Ciao programming language

Ciao is a programming language that builds up from a logic-based simple kernel, and is designed to be extensible and modular.

s(CASP) - The main contribution of s(CASP) is its ability to evaluate Constraint Answer Set Programs without a grounding phase, either before or during execution.

So ASP in s(CASP) is Answer Set Programming

CASP is ASP with constraints (ref)

For how s(CASP) came into SWI-Prolog see Initial port for sCASP, a top-down interpreter for ASP programs with constraints

s(ASP) - The s(ASP) Predicate Answer Set Programming System (ref)

The s() part is a play on Peano axioms. s() being the successor function. Make sense?

“A Short Tutorial on s(CASP), a Goal-directed Execution of Constraint Answer Set Programs” by Joaquín Arias, Gopal Gupta and Manuel Carro (pdf)


Hi @kathe , thank you for your interest in this thread. The book introduces ideas in plain language, then the language and symbols of logic (which I barely can follow), and then finally in answer set programming, which – at least in this book – uses Prolog syntax.

Are you working on a project that requires planning or querying events over time?

You may be able to read a copy of the book in a university library.

@jan , I thought this question was resolved, but when I copy-pasted my swish code and consulted it using the Windows SWI-Prolog application, I get many errors. I only removed the multiple :- use_module(library(scasp)). The file is attached. The errors are all like:

ERROR: c:/users/dvyd/appdata/local/temp/xpce3:13:
ERROR:    format/3: Type error: `text' expected, found `url('d:/documents/programming/prolog/bec.pl':12)' (a compound)` 
Warning: c:/users/dvyd/appdata/local/temp/xpce3:101:
Warning:    Local definition of user:(?)/1 overrides weak import from scasp

Maybe I’m consulting or calling the code incorrectly? I’m using “consult selection”. Any idea why the code works in SWISH and not locally?

bec.pl (2.6 KB)

Hi @d-vyd,

Thank you for responding with the details.
I don’t know anything about answer-set programming, would you be in a position to suggest/recommend a book for the same? One which is preferably language neutral! Or, at least uses Prolog!

If you wish to “learn” logic well enough to understand it’s language and symbols, refer to https://openlogicproject.org/ which provides free material.
Just remember to work through https://forallx.openlogicproject.org/ before approaching the book mentioned above.

I intend to learn how to apply KRR techniques for a personal project that requires commonsense reasoning.

Thanks @kathe . I’m new to answer set programming (ASP), too. I’m reading Knowledge Representation, Reasoning, and the Design of Intelligent Agents The Answer-Set Programming Approach. As far as I can tell, ASP always sits on top of a logic programming language like Prolog.

EDIT: I’m wrong. Please see Jan’s response below!

That results in an error. Fixed. But, why would you use “consult selection”? When using the Windows app, use either ?- [myfile]. or use the File/Consult. Properly installed it should also work to simply double click a .pl file. Not sure that is still enabled in the default installation. If I recall correctly you can also drop a .pl file on the window. And finally there is the compile buffer command that simply compiles the associated file. All should work …

The ?- holdsAt(light_on, 7). should not be in your file. That is what you run after loading the file at the toplevel.

No. ASP systems use a syntax that is inspired by Prolog. Except for s(CASP) most systems are not based on Prolog though. They use a combination of grounding and a SAT solver to arrive at the consistent models. The power of s(CASP) is that it needs no grounding and thus can reason about e.g. X #> 3 without knowing a finite set of values for X and that its top-down reasoning results in a proof tree that can be used for justification. Dealing with domains where grounding is not an issue and if you are not interested in the justification, get yourself a traditional ASP solver like Clingo to gain a lot of performance and get a lot more features for expressing your problem.


Thanks @jan . When you write “Fixed.” does that mean the fix is in GitHub - SWI-Prolog/swipl-devel: Main development repository and I should download and compile? I was just using consult selection to incrementally try to figure out where the error is. I usually use file consult.

Except for s(CASP) most systems are not based on Prolog though.

I didn’t know that. Thanks!

if you are not interested in the justification, get yourself a traditional ASP solver like Clingo to gain a lot of performance and get a lot more features for expressing your problem.

Justification is important to me. But, I should probably better understand the additional features that Clingo may offer so I can assess the tradeoff. @kathe, you may want to do the same if you are considering ASP.

The fixed consult for the selection is in one of the submodules of swipl-devel.git, the main source. But, as I explained, consult selection is not something you typically use.

@Jan, got it. Thanks.

@jan Thank you, and I would be even more grateful to receive information about what I should know before approaching Commonsense Reasoning: An Event Calculus Based Approach.

I’m a humble Prolog system programmer :slight_smile: If you want to know about s(CASP) and commonsense reasoning, visit the home page of Gopal Gupta

1 Like