/usr/share/racket/pkgs/racklog/racklog.scrbl is in racket-common 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 | #lang scribble/manual
@(require scribble/eval
(for-syntax racket)
(for-label racklog
(except-in racket _)))
@(define racklog-eval (make-base-eval))
@(racklog-eval '(require racklog))
@title{Racklog: Prolog-Style Logic Programming}
@author{Dorai Sitaram}
@margin-note{Adapted from Schelog by Dorai Sitaram for Racket by Dorai Sitaram,
John Clements, and Jay McCarthy.}
@defmodule[racklog]
Racklog is an @emph{embedding} of
Prolog-style logic programming in Racket. ``Embedding''
means you don't lose Racket: You can use Prolog-style and
conventional Racket code fragments alongside each other.
Racklog 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 Racket
subexpressions.
The Racklog implementation uses the approach to logic
programming for Scheme 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. In Racket these are provided by the operator
@racket[call-with-current-continuation] (aka @racket[call/cc]). This
allows Racklog to be an @emph{embedding}, ie, logic
programming is not built as a new language on top of Racket,
but is used alongside Racket's other features. Both styles
of programming may be mixed to any extent that a project
needs.
The Racklog user does not need to know about the
implementation mechanism or about @racket[call/cc] and
continuations to get on with the business of
doing logic programming with Racklog.
This text is a gentle introduction to Racklog syntax
and programming. It assumes a working knowledge of
Racket and an awareness of, if not actual programming
experience with, Prolog. If you need assistance for Prolog,
you may consult @cite["bratko" "ok:prolog" "aop"] or
many other excellent books and
online documents available.
@table-of-contents[]
@section[#:tag "simple"]{Simple Goals and Queries}
Racklog objects are the same as Racket objects. However, there
are two subsets of these objects that are of special
interest to Racklog: @emph{goals} and @emph{predicates}. We
will first look at some simple goals.
@secref{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 Racklog are:
@racketblock[
%true
%fail
]
The goal @racket[%true] succeeds. The goal @racket[%fail]
always fails.
(The names of all Racklog primitive objects
start with @litchar{%}. This is to avoid clashes with the names
of conventional Racket objects of related meaning.
User-created objects in Racklog are not required to
follow this convention.)
A Racklog user can @emph{query} a goal by wrapping it in a
@racket[%which]-form.
@racketblock[
(%which () %true)
]
evaluates to @racketresult[()], indicating success, whereas:
@racketblock[
(%which () %fail)
]
evaluates to @racket[#f], indicating failure.
Note 1: The second subexpression of the @racket[%which]-form
is the empty list @racketresult[()]. Later (@secref{solving-goals}),
we will see @racket[%which]es
with other lists as the second subform.
Henceforth, we will use the notation:
@interaction[(eval:alts E 'F)]
to say that @racket[E] @emph{evaluates to} @racket[F]. Thus,
@interaction[#:eval racklog-eval (%which () %true)]
@section[#:tag "predicates"]{Predicates}
More interesting goals are created by applying a special
kind of Racklog object called a @emph{predicate} (or
@emph{relation}) to other
Racklog objects. Racklog comes with some primitive
predicates, such as the arithmetic operators
@racket[%=:=] and @racket[%<],
standing for arithmetic ``equal'' and ``less than''
respectively. For example, the following are some goals
involving these predicates:
@interaction[
#:eval racklog-eval
(%which () (%=:= 1 1))
(%which () (%< 1 2))
(%which () (%=:= 1 2))
(%which () (%< 1 1))
]
Other arithmetic predicates are
@racket[%>] (``greater than''),
@racket[%<=] (``less than or equal''),
@racket[%>=] (``greater than or equal''), and
@racket[%=/=] (``not equal'').
Racklog predicates are not to be confused with conventional
Racket predicates (such as @racket[<] and @racket[=]). Racklog
predicates, when applied to arguments, produce goals
that
may either succeed or fail. Racket predicates, when applied
to arguments, yield a boolean value. Henceforth, we will
use the term ``predicate'' to mean Racklog predicates.
Conventional predicates will be explicitly called ``Racket
predicates''.
@subsection[#:tag "facts"]{Predicates Introducing Facts}
Users can create their own predicates using the Racklog form
@racket[%rel]. For example, let's
define the predicate @racket[%knows]:
@racketblock+eval[#:eval racklog-eval
(define %knows
(%rel ()
[('Odysseus 'TeX)]
[('Odysseus 'Racket)]
[('Odysseus 'Prolog)]
[('Odysseus 'Penelope)]
[('Penelope 'TeX)]
[('Penelope 'Prolog)]
[('Penelope 'Odysseus)]
[('Telemachus 'TeX)]
[('Telemachus 'calculus)]))
]
The expression has the expected meaning. Each
@emph{clause} in the @racket[%rel] establishes a @emph{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
@racket[%knows] has a clause that reads
@racket[[('Odysseus 'TeX)]], the goal
@racket[(%knows 'Odysseus 'TeX)]
will be true.
We can now get answers for the following types of queries:
@interaction[#:eval racklog-eval
(%which ()
(%knows 'Odysseus 'TeX))
(%which ()
(%knows 'Telemachus 'Racket))
]
@subsection[#:tag "rules"]{Predicates with Rules}
Predicates can be more complicated than the above bald
recitation of facts. The predicate clauses can be @emph{rules}, eg,
@racketblock+eval[#:eval racklog-eval
(define %computer-literate
(%rel (person)
[(person)
(%knows person 'TeX)
(%knows person 'Racket)]
[(person)
(%knows person 'TeX)
(%knows person 'Prolog)]))
]
This defines the predicate
@racket[%computer-literate] in
terms of the predicate @racket[%knows]. In effect, a person is
defined as computer-literate if they know TeX and
Racket, @emph{or} TeX and Prolog.
Note that this use of
@racket[%rel] employs a local @emph{logic variable} called @racket[_person].
In general, a @racket[%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 @racket[%rel].
The following query can now be answered:
@interaction[#:eval racklog-eval
(%which ()
(%computer-literate 'Penelope))
]
Since Penelope knows TeX and Prolog, she is computer-literate.
@subsection[#:tag "solving-goals"]{Solving Goals}
The above queries are yes/no questions. Racklog programming
allows more: We can formulate a goal with @emph{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:
@interaction[#:eval racklog-eval
(%which (what)
(%knows 'Odysseus what))
]
asks for an instantiation of the logic variable @racket[_what]
that satisfies the goal @racket[(%knows 'Odysseus what)].
In other words, we are asking, ``What does Odysseus know?''
Note that this use of @racket[%which] --- like @racket[%rel]
in the definition of @racket[%computer-literate] ---
uses a local logic
variable, @racket[_what]. In general, the second subform of
@racket[%which] can be a list of local logic variables. The
@racket[%which]-query returns an answer that is a list of
bindings, one for each logic variable mentioned in its
second subform. Thus,
@interaction[#:eval racklog-eval
(%which (what)
(%knows 'Odysseus what))
]
But that is not all that wily Odysseus knows. Racklog
provides a zero-argument procedure (``thunk'') called
@racket[%more]
that @emph{retries} the goal in the last
@racket[%which]-query for a different solution.
@interaction[#:eval racklog-eval
(%more)
]
We can keep pumping for more solutions:
@interaction[#:eval racklog-eval
(%more)
(%more)
(%more)
]
The final @racket[#f] shows that there are no more
solutions. This is because there are no more clauses in the
@racket[%knows] predicate that list Odysseus as knowing anything
else.
@subsection[#:tag "assert"]{Asserting Extra Clauses}
We can add more clauses to a predicate after it has already
been defined with a @racket[%rel]. Racklog provides the
@racket[%assert!] form for this purpose. Eg,
@racketblock+eval[#:eval racklog-eval
(%assert! %knows ()
[('Odysseus 'archery)])
]
tacks on a new clause at the end of the existing clauses
of the @racket[%knows]
predicate. Now, the query:
@interaction[#:eval racklog-eval
(%which (what)
(%knows 'Odysseus what))
]
gives TeX, Racket, Prolog, and Penelope, as before, but
a subsequent @racket[(%more)] yields a new result:
@interaction-eval[#:eval racklog-eval (begin (%more) (%more) (%more))]
@interaction[#:eval racklog-eval
(%more)
]
The Racklog form @racket[%assert-after!] is similar to @racket[%assert!] but
adds clauses @emph{before} any of the current clauses.
Both @racket[%assert!] and @racket[%assert-after!] assume that the variable
they are adding to already names a predicate (presumably
defined using @racket[%rel]).
In order to allow defining a predicate entirely through
@racket[%assert!]s, Racklog provides an empty predicate value
@racket[%empty-rel]. @racket[%empty-rel] takes any number of arguments
and always fails. A typical use of the
@racket[%empty-rel] and @racket[%assert!] combination:
@racketblock+eval[#:eval racklog-eval
(define %parent %empty-rel)
(%assert! %parent ()
[('Laertes 'Odysseus)])
(%assert! %parent ()
[('Odysseus 'Telemachus)]
[('Penelope 'Telemachus)])
]
(Racklog does not provide a predicate for @emph{retracting}
assertions, since we can keep track of older versions of
predicates using conventional Racket features (@racket[let] and @racket[set!]).)
@subsection[#:tag "local-vars"]{Local Variables}
The local logic variables of @racket[%rel]- and
@racket[%which]-expressions are in reality introduced by the
Racklog syntactic form called @racket[%let]. (@racket[%rel] and
@racket[%which] are macros written using @racket[%let].)
@racket[%let] introduces new lexically scoped logic variables.
Supposing, instead of
@interaction[#:eval racklog-eval
(%which (what)
(%knows 'Odysseus what))
]
we had asked
@interaction[#:eval racklog-eval
(%let (what)
(%which ()
(%knows 'Odysseus what)))
]
This query, too, succeeds five times, since
Odysseus knows five things. However, @racket[%which] emits
bindings only for the local variables that @emph{it}
introduces. Thus, this query emits @racketresult[()] five times before
@racket[(%more)] finally returns @racket[#f].
@section[#:tag "racket-w-logic"]{Using Conventional Racket Expressions in Racklog}
The arguments of Racklog predicates can be any Racket
objects. In particular, composite structures such as lists,
vectors, strings, hash tables, etc can be used, as also Racket expressions
using the full array of Racket's construction and
decomposition operators. For instance, consider the
following goal:
@racketblock[
(%member x '(1 2 3))
]
Here, @racket[%member] is a predicate, @racket[x] is a logic
variable, and @racket['(1 2 3)] is a structure. Given a suitably
intuitive definition for @racket[%member], the above goal
succeeds for @racket[x] = @racketresult[1], @racketresult[2], and @racketresult[3].
Now to defining predicates like @racket[%member]:
@racketblock[
(define %member
(%rel (x y xs)
[(x (cons x xs))]
[(x (cons y xs))
(%member x xs)]))
]
Ie, @racket[%member] is defined with three local variables:
@racket[x], @racket[y], @racket[xs]. It has two
clauses, identifying the two ways of determining membership.
The first clause of @racket[%member] states a fact: For any
@racket[x], @racket[x] is a member of a list whose head is also @racket[x].
The second clause of @racket[%member] is a rule: @racket[x] is a
member of a list if we can show that it is a member of the
@emph{tail} of that list. In other words, the original
@racket[%member] goal is translated into a @emph{sub}goal, which is also
a @racket[%member] goal.
Note that the variable @racket[y] in the definition of
@racket[%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.) Racklog
lets you use the expression @racket[(_)] to denote an anonymous
variable. (Ie, @racket[_] is a thunk that generates a fresh
anonymous variable at each call.) The predicate @racket[%member] can be
rewritten as
@racketblock[
(define %member
(%rel (x xs)
[(x (cons x (_)))]
[(x (cons (_) xs))
(%member x xs)]))
]
@subsection[#:tag "constructors"]{Constructors}
We can use constructors --- Racket procedures for creating
structures --- to simulate data types in Racklog. For
instance, let's define a natural-number data-type where
@racket[0] denotes zero, and @racket[(succ x)] denotes the natural number
whose immediate predecessor is @racket[x]. The constructor
@racket[succ] can
be defined in Racket as:
@racketblock+eval[#:eval racklog-eval
(define succ
(lambda (x)
(vector 'succ x)))
]
Addition and multiplication can be defined as:
@racketblock+eval[#:eval racklog-eval
(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)]))
]
We can do a lot of arithmetic with this in place. For
instance, the factorial predicate looks like:
@racketblock+eval[#:eval racklog-eval
(define %factorial
(%rel (x y y1)
[(0 (succ 0))]
[((succ x) y)
(%factorial x y1)
(%times (succ x) y1 y)]))
]
@subsection[#:tag "is"]{@racket[\%is]}
The above is a very inefficient way to do arithmetic,
especially when the underlying language Racket offers
excellent arithmetic facilities (including a comprehensive
number ``tower'' and exact rational arithmetic). One
problem with using Racket calculations directly in Racklog
clauses is that the expressions used may contain logic
variables that need to be dereferenced. Racklog provides
the predicate @racket[%is] that takes care of this. The goal
@racketblock[
(%is _X _E)
]
unifies @racket[_X] with the value of @racket[_E] considered as a
Racket expression. @racket[_E] can have logic variables, but
usually they should at least be bound, as unbound variables
may not be palatable values to the Racket operators used in
@racket[_E].
We can now directly use the numbers of Racket to write a
more efficient @racket[%factorial] predicate:
@racketblock+eval[#:eval racklog-eval
(define %factorial
(%rel (x y x1 y1)
[(0 1)]
[(x y) (%is x1 (- x 1))
(%factorial x1 y1)
(%is y (* y1 x))]))
]
A price that this efficiency comes with is that we can
use @racket[%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 Racket's factorial
directly:
@racketblock+eval[#:eval racklog-eval
(define %factorial
(%rel (x y)
[(x y)
(%is y (racket-factorial
x))]))
]
or better yet, ``in-line'' any calls to @racket[%factorial] with
@racket[%is]-expressions calling @racket[racket-factorial], where the
latter is defined in the usual manner:
@racketblock+eval[#:eval racklog-eval
(define racket-factorial
(lambda (n)
(if (= n 0) 1
(* n (factorial
(- n 1))))))
]
@subsection[#:tag "lexical-scoping"]{Lexical Scoping}
One can use Racket's lexical scoping to enhance predicate
definition. Here is a list-reversal predicate defined using
a hidden auxiliary predicate:
@racketblock+eval[#:eval racklog-eval
(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)])))
]
@racket[(revaux _X _Y _Z)] uses @racket[_Y] as an accumulator for
reversing @racket[_X] into @racket[_Z]. (@racket[_Y] starts out as @racketresult[()].
Each head of @racket[_X] is @racket[cons]ed on to @racket[_Y]. Finally, when
@racket[_X] has wound down to @racketresult[()], @racket[_Y] contains the reversed
list and can be returned as @racket[_Z].)
@racket[revaux] is used purely as a helper predicate for
@racket[%reverse], and so it can be concealed within a lexical
contour. We use @racket[letrec] instead of @racket[let] because
@racket[revaux] is a recursive procedure.
@subsection[#:tag "type-predicates"]{Type Predicates}
Racklog provides a couple of predicates that let the user
probe the type of objects.
The goal
@racketblock[
(%constant _X)
]
succeeds if @racket[_X] is an @emph{atomic} object.
The predicate @racket[%compound], the negation of @racket[%constant],
checks if its argument is not an atomic object.
The above are merely the logic-programming equivalents of
corresponding Racket predicates. Users can use the
predicate @racket[%is] and Racket predicates to write more type
checks in Racklog. Thus, to test if @racket[_X] is a string, the
following goal could be used:
@racketblock[
(%is #t (string? _X))
]
User-defined Racket predicates, in addition to primitive Racket
predicates, can be thus imported.
@section[#:tag "backtracking"]{Backtracking}
It is helpful to go into the following evaluation (@secref{rules})
in a
little detail:
@racketblock+eval[#:eval racklog-eval
(%which ()
(%computer-literate 'Penelope))
]
The starting goal
is:
@(define goal litchar)
@racketblock[
G0 = (%computer-literate Penelope)
]
(I've taken out the quote because @racketresult[Penelope] is the result
of evaluating @racket['Penelope].)
Racklog tries to match this with the head of the first
clause of @racket[%computer-literate]. It succeeds, generating a
binding @racket[[person . Penelope]].
But this means it now has two new goals --- @emph{subgoals}
--- to solve. These are the goals in the body of the
matching clause, with the logic variables substituted by
their instantiations:
@racketblock[
G1 = (%knows Penelope TeX)
G2 = (%knows Penelope Racket)
]
For @goal{G1}, Racklog attempts matches with the clauses of
@racket[%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
@racket[%computer-literate].)
Racklog then tries to solve @goal{G2} against the clauses of
@racket[%knows], and since there is no clause stating that
Penelope knows Racket, it fails.
All is not lost though. Racklog now @emph{backtracks} to the
goal that was solved just before, viz., @goal{G1}. It
@emph{retries} @goal{G1}, ie, tries to solve it in a
different way.
This entails searching down the previously unconsidered
@racket[%knows]
clauses for @goal{G1}, ie, the sixth onwards. Obviously,
Racklog fails again, because the fact that Penelope knows
TeX occurs only once.
Racklog now backtracks to the goal before @goal{G1}, ie,
@goal{G0}. We abandon the current successful match with the
first clause-head of @racket[%computer-literate], and try the
next clause-head. Racklog succeeds, again producing a binding
@racket[[person . Penelope]], and two new subgoals:
@racketblock[
G3 = (%knows Penelope TeX)
G4 = (%knows Penelope Prolog)
]
It is now easy to trace that Racklog finds both @goal{G3} and @goal{G4} to be
true. Since both of @goal{G0}'s subgoals are true, @goal{G0} is
itself considered true. And this is what Racklog reports. The
interested reader can now trace why the
following query has a different denouement:
@interaction[#:eval racklog-eval
(%which ()
(%computer-literate 'Telemachus))
]
@section[#:tag "unification"]{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, Racklog dereferences all already
bound logic variables. The resulting structures are then
compared to see if they are recursively identical. Thus,
@racket[1] unifies with @racket[1], and @racket[(list 1 2)] with @racket['(1 2)]; but @racket[1] and
@racket[2] do not unify, and neither do @racket['(1 2)] and @racket['(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, @racket[(list _x 1)], where @racket[_x] is an unbound logic
variable, unifies with @racket['(0 1)], producing the
binding
@racket[[_x 0]].
Unification is thus a goal, and Racklog makes the unification predicate
available to the user as @racket[%=]. Eg,
@interaction[#:eval racklog-eval
(%which (x)
(%= (list x 1) '(0 1)))
]
Racklog also provides the predicate @racket[%/=], the @emph{negation} of
@racket[%=]. @racket[(%/= _X _Y)] succeeds if and only if @racket[_X] does
@emph{not} unify with @racket[_Y].
Unification goals constitute the basic subgoals that all
Racklog 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 @secref{backtracking}, the goal
@racket[(%computer-literate 'Penelope)] succeeds because
(a) it unified with
@racket[(%computer-literate person)]; and then (b) with the binding
@racket[[person . Penelope]] in place, @racket[(%knows person 'TeX)]
unified with @racket[(%knows 'Penelope 'TeX)] and
@racket[(%knows person 'Prolog)] unified with @racket[(%knows 'Penelope 'Prolog)].
In contrast, the goal @racket[(%computer-literate 'Telemachus)]
fails because, with @racket[[person . Telemachus]],
the subgoals @racket[(%knows person 'Racket)] and
@racket[(%knows person 'Prolog)] have no facts they can
unify with.
@subsection{The Occurs Check}
A robust unification algorithm uses the @deftech{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.
Racklog uses the global parameter
@racket[use-occurs-check?] to decide whether to
use the occurs check. By default, this variable is
@racket[#f], ie, Racklog disables the occurs check. To
enable the check,
@racketblock[
(use-occurs-check? #t)
]
@section[#:tag "and-or"]{Conjuctions and Disjunctions}
Goals may be combined using the forms @racket[%and]
and @racket[%or]
to form compound goals. (For @racket[%not], see @secref{not}.)
Eg,
@interaction[#:eval racklog-eval
(%which (x)
(%and (%member x '(1 2 3))
(%< x 3)))
]
gives solutions for @racket[_x] that satisfy both the
argument goals of the @racket[%and].
Ie, @racket[_x] should both be a member of @racket['(1 2 3)]
@emph{and} be less than @racket[3]. Typing @racket[(%more)] gives another solution:
@interaction[#:eval racklog-eval
(%more)
(%more)
]
There are no more solutions, because @racket[[x 3]] satisfies
the first but not the second goal.
Similarly, the query
@interaction[#:eval racklog-eval
(%which (x)
(%or (%member x '(1 2 3))
(%member x '(3 4 5))))
]
lists all @racket[_x] that are members of either list.
@interaction[#:eval racklog-eval
(%more)
(%more)
(%more)
(%more)
(%more)
]
(Yes, @racket[([x 3])] is listed twice.)
We can rewrite the predicate @racket[%computer-literate]
from @secref{rules} using @racket[%and] and @racket[%or]:
@racketblock+eval[#:eval racklog-eval
(define %computer-literate
(%rel (person)
[(person)
(%or
(%and (%knows person
'TeX)
(%knows person
'Racket))
(%and (%knows person
'TeX)
(%knows person
'Prolog)))]))
]
Or, more succinctly:
@racketblock+eval[#:eval racklog-eval
(define %computer-literate
(%rel (person)
[(person)
(%and (%knows person
'TeX)
(%or (%knows person
'Racket)
(%knows person
'Prolog)))]))
]
We can even dispense with the @racket[%rel] altogether:
@racketblock+eval[#:eval racklog-eval
(define %computer-literate
(lambda (person)
(%and (%knows person
'TeX)
(%or (%knows person
'Racket)
(%knows person
'Prolog)))))
]
This last looks like a conventional Racket predicate
definition, and is arguably
the most readable format for a Racket programmer.
@section[#:tag "lv-manip"]{Manipulating Racklog Variables}
Racklog provides special predicates for probing logic
variables, without risking their getting bound.
@subsection[#:tag "var"]{Checking for Variables}
The goal
@racketblock[
(%== _X _Y)
]
succeeds if @racket[_X] and @racket[_Y] are @emph{identical} objects. This
is not quite the unification predicate @racket[%=], for @racket[%==]
doesn't touch unbound objects the way @racket[%=] does. Eg,
@racket[%==] will not equate an unbound logic variable with a
bound one, nor will it equate two unbound logic variables
unless they are the @emph{same} variable.
The predicate @racket[%/==] is the negation of @racket[%==].
The goal
@racketblock[
(%var _X)
]
succeeds if @racket[_X] isn't completely bound --- ie, it has at
least one unbound logic variable in its innards.
The predicate @racket[%nonvar] is the negation of @racket[%var].
@subsection[#:tag "freeze"]{Preserving Variables}
Racklog lets the user protect a term with variables from
unification by allowing that term to be treated as a
(completely) bound object. The predicates provided for this
purpose are
@racket[%freeze],
@racket[%melt], @racket[%melt-new], and @racket[%copy].
The goal
@racketblock[
(%freeze _S _F)
]
unifies @racket[_F] to the frozen version of @racket[_S]. Any lack
of bindings in @racket[_S] are preserved no matter how much you
toss @racket[_F] about.
The goal
@racketblock[
(%melt _F _S)
]
retrieves the object frozen in @racket[_F] into @racket[_S].
The goal
@racketblock[
(%melt-new _F _S)
]
is similar to @racket[%melt],
except that when @racket[_S] is made, the unbound variables in
@racket[_F] are replaced by brand-new unbound variables.
The goal
@racketblock[
(%copy _S _C)
]
is an abbreviation for @racket[(%freeze _S _F)]
followed by @racket[(%melt-new _F _C)].
@section[#:tag "cut"]{The Cut (@racket[!])}
The cut (called @racket[!]) is a special goal that is used to
prune backtracking options. Like the @racket[%true] goal, the
cut goal too succeeds, when accosted by the Racklog
subgoaling engine. However, when a further subgoal down the
line fails, and time comes to retry the cut goal, Racklog
will refuse to try alternate clauses for the predicate in
whose definition the cut occurs. In other words, the cut
causes Racklog to commit to all the decisions made from the
time that the predicate was selected to match a subgoal till
the time the cut was satisfied.
For example, consider again the @racket[%factorial]
predicate, as defined in @secref{is}:
@racketblock+eval[#:eval racklog-eval
(define %factorial
(%rel (x y x1 y1)
[(0 1)]
[(x y) (%is x1 (- x 1))
(%factorial x1 y1)
(%is y (* y1 x))]))
]
Clearly,
@interaction[#:eval racklog-eval
(%which ()
(%factorial 0 1))
(%which (n)
(%factorial 0 n))
]
But what if we asked for @racket[(%more)] for either query?
Backtracking will try
the second clause of @racket[%factorial], and sure enough the
clause-head unifies, producing binding @racket[[x . 0]].
We now get three subgoals. Solving the first, we get @racket[[x1 . -1]], and then we have to solve @racket[(%factorial -1 y1)]. It
is easy to see there is no end to this, as we fruitlessly
try to get the factorials of numbers that get more and more
negative.
If we placed a cut at the first clause:
@racketblock[
...
[(0 1) !]
...
]
the attempt to find more solutions for @racket[(%factorial 0 1)] is nipped in the bud.
Calling @racket[%factorial] with a @emph{negative} number would still cause an
infinite loop. To take care of that problem as well, we
use another cut:
@racketblock+eval[#:eval racklog-eval
(define %factorial
(%rel (x y x1 y1)
[(0 1) !]
[(x y) (%< x 0) ! %fail]
[(x y) (%is x1 (- x 1))
(%factorial x1 y1)
(%is y (* y1 x))]))
]
@interaction[#:eval racklog-eval
(%which ()
(%factorial 0 1))
(%more)
(%which ()
(%factorial -1 1))
]
Using @emph{raw} cuts as above can get very confusing. For this
reason, it is advisable to use it hidden away in
well-understood abstractions. Two such common abstractions
are the conditional and negation.
@subsection[#:tag "if-then-else"]{Conditional Goals}
An ``if ... then ... else ...'' predicate can be defined
as follows
@racketblock+eval[#:eval racklog-eval
(define %if-then-else
(%rel (p q r)
[(p q r) p ! q]
[(p q r) r]))
]
(Note that for the first time we have predicate arguments that
are themselves goals.)
Consider the goal
@racketblock[
G0 = (%if-then-else Gbool Gthen Gelse)
]
We first unify @goal{G0} with the first clause-head,
giving
@racket[[p . Gbool]], @racket[[q . Gthen]], @racket[[r . Gelse]]. @goal{Gbool} can
now either succeed or fail.
Case 1: If @goal{Gbool} fails, backtracking will cause the
@goal{G0} to unify with the second clause-head. @racket[r] is bound
to @goal{Gelse}, and so @goal{Gelse} is tried, as expected.
Case 2: If @goal{Gbool} succeeds, the cut commits to this
clause of the @racket[%if-then-else]. We now try @goal{Gthen}. If
@goal{Gthen} should now fail --- or even if we simply retry for
more solutions --- we are guaranteed that the second
clause-head will not be tried. If it were not for the cut,
@goal{G0} would attempt to unify with the second clause-head, which will
of course succeed, and @goal{Gelse} @emph{will} be tried.
@subsection[#:tag "not"]{Negation as Failure}
Another common abstraction using the cut is @emph{negation}.
The negation of goal @goal{G} is defined as @racket[(%not G)], where
the predicate @racket[%not] is defined as follows:
@racketblock+eval[#:eval racklog-eval
(define %not
(%rel ()
[(g) g ! %fail]
[(g) %true]))
]
Thus, @racket[g]'s negation is deemed a failure if @racket[g]
succeeds, and a success if @racket[g] fails. This is of course
confusing goal failure with falsity. In some cases, this
view of negation is actually helpful.
@section[#:tag "set-of"]{Set Predicates}
The goal
@racketblock[
(%bag-of _X _G _Bag)
]
unifies with @racket[_Bag] the list of all instantiations of
@racket[_X] for which @racket[_G] succeeds. Thus, the following query
asks for all the things known --- ie, the collection of things
such that someone knows them:
@interaction[#:eval racklog-eval
(%which (things-known)
(%let (someone x)
(%bag-of x (%knows someone x)
things-known)))
]
This is the only solution for this goal:
@interaction[#:eval racklog-eval
(%more)
]
Note that some things --- eg, TeX --- are enumerated
more than once. This is because more than one person knows
TeX. To remove duplicates, use the predicate
@racket[%set-of]
instead of @racket[%bag-of]:
@interaction[#:eval racklog-eval
(%which (things-known)
(%let (someone x)
(%set-of x (%knows someone x)
things-known)))
]
In the above, the free variable @racket[_someone] in the
@racket[%knows]-goal is used as if it
were existentially quantified. In contrast, Prolog's
versions of
@racket[%bag-of] and @racket[%set-of] fix it for each solution of the
set-predicate goal. We can do it too with some additional
syntax that identifies the free variable.
Eg,
@interaction[#:eval racklog-eval
(%which (someone things-known)
(%let (x)
(%bag-of x
(%free-vars (someone)
(%knows someone x))
things-known)))
]
The bag of things known by @emph{one} someone is
returned. That someone is Odysseus. The query can be
retried for more solutions, each listing the things known by
a different someone:
@interaction[#:eval racklog-eval
(%more)
(%more)
(%more)
(%more)
]
Racklog also provides two variants of these set predicates,
viz., @racket[%bag-of-1] and @racket[%set-of-1]. These act like @racket[%bag-of]
and @racket[%set-of] but fail if the resulting bag or set is empty.
@section{Racklog Module Language}
@defmodulelang[@racketmodname[racklog] #:module-paths (racklog/lang/lang)]
This module language accepts the syntax of Datalog (except clauses need not be safe) and compiles each predicate to a relation.
The accepted syntax is available in the @secref[#:doc '(lib "datalog/scribblings/datalog.scrbl")]{datalog} documentation.
@section[#:tag "glossary"]{Glossary of Racklog Primitives}
@(define-syntax (defpred stx)
(syntax-case stx ()
[(_ (id arg ...) pre ...)
(syntax/loc stx
(defproc (id arg ...)
goal/c
pre ...))]))
@(define-syntax-rule (defgoal id pre ...)
(defthing id goal/c pre ...))
@subsection{Racket Predicates}
@defproc[(logic-var? [x any/c]) boolean?]{Identifies a logic variable.}
@defproc[(atomic-struct? [x any/c]) boolean?]{
Identifies structures that the @racket[(current-inspector)] cannot inspect.}
@defproc[(atom? [x any/c]) boolean?]{
Identifies atomic values that may appear in Racklog programs. Equivalent to
the contract @racket[(or/c boolean? number? string? bytes? char? symbol?
regexp? pregexp? byte-regexp? byte-pregexp? keyword? null? procedure? void?
set? atomic-struct?)].}
@defproc[(compound-struct? [x any/c]) boolean?]{
Identifies structures that the @racket[(current-inspector)] can inspect.}
@defproc[(compound? [x any/c]) boolean?]{
Identifies compound values that may appear in Racklog programs. Equivalent to
the contract
@racket[(or/c pair? vector? mpair? box? hash? compound-struct?)].}
@defproc[(unifiable? [x any/c]) boolean?]{
Identifies values that may appear in Racklog programs. Essentialy either an
@racket[atom?], @racket[logic-var?], or @racket[compound?] that contains
@racket[unifiable?]s.}
@defproc[(answer-value? [x any/c]) boolean?]{
Identifies values that may appear in @racket[answer?]. Essentially
@racket[unifiable?]s that do not contain @racket[logic-var?]s.}
@defproc[(answer? [x any/c]) boolean?]{
Identifies answers returned by @racket[%more] and @racket[%which]. Equivalent
to the contract
@racket[(or/c false/c (listof (cons/c symbol? answer-value?)))].}
@defthing[goal/c contract?]{A contract for goals.}
@subsection{User Interface}
@defform[(%which (V ...) G ...)
#:contracts ([V identifier?]
[G goal/c])]{
Returns an @racket[answer?]
of the variables @racket[V], ..., that satisfies all of @racket[G],
... If @racket[G], ..., cannot be satisfied, returns @racket[#f].
Calling the thunk @racket[%more] produces more
instantiations, if available.}
@defproc[(%more) answer?]{
The thunk @racket[%more] produces more instantiations of the
variables in the most recent @racket[%which]-form that satisfy the
goals in that @racket[%which]-form. If no more solutions can
be found, @racket[%more] returns @racket[#f].}
@defform[(%find-all (V ...) G ...)
#:contracts ([V identifier?]
[G goal/c])]{
Like @racket[(list (%which (V ...) G ...) (%more) ...)] with as many @racket[(%more)]s as there are answers. (This will not terminate if there are an infinite number of answers.)
}
@subsection{Relations}
@defform/subs[(%rel (V ...) clause ...)
([clause [(E ...) G ...]])
#:contracts ([V identifier?]
[E expression?]
[G goal/c])]{
Returns a predicate function. Each clause @racket[C] signifies that
the goal created by applying the predicate object to anything that
matches @racket[(E ...)] is deemed to succeed if all the goals
@racket[G], ..., can, in their turn, be shown to succeed. The
variables @racket[V], ..., are local logic variables for
@racket[clause], ....}
@defpred[(%empty-rel [E unifiable?] ...)]{
The goal @racket[(%empty-rel E ...)] always fails. The @emph{value}
@racket[%empty-rel] is used as a starting value for predicates
that can later be enhanced with @racket[%assert!] and @racket[%assert-after!].}
@defform[(%assert! Pname (V ...) clause ...)
#:contracts ([Pname identifier?]
[V identifier?])]{
Adds the clauses
@racket[clauses], ..., to the @emph{end} of the predicate that is the value of
the Racket variable @racket[Pname]. The variables @racket[V], ..., are
local logic variables for @racket[clause], ....}
@defform[(%assert-after! Pname (V ...) clause ...)
#:contracts ([Pname identifier?]
[V identifier?])]{
Like @racket[%assert!], but adds the new clauses to the @emph{front}
of the existing predicate.}
@subsection{Racklog Variables}
@defproc[(_) logic-var?]{
A thunk that produces a new logic variable. Can be
used in situations where we want a logic variable but
don't want to name it. (@racket[%let], in contrast, introduces new
lexical names for the logic variables it creates.)
}
@defform[(%let (V ...) expr ...)
#:contracts ([V identifier?])]{
Introduces @racket[V], ..., as
lexically scoped logic variables to be used in @racket[expr], ...}
@subsection{Cut}
@defform[(%cut-delimiter . any)]{
Introduces a cut point. See @secref{cut}.}
@defidform[!]{
The cut goal, see @secref{cut}.
May only be used syntactically inside @racket[%cut-delimiter] or @racket[%rel].}
@subsection{Racklog Operators}
@defgoal[%fail]{
The goal @racket[%fail] always fails.}
@defgoal[%true]{
The goal @racket[%true] succeeds. Fails on retry.}
@defpred[(%repeat)]{
The goal @racket[(%repeat)] always succeeds (even on retries).
Useful for failure-driven loops.}
@defform[(%and G ...) #:contracts ([G goal/c])]{
The goal @racket[(%and G ...)] succeeds if all the goals
@racket[G], ..., succeed.}
@defform[(%or G ...) #:contracts ([G goal/c])]{
The goal @racket[(%or G ...)] succeeds if one of @racket[G], ..., tried
in that order, succeeds.}
@defpred[(%not [G goal/c])]{
The goal @racket[(%not G)] succeeds if @racket[G] fails.}
@defpred[(%if-then-else [G1 goal/c] [G2 goal/c] [G3 goal/c])]{
The goal @racket[(%if-then-else G1 G2 G3)] tries @racket[G1] first: if it
succeeds, tries @racket[G2]; if not, tries @racket[G3].}
@subsection{Unification}
@defpred[(%= [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%= E1 E2)] succeeds if @racket[E1] can be unified with
@racket[E2]. Any resulting bindings for logic variables are kept.}
@defpred[(%/= [E1 unifiable?] [E2 unifiable?])]{@racket[%/=] is the negation of @racket[%=].
The goal @racket[(%/= E1 E2)] succeeds if @racket[E1] can not be unified
with @racket[E2].}
@defpred[(%== [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%== E1 E2)] succeeds if @racket[E1] is @emph{identical}
to @racket[E2]. They should be structurally equal. If containing
logic variables, they should have the same variables in the
same position. Unlike a @racket[%=]-call, this goal will not bind
any logic variables.}
@defpred[(%/== [E1 unifiable?] [E2 unifiable?])]{
@racket[%/==] is the negation of @racket[%==].
The goal @racket[(%/== E1 E2)] succeeds if @racket[E1] and @racket[E2] are not
identical.}
@defform[(%is E1 E2)]{
The goal @racket[(%is E1 E2)] unifies with @racket[E1] the result of
evaluating @racket[E2] as a Racket expression. @racket[E2] may contain
logic variables, which are dereferenced automatically.
Fails if @racket[E2] contains unbound logic variables.}
@defparam[use-occurs-check? on? boolean?]{
If this is false (the default),
Racklog's unification will not use the occurs check.
If it is true, the occurs check is enabled.}
@subsection{Numeric Predicates}
@defpred[(%< [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%< E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is less than @racket[E2].}
@defpred[(%<= [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%<= E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is less than or equal to @racket[E2].}
@defpred[(%=/= [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%=/= E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is not equal to @racket[E2].}
@defpred[(%=:= [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%=:= E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is equal to @racket[E2].}
@defpred[(%> [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%> E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is greater than @racket[E2].}
@defpred[(%>= [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%>= E1 E2)] succeeds if @racket[E1] and @racket[E2] are bound to
numbers and @racket[E1] is greater than or equal to @racket[E2].}
@subsection{List Predicates}
@defpred[(%append [E1 unifiable?] [E2 unifiable?] [E3 unifiable?])]{
The goal @racket[(%append E1 E2 E3)] succeeds if @racket[E3] is unifiable
with the list obtained by appending @racket[E1] and @racket[E2].}
@defpred[(%member [E1 unifiable?] [E2 unifiable?])]{
The goal @racket[(%member E1 E2)] succeeds if @racket[E1] is a member
of the list in @racket[E2].}
@subsection{Set Predicates}
@defpred[(%set-of [E1 unifiable?] [G goal/c] [E2 unifiable?])]{
The goal @racket[(%set-of E1 G E2)] unifies with @racket[E2] the @emph{set}
of all the
instantiations of @racket[E1] for which goal @racket[G] succeeds.}
@defpred[(%set-of-1 [E1 unifiable?] [G goal/c] [E2 unifiable?])]{
Similar to @racket[%set-of], but fails if the set is empty.}
@defpred[(%bag-of [E1 unifiable?] [G goal/c] [E2 unifiable?])]{
The goal @racket[(%bag-of E1 G E2)] unifies with @racket[E2] the @emph{bag}
(multiset)
of all the
instantiations of @racket[E1] for which goal @racket[G] succeeds.}
@defpred[(%bag-of-1 [E1 unifiable?] [G goal/c] [E2 unifiable?])]{
Similar to @racket[%bag-of], but fails if the bag is empty.}
@defform[(%free-vars (V ...) G)
#:contracts ([V identifier?]
[G goal/c])]{
Identifies
the occurrences of the variables @racket[V], ..., in goal
@racket[G] as free. It is used to avoid existential quantification
in calls to set predicates (@racket[%bag-of], @racket[%set-of], &c.).}
@subsection{Racklog Predicates}
@defpred[(%compound [E unifiable?])]{
The goal @racket[(%compound E)] succeeds if @racket[E] is a compound
value.}
@defpred[(%constant [E unifiable?])]{
The goal @racket[(%constant E)] succeeds if @racket[E] is an atomic
value.}
@defpred[(%var [E unifiable?])]{
The goal @racket[(%var E)] succeeds if @racket[E] is not completely
instantiated, ie, it has at least one unbound variable in
it.}
@defpred[(%nonvar [E unifiable?])]{
@racket[%nonvar] is the negation of @racket[%var].
The goal @racket[(%nonvar E)] succeeds if @racket[E] is completely
instantiated, ie, it has no unbound variable in it.}
@subsection{Racklog Variable Manipulation}
@defpred[(%freeze [S unifiable?] [F unifiable?])]{
The goal @racket[(%freeze S F)] unifies with @racket[F] a new frozen
version of the structure in @racket[S]. Freezing implies that all
the unbound variables are preserved. @racket[F] can henceforth be
used as @emph{bound} object with no fear of its variables
getting bound by unification.}
@defpred[(%melt [F unifiable?] [S unifiable?])]{
The goal @racket[(%melt F S)] unifies @racket[S] with the thawed
(original) form of the frozen structure in @racket[F].}
@defpred[(%melt-new [F unifiable?] [S unifiable?])]{
The goal @racket[(%melt-new F S)] unifies @racket[S] with a thawed
@emph{copy} of the frozen structure in @racket[F]. This means
new logic variables are used for unbound logic variables in
@racket[F].}
@defpred[(%copy [F unifiable?] [S unifiable?])]{
The goal @racket[(%copy F S)] unifies with @racket[S] a copy of the
frozen structure in @racket[F].}
@bibliography[
@bib-entry[#:key "aop"
#:author "Leon Sterling and Ehud Shapiro"
#:url "http://mitpress.mit.edu/books/art-prolog"
#:title "The Art of Prolog, 2nd Edition"
#:location "MIT Press"
#:date "1994"
#:is-book? #t]
@bib-entry[#:key "bratko"
#:author "Ivan Bratko"
#:title "Prolog Programming for Artificial Intelligence"
#:location "Addison-Wesley"
#:date "1986"
#:is-book? #t]
@bib-entry[#:key "campbell"
#:author "J A Campbell (editor)"
#:title "Implementations of Prolog"
#:location "Ellis Horwood"
#:date "1984"
#:is-book? #t]
@bib-entry[#:key "ok:prolog"
#:author "Richard A O'Keefe"
#:url "http://mitpress.mit.edu/books/craft-prolog"
#:title "The Craft of Prolog"
#:location "MIT Press"
#:date "1990"
#:is-book? #t]
@bib-entry[#:key "logick"
#:author "Christopher T Haynes"
#:title "Logic continuations"
#:location "J Logic Program, vol 4, 157--176"
#:date "1987"]
@bib-entry[#:key "mf:prolog"
#:author "Matthias Felleisen"
#:title "Transliterating Prolog into Scheme"
#:location "Indiana U Comp Sci Dept Tech Report #182"
#:date "1985"]
]
@close-eval[racklog-eval]
|