Simply Logical: Intelligent Reasoning by Example (1994)
book.simply-logical.space
book.simply-logical.space
A group of tasks with prolog programs to accomplish them.
Without detracting from the value of this book, the name of book made me imagine an entirely different book and topic. A list of intelligent actions accomplished by humans. Some that resist programming efforts and all organized to a picture of the difficulty of defining intelligence.
Peter’s new book (from 2012) shifts more heavily towards statistical methods. This reflects the unreasonable effectiveness of scruffy methods against the neater approaches. A more modern versn of this logical approach would be something like answer-set programming or other constraint based models of logic.
list_length([], 0).
list_length([_|Ls], N) :-
N #> 0,
N #= N0 + 1,
list_length(Ls, N0).
All makes sense, except for the subclause "N #> 0". Why is this necessary, is there any way to generate a negative N?!Somewhat scary that one has to run programs forwards and backwards to ensure there are no hidden corner cases, but perhaps it becomes second nature for more experienced Prolog coders.
list_length(A, -1)
this is asking if any list A has a length of -1. If the guard is not there the second rule is valid, and a infinite recursion can result.> For example, given a specific length, we can ask whether there are lists of that length:
?- list_length(Ls, 3).
Ls = [_G1007, _G1087, _G1167] ;
false.
In the current situation, if you ask for `list_length(Ls, -1)`, this will fail, obviously because there are no lists with length -1. But if you remove the `N #> 0` clause, the code will happily try to find such a list, by checking for lists with length -2, -3, ... ad infinitum.Another way to look at this is that if you have a list `[_|Ls]` with length N, its length must be an integer greater than 0.