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 Unified Diffs Ignore Whitespace Patch

Changes to src/terms.js.

50
51
52
53
54
55
56

57

58
59
60
61

62
63
64
65
66
67
68
..
88
89
90
91
92
93
94

95

96
97
98
99
100
101
102
...
300
301
302
303
304
305
306


307
308
309
310
311
312
313
314
315
316
317
318
319
320

























321

    // All instances of the _ variable are identical
    if(v === "_")
        return terms.varWildcard;
    else
        this.name = v; 
}

terms.Variable.prototype.prototype = Term;

terms.Variable.prototype.toString = function() { return this.name; }
terms.Variable.prototype.tt = function() { return "variable"; }
terms.Variable.prototype.walk = function(f) { f(this); }
terms.Variable.prototype.isVariable = function() { return true; }


// Identify the special underscore variable (although you should always use
// the canonical instance below).
terms.Variable.prototype.isWildcard = function() { return this.name === "_"; };


// All instances of the _ variable are identical. In order to set this up, we
................................................................................
    if(!body.every(terms.isTerm))
        throw core.errors.InvalidTerm;

    this.head = head;
    this.body = body;
    this.arity = body.length;
}

terms.Compound.prototype.prototype = Term;

terms.Compound.prototype.toString = function() {
    // TODO: print infix operators better.
    // TODO: check to see if the `` are really needed.
    if(this.isList())
        return "[" + terms.toArray(this).join(',') + "]";

    if(this.arity === 0)
................................................................................
                ret += " , ";

            l = l.rhs();
        }
    }

    function stringify(t) {


        if(t.isAtom())
            return " '" + t + "' ";
        else if(t.isList())
            return "[ " + stringify_list(l) + " ]";
        else if(t.isVariable())
            return " ' " + t.name + "' ";
        else if(t.isCompound())
            return " '" + t.head + 
                   "'(" + t.body.map(stringify).join(" , ") + ") ";
    }

    return stringify(t);
}


























}(typeof exports === 'undefined' ? (this['arend']['terms'] = {}) : exports));







>
|
>




>







 







>
|
>







 







>
>
|



<
<








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
...
305
306
307
308
309
310
311
312
313
314
315
316
317


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

    // All instances of the _ variable are identical
    if(v === "_")
        return terms.varWildcard;
    else
        this.name = v; 
}

terms.Variable.prototype = Object.create(Term);
terms.Variable.prototype.constructor = terms.Variable;
terms.Variable.prototype.toString = function() { return this.name; }
terms.Variable.prototype.tt = function() { return "variable"; }
terms.Variable.prototype.walk = function(f) { f(this); }
terms.Variable.prototype.isVariable = function() { return true; }


// Identify the special underscore variable (although you should always use
// the canonical instance below).
terms.Variable.prototype.isWildcard = function() { return this.name === "_"; };


// All instances of the _ variable are identical. In order to set this up, we
................................................................................
    if(!body.every(terms.isTerm))
        throw core.errors.InvalidTerm;

    this.head = head;
    this.body = body;
    this.arity = body.length;
}

terms.Compound.prototype = Object.create(Term);
terms.Compound.prototype.constructor = terms.Compound;
terms.Compound.prototype.toString = function() {
    // TODO: print infix operators better.
    // TODO: check to see if the `` are really needed.
    if(this.isList())
        return "[" + terms.toArray(this).join(',') + "]";

    if(this.arity === 0)
................................................................................
                ret += " , ";

            l = l.rhs();
        }
    }

    function stringify(t) {
        if(t.isVariable())
            return " ' " + t.name + "' ";
        else if(t.isAtom())
            return " '" + t + "' ";
        else if(t.isList())
            return "[ " + stringify_list(l) + " ]";


        else if(t.isCompound())
            return " '" + t.head + 
                   "'(" + t.body.map(stringify).join(" , ") + ") ";
    }

    return stringify(t);
}

/* terms.equal(t1,t2)
 * Equality test for terms. Note that a variable is only equal to an 
 * identically-named variable. This is true even for the wildcard variable.
 */
terms.equal = function(t1,t2) {
    if(t1.tt() !== t2.tt())
        return false;
    else if(t1.isAtom())
        return t1.head === t2.head
    else if(t1.tt() === 'variable')
        return t1.name === t2.name;
    else {
        // Same functor and arity?
        if(t1.arity !== t2.arity || t1.head !== t2.head)
            return false;

        // Check all arguments for equality
        for(var i = 0; i < t1.arity; ++i) 
            if(!(terms.equal(t1.body[i], t2.body[i])))
                return false;
        
        return true;
    }
}

}(typeof exports === 'undefined' ? (this['arend']['terms'] = {}) : exports));