📄 readme
字号:
Unification ProgramThis program is the C++ implementation of the unification programfound in Chapter 3 - Logic. If you have two sentences, writtenLisp-like into two strings, you can use this program to unify them.This code was written by Kostadis Roussos, knr@cs.brown.edu.HOW DO I COMPILE IT? 1. Edit the Makefile to change the variables SUPPORT and CC if needed. 2. You should have the following files in this directory: Bind.C Bind.H KRUtility.C LogicNode.C LogicNode.H Parser.C Parser.H XDString.C testParser.C 3. Type "make" at the command line. 4. Run the program "prog". It should output the following text: bindings x (mother ?z ) bindings y (mother ?z )HOW DO I USE IT? This program lets you create sentences and try to unify them. You can see an example of using this program in testParser.C. You can change the variables in this file to do your own unifications. 1. Create the two sentences that are to be unified by creating two strings, like this: char hi[]= "(loves (mother ?z ) ?y )"; char jo[]= "(loves ?x ?x )"; Caution: you must use the exact syntax shown here. -- Start a grouping with "(" and end it with a ")". -- The ")" must have a space before it or else the program will not parse it corerctly. -- A symbol is a string of alphanumerical characters, with no spaces. -- A variable always begins with a "?" character, and can only be one character. "?ab" has two characters in its name, so it is NOT a legal variable. -- Unlike the text in the book, a variable is not enclosed by "(", and ")". -- Names are case-sensitive. "Mother" is different from "mother". 2. You must create the bindings database to perform unification, as demonstrated in testParser.C: BindCompare* comp = new BindCompare; Queue<Binding, BindCompare>* bindings = new Queue<Binding, BindCompare>(comp); 3. Take the two sentences, and create parse trees so that you can unify them. LogicNode* node, *node2; int i = 0, j = 0; node = parse(hi, i); node2 = parse(jo,j); 4. You can unify two parse trees with the "unify()" function. unify(node,node2, bindings); If unification fails the program will abort and say "does not unify". 5. After you have successfully unified the strings, the bindings database will be updated automatically. You can display the bindings with the "display()" method, like this: bindings->display(); ----------------------------------------------------------------------HOW DOES IT WORK, REALLY!Unfortunately, for the program to understand the strings as sentences,they must be "parsed" into a format that is easily evaluated, called a"parse tree". This is very similar to the way the Lisp code actuallyworks.The data types are LogicNode and Binding. LogicNode is a parse node inthe parse tree. It can have one of the three different attributes: - a group node - a constant node - a variable type nodeA LogicNode also has the concept of a child nodes and a current childnode being active. Binding represents the binding between a variabletype and another LogicNode.The three functions are parse, unify and bind.Parse takes as paramaters the string and the starting position of thestring to be parsed. It walks through the string creating the nodes ofthe parse tree. The parser is a simple recursive decent parser. Itreturns the root of the parse tree.Unify takes three paramaters: two LogicNodes (the roots of the twoparse trees) and a collection of the Bindings which is implemented asa queue. Unify examines the type of the LogicNodes and depending onboth types either - ends the process indicating failure - or attempts to bind a variable and something else - or returns, having succesfully unified the two sentences.Bind takes three parameters: the LogicNodes (a,b) to bind, and thequeue of bindings. The first parameter (a) must be a variable. Bindchecks first to see if variable (a) was previoulsy bound. If it was,then Bind calls unify with a and b, where a is now what the variable awas bound to. If a is unbound, it then checks whether b is a variableand if so whether b is bound. If b is bound then unify is called witha and what b is bound to. Otherwise, bind verifies that a does notoccur in b. To do that a is passed to b and b searches among its childnodes and their child nodes if that is the case to find a nodeidentical a. If that is the case then unification fails, else a isbound to b, the binding is added to the queue and bind returns to thecalling function.----------------------------------------------------------------------OBJECTS USED:LogicNode : the parse node Binding : the bindings between LogicNodesNOTES:An alternative implementation would be to simulate multi-methods andhave multiple objects, one for each type. That would certainly be inthe spirite of OOP. However, it would be less clear than theimplementation here.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -