prolog_cheatsheet.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Drummyfish's prolog cheatsheet, released under CC0 1.0.
  2. GENERAL:
  3. - Use swi-prolog (swipl) or GNU prolog (gprolog).
  4. - run from CLI like:
  5. swipl my_program.pl argument1 argument2
  6. - to run a query from a script, use snippet (will output all solutions):
  7. run_query( query_to_run ). % e.g. cat(X)
  8. :- run_query(Goal),call(Goal),write(Goal),nl,fail;true.
  9. :- halt.
  10. - Pure prolog only has HORN CLAUSES. These are disjunctions (or) of literals,
  11. of which at most one is non-negated. I.e.:
  12. not(X) or not(Y) or not(Z) or ... [or A]
  13. Which can be rewritten to conjunction (and) as:
  14. X and Y and Z and ... => A
  15. If only non-negated literal is present (A), we call it a FACT. If there is
  16. no positive literal, we call it a GOAL clause (X and Y and Z ...).
  17. SYNTAX & SEMANTICS:
  18. - case sensitive
  19. - Double and single quotes are not the same! (string vs atom, respectively)
  20. - % line comment
  21. - /* multi-line comment */
  22. - Basic syntactic element is a TERM. A term can be either of these:
  23. - ATOM: general purpose name, e.g.: abc, my_dog, 'my cat'
  24. - NUMBER: int or float, e.g.: 10, -3, 15.3
  25. - VARIABLE: names, start with uppercase or underscore, e.g.: X, Item, _item
  26. - COMPOUND TERM: atom with arguments, e.g. cat(X), dog_owner(me,lassie)
  27. - LIST: collection of terms in square brackets, e.g.: [cat, dog, horse]
  28. - STRING: sugar for list of either ints or atoms of length 1, e.g.: "hello"
  29. - The program is a sequence of CLAUSEs, each of which is of form:
  30. A :- B. /* e.g.: animal(X) :- dog(X). */
  31. which means B (body) implies A (head), i.e. B => A. We call these RULEs.
  32. - Clause with an empty body is a FACT (a rule that says head is always true):
  33. A. /* e.g.: dog(lassie). */
  34. - Clause with an empty head gets executed immediately:
  35. :- B. /* e.g.: :- halt. */
  36. - B (body) consists of PREDICATEs (functions which get called and return a
  37. bool value). They can be joined with , (and, conjunction) or ; (or,
  38. disjunction, but this is not a Horn Clause!). We call this a GOAL. E.g.:
  39. siblings(X,Y) :- parent_of(Z,X), parent_of(Z,Y).
  40. /* goal is: parent_of(Z,X), parent_of(Z,Y) */
  41. animal(X) :- cat(X);dog(X). /* if X is a cat or a dog, X is an animal */
  42. - QUERY is a question (usually run from REPL, to run from script see above)
  43. whose answer is to be computed. Its result is either bool, e.g.:
  44. ?- dog(lassie).
  45. Yes
  46. or a list of solutions (substitutions of variables such that the query is
  47. true), e.g.:
  48. ?- dog(X).
  49. dog(lassie)
  50. dog(doge)
  51. A special single underscore (_) variable can be used, which means
  52. "any term" (multiple _s in the same query don't mean the same thing as
  53. other variables).
  54. - UNIFICATION of two terms mean that they are either the same term, or
  55. variables in them can be substitued so that they become the same (can be
  56. tested with = operator).
  57. BUILT-IN PREDICATES:
  58. A -> B if-then (implication) often written also with else (C) as:
  59. A -> B ; C (means (A and B) or (not(A) and C))
  60. X = Y unification, true if X and Y unify
  61. R is E expression evaluation (arithmetic): true if R is a result (single
  62. number, NOT an expression) of evaluating expression E, which is
  63. e.g.: (5 + 4 / 2)
  64. E1 =:= E2 comparison (equality) of two expressions: true if the results of
  65. evaluating expressions E1 and E2 equal
  66. E1 =/= E2 negation of =:=
  67. E1 < E2 less than, similar to =:=
  68. E1 > E2 greater than, similar to <
  69. halt exits the program