# Prolog error

**URL:** <https://swi-prolog.discourse.group/t/prolog-error/9640>\
**Category:** General\
**Created:** [June 3, 2026, 1:46pm UTC](https://swi-prolog.discourse.group/t/prolog-error/9640 "2026-06-03T13:46:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgokane.kgokane](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@kgokane.kgokane](https://swi-prolog.discourse.group/u/kgokane.kgokane)\
**Post date:** [June 3, 2026, 1:46pm UTC](https://swi-prolog.discourse.group/t/prolog-error/9640/1 "2026-06-03T13:46:17Z")

</div>

Hi experts ,

Please advise what is wrong with this code.

Write a recursive Prolog procedure triangle(M,N) that determines the number of balls N in a triangle with M balls in the bottom level. Each level has one ball fewer than the previous level. The top level (base case) contains only 1 ball.

Base case

assert(triangle(1, 1)).

assert(triangle(M, N) :-

M \> 1,

M1 is M – 1,

assert(triangle(M1 , N1),

N is N1 + M,

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/e/e95e727d33f50818e69c9ed20e10957ef7a021e6.png)

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [June 3, 2026, 3:15pm UTC](https://swi-prolog.discourse.group/t/prolog-error/9640/2 "2026-06-03T15:15:31Z")

</div>

The brackets are mis-matched.

Also, M cannot be deprecated - use e.g. `M0 is M - 1` instead.

[Example](https://stackoverflow.com/a/16015891/17628336):

```prolog
?- assert((faster(X,Y) :- fast(X), slow(Y))).
true.

?- listing(faster).
:- dynamic faster/2.

faster(A, B) :-
    fast(A),
    slow(B).

```

Why use assert/1 at all? Would normally write the code in a program.

---

<div class="post-metadata">

**Author:** ![kgokane.kgokane](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@kgokane.kgokane](https://swi-prolog.discourse.group/u/kgokane.kgokane)\
**Post date:** [June 3, 2026, 4:57pm UTC](https://swi-prolog.discourse.group/t/prolog-error/9640/3 "2026-06-03T16:57:33Z")

</div>

executed all this commands, all of them do return true.Can you kindly please guide me using M and N, I can see that M=X and N=Y but I want to be relevant to the question.

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [June 3, 2026, 6:46pm UTC](https://swi-prolog.discourse.group/t/prolog-error/9640/4 "2026-06-03T18:46:15Z")

</div>

Using assert/1 for this makes no sense. Noboby would tell a beginner to assert predicates unless they were a sadist 😀

Stop using assert and write a normal program.
