-
Notifications
You must be signed in to change notification settings - Fork 0
/
thesis.tex
1759 lines (1362 loc) · 122 KB
/
thesis.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% demo.tex
%
% Enjoy, evolve, and share!
%
% Compile it as follows:
% latexmk
%
% Check file `dithesis.cls' for other configuration options.
%
\documentclass[inscr,ack,preface]{dithesis}
%\usepackage{graphicx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% User-specific package inclusions %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily\footnotesize,
breaklines=true,
}
\usepackage{tikz}
\usepackage{float}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{enumerate}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsmath}
\hypersetup{
unicode=true, % non-Latin characters in bookmarks
pdffitwindow=true, % page fit to window when opened
pdfnewwindow=true, % links in new window
pdfkeywords={}, % list of keywords
colorlinks=true, % false: boxed links; true: colored links
linkcolor=black, % color of internal links
citecolor=black, % color of links to bibliography
filecolor=black, % color of file links
urlcolor=black, % color of external links
pdftitle={Implementation of Constructive Negation in Higher-Order Logic Programming},
pdfauthor={Ergys Dona}, % author
pdfsubject={Logic Programming} % subject of the document
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% User-specific package inclusions %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[]
\newtheorem{example}{Example}[chapter]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% User-specific configuration %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \setcounter{tocdepth}{1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% User-specific configuration %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\h}{$\mathcal{H}$}
\newcommand{\hcn}{$\mathcal{H}_\mathsf{cn}$}
\newcommand{\msf}[1]{$\mathsf{#1}$}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Required Metadata %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% First name, last name
%
\authorFirstGr{Ergys}
\authorFirstAbrGr{E.} % abbreviation of first name
\authorMiddleGr{A.} % abbreviation of father's first name
\authorLastGr{Dona}
\authorFirstEn{Ergys}
\authorFirstAbrEn{E.} % abbreviation of first name
\authorMiddleEn{A.} % abbreviation of father's first name
\authorLastEn{Dona}
\authorSn{1115200900148}
%
% The title of the thesis
%
\titleEn{Implementation of Constructive Negation in Extensional Higher-Order Logic Programming}
\titleGr{Υλοποίηση Κατασκευαστικής Άρνησης σε Εκτατικό Λογικό Προγραμματισμό Υψηλής Τάξης}
%
% Month followed by Year
%
\dateGr{ΙΟΥΝΙΟΣ 2017}
\dateEn{JUNE 2017}
%
% Supervisor(s) info
%
\supervisorGr{Πάνος Ροντογιάννης}{Καθηγητής ΕΚΠΑ}
\supervisorGr{Άγγελος Χαραλαμπίδης}{Ερευνητής ΕΚΕΦΕ «Δημόκριτος»}
\supervisorEn{Panos Rondogiannis}{NKUA Professor}
\supervisorEn{Angelos Charalambidis}{N.C.S.R. ``Demokritos'' Researcher}
%
% Abstract, synopsis, inscription, ack, and preface pages.
%
\abstractEn{
\begin{english}
Constructive negation is a logic programming negation method that allows the handling of non-ground negative literals. Logic programming systems which are enhanced with constructive negation may produce not only equalities (assignments or unifications of variables), but also inequalities, that, roughly speaking, restrict variables to be different from some value.
Extensional higher-order logic programming is a logic programming paradigm that extends classical (first-order) logic programming by introducing higher-order terms, while preserving all the well-known properties of the former. These higher-order terms (variables) represent sets, which contain elements for which the former succeed.
An extensional higher-order logic programming language called HOPES (\h{}) has been recently proposed. \h{} has been enhanced with constructive negation to create \hcn{} (HOPES with constructive negation).
While the foundations of \hcn{} are based on strong and well-defined semantics, implementing a logic programming system (interpreter) for it proved to be a real challenge. The reason is that the enhanced definition of the \hcn{} proof procedure does not define any particular method for selecting the next subgoal to be satisfied. We show that, while the language definition guarantees that there exist paths in the proof tree that lead to correct answers, if a naive selection policy (e.g. always selecting the left-most literal, like PROLOG does) is adopted, then one has to take extra measures in order to avoid following paths that will never lead to correct solutions.
The contribution of this thesis consists of the redefinition of some of the rules in the \hcn{} proof procedure. We show that the redefined proof procedure allows the implementation of an interpreter for \hcn{}, which follows the left-most derivation rule of PROLOG and behaves correctly (only gives correct answers) for any given query.
\end{english}
}
\abstractGr{
\begin{greek}
Η κατασκευαστική άρνηση είναι μια μέθοδος άρνησης στον λογικό προγραμματισμό, η οποία επιτρέπει το χειρισμό μη-συγκεκριμενοποιημένων αρνητικών προτάσεων. Τα συστήματα λογικού προγραμματισμού τα οποία είναι επαυξημένα με κατασκευαστική άρνηση μπορούν δυνητικά να παράγουν όχι μόνο ισότητες (αναθέσεις ή ενοποιήσεις μεταβλητών), αλλά και ανισότητες, οι οποίες, γενικά μιλώντας, περιορίζουν τις μεταβλητές έτσι ώστε να είναι διαφορετικές από κάποια τιμή.
Ο εκτατικός λογικός προγραμματισμός υψηλής τάξης είναι ένα παράδειγμα λογικού προγραμματισμού που επεκτείνει τον κλασικό (πρώτης τάξης) λογικό προγραμματισμό εισάγοντας όρους υψηλής τάξης, ενώ παράλληλα διατηρεί τις καλά ορισμένες ιδιότητες του προηγούμενου. Αυτοί οι υψηλής τάξης όροι αναπαριστούν σύνολα, τα οποία περιλαμβάνουν στοιχεία για τα οποία οι προηγούμενοι είναι αληθείς.
Μια γλώσσα λογικού προγραμματισμού υψηλής τάξης με όνομα HOPES (\h{}) έχει προταθεί πρόσφατα. Η γλώσσα αυτή έχει επαυξηθεί με κατασκευαστική άρνηση για να διαμορφωθεί η γλώσσα \hcn{} (HOPES with constructive negation).
Ενώ η σημασιολογία της \hcn{} είναι καλά ορισμένη, η υλοποίηση ενός συστήματος λογικού προγραμματισμού (διερμηνευτή) για την τελευταία αποδείχθηκε δυσκολότερη υπόθεση απ'ότι αναμενόταν. Ο λόγος είναι πως η διαδικασία απόδειξης της \hcn{} δεν υποδεικνύει κανέναν συγκεκριμένο τρόπο επιλογής του επόμενου στόχου για ικανοποίηση. Δείχνουμε πως, ενώ ορισμός της γλώσσας εγγυάται την ύπαρξη μονοπατιών στο δέντρο απόδειξης, τα οποία οδηγούν σε σωστές λύσεις, εάν μια απλοϊκή πολιτική επιλογής (π.χ. η επιλογή πάντα του αριστερότερου υποστόχου, όπως κάνει η PROLOG) έχει υιοθετηθεί, τότε πρέπει να παρθούν επιπλέον μέτρα έτσι ώστε η υλοποίηση να αποφύγει μονοπάτια τα οποία δεν θα οδηγήσουν ποτέ σε σωστές λύσεις.
Η συνεισφορά αυτής της πτυχιακής έγκειται στον επαναορισμό κάποιων εκ των κανόνων της διαδικασίας απόδειξης της \hcn{}. Δείχνουμε ότι η νέα διαδικασία απόδειξης επιτρέπει την υλοποίηση ενός διερμηνευτή για την \hcn{}, o οποίος ακολουθεί τον κανόνα της PROLOG για επιλογή του πάντα αριστερότερου υποστόχου και παράλληλα συμπεριφέρεται σωστά (δίνει σωστές λύσεις μόνο) για κάθε επερώτηση.
\end{greek}
}
\acksEn{
I would like to deeply thank NCSR ``Demokritos'' associate Angelos Charalambidis for his guidance and overall support during the writing of this thesis. His inputs were of vital importance and the working methodology we followed helped me develop many important research skills and maintain a solid workflow.
I would also like to thank NKUA professor Panos Rondogiannis, for always being in line in order to help me and support my efforts. His way of teaching and his courses that I attended have sparkled my interest in the theoretical elements of computer science. This thesis is the result of that sparkle and I am very grateful to him.
}
\prefaceEn{
This thesis constitutes my final step towards completing the undergraduate degree programme in the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. It was carried out while working full-time in CERN, which added to the challenge.
During the process of selecting and taking up a thesis subject, I encountered many interesting topics and had discussions with many professors. I would like to thank them all for their patience, cooperation and tips.
The reason for selecting the current subject was that it was fascinating at first sight. I was no stranger to classical logic programming or high-order programming paradigms (e.g. Haskell), however it was the first time I encountered a combination of them.
\hcn{} is a really expressive language. Its higher-order semantics allow one to write even some of the most complex predicates in a very elegant fashion. Of course the main property that enables such elegance is constructive negation, which is the principal feature of \hcn{}.
\hcn{} borrows the semantics of constructive negation as defined by David Chan for the first-order case. It adapts them to the higher-order semantics of itself and does so perfectly in a theoretical level. However, implementing \hcn{} turned out to be a more challenging task than anticipated. The motivation behind this thesis is the identification and redefinition of those characteristics of the higher-order language \hcn{} that make it difficult for it to be implemented correctly.
A correct implementation of \hcn{} shall enable a whole world of applications to demonstrate themselves in solving interesting problems and this is what we want to achieve via this thesis.
}
\inscriptionEn{
\emph{Absence of evidence is not evidence of absence.} \\[8pt]
}
%
% Subject area and keywords
%
\subjectAreaGr{Λογικός Προγραμματισμός}
\subjectAreaEn{Logic Programming}
\keywordsGr{λογική, προγραμματισμός, άρνηση, υψηλή τάξη, εκτατικός}
\keywordsEn{logic, programming, negation, high-order, extensional}
%
% Set the .bib file containing your paper publications (leave the extension out)
%
% This is optional, but it should be specified when option 'lop' is passed to
% the document class.
%
% Then, inside the document environment, you may use the command '\nocitelop' to
% site your papers, as you would traditionally do with the commands '\cite' or
% '\nocite'.
%
% The papers are printed in reverse chronological order.
%
%\lopfile{mypapers/pubs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Required Metadata %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\frontmatter
\mainmatter
\chapter{INTRODUCTION}
\label{chap:intro}
\section{Objective}
\hcn{} is a extensional, higher-order, logic programming language with costructive negation \cite{DBLP:conf/kr/CharalambidisR14}. All the terms in the previous sentence will be explained later on. An implementation of this language has been attempted and can be found at the following GitHub repository: \url{https://github.com/acharal/hopes}.
While the aforementioned implementation works fine for positive first and higher-order terms, as well as for negative first-order terms, when it comes to negative higher-order terms, it falls short to deliver the intended results.
The objective of this thesis is, initially, the identification of the rules in the proof procedure of \hcn{} that are problematic and contribute to the impractical behaviour of the implementation. Thereinafter, the various ways to fix the unwanted behaviour shall be considered and a solution to the problem shall be given.
\section{Motivation}
Some problems (i.e. predicates) can be expressed better and more elegantly via negation. Giving the programmer the ability to write such programs, makes the language a very powerful tool.
\begin{example}\label{ex:subset}
Consider the following higher-order logic program:
\begin{lstlisting}[language=Prolog,frame=single]
subset(P, Q) :- not(non_subset(P, Q)).
non_subset(P, Q) :- P(X), not(Q(X)).
\end{lstlisting}
The above program contains two rules to define the \emph{subset} relation:
\begin{itemize}
\item \msf{P} is a subset of \msf{Q} if it does not hold that \msf{P} is not a subset of \msf{Q}.
\item \msf{P} is not a subset of \msf{Q} if there exists an \msf{X}, such that \msf{X} is in \msf{P}, but not in \msf{Q}.
\end{itemize}
Therefore, if there exists a predicate \msf{q} that is true for atoms \msf{0}, \msf{1} and \msf{2}, then the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- subset(P, q).
\end{lstlisting}
would return in \msf{P} all subsets of the set \msf{\left\{ 0, 1, 2 \right\}}.
What makes the above program powerful, is the ability to write the first rule, which makes use of constructive negation, in order to obtain answers by negating the very simple second rule. The second rule itself is not very useful in the sense that it states something very trivial.
If we try and think of how we would write the same program in a positive logic, then we would normally have to check whether every element of \msf{P} also exists in \msf{Q}. Namely, for all \msf{X \in P}, is \msf{Q(X)} true? This is considerably harder to express, if possible at all (in Horn logic), because it involves iterating through \msf{P} (in some way). However, the problem is way more easy to express in a negative logic: \msf{P} is a subset of \msf{Q} if there doesn't exist any \msf{X}, such that \msf{X} is in \msf{P} but not in \msf{Q}. Therefore, negation adds to the expressivity of the language, i.e. there are programs using negation that cannot be rephrased as positive ones.
\end{example}
The most common logic approach when it comes to negation in logic programming is negation as failure. Unfortunately, due to its nature, negation as failure does not provide variable bindings for a negated query. In SLDNF (the basic negation as failure proof procedure) only ``allowed'' programs (a subclass of all programs) can be proved. If the program is not allowed, then the correctness theorem for SLDNF does not hold. The real issue for higher-order logic programming is that there are many programs which are not allowed at all. Constructive negation alleviates both the above restrictions by introducing inequalities and the ``not in'' property for higher-order (set) variables. In the example above, notice that if the \texttt{not(non\_subset(P,q))} subgoal could not provide variable bindings, it would be impossible to answer the \texttt{subset(P,q)} query.
% The most common logic approach when it comes to negation in logic programming is negation as failure. Unfortunately, due to its nature, negation as failure does not provide variable bindings for a negated query. We would like an approach that, given a negated query, it returns variable bindings (or inequalities). Constructive negation does exactly that. In the example above, notice that if the \texttt{not(non\_subset(P,q))} subgoal could not provide variable bindings, it would be impossible to answer the \texttt{subset(P,q)} query.
The above example lays solid ground for solving generate and test type of problems. Suppose that we have a collection of objects that we want to test against some property. The \texttt{subset} predicate could serve as the generator of sets of objects that we want to test and will prove of special importance in the rest of the thesis. What we want, is to successfully implement the proof procedure that will give the expected answers to queries in programs like the one in example \ref{ex:subset}.
\subsection*{Preference-based queries}
The above paradigm can be applied to another interesting class of applications. An interpreter that supports higher-order predicates in conjunction with constructive negation is a system that can potentially answer preference-based queries.
In \cite{DBLP:conf/ppdp/CharalambidisRT16}, Charalambidis et al. propose the use of higher-order logic programming as a logical framework that handles preference-based queries. The aforementioned framework supports both \emph{preferences over tuples}, as well as \emph{preferences over sets}. It is shown that while the former can be easily expressed in first-order logic as well, preferences over tuples are more complex and could be better approached by using a higher-order logic programming language that supports existential higher-order variables. The extensional approach that is presented in \cite{DBLP:conf/ppdp/CharalambidisRT16}, is a natural candidate for implementing preferences over sets since the binding for higher-order variables are essentially sets.
In extensional languages, two sets that contain the same elements are considered equal. Working with sets in an extensional way is one of the main motivating points that led to the creation of \h{}. Working with and stating set preferences is one of the main motivating points that led to the creation of \hcn{}.
To give a flavour of the preferences over sets paradigm, imagine that there is a collection of books of various genres. Imagine that you have decided that you want to buy three books for your summer holidays and you really want them to be detective novels. However, you can only spend a limited time reading books and you get bored easily (it is summer after all), so you want them to have as few pages as possible.
Notice the order of preferences above. The first is the genre. You absolutely enjoy reading detective novels while on the beach. Even if a book of another genre contains fewer pages, you still prefer detective novels. This order of preferences may, depending on the collection of books, force you to buy less than three books (in case there are not enough, i.e. less than three, books of the selected genre in the collection).
The above problem can be reduced to the selection of the subsets of a collection of books that satisfy the imposed preferences. Those preferences over sets can be expressed more elegantly and more efficiently in an extensional higher-order language with constructive negation \cite{DBLP:conf/ppdp/CharalambidisRT16}.
\section{Overview}
To get a taste of where and how the current approach fails, consider a higher-order logic program containing the rules of example \ref{ex:subset}. The problematic point, which will be fully elaborated later, is the way that the current implementation constructs the higher-order variable values (sets).
Given the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- subset(P, q).
\end{lstlisting}
Then, the naive approach which is currently employed and involves the selection of the left-most subgoal (exactly like PROLOG does), will answer:
\begin{lstlisting}[language=Prolog,frame=single]
yes
P = { 0 } ;
yes
P = { 0, 0 } ;
yes
P = { 0, 0, 0 } ;
...
\end{lstlisting}
The problem comes down to the way the next candidate value to include in \msf{P} is considered. The naive approach takes no information into account from the context (namely, the values already in \msf{P}) and leads to the above result. The problem is two-fold: not only the returned answers contain duplicates, but also the procedure gets ``stuck'', i.e. some correct answers will never be returned (e.g. in the above, the implementation will continue returning ``sets'' that only contain zeroes, for ever).
The proposed approach involves the introduction of restrictions (constraints), so that the answer set which represents \msf{P} will only contain the expected members. This will in turn enable the implementation to avoid getting into a loop of returning meaningless answers, like above.
\section{Thesis Structure}
The rest of this thesis is organised as follows. Chapter \ref{chap:background} provides some background on the essential terms of negation as failure, constructive negation and higher-order logic programming. In chapter \ref{chap:hcn} the higher-order logic programming language \hcn{} is defined. In chapter \ref{chap:proof} the rules that make up the proof procedure of \hcn{} are outlined and the problem that makes the implementation challenging is identified and presented. Chapter \ref{chap:approach} shows the proposed approach to overcome the problem and outlines possible limitations. Finally, chapter \ref{chap:conclusion} concludes the thesis and outlines the various possibilities for future work.
\chapter{BACKGROUND}
\label{chap:background}
\section{Negation as Failure}
Negation as failure \cite{Lloyd:1987:FLP:39279} is the most widely used approach for handling negation in logic programming systems. The simplicity in its implementation as well as its efficiency make it a really attractive option to employ.
These two advantages of negation as failure originate from the simplicity of its operational semantics: \emph{If P cannot be proved based on the facts of the knowledge base (of the program), then assume that P is false}. The previous statement is also know as the \emph{closed world assumption rule}. This is an extremely simple approach that greatly simplifies the handling of negative literals. Given a negative literal, e.g. \msf{not(p)}, all one has to do is run the positive version of it, i.e. \msf{p}. If it succeeds, then fail. If it fails, then succeed.
This behaviour can be easily demonstrated if we consider the usual negation as failure implementation in Prolog, using cut and fail:
\begin{lstlisting}[language=Prolog,frame=single]
not(Goal) :- call(Goal), !, fail.
not(Goal).
\end{lstlisting}
A closer look at the above implementation immediately reveals the basic restriction of negation as failure. Note that nowhere in the above description variable bindings are discussed. This is because negation as failure can only be used as a test against program facts and cannot produce any variable bindings. This means that it can only handle ground literals (that is, literals that only contain ground atoms or, put otherwise, literals containing no variables).
\begin{example} Consider the following logic program:
\begin{lstlisting}[language=Prolog,frame=single]
p(X) :- not(q(X)).
q(X) :- not(r(X)).
r(1).
r(2).
\end{lstlisting}
and the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- p(X).
\end{lstlisting}
Negation as failure will indeed confirm that \msf{p(X)} is true by answering yes, but it will be unable to provide any bindings for \msf{X}. This is due to the \texttt{fail} in the implementation, which causes any bindings that may have been produced by the \texttt{call} meta-predicate call to be lost.
\end{example}
Another problem with negation as failure is that its implementation is unsound.
\begin{example} Consider the following logic program:
\begin{lstlisting}[language=Prolog,frame=single]
p(X) :- not(q(X)), r(X).
q(a).
r(b).
\end{lstlisting}
Considering the semantics of negation as failure, the predicate \msf{p(X)} seems to succeed for \msf{X = b} and to fail for \msf{X = a}. Indeed, that is the case:
\begin{lstlisting}[language=Prolog,frame=single]
?- p(a).
no
?- p(b).
yes
\end{lstlisting}
Therefore, if a query that asks for all \msf{X}, such that \msf{p(X)} is true is posed, the answer \msf{X = b} is expected. However, this is not the case because, as already stated, negation as failure cannot handle non-ground negative literals in a sound manner:
\begin{lstlisting}[language=Prolog,frame=single]
?- p(X).
no
\end{lstlisting}
\end{example}
\section{Constructive Negation}
The concept of Constructive Negation was first introduced by Chan \cite{DBLP:conf/iclp/Chan88} as an alternative to negation as failure. Constructive negation promises the handling of negative non-ground literals, by returning not only variable equalities (bindings) but also inequalities.
The idea behind constructive negation is simple: if a negated query, e.g. \msf{\lnot Q} is given, we run the positive version of it, i.e. \msf{Q}, and obtain the answers as a disjunction. Then, we return the negation of answers to \msf{Q} as an answer to \msf{\lnot Q}.
\begin{example} Suppose that we have the following very simple knowledge base:
\begin{lstlisting}[language=Prolog,frame=single]
p(a).
p(b).
p(c).
\end{lstlisting}
and the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- not(p(X)).
\end{lstlisting}
We first run the positive version of the query, i.e. \msf{p(X)} and obtain the answer the disjunction: \msf{X = a \lor X = b \lor X = c}. Then, we return the negated disjunction as an answer to the original query: \msf{X \ne a \land X \ne b \land X \ne c}.
\end{example}
\begin{example} Consider the following logic program:
\begin{lstlisting}[language=Prolog,frame=single]
p(X) :- not(q(X)).
q(a).
q(b).
\end{lstlisting}
then, for the query \msf{p(X)} the answer will be:
\begin{lstlisting}[language=Prolog,frame=single]
?- p(X).
yes
X /= a, X /= b
\end{lstlisting}
\end{example}
In the above example, notice how inequalities can be generated at any point of the computation where a negative literal is encountered. This resembles the constraint logic programming scheme, however simpler, because the restrictions here are only inequalities (\msf{\ne}) and not e.g. \msf{\le} or \msf{\ge}.
Please note that the above examples are fairly simple, in order to demonstrate the idea of constructive negation. In reality, a more complex formula is used in order to negate the answers to the positive version of the negated query. Furthermore, in both examples, notice how the positive version of the negated query returns a finite number of answers. How does constructive negation handle the negation of a disjunction of infinite answers? This issue is addressed in \cite{DBLP:conf/slp/Chan89}, where the constructive negation rule is extended to support the negation of infinite answers.
We will not analyse the techniques that are used in \cite{DBLP:conf/iclp/Chan88} and \cite{DBLP:conf/slp/Chan89} in order to apply the constructive negation rule in first-order logic programming. The reader is encouraged to refer to the respective texts. However, we will outline an important equivalence, that the constructive negation scheme makes use of when it encounters equalities, in order to negate the answers. This equivalence is of special importance to us, because it is the one that gets extended in order to apply the constructive negation rule to higher-order variables.
Let the goal list be \msf{A = A_1, \ldots, A_n} and the selected expression be \msf{A_i}, where \msf{A_i} is \msf{X=s}. Also, let \msf{A'} be the rest of the goal list except \msf{A_i}, i.e. \msf{A' = A_1, \ldots, A_{i-1}, A_{i+1}, \ldots, A_{n}}. Then, the negation formula is the following:
\begin{equation}\label{eq:cneg}
\forall \mathsf{X} \Big(
\lnot\exists \textbf{Y, Z} \big( \mathsf{X=s} \land \mathsf{A'} \big) ~\equiv~
\lnot\exists \textbf{Y} \left(\mathsf{X=s}\right) \lor \exists \textbf{Y} \big( \mathsf{X=s} \land \lnot\exists \textbf{Z} ~ \mathsf{A'} \big)
\Big)
\end{equation}
where \msf{\textbf{Y}} are the variables in \msf{s} and the free variables in \msf{A'} are contained in \msf{\textbf{Y}} and \msf{\textbf{Z}}.
The above formula is implied by the equality theory and is the mathematical formalisation of taking each component from the goal list \msf{A}, negating it and then recombining the components. We will later extend the above formula to apply to higher-order terms.
To better understand why the above formula works, consider the following:
\begin{align*}
\mathsf{\lnot \left( A \land B \right)} & ~\Leftrightarrow~ \mathsf{\lnot A \lor \lnot B} & ~ \\
& ~\Leftrightarrow~ \mathsf{true \land \left( \lnot A \lor \lnot B \right)} & \left( \mathsf{true} \land P \Leftrightarrow P \right) \\
& ~\Leftrightarrow~ \mathsf{\left( \lnot A \lor A \right) \land \left( \lnot A \lor \lnot B \right)} & \left( P \lor \lnot P \Leftrightarrow \mathsf{true} \right) \\
& ~\Leftrightarrow~ \mathsf{\lnot A \lor \left( A \land \lnot B \right)} & (\text{rev. distributive law})
\end{align*}
The reason why Chan decides to use the above formula in \cite{DBLP:conf/iclp/Chan88} when negating equalities is that the \msf{X=s} equality in the second term of the right-hand side of the equivalence may generate some interesting variable bindings that may lead to some extra welcome answers.
\begin{example} Consider the following logic program that defines the \texttt{even} predicate:
\begin{lstlisting}[language=Prolog,frame=single]
even(0).
even(s(X)) :- not(even(X)).
\end{lstlisting}
\end{example}
The above program defines a negation-based rule for the \texttt{even} property of natural numbers, which are encoded in the following notation:
\[
\mathsf{s^{n}(0) = n, ~~ n \ge 0}
\]
When asked the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- even(X).
\end{lstlisting}
then using constructive negation, the implementation will answer:
\begin{lstlisting}[language=Prolog,frame=single]
yes
X = 0 ;
yes
X = s(V2)
V2 /= 0
V2 /= s(*V5) ;
yes
X = s(s(0)) ;
yes
X = s(s(s(V10)))
V10 /= 0
V10 /= s(*V14) ;
yes
X = s(s(s(s(0)))) ;
...
\end{lstlisting}
The second and the fourth answers above give a format for \texttt{X} along with some restrictions for some variables and they result from the constructive negation rule in equation \ref{eq:cneg}. For example, the second answer says that \texttt{X} is of the form \texttt{s(V2)}, but \texttt{V2} is neither \texttt{0} nor \texttt{s(V5)}, for all \texttt{V5} (the star universally quantifies the following variable).
\section{Extensional Higher-Order Logic Programming}
Extensional higher-order logic programming was originally introduced by William W. Wadge \cite{DBLP:conf/slp/Wadge91} with Charalambidis et al. having also worked on that idea recently \cite{DBLP:journals/tocl/CharalambidisHRW13}. The paragim is essentially an extension of classical, first-order logic programming and its principal idea is that program predicates denote sets of objects and one can reason about such sets, i.e. use them in rules and facts. By looking at the name of this paradigm, we can see that it is made up of two terms: \emph{extensional} and \emph{higher-order}.
The term \emph{higher-order} refers to the capability of applying and passing around predicates and predicate variables as parameters. This is the most popular feature of higher-order logic programming languages like HiLog and allows for the sets-based reasoning that was mentioned above.
The term \emph{extensional} refers to the way program predicates can be passed and used, or otherwise, to what makes up a predicate or what a predicate really is. In an extensional language, two predicates are considered to be equal (i.e. the same) if they are true for the same set of arguments. The opposite of this term is \emph{intentional} and in languages belonging to this category (e.g. HiLog), predicates are more than just a set of arguments for which they are true (e.g. they are also discriminated by their name).
The difference between intensionality and extensionality is a fundamental one and, while at first sight intensionality may seem more natural for real world relations, extensionality better materialises our mathematical definition of what a relation is. To better understand the above, consider the following example from \cite{DBLP:journals/tocl/CharalambidisHRW13}.
\begin{example} Consider the predicate \texttt{all\_members(L, P)}, which is true if all elements of a list \texttt{L} have property \texttt{P}:
\begin{lstlisting}[language=Prolog,frame=single]
all_members([], P).
all_members([H|T], P) :- P(H), all_members(T, P).
\end{lstlisting}
If there exist two predicates \texttt{p} and \texttt{q} in our program that are true for exactly the same terms, e.g. the terms \texttt{a}, \texttt{b} and \texttt{c}, then we would expect both the following queries to succeed:
\begin{lstlisting}[language=Prolog,frame=single]
?- all_members([a,b,c], p).
yes
?- all_members([a,b,c], q).
yes
\end{lstlisting}
However, this is not guaranteed under the context of an intensional language.
\end{example}
The notions of extensional higher-order logic programming and constructive negation proved to be very compatible and, based on that observation, the \hcn{} language was created \cite{DBLP:conf/kr/CharalambidisR14}.
\chapter{THE HIGHER-ORDER LANGUAGE \hcn}
\label{chap:hcn}
\section{Preliminaries}
The reader is presumed to be familiar with the basic concepts and definitions of classical Logic Programming \cite{Lloyd:1987:FLP:39279}. For example, the concepts \textbf{term}, \textbf{formula}, \textbf{atom}, \textbf{(program) clause} and \textbf{goal} are defined as in classical Logic Programming, with slight extensions on their definitions where necessary.
\section{Basic Definitions}
We describe the language by giving some definitions from the text where \hcn{} is defined \cite{DBLP:conf/kr/CharalambidisR14}, for the readers' convenience. These definitions are crucial to the understanding of the rest of the text.
\begin{definition}{\emph{Types}}
The type system of \hcn{} is based on two base types: $o$, the type of the boolean domain and $\iota$, the type of the individuals (data objects). For example, every classical logic programming term is of type $\iota$.
The types of \hcn{} are defined as follows:
\begin{center}
\begin{tabular}{llll}
$\sigma$ & $:=$ & $\iota ~~|~ \left( \iota \rightarrow \sigma \right)$ & \hspace*{2.5cm} \emph{(functional)} \\
$\rho$ & $:=$ & $\iota ~~|~ \pi$ & \hspace*{2.5cm} \emph{(argument)} \\
$\pi$ & $:=$ & $o ~~|~ \left( \rho \rightarrow \pi \right)$ & \hspace*{2.5cm} \emph{(predicate)} \\
\end{tabular}
\end{center}
The argument type $\rho$ consists of the following subtypes:
\begin{center}
\begin{tabular}{llll}
$\mu$ & $:=$ & $\iota ~~|~ \kappa$ & \hspace*{2.5cm} \emph{(existential)} \\
$\kappa$ & $:=$ & $\iota \rightarrow o ~~|~ \left( \iota \rightarrow \kappa \right)$ & \hspace*{2.5cm} \emph{(set)} \\
\end{tabular}
\end{center}
\end{definition}
The operator \msf{\rightarrow} is right-associative.
Therefore, a functional type that is different from \msf{\iota} can also be written as \msf{\iota^n \rightarrow \iota}, \msf{n \ge 1}.
Similarly, a set type can also be written in the form \msf{\iota^n \rightarrow \mathnormal{o}}, \msf{n \ge 1}.
Finally, every predicate type $\pi$ can be written in the form \msf{\mathnormal{\rho}_1 \rightarrow \dots \rightarrow \mathnormal{\rho}_n \rightarrow \mathnormal{o}}, \msf{n \ge 0} (for \msf{n = 0} we assume that $\pi = o$).
\begin{definition}{\emph{Alphabet}}
The alphabet of \hcn{} consists of:
\begin{enumerate}
\item \emph{Predicate variables} of every predicate type $\pi$.
\item \emph{Predicate constants} of every predicate type $\pi$.
\item \emph{Individual variables} of type $\iota$.
\item \emph{Individual constants} of type $\iota$.
\item \emph{Function symbols} of every functional type $\sigma \ne \iota$.
\item The following logical constant symbols:
\begin{enumerate}
\item the \emph{propositional constants} \msf{false} and \msf{true}
of type $o$;
\item the \emph{equality constant} \msf{\approx}
of type $\iota \rightarrow \iota \rightarrow o$;
\item the \emph{generalised disjunction and conjunction constants} \msf{\bigvee_{\pi}} and \msf{\bigwedge_{\pi}}
of type $\pi \rightarrow \pi \rightarrow \pi$,
for every predicate type $\pi$;
\item the \emph{equivalence constants} $\leftrightarrow_{\pi}$
of type $\pi \rightarrow \pi \rightarrow o$,
for every predicate type $\pi$;
\item the \emph{existential quantifiers} $\exists_{\mu}$
of type $\left( \mu \rightarrow o \right)$,
for every existential type $\mu$;
\item the \emph{negation constant} $\sim$ of type $o \rightarrow o$.
\end{enumerate}
\item The \emph{abstractor} $\lambda$ and the \emph{parentheses} ``\msf{(}'' and ``\msf{)}''.
\end{enumerate}
We define the set consisting of the predicate variables and the individual variables of \hcn{}, as the \emph{argument variables} of \hcn{}.
\end{definition}
\begin{definition}{\emph{Body expressions}}
The set of body expressions of \hcn{} is recursively defined as follows:
\begin{enumerate}
\item
\begin{enumerate}
\item Every predicate variable (respectively, predicate constant) of type $\pi$ is a body expression of type $\pi$;
\item Every individual variable (respectively, individual constant) of type $\iota$ is a body expression of type $\iota$;
\item The propositional constants \msf{false} and \msf{true} are body expressions of type $o$.
\end{enumerate}
\item If \msf{f} is an \msf{n}-ary function symbol and \msf{E_1, \ldots, E_n} are body expressions of type $\iota$, then \msf{\left( f ~~ E_1 ~ \cdots ~ E_n \right)} is a body expression of type $\iota$.
\item If \msf{E_1} is a body expression of type $\rho \rightarrow \pi$ and \msf{E_2} is a body expression of type $\rho$, then \msf{\left( E_1 ~ E_2 \right)} is a body expression of type $\pi$.
\item If \msf{V} is an argument variable of type $\rho$ and \msf{E} is a body expression of type $\pi$, then \msf{\left( \lambda V. E \right)} is a body expression of type $\rho \rightarrow \pi$.
\item If \msf{E_1, E_2} are body expressions of type $\pi$, then \msf{\left( E_1 \bigwedge_{\pi} E_2 \right)} and \msf{\left( E_1 \bigvee_{\pi} E_2 \right)} are body expressions of type $\pi$.
\item If \msf{E_1, E_2} are body expressions of type $\iota$, then \msf{\left( E_1 \approx E_2 \right)} is a body expression of type $o$.
\item If \msf{E} is a body expression of type $o$ and \msf{V} is an existential variable of type $\mu$, then \msf{\left( \exists_{\mu} V ~ E \right)} is a body expression of type $o$.
\item If \msf{E} is a body expression of type $o$, then \msf{\left( \sim E \right)} is a body expression of type $o$.
\end{enumerate}
\end{definition}
The notions of \emph{free} and \emph{bound} variables are defined as usual. A body expression is called \emph{closed} if it does not contain any free variables.
In the following, we will write \msf{\widehat{A}} to denote a (possibly empty) sequence \msf{\langle A_1, \ldots, A_n \rangle}.
A body expression of the form \msf{\left( E_1 \approx E_2 \right)} will be called an \emph{equality}, while a body expression of the form \msf{\sim \exists \widehat{V} \left( E_1 \approx E_2 \right)} will be called an \emph{inequality}; in the latter case \msf{\widehat{V}} may be empty, in which case the inequality is of the form \msf{\sim \left( E_1 \approx E_2 \right)}.
Let \msf{\widehat{E}} and \msf{\widehat{E}'}, where all \msf{E_i}, \msf{E_i'} are of type $\iota$. We will write \msf{\left( \widehat{E} \approx \widehat{E}' \right)} to denote the expression \msf{\left( E_1 \approx E_1' \right) \land \dots \land \left( E_n \approx E_n' \right)}; if \msf{n = 0}, then the conjunction is the constant \msf{true}.
\begin{definition}{\emph{Clausal expressions}}
The set of clausal expressions of \hcn{} is defined as follows:
\begin{enumerate}
\item If \msf{p} is a predicate constant of type $\pi$ and \msf{E} is a closed body expression of type $\pi$, then \msf{p \leftarrow_{\pi} E} is a clausal expression of \hcn{}, also called a \emph{program clause}.
\item if \msf{E} is a body expression of type $o$ and each free variable in \msf{E} is of type $\mu$ (existential), then \msf{false \leftarrow_{o} E} (or \msf{\leftarrow_{o} E}, or just \msf{\leftarrow E} is a clausal expression of \hcn{}, also called a \emph{goal clause}.
\item If \msf{p} is a predicate constant of type $\pi$ and \msf{E} is a closed body expression of type $\pi$, then \msf{p \leftrightarrow_{\pi} E} is a clausal expression of \hcn{}, also called a \emph{completion expression}.
\end{enumerate}
All clausal expressions of \hcn{} have type $o$.
\end{definition}
\begin{definition}{\emph{Program}}
A program of \hcn{} is a finite set of program clauses of \hcn{}.
\end{definition}
\begin{definition}{\emph{Completed definition of predicate}}\label{def:compdef}
Let \msf{P} be a program and let \msf{p} be a predicate constant of type $\pi$. Then, the \emph{completed definition} for \msf{p} with respect to \msf{P} is obtained as follows:
\begin{itemize}
\item if there exist exactly \msf{k > 0} program clauses of the form \msf{p \leftarrow_{\pi} E_{i}}, where \msf{i \in \left\{ 1, \ldots, k \right\}} for \msf{p} in \msf{P}, then the completed definition for \msf{p} is the expression \msf{p \leftrightarrow_{\pi} E}, where \msf{E = E_1 \bigvee_{\pi} \cdots \bigvee_{\pi} E_k}.
\item if there are no program clauses for \msf{p} in \msf{P}, then the completed definition for \msf{p} is the expression \msf{p \leftrightarrow_{\pi} E}, where \msf{E} is of type $\pi$ and \msf{E = \lambda \widehat{X}.false}.
\end{itemize}
The expression \msf{E} on the right-hand side of the completed definition of \msf{p} will be called the \emph{completed expression} for \msf{p} with respect to \msf{P}.
\end{definition}
\begin{definition}{\emph{Completion of program}}
Let \msf{P} be a program. Then, the \emph{completion} \msf{comp(P)} of \msf{P} is the set consisting of all the completed definitions for all predicate constants that appear in \msf{P}.
\end{definition}
\section{Examples}
The following examples consist valid higher-order logic programs with respect to \hcn{}.
\begin{example} Consider the following program:
\begin{lstlisting}[language=Prolog,frame=single]
p(Q) :- Q(a), Q(b).
\end{lstlisting}
When taking into account that \texttt{p} represents a set, we can read the above rule as: ``\texttt{p} is a predicate that is true for all relations that contain, at least, \texttt{a} and \texttt{b}''.
Therefore, given the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- p(R).
\end{lstlisting}
the implementation is expected to answer something of the form:
\begin{lstlisting}[language=Prolog,frame=single,mathescape=true]
yes
R = { a, b } $\cup$ L
\end{lstlisting}
Following \hcn{} syntax, the above rule would be written as:
\begin{lstlisting}[mathescape=true]
p $\leftarrow$ $\lambda$Q.((Q a) $\land$ (Q b))
\end{lstlisting}
In the above notation, notice how the parameter list of the definition of \texttt{p} is placed on the right hand side (because of \texttt{p} being defined as a lambda expression).
\end{example}
\begin{example} Consider the following higher-order logic program:
\begin{lstlisting}[language=Prolog,frame=single]
band(B) :- singer(S), B(S), guitarist(G), B(G).
\end{lstlisting}
The above rule states that \texttt{B} is a band if it contains at least one singer and one guitarist. Notice how \texttt{S} and \texttt{G} are first-order variables, while \texttt{B} is a higher-order (to be exact, second-order) one.
One interesting thing to notice here, is how \texttt{B} is constructed. Subgoal \texttt{singer\{G\}} will enumerate all possible singers, while subgoal \texttt{guitarist\{G\}} will enumerate all possible guitarists. Finally, subgoal \texttt{B\{X\}} ``associates'' (includes) the respective \texttt{X} element to the answer set.
Therefore, given a knowledge base consisting of singers and guitartists, the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- band(B).
\end{lstlisting}
will return in \texttt{B} all the combinations of the singers and guitartists that may consist a band (with respect to the definition given by the program).
Following \hcn{} syntax, the above rule would be written as:
\begin{lstlisting}[mathescape=true]
band $\leftarrow$ $\lambda$B.$\exists$S,G((singer S) $\land$ (B S) $\land$ (guitarist G) $\land$ (B G))
\end{lstlisting}
and the query, would be written as:
\begin{lstlisting}[mathescape=true]
$\leftarrow$ band B
\end{lstlisting}
As explained in \cite{DBLP:conf/kr/CharalambidisR14}, \hcn{} operates under the assumption that free variables in a query are the ones for which the proof procedure will attempt to find bindings in order for the goal to be satisfied. That is why variable \texttt{B} above is not existentially quantified.
\end{example}
\chapter{THE PROOF PROCEDURE}
\label{chap:proof}
The proof procedure of \hcn{} incorporates and adapts the constructive negation rules, as described by David Chan in \cite{DBLP:conf/slp/Chan89}, to the higher-order case.
Before outlining the procedure itself, we give the following definition, which categorises the various inequalities based on the expressions on their left and right hand side (\cite{DBLP:conf/iclp/Chan88}, \cite{DBLP:conf/kr/CharalambidisR14}).
\begin{definition}{}
\label{def:inequalities}
An inequality \msf{\sim\exists \widehat{V} \left( E_1 \approx E_2 \right)} is considered:
\begin{itemize}
\item \emph{valid} if \msf{E_1} and \msf{E_2} cannot be unified;
\item \emph{unsatisfiable} if is there is a substitution $\theta$ that unifies \msf{E_1} and \msf{E_2} and contains only bindings of variables in \msf{\widehat{V}};
\item \emph{satisfiable} if it is not unsatisfiable.
\end{itemize}
Also an inequality will be called \emph{primitive} if it is satisfiable, non-valid and either \msf{E_1} or \msf{E_2} is a variable.
\end{definition}
\section{The procedure}
In this section, we define the proof procedure, as proposed by Charalambidis et al. in \cite{DBLP:conf/kr/CharalambidisR14}. The procedure consists of three definitions. We explain each definition in its own section.
\begin{definition}{\emph{Single-step derivation}}
\label{def:singlestepderivation}
Let \msf{P} be a program and let \msf{G_{k}} and \msf{G_{k+1}} be goal clauses.
Let \msf{G_{k}} be a conjunction \msf{\leftarrow A_1 \land \dots \land A_n}, where each \msf{A_i} is a body expression of type $o$.
Let \msf{A_i} be one of \msf{A_1, \ldots, A_n} and let us call it the \emph{selected expression}.
Let \msf{A' = A_1 \land \dots \land A_{i-1} \land A_{i+1} \land \dots \land A_n}.
We say that \msf{G_{k+1}} \emph{is derived in one step} from \msf{G_k} using \msf{\theta}, if one of the following conditions applies:
\begin{enumerate}
\item if \msf{A_i} is \msf{true} and \msf{n > 1},
then \msf{G_{k+1} = \leftarrow A'} is derived from \msf{G_k}
using \msf{\theta = \epsilon};
\item if \msf{A_i} is \msf{\left( E_1 \lor E_2 \right)},
then \msf{G_{k+1} = \leftarrow A_1 \land \dots \land E_j \land \dots \land A_n} is derived from \msf{G_k}
using \msf{\theta = \epsilon}, where \msf{j \in \{1,2\}};
\item if \msf{A_i} is \msf{\left( \exists V ~ E \right)},
then \msf{G_{k+1} = \leftarrow A_1 \land \dots \land E \land \dots \land A_n} is derived from \msf{G_k} using \msf{\theta = \epsilon};
\item if \msf{A_i \rightsquigarrow A_i'}, namely \msf{A_i} is reduced to \msf{A_i'},
then \msf{G_{k+1} = \leftarrow A_1 \land \dots \land A_i' \land \dots \land A_n} is derived from \msf{G_k} using \msf{\theta = \epsilon};
\item if \msf{A_i} is \msf{\left( E_1 \approx E_2 \right)},
then \msf{G_{k+1} = \leftarrow A' \theta} is derived from \msf{G_k} using \msf{\theta = mgu \left( E_1, E_2 \right)};
\item if \msf{A_i} is \msf{\left( R ~ \widehat{E} \right)}
and \msf{R} is a predicate variable of type \msf{\kappa}
then \msf{G_{k+1} = \leftarrow A' \theta} is derived from \msf{G_k}
using \msf{\theta = \left\{ R \middle/ \left( \lambda \widehat{X} . \left( \widehat{X} \approx \widehat{E} \right) \bigvee_{\kappa} R' \right) \right\}},
where \msf{R'} is a fresh predicate variable of type \msf{\kappa};
\item if \msf{A_i} is \msf{\sim \exists \widehat{V} ~ E}
and \msf{A_i} is negatively-reduced to \msf{A_i'}, \\
then \msf{G_{k+1} = \leftarrow A_1 \land \dots \land A_i' \land \dots \land A_n} is derived from \msf{G_k} using \msf{\theta = \epsilon};
\item if \msf{A_i} is \msf{\sim \exists \widehat{V} \left( R ~ \widehat{E} \right)}
and \msf{R} is a predicate variable of type \msf{\kappa}
and \msf{R \not\in \widehat{V}}, \\
then \msf{G_{k+1} = \leftarrow A' \theta} is derived from \msf{G_k}
using \msf{\theta = \left\{ R \middle/ \left( \lambda \widehat{X} . \sim \exists \widehat{V} \left( \widehat{X} \approx \widehat{E} \right) \bigwedge_{\kappa} R' \right) \right\}},
where \msf{R'} is a fresh predicate variable of type \msf{\kappa};
\item is \msf{A_i} is \msf{\sim \exists \widehat{V} \sim \left( R ~ \widehat{E} \right)}
and \msf{R} is a predicate variable of type \msf{\kappa}
and \msf{R \not\in \widehat{V}}, \\
then \msf{G_{k+1} = \leftarrow A' \theta} is derived from \msf{G_k}
using \msf{\theta = \left\{ R \middle/ \left( \lambda \widehat{X} . ~ \exists \widehat{V} \left( \widehat{X} \approx \widehat{E} \right) \bigvee_{\kappa} R' \right) \right\}}.
\end{enumerate}
Definition \ref{def:singlestepderivation} defines the single-step derivation of goals and is actually an extension of single-step derivation of classical logic programming to support higher-order terms and constructive negation. This definition is the most general of the three, in the sense that it delegates the handling of more ``special'' or complex subgoals to the other two definitions.
\end{definition}
\begin{definition}{\emph{Reduction}}
\label{def:reduction}
Let \msf{P} be a program and \msf{E}, \msf{E'} be body expressions of type $o$.
We say that \msf{E} is reduced (with respect to \msf{P}) to \msf{E'} (denoted as \msf{E \rightsquigarrow E'}) if one of following conditions applies:
\begin{enumerate}
\item \msf{p ~ \widehat{A} \rightsquigarrow E ~ \widehat{A}}, where \msf{E} is the completed expression for \msf{p} with respect to \msf{P};
\item \msf{\left( \lambda X. E \right) B ~ \widehat{A} \rightsquigarrow E\left\{ X / B \right\} \widehat{A}};
\item \msf{\left( E_1 \bigvee_{\pi} E_2 \right) \widehat{A} \rightsquigarrow \left( E_1 ~ \widehat{A} \right) \lor_{o} \left( E_2 ~ \widehat{A} \right)};
\item \msf{\left( E_1 \bigwedge_{\pi} E_2 \right) \widehat{A} \rightsquigarrow \left( E_1 ~ \widehat{A} \right) \land_{o} \left( E_2 ~ \widehat{A} \right)}.
\end{enumerate}
Definition \ref{def:reduction} is the simplest definition among the three. It corresponds to simple operations on a subgoal, in order to advance the its evaluation. More specifically, definition \ref{def:reduction}.1 replaces a predicate constant by its defined expressions within the program. Definition \ref{def:reduction}.2 corresponds to a $\beta$-reduction for a lambda expression, whereas definitions \ref{def:reduction}.3 and \ref{def:reduction}.4 ``transform'' the generalised disjunction and conjunction predicate constants $\bigvee_{\pi}$ and $\bigwedge_{\pi}$ to the normal boolean operators $\lor$ and $\land$, respectively.
\end{definition}
\begin{definition}{\emph{Negative reduction}}
\label{def:negreduction}
Let \msf{P} be a program and \msf{B}, \msf{B'} be body expressions where \msf{B = \sim \exists \widehat{U} \left( A_1 \land \dots \land A_n \right)} and each \msf{A_i} is a body expression except for conjunction.
Let \msf{A_i} be the selected expression and \msf{A' = A_1 \land \dots \land A_{i-1} \land A_{i+1} \land \dots \land A_n}.
Then, we say that \msf{B} is \emph{negatively-reduced} to \msf{B'} if one of the following conditions applies:
\begin{enumerate}
\item if \msf{A_i} is \msf{false}, then \msf{B' = true};
\item if \msf{A_i} is \msf{true} and \msf{n = 1}, then \msf{B' = false},
else \msf{B' = \sim \exists \widehat{U} ~ A'};
\item if \msf{A_i} is \msf{\left( E_1 \lor E_2 \right)},
then \msf{B' = B_1' \land B_2'}, \\ where \msf{B_j' = \sim \exists \widehat{U} \left( A_1 \land \dots \land E_j \land \dots \land A_n \right)}, for \msf{j \in \{ 1, 2 \}};
\item if \msf{A_i} is \msf{\left( \exists V ~ E \right)},
then \msf{B' = \sim \exists \widehat{U} V \left( A_1 \land \dots \land E \land \dots \land A_n \right)};
\item if \msf{A_i \rightsquigarrow A_i'}, namely \msf{A_i} is reduced to \msf{A_i'},
then \msf{B' = \sim \exists \widehat{U} \left( A_1 \land \dots \land A_i' \land \dots \land A_n \right)};
\item if \msf{A_i} is \msf{\left( E_1 \approx E_2 \right)}, then:
\begin{enumerate}
\item if \msf{\sim \exists \widehat{U} \left( E_1 \approx E_2 \right)}
is valid, then \msf{B' = true};
\item if \msf{\sim \exists \widehat{U} \left( E_1 \approx E_2 \right)}
is non-valid and neither \msf{E_1} nor \msf{E_2} is a variable,
\\then \msf{B' = \sim \exists \widehat{U} \left( A_1 \land \dots \land A_{i-1} \land \left( \widehat{X} \approx \widehat{X} \theta \right) \land A_{i+1} \land \dots \land A_n \right)},
\\where \msf{\theta = unify \left( E_1, E_2 \right)} and \msf{\widehat{X} = dom\left( \theta \right)};
\item if \msf{\sim \exists \widehat{U} \left( E_1 \approx E_2 \right)} is unsatisfiable and either \msf{E_1} or \msf{E_2} is a variable in \msf{\widehat{U}},
\\then \msf{B' = \sim \exists \widehat{U} \left( A' \theta \right)}, where \msf{\theta = \{ X / E \}} and \msf{X} is the one expression that is a variable in \msf{\widehat{U}} and \msf{E} is the other;
\item if \msf{\sim \exists \widehat{U} \left( E_1 \approx E_2 \right)}
is primitive and \msf{n > 1}, \\
then \msf{B' = \sim \exists \widehat{U}_1 ~ A_i \lor \exists \widehat{U}_1 \left( A_i ~\land \sim \exists \widehat{U}_2 ~ A' \right)}, where \msf{\widehat{U}_1} are the variables in \msf{\widehat{U}} that are free in \msf{A_i} and \msf{\widehat{U}_2} are the variables in \msf{\widehat{U}} not in \msf{\widehat{U}_1};
\end{enumerate}
\item if \msf{A_i} is \msf{\left( R ~ \widehat{E} \right)} and \msf{R} is a predicate variable, then:
\begin{enumerate}
\item if \msf{R \in \widehat{U}},
then \msf{B' = \sim \exists \widehat{U}' \left( A' \theta \right)},
where substitution \msf{\theta = \left\{ R \middle/ \left( \lambda X . \left( X \approx E \right) \bigvee_{\kappa} R' \right) \right\}}, \msf{R'} is a predicate variable of the same type as \msf{R} and \msf{\widehat{U}'} is the same as \msf{\widehat{U}}, except that variable \msf{R} has been replaced with \msf{R'};
\item if \msf{R \not \in \widehat{U}} and \msf{n > 1},
then \msf{B' = \sim \exists \widehat{U}_1 ~ A_i \lor \exists \widehat{U}_1 \left( A_i ~\land \sim \exists \widehat{U}_2 ~ A' \right) \land B},
where \msf{\widehat{U}_1} are the variables in \msf{\widehat{U}} that are free in \msf{A_i} and \msf{\widehat{U}_2} are the variables in \msf{\widehat{U}} not in \msf{\widehat{U}_1};
\end{enumerate}
\item if \msf{A_i} is \msf{\sim \exists \widehat{V} ~ E} and \msf{A_i} is negatively-reduced to \msf{A_i'}, \\
then \msf{B' = \sim \exists \widehat{U} \left( A_1 \land \dots \land A_i' \land \dots \land A_n \right)};
\item if \msf{A_i} is a primitive inequality \msf{\sim \exists \widehat{V} \left( E_1 \approx E_2 \right)}, then:
\begin{enumerate}
\item if \msf{A_i} contains free variables in \msf{\widehat{U}} and \msf{A'} is a conjunction of primitive inequalities, then \msf{B' = \sim \exists \widehat{U} ~ A'};
\item if \msf{A_i} does not contain any free variables in \msf{\widehat{U}}, then \msf{B' = \exists \widehat{V} \left( E_1 \approx E_2 \right) \lor \sim \exists \widehat{U} ~ A'};
\end{enumerate}
\item if \msf{A_i} is \msf{\sim \exists \widehat{V} \left( R ~ \widehat{E} \right)} and \msf{R \not\in \widehat{V}} is a predicate variable,
then:
\begin{enumerate}
\item if \msf{R \in \widehat{U}},
then \msf{B' = \sim \exists \widehat{U}' \left( A' \theta \right)},
where substitution \\ \msf{\theta = \left\{ R \middle/ \left( \lambda X . \sim \exists \widehat{V} \left( X \approx E \right) \bigwedge_{\kappa} R' \right) \right\}},
\msf{R'} is a predicate variable of the same type \msf{\kappa} as \msf{R}
and \msf{\widehat{U}'} is the same as \msf{\widehat{U}}, except that variable \msf{R} has been replaced with \msf{R'};
\item if \msf{R \not\in \widehat{U}} and \msf{n > 1},
then \msf{B' = \sim \exists \widehat{U}_1 ~ A_i \lor \exists \widehat{U}_1 \left( A_i ~ \land \sim \exists \widehat{U}_2 ~ A' \right) \land B},
where \msf{\widehat{U}_1} are the variables in \msf{\widehat{U}} that are free in \msf{A_i} and \msf{\widehat{U}_2} are the variables in \msf{\widehat{U}} not in \msf{\widehat{U}_1};
\item if \msf{R \not\in \widehat{U}}, \msf{n = 1} and \msf{\widehat{V}} is non-empty, \\
then \msf{B' = \exists \widehat{V} \sim \exists \widehat{U} \left( \sim \left( R ~ \widehat{E} \right) \land \sim \exists \widehat{V}' \left( R ~ \widehat{E}' \right) \right)};
\end{enumerate}
\item if \msf{A_i} is \msf{\sim \exists \widehat{V} \sim \left( R ~ \widehat{E} \right)} and \msf{R} is a predicate variable and \msf{R \not\in \widehat{V}}, then:
\begin{enumerate}
\item if \msf{R \in \widehat{U}},
then \msf{B' = \sim \exists \widehat{U}' \left( A' \theta \right)},
where substitution \\ \msf{\theta = \left\{ R \middle/ \left( \lambda X . \exists \widehat{V} \left( X \approx E \right) \bigvee_{\kappa} R' \right) \right\}},
\msf{R'} is a predicate variable of the same type \msf{\kappa} as \msf{R} and \msf{\widehat{U}'} is the same as \msf{\widehat{U}}, except that variable \msf{R} has been replaced with \msf{R'};
\item if \msf{R \not\in \widehat{U}} and \msf{n > 1},
then \msf{B' = \sim \exists \widehat{U}_1 ~ A_i \lor \exists \widehat{U}_1 \left( A_i ~ \land \sim \exists \widehat{U}_2 ~ A' \right) \land B};
\item if \msf{R \not\in \widehat{U}}, \msf{n = 1} and \msf{\widehat{V}} is non-empty, \\
then \msf{B' = \exists \widehat{V} \sim \exists \widehat{U} \left( \left( R ~ \widehat{E} \right) \land \sim \exists \widehat{V}' \sim \left( R ~ \widehat{E}' \right) \right)}.
\end{enumerate}
\end{enumerate}
Definition \ref{def:negreduction} is the most complex of the three definitions that make up the proof procedure. The reason is that it handles negative subgoals. In order to achieve that, it has to employ the constructive negation paradigm as defined by Chan \cite{DBLP:conf/slp/Chan89}, however extended in order to support higher order terms. These higher-order terms are in essence set variables, therefore the procedure in our case shall not only return ``equality'' relations, but also ``belongs to'' relations.
\end{definition}
\section{Extending constructive negation for \h{}}
Recall that predicates in \h{} represent sets. In \h{}, a set is constructed via the $\bigvee_{\pi}$ operator, (of type $\pi \rightarrow \pi \rightarrow \pi$) which is a primitive and generalised version of the set union operator ($\cup$).
\begin{example} Lets assume that a predicate \msf{P} in a program is true for the atoms \msf{a}, \msf{b} and \msf{c}. Therefore, it represents the following set that contains (at least) \msf{a}, \msf{b} and \msf{c}:
\begin{center}
\msf{P = \left\{ a, b, c \right\} \cup L}
\end{center}
The above bracket-notion is just a short (or pretty-print) version of the following:
\begin{center}
\msf{P = \{ a \} ~\bigvee_{\pi}~ \{ b \} ~\bigvee_{\pi}~ \{ c \} ~\bigvee_{\pi}~ L}
\end{center}
\end{example}
In order to extend the constructive negation rule for the higher-order logic programming language \h{}, we have to keep in mind that we are also dealing with higher-order (set) variables like in the example above.
Recall equation \ref{eq:cneg}, which outlines the rule that is applied in constructive negation, when negating equality subgoals. This rule works well when first order literals (equalities or inequalities) are selected and is employed in rule 6(d) of definition \ref{def:negreduction}. We now want to extend this rule to support higher-order predicate variables. The reason why we extend the aforementioned rule is because it is the rule that handles equalities. In the higher-order case, the analogous of an equality, is an element belonging to a set.
When considering the higher-order terms of \h{}, then a more sophisticated version of that formula is required. The reason is that higher-order terms are not ``flat''. To understand what that means, let us consider the principal difference between first and higher-order logic programming: the latter allows the usage of uninstantiated predicate variables, which denote sets. Since they denote sets, we would like to express two properties:
\begin{itemize}
\item that an element belongs to a set.
\item that an element does not belong to a set.
\end{itemize}
The first point is implemented via positive computation logic, while the second point requires negative computation logic. Furthermore, the first point may also be the result of the application of double negation.
What the implementation is supposed to do when it encounters a higher-order (predicate) variable, is to perform a systematic enumeration of the possible values that variable can take. The variable itself represents a set, therefore the implementation must be able to generate multiple values that ``belong'' to the set that the variable represents.
Suppose that a negated expression (goal list) \msf{B} contains the subgoal \msf{(R ~ E)}, where \msf{R} is a predicate variable and \msf{E} is an expression. The way \msf{R} will be constructed is by first adding variables to it and then producing bindings for those variables. It is clear that the formula in equation \ref{eq:cneg} is not enough to add multiple values to \msf{R}, because the two terms of the conjunction will argue about whether \msf{E}, and only \msf{E}, belongs or not to the set.
What we would like to do is to continue arguing about more variables, i.e. try to satisfy the rest of the goal list if \msf{R} contained more variables. In essence, what we want to do is, after having argued about \msf{R} containing \msf{E}, to argue about \msf{R} not containing anything else or \msf{R} containing one more element. If in every step we repeat this process, we can argue about \msf{R} containing as many elements as it can.
The above idea translates to appending \msf{B} (the negated expression) itself in conjunction with the second term of the disjunction in rule \ref{eq:cneg}. This is the approach that \hcn{} takes and is reflected in rules \ref{def:negreduction}.7 (b), \ref{def:negreduction}.10 (b) and \ref{def:negreduction}.11 (b). Notice however, that this approach automatically makes the rule (i.e. the procedure) recursive. And, as we know, for a recursive procedure to stop at some point, there has to be some condition that, when satisfied, stops it. The above procedure seems to have none. We analyse this issue further in the next section.
Notice however that, theoretically, the above procedure is perfectly fine, because the rules do not make any assumption about the expression selection policy. There exist paths in the computation tree that ``escape'' from the infinite recursion and a non-deterministic policy may follow these paths. The problem is that when it comes to implementing things (writing algorithms), we cannot employ non-deterministic strategies.
\section{Identifying the Problem}
While being theoretically correct, the above approach does not work if implemented in a naive way (for example, selecting the left-most reducable literal in every step of the derivation). The reason is the repeated subgoal \msf{B}. As discussed earlier, when a higher-order variable \msf{R} is encountered, \msf{B} is re-added to the goal list in order to explore whether \msf{R} can contain extra elements. However, notice that \msf{B} is repeated exactly ``as is'', without any care being taken in order to restrict the new values that are to be added to \msf{R}.
If we now look at this procedure from an implementation point of view, it is only logical that exactly the same computation steps will be repeated, because \msf{B} is exactly the same as before. It seems inevitable that the new goal will produce exactly the same bindings as the previous one. And this is actually the case, which is easily demonstrable if we look at the proof procedure computation steps of an example.
\begin{example}
\label{ex:subsettrace}
Because program predicates represent sets, the most typical example we can investigate and which also shows off the problem pretty well, is the \texttt{subset} predicate, which was first presented in example \ref{ex:subset}.
This time we make sure that our program also contains some facts, so that we can run \texttt{subset} against the facts' predicate. So, consider the following higher-order logic program:
\begin{lstlisting}[language=Prolog,frame=single]
subset(P, Q) :- not(non_subset(P, Q)).
non_subset(P, Q) :- P(X), not(Q(X)).
q(0).
q(1).
q(2).
\end{lstlisting}
and the query:
\begin{lstlisting}[language=Prolog,frame=single]
?- subset(P, q).
\end{lstlisting}
which asks for all subsets of \msf{q}. Since \msf{q} represents the set \msf{\left\{ 0, 1, 2 \right\}}, normally, 8 answers are expected and their order does not matter. Let us see what happens if we run the query by hand, highlighting the rules of the proof procedure that are applied at each step. In each step, we will show the pending goal list, as well as the unifications that are produced at that point of the computation.
The steps shown next are the steps that the current implementation follows, when given the above program and query. Please note that a left to right derivation order, just like in PROLOG, is followed. Also note that, following PROLOG notation, a comma (\texttt{,}) represents a logical AND, while a semicolon (\texttt{;}) represents a logical OR.
\begin{enumerate}[\bfseries Step 1:]
\item The first step is just the expansion of the \texttt{subset} query:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\lambda$ P Q. $\sim$(non_subset(P, Q))(P, q) ?
\end{lstlisting}
\item By applying reduction rule \ref{def:reduction}.2, we get:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$(non_subset(P, q)) ?
\end{lstlisting}
\item Next, the \texttt{non\_subset} predicate is expanded:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\lambda$ P Q. ($\exists$X P(X), $\sim$Q(X))(P, q)) ?
\end{lstlisting}
\item By applying reduction rule \ref{def:reduction}.2, we get:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 P(V4), $\sim$q(V4)) ?
\end{lstlisting}
Notice that in this step that, while variable \texttt{X} is renamed to \texttt{V4}, the query variable \texttt{P} is left intact.
\item This is the first interesting step of the example. We are inside the scope of a negated expression, therefore one of the negative reduction (definition \ref{def:negreduction}) rules will be applied. Notice that, with respect to the aforementioned definition, \msf{\widehat{U} = \langle \texttt{V4} \rangle}. Since the selected literal is \texttt{P(V4)}, rule \ref{def:negreduction}.7 matches, with \msf{R = \texttt{P}} and \msf{\widehat{E} = \langle \texttt{V4} \rangle}. Since \msf{R \not\in \widehat{U}} and \msf{n=2}, we follow case (b):
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 P(V4));
($\exists$V4 P(V4), $\sim$($\sim$q(V4))), $\sim$($\exists$V4 P(V4), $\sim$q(V4)) ?
\end{lstlisting}
The above is an application of the extended constructive negation rule for higher-order predicate variables, as proposed in \cite{DBLP:conf/kr/CharalambidisR14}.
\item We now have two paths to follow (logical OR). Rule \ref{def:singlestepderivation}.2 is applied and, initially, the first path is followed:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 P(V4)) ?
\end{lstlisting}
\item Rule \ref{def:singlestepderivation}.8 is applied and substitution \msf{\theta=\left\{ \texttt{P} / \left( \lambda \texttt{X}. \sim\exists \texttt{V4} ( \texttt{X} = \texttt{V4}) \bigwedge_{\kappa} \texttt{P'} \right) \right\}} is produced. What this substitution says is that there doesn't exist any variable in \texttt{P}, i.e. \texttt{P} is the empty set. The goal list is now empty, and the first answer is returned:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
yes
P = { }
\end{lstlisting}
So far so good, the empty set is a subset every possible set.
\item At this point, the implementation backtracks and the second path of step 5 is followed. Notice that in the goal list there are two mathematical contexts, as suggested by the parentheses and the variable quantifications.
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
($\exists$V4 P(V4), $\sim$($\sim$q(V4))), $\sim$($\exists$V4 P(V4), $\sim$q(V4)) ?
\end{lstlisting}
\item The selected expression is \texttt{($\exists$V4 P(V4), $\sim$($\sim$q(V4)))}, which matches with rule \ref{def:singlestepderivation}.3, where \msf{V = \texttt{V4}} and \msf{E = \texttt{P(V4), $\sim$($\sim$q(V4))}}.
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
P(V5), $\sim$($\sim$q(V5)), $\sim$($\exists$V4 P(V4), $\sim$q(V4)) ?
\end{lstlisting}
Notice that the \texttt{$\exists$V4} quantification is removed and a fresh variable is used.
\item The selected expression is now \texttt{P(V5)}, which matches rule \ref{def:singlestepderivation}.6, where \msf{R = \texttt{P}} and \msf{\widehat{E} = \langle \texttt{V5} \rangle}.
Therefore, substitution \msf{\theta = \left\{ \texttt{P} / \left( \lambda \texttt{X}. (\texttt{X} = \texttt{V5}) \bigvee_{\kappa} \texttt{V8} \right) \right\}} is produced and the goal list becomes:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\sim$q(V5)), $\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
\item The selected expression is now \texttt{$\sim$($\sim$q(V5))}. Since \texttt{q} is a predicate constant, it will be replaced by the completed definition for \texttt{q} with respect to the input program (definition \ref{def:compdef}). That is, \msf{q \leftrightarrow_{\pi} E}, where \msf{E = (\lambda X. X=0) \bigvee_{\kappa} (\lambda X. X=1) \bigvee_{\kappa} (\lambda X. X=2)}.
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\sim$(($\lambda$X.X=0) $\vee_{\kappa}$ ($\lambda$X.X=1) $\vee_{\kappa}$ ($\lambda$X.X=2))(V5)),
$\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
\item After reducing the completed expression with \texttt{V5} according to definition \ref{def:reduction} and applying the distributive law for the inner negation ($\sim$), we end up with the following goal list:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\sim$(($\lambda$X.X=0)(V5)), $\sim$(($\lambda$X.X=1)(V5)), $\sim$(($\lambda$X.X=2)(V5))),
$\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
And after reducing the first expression of the completed definition, we get:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\sim$(V5=0), $\sim$(($\lambda$X.X=1)(V5)), $\sim$(($\lambda$X.X=2)(V5))),
$\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
\item We are inside the scope of a negated expression where \msf{\widehat{U}} is empty and the selected expression is \texttt{$\sim$(V5=0)}, which is a primitive inequality (definition \ref{def:inequalities}). Rule \ref{def:negreduction}.9 (b) matches and the resulting goal list is:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
V5=0; $\sim$($\sim$(($\lambda$X.X=1)(V5)), $\sim$(($\lambda$X.X=2)(V5))),
$\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
The logical OR takes precedence and creates two possible paths. Initially, the first one is followed:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
V5=0, $\sim$($\exists$V4 (($\lambda$X.X=V5) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
The selected expression is \texttt{V5=0}, which is a simple unification. Rule \ref{def:singlestepderivation}.5 is applied, which gives:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 (($\lambda$X.X=0) $\vee_{\kappa}$ V8)(V4), $\sim$q(V4)) ?
\end{lstlisting}
Here the situation becomes interesting, because we are now ready to compute the \msf{\ldots \land B} part of the constructive negation rule (definition \ref{def:negreduction}.7 (b)).
\item We are once more inside the scope of a negated expression where \msf{\widehat{U} = \langle \texttt{V4} \rangle}. The selected expression is \texttt{(($\lambda$X.X=0) $\vee_{\kappa}$ V8)(V4)} and after applying the reduction rule 3 from definition \ref{def:reduction}, we get:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 ($\lambda$X.X=0)(V4); V8(V4), $\sim$q(V4)) ?
\end{lstlisting}
Now, the logical OR again takes precedence, thus the selected expression is \texttt{($\lambda$X.X=0)(V4); V8(V4)}. This means that rule \ref{def:negreduction}.3 is applied and the resulting goal list is:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 ($\lambda$X.X=0)(V4), $\sim$q(V4)), $\sim$($\exists$V4 V8(V4), $\sim$q(V4)) ?
\end{lstlisting}
Which after reducing the lambda expression (rule \ref{def:reduction}.2), becomes:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 V4=0, $\sim$q(V4)), $\sim$($\exists$V4 V8(V4), $\sim$q(V4)) ?
\end{lstlisting}
And after applying the unification, gives:
\begin{lstlisting}[language=Prolog,%
frame=single,breaklines=false,mathescape=true]
$\sim$($\exists$V4 $\sim$q(0)), $\sim$($\exists$V4 V8(V4), $\sim$q(V4)) ?