Sunday, November 30, 2025

Return of the infix operators (Prolog 8)

Yesterday I worked for a few hours on the Prolog interpreter, primarily restoring the infix operators that had temporarily been removed. As opposed to last time1, there were no nasty problems that these operators caused. That said, several interesting problems occurred. Compilation with Delphi 10 did not cause any problems except with logging; this routine had to be replaced. But the resulting file is about 10 times larger with D10.

The first problem that occurred was with the token 03:30; the tokeniser was turning this into three different tokens that of course wasn't what I wanted. Another problem that was easily fixed was with the rule divide (Operator1, Operator2, Result):- Result is Operator1 div Operator2. The parser crashed on the first token 'divide' as I had added 'div' as a special token. Even the factorial function worked perfectly.

After the factorial finished, I naively asked whether the token 24 (which is factorial 4, or 4!) needed to be added to the symbol table. CoPilot ran with this and suggested that we define a new kind of term, a numeric literal. This caused many problems as several functions had to be updated to take this new kind into account. It also solved a problem that had yet to arise, in two comparison functions, 'greater than' and 'less than'. These function were comparing the alphanumerical value of terms, eg '11' and '2'; when these values are compared, '11' is less than '2', which of course is not what is wanted. So comparing the numbers gave the correct result. Until now, the 'greater than' function had never seen a number greater than 9, so there was no problem comparing 9 to 0. 

Once everything was working correctly, I thought that I would try the next type of problem that the interpreter has to solve - lists. First I entered a (meaningless) fact, p([1,2,3]). Then I queried ?- p ([Head|Tail]). To my surprise, the solution Head = 1, Tail = [2,3] was presented. Correct! I then defined the 'member' rules

member(X, [X|_]).
member(X,[_|Y]):- member(X,Y).

Then I ran queries like ?- member(d,[a,b,c,d,e,f,g). (yes) and ?- member(2,[3,a,4,f). (no). I was quite surprised that these queries worked without having to add any special code. But the 'append' rules didn't work, so obviously there is work to be done regarding resolving lists. This will wait until next week.

Internal links
[1] 2037



This day in blog history:

Blog #Date TitleTags
14830/11/2008Criminal JusticeTV series
65330/11/2013Arik EinsteinIsrael, Obituary
98830/11/2016Backing up dataComputers
155730/11/2022Mid-night revelationsDBA

No comments: