# Event Calculus in SWI-Prolog?

**URL:** <https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233>\
**Category:** Predicate\
**Created:** [April 7, 2022, 9:05am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233 "2022-04-07T09:05:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 7, 2022, 9:05am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/1 "2022-04-07T09:05:33Z")

</div>

I’ve started reading Mueller’s _[Commonsense Reasoning](https://www.elsevier.com/books/commonsense-reasoning/mueller/978-0-12-801416-5) 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](https://github.com/JanWielemaker/sCASP/blob/swipl/examples/benchmark_EventCalculus/lopstr19/event-calculus/bec_theory.pl), I see errors regarding the syntax `T1 .<. T, T .<. T2`:

```prolog
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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 7, 2022, 10:50am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/2 "2022-04-07T10:50:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 7, 2022, 7:15pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/3 "2022-04-07T19:15:59Z")

</div>

> [@jan](#):
>
> 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](https://www.swi-prolog.org/pldoc/man?section=arithpreds) 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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 8, 2022, 6:48am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/4 "2022-04-08T06:48:19Z")

</div>

> [@d-vyd](#):
>
> `#<` triggers “predicate not defined”

`(#<)/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](https://swish.swi-prolog.org/p/HqwgnYZA.swinb) to show what happens. Plain Prolog `(<)/2` is just arithmetic and demands both sides to be known before it can be called.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 8, 2022, 8:48am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/5 "2022-04-08T08:48:48Z")

</div>

@jan , I’ve [created a SWISH notebook](https://swish.swi-prolog.org/p/sCASP%20Basic%20Event%20Calculus.swinb) 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?

```prolog
%% 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)

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 8, 2022, 12:38pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/6 "2022-04-08T12:38:32Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 8, 2022, 1:01pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/7 "2022-04-08T13:01:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kathe](https://avatars.discourse-cdn.com/v4/letter/k/67e7ee/32.png) [@kathe](https://swi-prolog.discourse.group/u/kathe)\
**Post date:** [April 9, 2022, 5:57am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/8 "2022-04-09T05:57:26Z")

</div>

> [@d-vyd](#):
>
> I’ve started reading Mueller’s _[Commonsense Reasoning](https://www.elsevier.com/books/commonsense-reasoning/mueller/978-0-12-801416-5) 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.

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 9, 2022, 8:38am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/9 "2022-04-09T08:38:11Z")

</div>

[Ciao](http://ciao-lang.org/) - 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](https://en.wikipedia.org/wiki/Answer_set_programming)

CASP is ASP with constraints ([ref](https://swi-prolog.discourse.group/t/s-casp-meets-json/4600))

For how s(CASP) came into SWI-Prolog see [Initial port for sCASP, a top-down interpreter for ASP programs with constraints](https://swi-prolog.discourse.group/t/initial-port-for-scasp-a-top-down-interpreter-for-asp-programs-with-constraints/4130)

s(ASP) - The s(ASP) Predicate Answer Set Programming System ([ref](https://www.cs.nmsu.edu/ALP/2017/04/the-sasp-predicate-answer-set-programming-system/))

The s() part is a play on [Peano axioms](https://en.wikipedia.org/wiki/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](http://ceur-ws.org/Vol-2970/gdepaper1.pdf))

HTH

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 10, 2022, 4:53am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/10 "2022-04-10T04:53:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 10, 2022, 5:27am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/11 "2022-04-10T05:27:54Z")

</div>

@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:

```prolog
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](https://swi-prolog.discourse.group/uploads/short-url/iq1ajSKQ9HbQ6JjLRRszKfu7eP7.pl) (2.6 KB)

---

<div class="post-metadata">

**Author:** ![kathe](https://avatars.discourse-cdn.com/v4/letter/k/67e7ee/32.png) [@kathe](https://swi-prolog.discourse.group/u/kathe)\
**Post date:** [April 10, 2022, 5:45am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/12 "2022-04-10T05:45:36Z")

</div>

Hi @d-vyd,

> [@d-vyd](#):
>
> 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.

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/](https://openlogicproject.org/) which provides free material.  
Just remember to work through [https://forallx.openlogicproject.org/](https://forallx.openlogicproject.org/) before approaching the book mentioned above.

> [@d-vyd](#):
>
> Are you working on a project that requires planning or querying events over time?

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

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 10, 2022, 6:03am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/13 "2022-04-10T06:03:15Z")

</div>

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](https://www.amazon.com/Knowledge-Representation-Reasoning-Design-Intelligent-ebook/dp/B00HAFO19E)_. 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!

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 10, 2022, 8:09am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/14 "2022-04-10T08:09:03Z")

</div>

> [@d-vyd](#):
>
> I’m using “consult selection”.

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 …

> [@d-vyd](#):
>
> `Warning: Local definition of user:(?)/1 overrides weak import from scasp`

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

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 10, 2022, 8:14am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/15 "2022-04-10T08:14:31Z")

</div>

> [@d-vyd](#):
>
> As far as I can tell, ASP always sits on top of a logic programming language like Prolog.

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.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 10, 2022, 10:20am UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/16 "2022-04-10T10:20:32Z")

</div>

Thanks @jan . When you write “Fixed.” does that mean the fix is in [GitHub - SWI-Prolog/swipl-devel: SWI-Prolog Main development repository](https://github.com/SWI-Prolog/swipl-devel) 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](https://potassco.org/clingo/) may offer so I can assess the tradeoff. @kathe, you may want to do the same if you are considering ASP.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 10, 2022, 4:17pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/17 "2022-04-10T16:17:36Z")

</div>

> [@d-vyd](#):
>
> When you write “Fixed.” does that mean the fix is in [GitHub - SWI-Prolog/swipl-devel: Main development repository](https://github.com/SWI-Prolog/swipl-devel) and I should download and compile?

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.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [April 10, 2022, 4:56pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/18 "2022-04-10T16:56:14Z")

</div>

@Jan, got it. Thanks.

---

<div class="post-metadata">

**Author:** ![kathe](https://avatars.discourse-cdn.com/v4/letter/k/67e7ee/32.png) [@kathe](https://swi-prolog.discourse.group/u/kathe)\
**Post date:** [April 11, 2022, 4:17pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/19 "2022-04-11T16:17:52Z")

</div>

> [@jan](#):
>
> > [@d-vyd](#):
> >
> > As far as I can tell, ASP always sits on top of a logic programming language like Prolog.
> 
> 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.

@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](https://www.elsevier.com/books/commonsense-reasoning/mueller/978-0-12-801416-5).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 11, 2022, 6:46pm UTC](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233/20 "2022-04-11T18:46:17Z")

</div>

> [@kathe](#):
>
> even more grateful to receive information about what I should know before

I’m a humble Prolog system programmer 🙂 If you want to know about s(CASP) and commonsense reasoning, visit the [home page of Gopal Gupta](https://personal.utdallas.edu/~gupta/)

[Next page](https://swi-prolog.discourse.group/t/event-calculus-in-swi-prolog/5233.md?page=2)
