Check-in [0ae8435916]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixed a bug in the prototype chain for Terms, added a term equality function.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0ae843591645a46be03cc2fd809622240bbb7f8a
User & Date: andy 2015-04-14 12:55:35
Context
2015-04-14
13:50
Fixed a couple other bugs related to atoms-as-compounds. Changed the definition of symbolic tokens to *not* include commas, as it was misparsing things like `a,` as a single token. check-in: 5423c478ca user: andy tags: trunk
12:55
Fixed a bug in the prototype chain for Terms, added a term equality function. check-in: 0ae8435916 user: andy tags: trunk
12:55
Fixed an old but unnoticed bug in the parsing of termlists, introduced by the change from atoms being their own type to being 0-arity compounds. check-in: 27e97d3b88 user: andy tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/terms.js.

    50     50   
    51     51       // All instances of the _ variable are identical
    52     52       if(v === "_")
    53     53           return terms.varWildcard;
    54     54       else
    55     55           this.name = v; 
    56     56   }
    57         -terms.Variable.prototype.prototype = Term;
           57  +
           58  +terms.Variable.prototype = Object.create(Term);
           59  +terms.Variable.prototype.constructor = terms.Variable;
    58     60   terms.Variable.prototype.toString = function() { return this.name; }
    59     61   terms.Variable.prototype.tt = function() { return "variable"; }
    60     62   terms.Variable.prototype.walk = function(f) { f(this); }
    61     63   terms.Variable.prototype.isVariable = function() { return true; }
           64  +
    62     65   
    63     66   // Identify the special underscore variable (although you should always use
    64     67   // the canonical instance below).
    65     68   terms.Variable.prototype.isWildcard = function() { return this.name === "_"; };
    66     69   
    67     70   
    68     71   // All instances of the _ variable are identical. In order to set this up, we
................................................................................
    88     91       if(!body.every(terms.isTerm))
    89     92           throw core.errors.InvalidTerm;
    90     93   
    91     94       this.head = head;
    92     95       this.body = body;
    93     96       this.arity = body.length;
    94     97   }
    95         -terms.Compound.prototype.prototype = Term;
           98  +
           99  +terms.Compound.prototype = Object.create(Term);
          100  +terms.Compound.prototype.constructor = terms.Compound;
    96    101   terms.Compound.prototype.toString = function() {
    97    102       // TODO: print infix operators better.
    98    103       // TODO: check to see if the `` are really needed.
    99    104       if(this.isList())
   100    105           return "[" + terms.toArray(this).join(',') + "]";
   101    106   
   102    107       if(this.arity === 0)
................................................................................
   300    305                   ret += " , ";
   301    306   
   302    307               l = l.rhs();
   303    308           }
   304    309       }
   305    310   
   306    311       function stringify(t) {
   307         -        if(t.isAtom())
          312  +        if(t.isVariable())
          313  +            return " ' " + t.name + "' ";
          314  +        else if(t.isAtom())
   308    315               return " '" + t + "' ";
   309    316           else if(t.isList())
   310    317               return "[ " + stringify_list(l) + " ]";
   311         -        else if(t.isVariable())
   312         -            return " ' " + t.name + "' ";
   313    318           else if(t.isCompound())
   314    319               return " '" + t.head + 
   315    320                      "'(" + t.body.map(stringify).join(" , ") + ") ";
   316    321       }
   317    322   
   318    323       return stringify(t);
   319    324   }
          325  +
          326  +/* terms.equal(t1,t2)
          327  + * Equality test for terms. Note that a variable is only equal to an 
          328  + * identically-named variable. This is true even for the wildcard variable.
          329  + */
          330  +terms.equal = function(t1,t2) {
          331  +    if(t1.tt() !== t2.tt())
          332  +        return false;
          333  +    else if(t1.isAtom())
          334  +        return t1.head === t2.head
          335  +    else if(t1.tt() === 'variable')
          336  +        return t1.name === t2.name;
          337  +    else {
          338  +        // Same functor and arity?
          339  +        if(t1.arity !== t2.arity || t1.head !== t2.head)
          340  +            return false;
          341  +
          342  +        // Check all arguments for equality
          343  +        for(var i = 0; i < t1.arity; ++i) 
          344  +            if(!(terms.equal(t1.body[i], t2.body[i])))
          345  +                return false;
          346  +        
          347  +        return true;
          348  +    }
          349  +}
   320    350   
   321    351   }(typeof exports === 'undefined' ? (this['arend']['terms'] = {}) : exports));