-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.tex
1578 lines (1217 loc) · 40.9 KB
/
index.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
\magnification\magstephalf
% Last modified 2020-02-19
\input tex2page
\input tgtermes
\input plainsection
\input btxmac
\input defun
\texonly
%\input 2col
\sidemargin 1.75 true in
% avoiding overfull boxes, without making
% paragraphs too bad
\pretolerance -1
\emergencystretch 5pt
\tolerance 3000
\hfuzz 1pt
\hyphenpenalty -1000
\exhyphenpenalty -1000
\doublehyphendemerits -100000
\finalhyphendemerits -100000
% ! is special char for makeindex
%\def\bang{!}
\let\n\noindent
\let\origverb\verb
\def\verb{\def\verbatimhook{\parindent0pt \relax}\origverb}
\def\p{\let\verbatimhook\relax\origverb}
%sign for “evaluates to”
\def\y{$\Rightarrow$}
%notation for true nil
\def\t{{\tt()}$^{\rm true}$}
\overfullrule 0pt
\def\ar/#1{{\it/#1\/}}
\hyphenation{sche-log}
\let\ab\allowbreak
\font\tentt cmtt10
%that’s all
%\input ptm
\endtexonly
\ifx\shipout\UnDeFiNeD
\def\defun#1#2{%
\evalh{(do-end-para)}%
\rawhtml<table bgcolor="#e5e5e5" width=90% cellpadding=0 cellspacing=0><tr ><td align=left>\endrawhtml
#2%
\rawhtml</td><td align=right>\endrawhtml{#1}%
\rawhtml</td></tr></table>\endrawhtml
\evalh{(do-para)}}
\def\t{()\rawhtml<sup><small>true</small></sup>\endrawhtml}
\def\y{{\tt =>}}
\fi
\let\byline\centerline
\def\defproc{\defun{[{\it procedure}]}}
\def\defpred{\defun{[{\it predicate}]}}
\def\defmac{\defun{[{\it macro}]}}
\def\defgoal{\defun{[{\it goal}]}}
\def\defflag{\defun{[{\it flag}]}}
\ifx\shipout\UnDeFiNeD\else
\let\htmlpagebreak\relax
\fi
\def\newsection{\htmlpagebreak\section}
\let\q\scm
\let\sverbatim\scm
\scmkeyword{%which %rel %assert %assert-a %let %and %or %is
! %free-vars
}
\texonly
\def\t{()$^t$}
\def\y{$\Rightarrow$}
\endtexonly
\title{Programming in Schelog}
\byline{\urlh{../index.html}{Dorai
Sitaram}}
%\byline{\urlh{mailto:[email protected]}{[email protected]}}
\byline{\urlh{https://github.com/ds26gte/schelog}{\htmlonly Download
\endhtmlonly Version \input ./schelog-version }}
\htmlonly
\byline{\urlh{https://github.com/ds26gte/schelog/blob/master/INSTALL.adoc}{Installation instructions}}
\endhtmlonly
\bigskip
\n Schelog is an {\em embedding} of
Prolog-style logic programming in Scheme. “Embedding”
means you don’t lose Scheme: You can use Prolog-style and
conventional Scheme code fragments alongside each other.
Schelog contains the full repertoire of Prolog features,
including meta-logical and second-order (“set”)
predicates, leaving out only those features that could more
easily and more efficiently be done with Scheme
subexpressions.
The Schelog implementation uses the approach to logic
programming described in Felleisen \cite{mf:prolog} and
Haynes \cite{logick}. In contrast to earlier Lisp simulations of
Prolog \cite{campbell},
which used explicit continuation
arguments to store failure (backtrack) information, the
Felleisen and Haynes model uses the implicit reified
continuations of Scheme as provided by the operator
\q{call-with-current-continuation} (aka \q{call/cc}). This
allows Schelog to be an {\em embedding}, ie, logic
programming is not built as a new language on top of Scheme,
but is used alongside Scheme’s other features. Both styles
of programming may be mixed to any extent that a project
needs.
The Schelog user does not need to know about the
implementation mechanism or about \q{call/cc} and
continuations to get on with the business of
doing logic programming with Schelog.
This text is a gentle introduction to Schelog syntax
and programming. It assumes a working knowledge of
Scheme and an awareness of, if not actual programming
experience with, Prolog. If you need assistance in
either language, you may consult
\cite{sicp,tls,tss,eopl,r5rs,tyscheme} for Scheme, and
\cite{bratko,ok:prolog,aop} for Prolog.
There are doubtless many other excellent books and
online documents available.
\beginsection Contents
\tableofcontents
\newsection{Simple Goals and Queries}
\label{simple}
Schelog objects are the same as Scheme objects. However, there
are two subsets of these objects that are of special
interest to Schelog: {\em goals} and {\em predicates}. We
will first look at some simple goals.
Section \ref{predicates} will introduce predicates and ways
of making complex goals using predicates.
A goal is an object whose truth or falsity we can check. A
goal that turns out to be true is said to succeed.
A goal that turns out to be false is said to
fail.
Two simple goals that are provided in Schelog are:
\sverbatim{
%true
%fail
}
\n The goal \q{%true} succeeds. The goal \q{%fail}
always fails.
(The names of all Schelog primitive objects
start with \q{%}. This is to avoid clashes with the names
of conventional Scheme objects of related meaning.
User-created objects in Schelog are not required to
follow this convention.)
A Schelog user can {\em query} a goal by wrapping it in a
\q{%which}-form.
\sverbatim{
(%which () %true)
}
\n evaluates to \q{()}, indicating success, whereas:
\sverbatim{
(%which () %fail)
}
\n evaluates to \q{#f}, indicating failure.
Note 1: The second subexpression of the \q{%which}-form
is the empty list \q{()}. Later (sec \ref{solving-goals}),
we will see \q{%which}es
with other lists as the second subform.
Note 2: The distinction between successful and failing goals
depends on Scheme’s distinguishing \q{#f} from
\q{()}. We will see later (sec \ref{false-vs-nil})
what to do in Scheme dialects where \q{#f} and \q{()} are
identical. For the moment, we will use the annotation
\q{|t} to signal that \q{()} is being used as a true
value.
Henceforth, we will use the notation:
\sverbatim{
E |y F
}
\n to say that \q{E} {\em evaluates to} \q{F}. Thus,
\sverbatim{
(%which () %true) |y |t
}
\newsection{Predicates}
\label{predicates}
More interesting goals are created by applying a special
kind of Schelog object called a {\em predicate} (or
{\em relation}) to other
Schelog objects. Schelog comes with some primitive
predicates, such as the arithmetic operators
\q{%=:=} and \q{%<},
standing for arithmetic “equal” and “less than”
respectively. For example, the following are some goals
involving these predicates:
\sverbatim{
(%which () (%=:= 1 1)) |y |t
(%which () (%< 1 2)) |y |t
(%which () (%=:= 1 2)) |y #f
(%which () (%< 1 1)) |y #f
}
\n Other arithmetic predicates are
\q{%>} (“greater than”),
\q{%<=} (“less than or equal”),
\q{%>=} (“greater than or equal”), and
\q{%=/=} (“not equal”).
Schelog predicates are not to be confused with conventional
Scheme predicates (such as \q{<} and \q{=}). Schelog
predicates, when applied to arguments, produce goals
that
may either succeed or fail. Scheme predicates, when applied
to arguments, yield a boolean value. Henceforth, we will
use the term “predicate” to mean Schelog predicates.
Conventional predicates will be explicitly called “Scheme
predicates”.
\subsection{Predicates Introducing Facts}
\label{facts}
Users can create their own predicates using the Schelog form
\q{%rel}. For example, let’s
define the predicate \q{%knows}:
\sverbatim{
(define %knows
(%rel ()
[('Odysseus 'TeX)]
[('Odysseus 'Scheme)]
[('Odysseus 'Prolog)]
[('Odysseus 'Penelope)]
[('Penelope 'TeX)]
[('Penelope 'Prolog)]
[('Penelope 'Odysseus)]
[('Telemachus 'TeX)]
[('Telemachus 'calculus)]))
}
\n The expression has the expected meaning. Each
{\em clause} in the \q{%rel} establishes a {\em fact}:
Odysseus
knows TeX, Telemachus knows calculus, \&c. In general, if we
apply the predicate to the arguments in any one of its
clauses, we will get a successful goal. Thus, since
\q{%knows} has a clause that reads
\q{[('Odysseus 'TeX)]}, the goal
\q{(%knows 'Odysseus 'TeX)}
will be true.
(In the code in
this text, brackets have the same behavior as parentheses.
We use a mix of brackets and parentheses solely to improve
the readability of the code for humans.]
We can now get answers for the following types of queries:
\sverbatim{
(%which ()
(%knows 'Odysseus 'TeX))
|y |t
(%which ()
(%knows 'Telemachus 'Scheme))
|y #f
}
\subsection{Predicates with Rules}
\label{rules}
Predicates can be more complicated than the above bald
recitation of facts. The predicate clauses can be {\em rules}, eg,
\sverbatim{
(define %computer-literate
(%rel (person)
[(person)
(%knows person 'TeX)
(%knows person 'Scheme)]
[(person)
(%knows person 'TeX)
(%knows person 'Prolog)]))
}
\n This defines the predicate
\q{%computer-literate} in
terms of the predicate \q{%knows}. In effect, a person is
defined as computer-literate if they know TeX and
Scheme, {\em or} TeX and Prolog.
Note that this use of
\q{%rel} employs a local {\em logic variable} called \q{person}.
In general, a \q{%rel}-expression can have a list of symbols
as its second subform. These name new logic variables that
can be used within the body of the \q{%rel}.
The following query can now be answered:
\sverbatim{
(%which ()
(%computer-literate 'Penelope))
|y |t
}
\n Since Penelope knows TeX and Prolog, she is computer-literate.
\subsection{Solving Goals}
\label{solving-goals}
The above queries are yes/no questions. Logic programming
allows more: We can formulate a goal with {\em uninstantiated}
logic variables and then ask the querying process to
provide, if possible, values for these variables that cause
the goal to succeed. For instance, the query:
\sverbatim{
(%which (what)
(%knows 'Odysseus what))
}
\n asks for an instantiation of the logic variable \q{what}
that satisfies the goal \q{(%knows 'Odysseus what)}.
In other words, we are asking, “What does Odysseus know?”
Note that this use of \q{%which} — like \q{%rel}
in the definition of \q{%computer-literate} —
uses a local logic
variable, \q{what}. In general, the second subform of
\q{%which} can be a list of local logic variables. The
\q{%which}-query returns an answer that is a list of
bindings, one for each logic variable mentioned in its
second subform. Thus,
\sverbatim{
(%which (what)
(%knows 'Odysseus what))
|y ([what TeX])
}
\n But that is not all that wily Odysseus knows. Schelog
provides a zero-argument procedure (“thunk”) called
\q{%more}
that {\em retries} the goal in the last
\q{%which}-query for a different solution.
\sverbatim{
(%more) |y ([what Scheme])
}
\n We can keep pumping for more solutions:
\sverbatim{
(%more) |y ([what Prolog])
(%more) |y ([what Penelope])
(%more) |y #f
}
\n The final \q{#f} shows that there are no more
solutions. This is because there are no more clauses in the
\q{%knows} predicate that list Odysseus as knowing anything
else.
\subsubsection{A Note on {\tt\#f} vs {\tt()}}
\label{false-vs-nil}
It is now clear why \q{|t} was the right choice for truth in
the previous yes/no \q{%which}-queries that had no logic
variables (sec \ref{simple}). \q{%which} returns a
list of bindings
for true
goals: the list is empty when there are no variables.
For such Schemes as don’t distinguish between \q{()} and
\q{#f}, we can still ask fruitful yes/no queries. Simply
use a dummy local
variable in the
\q{%which}-expression. Truth will give an (ignorable)
binding for the dummy variable, while falsity will, as
usual, produce \q{#f}.
\sverbatim{
(%which (bingo)
(%knows 'Odysseus 'TeX))
|y ([bingo _])
(%which (bingo)
(%knows 'Odysseus 'calculus))
|y #f
}
\subsection{ Asserting Extra Clauses}
\label{assert}
We can add more clauses to a predicate after it has already
been defined with a \q{%rel}. Schelog provides the
\q{%assert} form for this purpose. Eg,
\sverbatim{
(%assert %knows ()
[('Odysseus 'archery)])
}
\n tacks on a new clause at the end of the existing clauses
of the \q{%knows}
predicate. Now, the query:
\sverbatim{
(%which (what)
(%knows 'Odysseus what))
}
\n gives TeX, Scheme, Prolog, and Penelope, as before, but
a subsequent \q{(%more)} yields a new result: \q{archery}.
The Schelog form \q{%assert-a} is similar to \q{%assert} but
adds clauses {\em before} any of the current clauses.
Both \q{%assert} and \q{%assert-a} assume that the variable
they are adding to already names a predicate (presumably
defined using \q{%rel}).
In order to allow defining a predicate entirely through
\q{%assert}s, Schelog provides an empty predicate value
\q{%empty-rel}. \q{%empty-rel} takes any number of arguments
and always fails. A typical use of the
\q{%empty-rel} and \q{%assert} combination:
\sverbatim{
(define %parent %empty-rel)
(%assert %parent ()
[('Laertes 'Odysseus)])
(%assert %parent ()
[('Odysseus 'Telemachus)]
[('Penelope 'Telemachus)])
}
(Schelog does not provide a predicate for {\em retracting}
assertions, since we can keep track of older versions of
predicates using conventional Scheme features (\q{let} and \q{set!}).)
\subsection{Local Variables}
\label{local-vars}
The local logic variables of \q{%rel}- and
\q{%which}-expressions are in reality introduced by the
Schelog syntactic form called \q{%let}. (\q{%rel} and
\q{%which} are macros written using \q{%let}.)
\q{%let} introduces new lexically scoped logic variables.
Supposing, instead of
\sverbatim{
(%which (what)
(%knows 'Odysseus what))
}
\n we had asked
\sverbatim{
(%let (what)
(%which ()
(%knows 'Odysseus what)))
}
\n This query, too, succeeds five times, since
Odysseus knows five things. However, \q{%which} emits
bindings only for the local variables that {\em it}
introduces. Thus, this query emits \q{|t} five times before
\q{(%more)} finally returns \q{#f}.
\newsection{Using Conventional Scheme Expressions in Schelog}
\label{scheme-w-schelog}
The arguments of Schelog predicates can be any Scheme
objects. In particular, composite structures such as lists,
vectors and strings can be used, as also Scheme expressions
using the full array of Scheme’s construction and
decomposition operators. For instance, consider the
following goal:
\sverbatim{
(%member x '(1 2 3))
}
\n Here, \q{%member} is a predicate, \q{x} is a logic
variable, and \q{'(1 2 3)} is a structure. Given a suitably
intuitive definition for \q{%member}, the above goal
succeeds for \q{x} = \q{1}, \q{2}, and \q{3}.
Now to defining predicates like \q{%member}:
\sverbatim{
(define %member
(%rel (x y xs)
[(x (cons x xs))]
[(x (cons y xs))
(%member x xs)]))
}
\n Ie, \q{%member} is defined with three local variables:
\q{x}, \q{y}, \q{xs}. It has two
clauses, identifying the two ways of determining membership.
The first clause of \q{%member} states a fact: For any
\q{x}, \q{x} is a member of a list whose head is also \q{x}.
The second clause of \q{%member} is a rule: \q{x} is a
member of a list if we can show that it is a member of the
{\em tail} of that list. In other words, the original
\q{%member} goal is translated into a {\em sub}goal, which is also
a \q{%member} goal.
Note that the variable \q{y} in the definition of
\q{%member} occurs only once in the second clause. As such,
it doesn’t need you to make the effort of naming it. (Names
help only in matching a second occurrence to a first.) Schelog
lets you use the expression \q{(_)} to denote an anonymous
variable. (Ie, \q{_} is a thunk that generates a fresh
anonymous variable at each call.) The predicate \q{%member} can be
rewritten as
\sverbatim{
(define %member
(%rel (x xs)
[(x (cons x (_)))]
[(x (cons (_) xs))
(%member x xs)]))
}
\subsection{Constructors}
\label{constructors}
We can use constructors — Scheme procedures for creating
structures — to simulate data types in Schelog. For
instance, let’s define a natural-number data-type where
\q{0} denotes zero, and \q{(succ x)} denotes the natural number
whose immediate predecessor is \q{x}. The constructor
\q{succ} can
be defined in Scheme as:
\sverbatim{
(define succ
(lambda (x)
(vector 'succ x)))
}
\n Addition and multiplication can be defined as:
\sverbatim{
(define %add
(%rel (x y z)
[(0 y y)]
[((succ x) y (succ z))
(%add x y z)]))
(define %times
(%rel (x y z z1)
[(0 y 0)]
[((succ x) y z)
(%times x y z1)
(%add y z1 z)]))
}
\n We can do a lot of arithmetic with this in place. For
instance, the factorial predicate looks like:
\sverbatim{
(define %factorial
(%rel (x y y1)
[(0 (succ 0))]
[((succ x) y)
(%factorial x y1)
(%times (succ x) y1 y)]))
}
\subsection{\tt\%is}
\label{is}
The above is a very inefficient way to do arithmetic,
especially when the underlying language Scheme offers
excellent arithmetic facilities (including a comprehensive
number “tower” and exact rational arithmetic). One
problem with using Scheme calculations directly in Schelog
clauses is that the expressions used may contain logic
variables that need to be dereferenced. Schelog provides
the predicate \q{%is} that takes care of this. The goal
\sverbatim{
(%is X E)
}
\n unifies \q{X} with the value of \q{E} considered as a
Scheme expression. \q{E} can have logic variables, but
usually they should at least be bound, as unbound variables
may not be palatable values to the Scheme operators used in
\q{E}.
We can now directly use the numbers of Scheme to write a
more efficient \q{%factorial} predicate:
\sverbatim{
(define %factorial
(%rel (x y x1 y1)
[(0 1)]
[(x y) (%is x1 (- x 1))
(%factorial x1 y1)
(%is y (* y1 x))]))
}
\n A price that this efficiency comes with is that we can
use \q{%factorial} only with its first argument already
instantiated. In many cases, this is not an unreasonable
constraint. In fact, given this limitation, there is
nothing to prevent us from using Scheme’s factorial
directly:
\sverbatim{
(define %factorial
(%rel (x y)
[(x y)
(%is y (scheme-factorial
x))]))
}
\n or better yet, “in-line” any calls to \q{%factorial} with
\q{%is}-expressions calling \q{scheme-factorial}, where the
latter is defined in the usual manner:
\sverbatim{
(define scheme-factorial
(lambda (n)
(if (= n 0) 1
(* n (factorial
(- n 1))))))
}
\subsection{Lexical Scoping}
\label{lexical-scoping}
One can use Scheme’s lexical scoping to enhance predicate
definition. Here is a list-reversal predicate defined using
a hidden auxiliary predicate:
\sverbatim{
(define %reverse
(letrec
([revaux
(%rel (x y z w)
[('() y y)]
[((cons x y) z w)
(revaux y
(cons x z) w)])])
(%rel (x y)
[(x y) (revaux x '() y)])))
}
\n \q{(revaux X Y Z)} uses \q{Y} as an accumulator for
reversing \q{X} into \q{Z}. (\q{Y} starts out as \q{()}.
Each head of \q{X} is \q{cons}ed on to \q{Y}. Finally, when
\q{X} has wound down to \q{()}, \q{Y} contains the reversed
list and can be returned as \q{Z}.)
\q{revaux} is used purely as a helper predicate for
\q{%reverse}, and so it can be concealed within a lexical
contour. We use \q{letrec} instead of \q{let} because
\q{revaux} is a recursive procedure.
\subsection{Type Predicates}
\label{type-predicates}
Schelog provides a couple of predicates that let the user
probe the type of objects.
The goal
\sverbatim{
(%constant X)
}
\n succeeds if \q{X} is an {\em atomic} object, ie, not a
list or vector.
The predicate \q{%compound}, the negation of \q{%constant},
checks if its argument is indeed a list or a vector.
The above are merely the logic-program\-ming equivalents of
corresponding Scheme predicates. Users can use the
predicate \q{%is} and Scheme predicates to write more type
checks in Schelog. Thus, to test if \q{X} is a string, the
following goal could be used:
\sverbatim{
(%is #t (string? X))
}
\n User-defined Scheme predicates, in addition to primitive Scheme
predicates, can be thus imported.
\newsection{Backtracking}
\label{backtracking}
It is helpful to go into the following evaluation (sec \ref{rules})
in a
little detail:
\sverbatim{
(%which ()
(%computer-literate 'Penelope))
|y |t
}
\n The starting goal
is:
\sverbatim{
G0 = (%computer-literate Penelope)
}
\n (I’ve taken out the quote because \q{Penelope} is the result
of evaluating \q{'Penelope}.)
Schelog tries to match this with the head of the first
clause of \q{%computer-literate}. It succeeds, generating a
binding \q{[person Penelope]}.
But this means it now has two new goals — {\em subgoals}
— to solve. These are the goals in the body of the
matching clause, with the logic variables substituted by
their instantiations:
\sverbatim{
G1 = (%knows Penelope TeX)
G2 = (%knows Penelope Scheme)
}
\n For \q{G1}, Schelog attempts matches with the clauses of
\q{%knows}, and succeeds at the fifth try. (There are no
subgoals in this case, because the bodies of these “fact”
clauses are empty, in contrast to the “rule” clauses of
\q{%computer-literate}.)
Schelog then tries to solve \q{G2} against the clauses of
\q{%knows}, and since there is no clause stating that
Penelope knows Scheme, it fails.
All is not lost though. Schelog now {\em backtracks} to the
goal that was solved just before, viz., \q{G1}. It
{\em retries} \q{G1}, ie, tries to solve it in a
different way.
This entails searching down the previously unconsidered
\q{%knows}
clauses for \q{G1}, ie, the sixth onwards. Obviously,
Schelog fails again, because the fact that Penelope knows
TeX occurs only once.
Schelog now backtracks to the goal before \q{G1}, ie,
\q{G0}. We abandon the current successful match with the
first clause-head of \q{%computer-literate}, and try the
next clause-head. Schelog succeeds, again producing a binding
\q{[person Penelope]}, and two new subgoals:
\sverbatim{
G3 = (%knows Penelope TeX)
G4 = (%knows Penelope Prolog)
}
\n It is now easy to trace that Schelog finds both \q{G3} and \q{G4} to be
true. Since both of \q{G0}’s subgoals are true, \q{G0} is
itself considered true. And this is what Schelog reports. The
interested reader can now trace why the
following query has a different denouement:
\sverbatim{
(%which ()
(%computer-literate 'Telemachus))
|y #f
}
\newsection{Unification}
\label{unification}
When we say that a goal matches with a clause-head, we mean
that the predicate and argument positions line up. Before
making this comparison, Schelog dereferences all already
bound logic variables. The resulting structures are then
compared to see if they are recursively identical. Thus,
\q{1} unifies with \q{1}, and \q{(list 1 2)} with \q{'(1
2)}; but \q{1} and
\q{2} do not unify, and neither do \q{'(1 2)} and \q{'(1
3)}.
In general, there could be quite a few uninstantiated logic
variables in the compared objects. Unification will then
endeavor to find the most natural way of binding these
variables so that we arrive at structurally identical
objects. Thus, \q{(list x 1)}, where \q{x} is an unbound logic
variable, unifies with \q{'(0 1)}, producing the
binding
\q{[x 0]}.
Unification is thus a goal, and Schelog makes the unification predicate
available to the user as \q{%=}. Eg,
\sverbatim{
(%which (x)
(%= (list x 1) '(0 1))
|y ([x 0])
}
\n Schelog also provides the predicate \q{%/=}, the {\em negation} of
\q{%=}. \q{(%/= X Y)} succeeds if and only if \q{X} does
{\em not} unify with \q{Y}.
Unification goals constitute the basic subgoals that all
Schelog goals devolve to. A goal succeeds because all the
eventual unification subgoals that it decomposes to in at
least one of its subgoal-branching succeeded. It fails
because every possible subgoal-branching was thwarted by the
failure of a crucial unification subgoal.
Going back to the example in sec \ref{backtracking}, the goal
\q{(%computer-literate 'Penelope)} succeeds because
(a) it unified with
\q{(%computer-literate person)}; and then (b) with the binding
\q{[person Penelope]} in place, \q{(%knows person 'TeX)}
unified with \q{(%knows 'Penelope 'TeX)} and
\q{(%knows person 'Prolog)} unified with \q{(%knows 'Penelope
'Prolog)}.
In contrast, the goal \q{(%computer-literate 'Telemachus)}
fails because, with \q{[person Telemachus]},
the subgoals \q{(%knows person 'Scheme)} and
\q{(%knows person 'Prolog)} have no facts they can
unify with.
\subsection{The Occurs Check}
A robust unification algorithm uses the {\em
occurs check}, which ensures that a logic variable
isn’t bound to a structure that contains itself.
Not performing the check can cause the unification
to go into an infinite loop in some cases. On the
other hand, performing the occurs check greatly
increases the time taken by unification, even in cases
that wouldn’t require the check.
Schelog uses the global variable
\q{*schelog-use-occurs-check?*} to decide whether to
use the occurs check. By default, this variable is
\q{#f}, ie, Schelog disables the occurs check. To
enable the check,
\q{
(set! *schelog-use-occurs-check?* #t)
}
\newsection{Conjuctions and Disjunctions}
\label{and-or}
Goals may be combined using the forms \q{%and}
and \q{%or}
to form compound goals. (For \q{%not}, see sec \ref{not}.)
Eg,
\sverbatim{
(%which (x)
(%and (%member x '(1 2 3))
(%< x 3)))
}
\n gives solutions for \q{x} that satisfy both the
argument goals of the \q{%and}.
Ie, \q{x} should both be a member of \q{'(1 2 3)}
{\em and} be less than \q{3}. The first
solution is
\sverbatim{
([x 1])
}
\n Typing \q{(%more)} gives another solution:
\sverbatim{
([x 2])
}
\n There are no more solutions, because \q{[x 3]} satisfies
the first but not the second goal.
Similarly, the query
\sverbatim{
(%which (x)
(%or (%member x '(1 2 3))
(%member x '(3 4 5))))
}
\n lists all \q{x} that are members of either list.
\sverbatim{
([x 1])
(%more) |y ([x 2])
(%more) |y ([x 3])
(%more) |y ([x 3])
(%more) |y ([x 4])
(%more) |y ([x 5])
}
\n (Yes, \q{([x 3])} is listed twice.)
We can rewrite the predicate \q{%computer-literate}
from sec \ref{rules} using \q{%and} and \q{%or}:
\sverbatim{
(define %computer-literate
(%rel (person)
[(person)
(%or
(%and (%knows person
'TeX)
(%knows person
'Scheme))
(%and (%knows person
'TeX)
(%knows person
'Prolog)))]))
}
\n Or, more succinctly:
\sverbatim{
(define %computer-literate
(%rel (person)
[(person)
(%and (%knows person
'TeX)
(%or (%knows person
'Scheme)
(%knows person
'Prolog)))]))
}
\n We can even dispense with the \q{%rel} altogether:
\sverbatim{
(define %computer-literate
(lambda (person)
(%and (%knows person
'TeX)
(%or (%knows person
'Scheme)
(%knows person
'Prolog)))))