├── README ├── example.html ├── interpreter.js ├── logic_programming.js └── streams.js /README: -------------------------------------------------------------------------------- 1 | This library is a JavaScript adaptation of the logic programming system 2 | from the book "Structure and Interpretation of Computer Programs", 3 | Section 4.4: http://mitpress.mit.edu/sicp/full-text/book/book.html 4 | 5 | A simple command line interpreter is provided. It requires the Rhino 6 | JavaScript engine and can be run in the command line with: 7 | 8 | jrunscript interpreter.js 9 | 10 | There is also an entirely browser based version: 11 | 12 | example.html 13 | 14 | 15 | Programs written for the logic programming system are composed of a sequence 16 | of elements called terms. There are 3 different kinds of terms: 17 | 18 | 1) Atoms: these are general purpose names with no inherent meaning. Atoms 19 | are strings that start with lowercase letters and can contain lowercase 20 | letters, uppercase letters and underscores. Example atoms include: 21 | natural, fooBar, foo_bar. 22 | 23 | 2) Variables: resemble variables in logic in that they are placeholders for 24 | arbitrary terms. They are denoted by strings that start with an uppercase 25 | letter and can contain lowercase letters, uppercase letters and 26 | underscores. Example Variables include: X, Y, Foo, Foo_bar. 27 | 28 | 3) Compound Terms: are composed from an atom called a functor and a number of 29 | arguments, which are again terms. Compound terms are written as a functor 30 | followed by a comma-separated list of argument terms, which is contained 31 | in parentheses. 32 | 33 | Top level compound terms can be of 1 of 3 types determined by the value of 34 | their functor: 35 | 36 | 1) fact - a compound term of arity 1, where the first argument is a term 37 | to be added to the system's database. Example facts include: 38 | fact(likes(fred, wilma)), fact(plus(two, two, four)). 39 | 40 | 2) rule - a compound term of arity 2, where the first argument is the head 41 | of the rule and the second argument is the body of the rule. Example 42 | rules include: rule(natural(succ(X)), natural(X)), 43 | rule(likes(X, Y), and(likes(X, Z), likes(Z, Y)). 44 | 45 | 3) query - a compound term of arity 1, where the first argument is a goal 46 | term that the system must try to resolve, by finding appropriate values 47 | for the variables. Example queries include: 48 | query(likes(fred, X)), query(natural(succ(succ(zero)))). 49 | 50 | The system supports 3 special forms for logic operations which are represented 51 | by compound terms with functors: 52 | 53 | 1) and - a compound term of arity n, which dictates that all the arguments 54 | have to simultaneously resolve for the whole term to resolve successfully. 55 | 56 | 2) or - a compound term of arity n, which dictates that at least one of the 57 | arguments must successfully resolve for the whole term to resolve 58 | successfully. 59 | 60 | 3) not - a compound term of arity 1, which dictates that at the term resolves 61 | successfully if the argument can not resolve. 62 | 63 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 12 | 13 | 14 |18 | 44 | 45 | | 46 |
47 | 48 | Programs written for the logic programming system are 49 | composed of a sequence of elements called terms. There 50 | are 3 different kinds of terms: 51 | 52 |
74 | Top level compound terms can be of 1 of 3 types determined by the 75 | value of their functor: 76 | 77 |
98 | The system supports 3 special forms for logic operations which are 99 | represented by compound terms with functors: 100 | 101 |
|
118 |